Advertisement
Guest User

Untitled

a guest
Mar 24th, 2017
72
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.35 KB | None | 0 0
  1. #! /bin/sh
  2. counter=0
  3. zabbix_counter=$(ps -ef | grep zabbix_agentd | grep -v grep | wc -l)
  4.  
  5. start(){
  6. echo "-------------------------------------------------------------------"
  7. echo " LAUNCHING ZABBIX AGENT"
  8.  
  9. if [ $zabbix_counter -gt 0 ]; then
  10. echo " * Zabbix agent was previously running"
  11. echo " * Number of Zabbix agentd instances= $zabbix_counter"
  12. echo "-----------------------------------------------------------------"
  13. fi
  14.  
  15. # Checking if the user is able to start the agent.... if the user is not able to, script performs su to
  16. # the user zabbix and starts the agent
  17.  
  18. if [ $(whoami) != "zabbix" ];then
  19. sudo -u zabbix zabbix_agentd
  20. else
  21. # Script is acting as the zabbix user, so it can start the agent.
  22. zabbix_agentd
  23. fi
  24. sleep 10
  25.  
  26. zabbix_counter=$(ps -ef | grep zabbix_agentd | grep -v grep | wc -l)
  27. if [ $zabbix_counter -gt 0 ]; then
  28. echo " * Zabbix agent succesfully started"
  29. echo " * Number of zabbix agentd instances= $zabbix_counter"
  30. echo "-------------------------------------------------------------------"
  31. else
  32. echo " * Zabbix agent couldn't be started, check Zabbix logs"
  33. echo "-------------------------------------------------------------------"
  34. fi
  35.  
  36. }
  37.  
  38. stop(){
  39. # Checking if the user is able to stop the agent.... if the user is not able to, script performs su to
  40. # the user zabbix and kills the agent. Also script tries to kill zabbix-agent processes 5 times using a counter, if at
  41. # the fith try the agent is still there, script outputs a message to the console.
  42.  
  43. echo "-------------------------------------------------------------------"
  44. echo " STOPPING ZABBIX AGENT"
  45.  
  46. if [ $zabbix_counter -eq 0 ]; then
  47. echo " * Zabbix agent was not running on this machine"
  48. echo "-------------------------------------------------------------------"
  49. fi
  50.  
  51. while [ $zabbix_counter -gt 0 ] && [ $counter -lt 5 ] ; do
  52.  
  53. let counter=counter+1
  54.  
  55. echo " * Number of Attempts (Max 5)=$counter"
  56. echo " * Stopping zabbix.."
  57. echo " * Number of zabbix agentd instances= $zabbix_counter"
  58.  
  59. if [ $(whoami) != "zabbix" ];then
  60. sudo -u zabbix killall zabbix_agentd > /dev/null &
  61. else
  62. killall zabbix_agentd > /dev/null &
  63. fi
  64.  
  65. sleep 10
  66. # Script has a 10 second delay to avoid attempting to kill a process that is still shutting down. If the script
  67. # can't kill the processes, an error will appear.
  68. # After 10 seconds script checks again the number of zabbix_agentd processes running,
  69. # if it's 0, script will exit the loop and continue on
  70.  
  71. zabbix_counter=$(ps -ef | grep zabbix_agentd | grep -v grep | wc -l)
  72.  
  73. done
  74.  
  75. if [ $zabbix_counter -gt 0 ]; then
  76. echo " * Zabbix agent couldn't be stopped, check Zabbix logs"
  77. echo "-------------------------------------------------------------------"
  78. fi
  79.  
  80. if [ $zabbix_counter -eq 0 ]; then
  81. echo " * Zabbix agent successfully stopped"
  82. echo "-------------------------------------------------------------------"
  83. fi
  84.  
  85. }
  86.  
  87. restart(){
  88. stop
  89. # Gives system some time to stop processes before script restarts service
  90. sleep 10
  91. # Now script can start the agent again
  92. start
  93. }
  94.  
  95. case "$1" in
  96. start)
  97. start
  98. ;;
  99. stop)
  100. stop
  101. ;;
  102. restart)
  103. restart
  104. ;;
  105. *)
  106. echo "Usage: zabbix {start|stop|restart}"
  107. exit 1
  108. esac
  109.  
  110. exit 0
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement