Ad Widget

Collapse

*Script* Automatic and Manual Service Restart

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Parasin
    Member
    Zabbix Certified Specialist
    • Dec 2014
    • 53

    #1

    *Script* Automatic and Manual Service Restart

    Hi all,

    This may have been posted before, but I didn't see it listed
    I have created 2 separate scripts that restart Zabbix services for you.
    • Automated Check and Restart: You can create a crontab entry to run the script at the interval of your choice, and it will check if the server and agent are running. If they are not, the script will start them and send an email to an admin user that the script ran.
    • Manual Restart: This script automates the restart of the Zabbix Agent and Server. It can be modified so that you can restart only the services of your choosing. Once you call the script, it will stop the services and then start them back up. This removes the need for typing each individual command.


    These scripts are by no means "perfect", but they DO work on RHEL6; I have tested them extensively.

    If you have improvements, feel free to post your updates below
    Let me know if these help you out!

    Automated Check and Restart
    Code:
    #!/bin/bash
    zabbix_server="service zabbix-server"
    zabbix_agent="service zabbix-agent"
    logfile=zabbix_auto_restart.log
    logfilePath=/etc/scripts/zabbix/$logfile
    zabbix_server_running=0
    zabbix_agent_running=0
    
    grep_agent (){
    	local retval=$(ps -ef | grep -v grep | grep zabbix_agentd | wc -l)
    	echo $retval
    }
    
    grep_server (){
    	local retval=$(ps -ef | grep -v grep | grep zabbix_server | wc -l)
    	echo $retval
    }
    
    check_zabbix_agentd (){
    	if (( $(grep_agent) <= 0 ))
    	then
    	   $zabbix_agent start
    	   echo `date` "$zabbix_agent was stopped... Restarting" >> $logfile
    	   echo "************************************************" >> $logfile
    
    	   #Send email to notify that the script ran
    	   echo "$(date) $zabbix_agent was restarted from zabbix_restart.sh" | mutt -s "Zabbix Auto-restart Script Just Ran" <user email address>
    
    	else
    	   let zabbix_agent_running=1
    	fi
    }
    
    check_zabbix_server (){
    	if (( $(grep_server) <= 0 ))
    	then
    	   $zabbix_server start
    	   echo `date` "$zabbix_server was stopped... Restarting" >> $logfile
    	   echo "************************************************" >> $logfile
    
    	   #Send email to notify that the script ran
    	   echo "$(date) $zabbix_server was restarted from zabbix_restart.sh" | mutt -s "Zabbix Auto-restart Script Just Ran" <user email address>
    
    	else
    	   let zabbix_server_running=1
    	fi
    }
    
    main_loop (){
    	until ((zabbix_server_running == 1 && zabbix_agent_running == 1));
    	do
    		check_zabbix_agentd
    		check_zabbix_server
    		sleep 1.5
    	done
    }
    
    main_loop
    Manual Restart
    Code:
    #!/bin/bash
    logfile=zabbix_manual_restart.log
    logfilePath=/etc/scripts/zabbix/$logfile
    service="zabbix-agent"
    service2="zabbix-server"
    
    grep_agent (){
    	local retval=$(ps -ef | grep -v grep | grep zabbix_agentd | wc -l)
    	echo $retval
    }
    
    grep_server (){
    	local retval=$(ps -ef | grep -v grep | grep zabbix_server | wc -l)
    	echo $retval
    }
    
    stop_services (){
    	#Make sure that the zabbix-agent service is stopped
    	echo `date` "Verifying $service is stopped" >> $logfilePath
    	service $service stop
    
    	#Make sure that the zabbix-server service is stopped
    	echo `date` "Verifying $service2 is stopped" >> $logfilePath
    	service $service2 stop
    
    	#Double-check they are stopped, print confirmation
    	if (( $(grep_agent)  == 0 )) && (( $(grep_server) == 0 ))
    	then
    	  echo "Zabbix Services have been stopped successfully"
    	fi
    }
    
    start_agent (){
    	echo `date` "Restarting $service" >> $logfilePath
    	service $service start
    }
    
    start_server (){
    	echo `date` "Restarting $service2" >> $logfilePath
    	service $service2 start
    }
    
    service_start_loop (){
    	local num_agents=$(grep_agent)
    	local num_server=$(grep_server)
    	
    	echo "num_agents on start_service call: $num_agents"
    	echo "num_agents on start_service call: $num_agents" >> $logfilePath
    	echo "num_server on start_service call: $num_server"
    	echo "num_server on start_service call: $num_server" >> $logfilePath
    	
    	#While zabbix-agent OR zabbix-server are stopped, attempt to start them
    	while (( $num_agents <= 0 )) || (( $num_server <= 0 ));
    	do
    		if (( $num_agents <= 0  && $num_server <= 0 ))
    		then
    			echo "Both were stopped"
    			echo "Both were stopped" >> $logfilePath
    			start_agent
    			start_server
    		elif (( $num_server <= 0 ))
    		then
    			echo "Server was stopped"
    			echo "Server was stopped" >> $logfilePath
    			start_server
    		else
    			echo "Agent was stopped"
    			echo "Agent was stopped" >> $logfilePath
    			start_agent
    		fi
    
    		sleep 1.5
      
    		num_agents=$(grep_agent)
    		num_server=$(grep_server)
    		echo "num_agents on start_service end: $num_agents"
    		echo "num_agents on start_service end: $num_agents" >> $logfilePath
    		echo "num_server on start_service end: $num_server"
    		echo "num_server on start_service end: $num_server" >> $logfilePath
    	done
    }
    
    #Script completed successfully, display confirmation message, and send email to user
    finalize (){
    	echo `date` "Zabbix services restarted successfully - script complete" >> $logfilePath
    	echo "*********************************************************" >> $logfilePath
    	echo "Zabbix services restarted successfully - script complete"
    	echo "Log updated and saved to: $logfilePath"
    
    	#Send email to notify that the script ran
    	echo "$(date) Zabbix services were restarted from zabbix_restart.sh" | mutt -s "Zabbix Restart Script Just Ran" <user email address>
    	echo "Email sent to system admin"
    }
    
    stop_services
    sleep 1.5
    service_start_loop
    finalize
    Last edited by Parasin; 19-06-2015, 01:31.
  • Parasin
    Member
    Zabbix Certified Specialist
    • Dec 2014
    • 53

    #2
    Updated the scripts, improved functionality and better logging of actions taken by the script

    Comment

    Working...