#!/bin/bash # Rocky9 compatible # Author: gm . /usr/src/m2/framework/bash_functions.sh . /usr/src/m2/framework/settings.sh gui_services="httpd memcached" db_services="m2_alerts" # Do NOT add mysqld here es_services="elasticsearch" fs_services="freeswitch" radius_services="radiusd" k_detect_os fix_enabled_services() { local services_string="$1" local service_name if (( centos_version != 6 )); then # centos 7 / r9 for service_name in $services_string; do if systemctl status "$service_name" 2> /dev/null | grep -Fq "Active: active"; then report "Service $service_name is running" 0 else report "Service $service_name is not running, but it should, starting it" 3 service "$service_name" start fi if systemctl list-unit-files --type service | grep "$service_name" | grep -Fq 'enabled'; then report "Service $service_name is enabled to auto-start" 0 else # double check with chkconfig if chkconfig --list 2>/dev/null | grep "$service_name" | grep -Fq '5:on'; then report "Service $service_name is enabled to auto-start" 0 else report "Service $service_name is not enabled to auto-start, but it should, enabling it" 4 chkconfig --level 345 "$service_name" on systemctl enable "$service_name" fi fi done else # centos 6 for service_name in $services_string; do if service "$service_name" status 2> /dev/null | grep -Fq "is running"; then report "Service $service_name is running" 0 else report "Service $service_name is not running, but it should, starting it" 3 service "$service_name" start fi if chkconfig --list | grep "$service_name" | grep -Fq '5:on'; then report "Service $service_name is enabled to auto-start" 0 else report "Service $service_name is not enabled to auto-start, but it should, enabling it" 4 chkconfig --level 345 "$service_name" on fi done fi } fix_disabled_services() { local services_string="$1" local service_name if (( centos_version != 6 )); then # centos 7 / r9 for service_name in $services_string; do if systemctl status "$service_name" 2> /dev/null | grep -Fq "running"; then report "Service $service_name is running, but it should NOT be running. Stopping it" 3 systemctl stop "$service_name" else report "Service $service_name is not running" 0 fi if systemctl list-unit-files --type service | grep "$service_name" | grep -Fq 'enabled'; then report "Service $service_name is not disabled on all levels, but it should. Disabling it" 4 systemctl disable "$service_name" else report "Service $service_name is disabled on all levels" 0 fi done else # centos 6 for service_name in $services_string; do if service "$service_name" status 2> /dev/null | grep -Fq "is running"; then report "Service $service_name is running, but it should NOT be running. Stopping it" 3 service "$service_name" stop else report "Service $service_name is not running" 0 fi if chkconfig --list | grep "$service_name" | grep -Fq 'on'; then report "Service $service_name is not disabled on all levels, but it shold. Disabling it" 4 chkconfig "$service_name" off else report "Service $service_name is disabled on all levels" 0 fi done fi } fix_db_crons_and_services() { local cron_action if [[ "$DB_PRESENT" == "1" ]]; then if [[ $MAIN_DB == "1" || $MAIN_DB == "-1" ]]; then if [[ $MAIN_DB == "1" ]]; then report "Main database present, checking if all crons and services are installed and enabled" 3 else report "DB_PRESENT=1 but MAIN_DB is not defined, please set it in /etc/m2/system.conf and run /usr/src/m2/maintenance/fix_crons_and_services.sh again" 2 fi /usr/src/m2/mysql/partitions/partitions_check.sh for cron_action in $M2_CRONS_FOR_MAIN_DB; do if [[ -e "/etc/cron.d/$cron_action" ]]; then report "$cron_action is present" 0 else report "$cron_action is NOT present but it SHOULD, installing it" 4 cp -fr "/usr/src/m2/scripts/cronjobs/$cron_action" /etc/cron.d/ chmod 0644 "/etc/cron.d/$cron_action" fi done fix_enabled_services "$db_services" elif [[ $MAIN_DB == "0" ]]; then report "MAIN_DB=0, checking if all crons and services for MAIN_DB are removed" 3 for cron_action in $M2_CRONS_FOR_MAIN_DB; do if [[ -e "/etc/cron.d/$cron_action" ]]; then report "$cron_action is present, but it should not be, deleting it" 4 rm -f "/etc/cron.d/$cron_action" else report "$cron_action is NOT present" 0 fi done fix_disabled_services "$db_services" else report "Something went wrong. Unexpected value $MAIN_DB for MAIN_DB" fi else report "DB is not present, checking if all crons and services for DB=0 are removed" 3 for cron_action in $M2_CRONS_FOR_MAIN_DB; do if [[ -e "/etc/cron.d/$cron_action" ]]; then report "$cron_action is present, but it should not be, deleting it" 4 rm -f "/etc/cron.d/$cron_action" else report "$cron_action is NOT present" 0 fi done fix_disabled_services "$db_services" #TO DO: quick fix, abstract better here service mysqld stop fi } fix_crons_and_services() { local option="$1" local option_name="$2" local crons_string="$3" local services_string="$4" local cron_action local service_name if [[ $option == "1" ]]; then report "$option_name=1, checking if all crons and services for $option_name=1 are installed" 3 apache_listen_on_https=$(netstat -antp | grep -F http | grep -cF :443) for cron_action in $crons_string; do if [[ -e "/etc/cron.d/$cron_action" ]]; then report "$cron_action is present" 0 else report "$cron_action is NOT present but it SHOULD, installing it" 4 if [[ $option_name == "GUI_PRESENT" ]]; then cp -fr "/usr/src/m2/gui/cronjobs/$cron_action" /etc/cron.d/ if (( apache_listen_on_https > 0)); then sed -i 's#http:#https:#' /etc/cron.d/"$cron_action" fi chmod 0644 "/etc/cron.d/$cron_action" else cp -fr "/usr/src/m2/scripts/cronjobs/$cron_action" /etc/cron.d/ chmod 0644 "/etc/cron.d/$cron_action" fi fi done fix_enabled_services "$services_string" elif [[ $option == "0" ]]; then report "$option_name=0, checking if all crons and services for $option_name=0 are removed" 3 for cron_action in $crons_string; do if [[ -e "/etc/cron.d/$cron_action" ]]; then report "$cron_action is present, but it should not be, deleting it" 4 rm -f "/etc/cron.d/$cron_action" else report "$cron_action is NOT present" 0 fi done fix_disabled_services "$services_string" elif [[ $option == "-1" ]]; then report "Option $option_name is not defined. Add option $option_name in /etc/m2/system.conf and run /usr/src/m2/maintenance/fix_crons_and_services.sh again" 2 else report "Something went wrong. Unexpected value $option for $option_name" fi } check_mysql_version() { if [[ "$DB_PRESENT" != "1" ]]; then return fi if (( centos_version != 6 )); then # centos 7 / r9 if mysql -V | grep -q "5.7"; then report "MySQL version: 5.7" 0 else if [[ $ROCKY9 == 1 ]]; then if mysql -V | grep -q "8."; then report "MySQL version: 8" 0 else report "MySQL version is not 8!" 6 mysql -V fi else report "MySQL version is not 5.7!" 6 mysql -V fi fi else # centos 6 if mysql -V | grep -q "5."; then report "MySQL version: 5.x" 0 else report "MySQL version is not 5.x" 6 mysql -V fi fi } check_radius_version() { if [[ "$RADIUS_PRESENT" != "1" ]]; then return fi rv=`/usr/local/sbin/radiusd -v | grep -Po 'FreeRADIUS Version \d+\.\d+\.\d+' | head -n 1 | grep -Po '\d+' | head -n 1` if [[ "$rv" == "3" ]]; then report "Radius version: 3" 0 else report "Radius version is not 3!" 6 radiusd -v | head -n 1 fi } check_core_version() { if [[ "$RADIUS_PRESENT" != "1" ]]; then return fi cv=`m2 show status | grep "Core version" | awk '{print $4}'` report "Core version: $cv" 0 } report "" 8 report "--- FINAL CHECK ---" 8 read_m2_settings check_mysql_version check_radius_version fix_enabled_services "m2_server_loadstats" service m2_server_loadstats restart > /dev/null 2>&1 # just in case fix_db_crons_and_services fix_crons_and_services "$GUI_PRESENT" "GUI_PRESENT" "$M2_CRONS_FOR_GUI" "$gui_services" fix_crons_and_services "$ES_PRESENT" "ES_PRESENT" "$M2_CRONS_FOR_ES" "$es_services" fix_crons_and_services "$FREESWITCH_PRESENT" "FREESWITCH_PRESENT" "$M2_CRONS_FOR_FS" "$fs_services" fix_crons_and_services "$RADIUS_PRESENT" "RADIUS_PRESENT" "$M2_CRONS_FOR_RADIUS" "$radius_services" check_core_version