From 592a6f7e58c3ef4b2e85aef467440333abca7333 Mon Sep 17 00:00:00 2001 From: Christoph Date: Thu, 27 Dec 2018 17:15:48 +0100 Subject: [PATCH] check_webservice_load.sh: don't start/stop services ig packagemanagement (apt,dpkg,..) ist running. --- check_webservice_load.sh | 289 ++++++++++++++++++++++++++++++++------- 1 file changed, 242 insertions(+), 47 deletions(-) diff --git a/check_webservice_load.sh b/check_webservice_load.sh index 33699db..4a28241 100755 --- a/check_webservice_load.sh +++ b/check_webservice_load.sh @@ -104,6 +104,36 @@ ok (){ echo "" } + +detect_os () { + + if $(which lsb_release > /dev/null 2>&1) ; then + + os_dist="$(lsb_release -i | awk '{print tolower($3)}')" + os_version="$(lsb_release -r | awk '{print tolower($2)}')" + os_codename="$(lsb_release -c | awk '{print tolower($2)}')" + + if [[ "$os_dist" = "debian" ]]; then + if $(echo "$os_version" | grep -q '\.') ; then + os_version=$(echo "$os_version" | cut --delimiter='.' -f1) + fi + fi + + elif [[ -e "/etc/os-release" ]]; then + + . /etc/os-release + + os_dist=$ID + os_version=${VERSION_ID} + + fi + + # remove whitespace from os_dist and os_version + os_dist="${os_dist// /}" + os_version="${os_version// /}" + +} + trim() { local var="$*" var="${var#"${var%%[![:space:]]*}"}" # remove leading whitespace characters @@ -126,6 +156,10 @@ else LOGGING=false fi +# - Detect linux distribution +# - +detect_os + #--------------------------------------- @@ -714,9 +748,21 @@ stop_nginx() { stop_mysql() { + send_msg=$1 send_msg=${send_msg:=true} + # - Don't Start/Stop Services if package management is running (apt,aptitude,dpkg) + # - + if [[ "${os_dist,,}" = "debian" ]] || [[ "${os_dist,,}" = "ubuntu" ]] ; then + if $(lsof /var/lib/dpkg/lock >/dev/null 2>&1) ; then + if $terminal ; then + warn "It seems that package management (apt,dpkg) is running. Exiting '${FUNCNAME[0]}' now.." + fi + return 1 + fi + fi + ## - Stop MySQL Service ## - if [[ -n "$MYSQL_INIT_SCRIPT" ]]; then @@ -783,6 +829,17 @@ stop_postgresql() { send_msg=$1 send_msg=${send_msg:=true} + # - Don't Start/Stop Services if package management is running (apt,aptitude,dpkg) + # - + if [[ "${os_dist,,}" = "debian" ]] || [[ "${os_dist,,}" = "ubuntu" ]] ; then + if $(lsof /var/lib/dpkg/lock >/dev/null 2>&1) ; then + if $terminal ; then + warn "It seems that package management (apt,dpkg) is running. Exiting '${FUNCNAME[0]}' now.." + fi + return 1 + fi + fi + ## - Stop PostgreSQL database service ## - if [[ -n "$PSQL_SERVICE_FILE" ]] ; then @@ -872,6 +929,17 @@ stop_php_fpm() { _msg=false + # - Don't Start/Stop Services if package management is running (apt,aptitude,dpkg) + # - + if [[ "${os_dist,,}" = "debian" ]] || [[ "${os_dist,,}" = "ubuntu" ]] ; then + if $(lsof /var/lib/dpkg/lock >/dev/null 2>&1) ; then + if $terminal ; then + warn "It seems that package management (apt,dpkg) is running. Exiting '${FUNCNAME[0]}' now.." + fi + return 1 + fi + fi + ## - Stop PHP-FPM Service ## - if [[ "${start_stop_method_php_fpm["$__version"]}" = "systemd_service" ]]; then @@ -912,6 +980,17 @@ stop_php_fpm() { break fi + # - Don't Start/Stop Services if package management is running (apt,aptitude,dpkg) + # - + if [[ "${os_dist,,}" = "debian" ]] || [[ "${os_dist,,}" = "ubuntu" ]] ; then + if $(lsof /var/lib/dpkg/lock >/dev/null 2>&1) ; then + if $terminal ; then + warn "It seems that package management (apt,dpkg) is running. Exiting '${FUNCNAME[0]}' now.." + fi + return 1 + fi + fi + for _PID in $PIDS ; do kill -9 $_PID > /dev/null 2>&1 done @@ -957,6 +1036,17 @@ stop_php_fpm() { break fi + # - Don't Start/Stop Services if package management is running (apt,aptitude,dpkg) + # - + if [[ "${os_dist,,}" = "debian" ]] || [[ "${os_dist,,}" = "ubuntu" ]] ; then + if $(lsof /var/lib/dpkg/lock >/dev/null 2>&1) ; then + if $terminal ; then + warn "It seems that package management (apt,dpkg) is running. Exiting '${FUNCNAME[0]}' now.." + fi + return 1 + fi + fi + for _PID in $PIDS ; do kill -9 $_PID > /dev/null 2>&1 done @@ -985,6 +1075,17 @@ stop_redis() { send_msg=$1 send_msg=${send_msg:=true} + # - Don't Start/Stop Services if package management is running (apt,aptitude,dpkg) + # - + if [[ "${os_dist,,}" = "debian" ]] || [[ "${os_dist,,}" = "ubuntu" ]] ; then + if $(lsof /var/lib/dpkg/lock >/dev/null 2>&1) ; then + if $terminal ; then + warn "It seems that package management (apt,dpkg) is running. Exiting '${FUNCNAME[0]}' now.." + fi + return 1 + fi + fi + ## - Stop Redis Caching Service ## - if [[ -n "$REDIS_SERVICE_FILE" ]] ; then @@ -1034,6 +1135,17 @@ stop_redis() { break fi + # - Don't Start/Stop Services if package management is running (apt,aptitude,dpkg) + # - + if [[ "${os_dist,,}" = "debian" ]] || [[ "${os_dist,,}" = "ubuntu" ]] ; then + if $(lsof /var/lib/dpkg/lock >/dev/null 2>&1) ; then + if $terminal ; then + warn "It seems that package management (apt,dpkg) is running. Exiting '${FUNCNAME[0]}' now.." + fi + return 1 + fi + fi + for _PID in $PIDS ; do kill -9 $_PID > /dev/null 2>&1 done @@ -1049,6 +1161,17 @@ start_mysql() { send_msg=$1 send_msg=${send_msg:=true} + # - Don't Start/Stop Services if package management is running (apt,aptitude,dpkg) + # - + if [[ "${os_dist,,}" = "debian" ]] || [[ "${os_dist,,}" = "ubuntu" ]] ; then + if $(lsof /var/lib/dpkg/lock >/dev/null 2>&1) ; then + if $terminal ; then + warn "It seems that package management (apt,dpkg) is running. Exiting '${FUNCNAME[0]}' now.." + fi + return 1 + fi + fi + if [[ -n "$MYSQL_INIT_SCRIPT" ]]; then $MYSQL_INIT_SCRIPT start > /dev/null 2>&1 else @@ -1104,6 +1227,17 @@ start_postgresql() { send_msg=${send_msg:=true} + # - Don't Start/Stop Services if package management is running (apt,aptitude,dpkg) + # - + if [[ "${os_dist,,}" = "debian" ]] || [[ "${os_dist,,}" = "ubuntu" ]] ; then + if $(lsof /var/lib/dpkg/lock >/dev/null 2>&1) ; then + if $terminal ; then + warn "It seems that package management (apt,dpkg) is running. Exiting '${FUNCNAME[0]}' now.." + fi + return 1 + fi + fi + ## - Start PostgreSQL Webservice ## - if [[ -n "$PSQL_SERVICE_FILE" ]] ; then @@ -1162,6 +1296,17 @@ start_apache() { send_msg=${send_msg:=true} + # - Don't Start/Stop Services if package management is running (apt,aptitude,dpkg) + # - + if [[ "${os_dist,,}" = "debian" ]] || [[ "${os_dist,,}" = "ubuntu" ]] ; then + if $(lsof /var/lib/dpkg/lock >/dev/null 2>&1) ; then + if $terminal ; then + warn "It seems that package management (apt,dpkg) is running. Exiting '${FUNCNAME[0]}' now.." + fi + return 1 + fi + fi + ## - Start Apache Webservice ## - if [[ -n "$APACHE_SERVICE_FILE" ]] ; then @@ -1219,6 +1364,17 @@ start_nginx() { send_msg=${send_msg:=true} + # - Don't Start/Stop Services if package management is running (apt,aptitude,dpkg) + # - + if [[ "${os_dist,,}" = "debian" ]] || [[ "${os_dist,,}" = "ubuntu" ]] ; then + if $(lsof /var/lib/dpkg/lock >/dev/null 2>&1) ; then + if $terminal ; then + warn "It seems that package management (apt,dpkg) is running. Exiting '${FUNCNAME[0]}' now.." + fi + return 1 + fi + fi + ## - Start Nginx Webservice ## - if [[ -n "$NGINX_SERVICE_FILE" ]] ; then @@ -1279,6 +1435,17 @@ start_php_fpm() { start_php_fpm_failed=false + # - Don't Start/Stop Services if package management is running (apt,aptitude,dpkg) + # - + if [[ "${os_dist,,}" = "debian" ]] || [[ "${os_dist,,}" = "ubuntu" ]] ; then + if $(lsof /var/lib/dpkg/lock >/dev/null 2>&1) ; then + if $terminal ; then + warn "It seems that package management (apt,dpkg) is running. Exiting '${FUNCNAME[0]}' now.." + fi + return 1 + fi + fi + if [[ "${start_stop_method_php_fpm[$__version]}" = "systemd_service" ]]; then systemctl restart ${start_stop_file_php_fpm[$__version]} > /dev/null 2>&1 elif [[ "${start_stop_method_php_fpm["$__version"]}" = "init_script" ]]; then @@ -1344,6 +1511,17 @@ start_redis() { send_msg=${send_msg:=true} + # - Don't Start/Stop Services if package management is running (apt,aptitude,dpkg) + # - + if [[ "${os_dist,,}" = "debian" ]] || [[ "${os_dist,,}" = "ubuntu" ]] ; then + if $(lsof /var/lib/dpkg/lock >/dev/null 2>&1) ; then + if $terminal ; then + warn "It seems that package management (apt,dpkg) is running. Exiting '${FUNCNAME[0]}' now.." + fi + return 1 + fi + fi + ## - Start Redis Caching Service ## - if [[ -n "$REDIS_SERVICE_FILE" ]] ; then @@ -1399,78 +1577,84 @@ restart_apache() { ## - Stop Apache Webservice ## - - stop_apache + if stop_apache ; then - sleep 2 + sleep 2 - ## - Start Apache Webservice - ## - - start_apache + ## - Start Apache Webservice + ## - + start_apache + fi } restart_nginx() { ## - Stop Apache Webservice ## - - stop_nginx + if stop_nginx ; then - sleep 2 + sleep 2 - ## - Start Apache Webservice - ## - - start_nginx + ## - Start NGINX Webservice + ## - + start_nginx + fi } restart_mysql(){ ## - Stop MySQL Service ## - - stop_mysql + if stop_mysql ; then - sleep 2 + sleep 2 - ## - Start MySQL Service - ## - - start_mysql + ## - Start MySQL Service + ## - + start_mysql + fi } restart_postgresql(){ ## - Stop PostgreSQL Service ## - - stop_postgresql + if stop_postgresql ; then - sleep 2 + sleep 2 - ## - Start MySQL Service - ## - - start_postgresql + ## - Start MySQL Service + ## - + start_postgresql + fi } restart_php_fpm(){ ## - Stop MySQL Servive ## - - stop_php_fpm $1 + if stop_php_fpm $1 ; then - sleep 2 + sleep 2 - ## - Start MySQL Service - ## - - start_php_fpm $1 + ## - Start MySQL Service + ## - + start_php_fpm $1 + fi } restart_redis(){ ## - Stop MySQL Servive ## - - stop_redis + if stop_redis ; then - sleep 2 + sleep 2 - ## - Start MySQL Service - ## - - start_redis + ## - Start MySQL Service + ## - + start_redis + fi } graceful_restart_php_fpm() { @@ -1482,6 +1666,17 @@ graceful_restart_php_fpm() { start_php_fpm_failed=false + # - Don't Start/Stop Services if package management is running (apt,aptitude,dpkg) + # - + if [[ "${os_dist,,}" = "debian" ]] || [[ "${os_dist,,}" = "ubuntu" ]] ; then + if $(lsof /var/lib/dpkg/lock >/dev/null 2>&1) ; then + if $terminal ; then + warn "It seems that package management (apt,dpkg) is running. Exiting '${FUNCNAME[0]}' now.." + fi + return 1 + fi + fi + if [[ "$start_stop_method_php_fpm" = "systemd_service" ]]; then systemctl restart $start_stop_method_php_fpm["$__version"] > /dev/null 2>&1 elif [[ "$start_stop_method_php_fpm" = "init_script" ]]; then @@ -1700,10 +1895,10 @@ if $check_mysql ; then if [ "X${PID}X" = "XX" ];then - echo -e "\nMySQL is not running! - Try to start it.." >> $LOCK_DIR/extra_msg.txt - echo -e "=========================================" >> $LOCK_DIR/extra_msg.txt + echo -e "\nMySQL is not running! - Try to restart service.." >> $LOCK_DIR/extra_msg.txt + echo -e "=================================================" >> $LOCK_DIR/extra_msg.txt - error "MySQL is not running! - Try to start it.." + error "MySQL is not running! - Try to restart it.." restart_mysql @@ -1746,10 +1941,10 @@ if $check_postgresql ; then if [ "X${PSQL_MAIN_PID}X" = "XX" ];then - echo -e "\nPostgreSQL database service is not running! - Try to start it.." > $LOCK_DIR/extra_msg.txt - echo -e "===============================================================" >> $LOCK_DIR/extra_msg.txt + echo -e "\nPostgreSQL database service is not running! - Try to restart service.." > $LOCK_DIR/extra_msg.txt + echo -e "======================================================================" >> $LOCK_DIR/extra_msg.txt - error "PostgeSQL database service is not running! - Try to start it.." + error "PostgeSQL database service is not running! - Try to restart service.." restart_postgresql @@ -1786,10 +1981,10 @@ if $check_apache ; then if [ "X${PID}X" = "XX" ];then - echo -e "\nApache Webservice is not running! - Try to start it.." > $LOCK_DIR/extra_msg.txt - echo -e "=====================================================" >> $LOCK_DIR/extra_msg.txt + echo -e "\nApache Webservice is not running! - Try to restart service.." > $LOCK_DIR/extra_msg.txt + echo -e "============================================================" >> $LOCK_DIR/extra_msg.txt - error "Apache webservice is not running! - Try to start it.." + error "Apache webservice is not running! - Try to restart service.." restart_apache @@ -1830,8 +2025,8 @@ if $check_nginx ; then PID=`ps aux | grep "$NGINXD" | grep -e "^root" | grep -v grep | awk '{print$2}'` if [ "X${PID}X" = "XX" ];then - echo -e "\nNginx Webservice is not running! - Try to start it.." > $LOCK_DIR/extra_msg.txt - echo -e "====================================================" >> $LOCK_DIR/extra_msg.txt + echo -e "\nNginx Webservice is not running! - Try to restart service.." > $LOCK_DIR/extra_msg.txt + echo -e "===========================================================" >> $LOCK_DIR/extra_msg.txt error "Nginx webservice is not running! - Try to start it.." @@ -1886,10 +2081,10 @@ if $check_php_fpm ; then if [ "X${PID}X" = "XX" ];then - echo -e "\nPHP-FPM v$_version is not running! - Try to start the service.." > $LOCK_DIR/extra_msg.txt - echo -e "====================================================" >> $LOCK_DIR/extra_msg.txt + echo -e "\nPHP-FPM v$_version is not running! - Try to restart the service.." > $LOCK_DIR/extra_msg.txt + echo -e "======================================================" >> $LOCK_DIR/extra_msg.txt - error "PHP-FPM v$_version is not running! - Try to start the service.." + error "PHP-FPM v$_version is not running! - Try to restart the service.." restart_php_fpm $_version @@ -1931,10 +2126,10 @@ if $check_redis ; then PID=`ps aux | grep "$REDISD" | grep -v grep | awk '{print$2}'` if [ "X${PID}X" = "XX" ];then - echo -e "\nRedis Caching Service is not running! - Try to start it.." > $LOCK_DIR/extra_msg.txt - echo -e "=========================================================" >> $LOCK_DIR/extra_msg.txt + echo -e "\nRedis Caching Service is not running! - Try to restart service.." > $LOCK_DIR/extra_msg.txt + echo -e "================================================================" >> $LOCK_DIR/extra_msg.txt - error "Redis Caching Service is not running! - Try to start it.." + error "Redis Caching Service is not running! - Try to restart it.." restart_redis