Guest User

Untitled

a guest
Sep 2nd, 2018
126
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Bash 5.44 KB | None | 0 0
  1. #!/bin/sh
  2. #
  3. # chkconfig: 345 99 01
  4. # description: Nagios network monitor
  5. #
  6. # File : nagios
  7. #
  8. # Author : Jorge Sanchez Aymar (jsanchez@lanchile.cl)
  9. #
  10. # Changelog :
  11. #
  12. # 1999-07-09 Karl DeBisschop <kdebisschop@infoplease.com>
  13. #  - setup for autoconf
  14. #  - add reload function
  15. # 1999-08-06 Ethan Galstad <egalstad@nagios.org>
  16. #  - Added configuration info for use with RedHat's chkconfig tool
  17. #    per Fran Boon's suggestion
  18. # 1999-08-13 Jim Popovitch <jimpop@rocketship.com>
  19. #  - added variable for nagios/var directory
  20. #  - cd into nagios/var directory before creating tmp files on startup
  21. # 1999-08-16 Ethan Galstad <egalstad@nagios.org>
  22. #  - Added test for rc.d directory as suggested by Karl DeBisschop
  23. # 2000-07-23 Karl DeBisschop <kdebisschop@users.sourceforge.net>
  24. #  - Clean out redhat macros and other dependencies
  25. # 2003-01-11 Ethan Galstad <egalstad@nagios.org>
  26. #  - Updated su syntax (Gary Miller)
  27. #
  28. # Description: Starts and stops the Nagios monitor
  29. #              used to provide network services status.
  30. #
  31.  
  32. # Load any extra environment variables for Nagios and its plugins
  33. if test -f /etc/sysconfig/nagios; then
  34.     . /etc/sysconfig/nagios
  35. fi
  36.  
  37. status_nagios ()
  38. {
  39.  
  40.     if test -x $NagiosCGI/daemonchk.cgi; then
  41.         if $NagiosCGI/daemonchk.cgi -l $NagiosRunFile; then
  42.                 return 0
  43.         else
  44.             return 1
  45.         fi
  46.     else
  47.         if ps -p $NagiosPID > /dev/null 2>&1; then
  48.                 return 0
  49.         else
  50.             return 1
  51.         fi
  52.     fi
  53.  
  54.     return 1
  55. }
  56.  
  57.  
  58. printstatus_nagios()
  59. {
  60.  
  61.     if status_nagios $1 $2; then
  62.         echo "nagios (pid $NagiosPID) is running..."
  63.     else
  64.         echo "nagios is not running"
  65.     fi
  66. }
  67.  
  68.  
  69. killproc_nagios ()
  70. {
  71.  
  72.     kill $2 $NagiosPID
  73.  
  74. }
  75.  
  76.  
  77. pid_nagios ()
  78. {
  79.  
  80.     if test ! -f $NagiosRunFile; then
  81.         echo "No lock file found in $NagiosRunFile"
  82.         exit 1
  83.     fi
  84.  
  85.     NagiosPID=`head -n 1 $NagiosRunFile`
  86. }
  87.  
  88.  
  89. # Source function library
  90. # Solaris doesn't have an rc.d directory, so do a test first
  91. if [ -f /etc/rc.d/init.d/functions ]; then
  92.     . /etc/rc.d/init.d/functions
  93. elif [ -f /etc/init.d/functions ]; then
  94.     . /etc/init.d/functions
  95. fi
  96.  
  97. prefix=/usr
  98. exec_prefix=/usr
  99. NagiosBin=/usr/bin/nagios
  100. NagiosCfgFile=/etc/nagios/nagios.cfg
  101. NagiosStatusFile=/var/log/nagios/status.dat
  102. NagiosRetentionFile=/var/log/nagios/retention.dat
  103. NagiosCommandFile=/var/log/nagios/rw/nagios.cmd
  104. NagiosVarDir=/var/log/nagios
  105. #NagiosRunFile=/var/log/nagios/nagios.pid
  106. NagiosRunFile=/var/log/nagios/nagios.pid
  107. NagiosLockDir=/var/lock/subsys
  108. NagiosLockFile=nagios
  109. NagiosCGIDir=/usr/lib/nagios/cgi
  110. NagiosUser=nagios
  111. NagiosGroup=nagios
  112.          
  113.  
  114. # Check that nagios exists.
  115. if [ ! -f $NagiosBin ]; then
  116.     echo "Executable file $NagiosBin not found.  Exiting."
  117.     exit 1
  118. fi
  119.  
  120. # Check that nagios.cfg exists.
  121. if [ ! -f $NagiosCfgFile ]; then
  122.     echo "Configuration file $NagiosCfgFile not found.  Exiting."
  123.     exit 1
  124. fi
  125.          
  126. # See how we were called.
  127. case "$1" in
  128.  
  129.     start)
  130.         echo -n "Starting nagios:"
  131.         $NagiosBin -v $NagiosCfgFile > /dev/null 2>&1;
  132.         if [ $? -eq 0 ]; then
  133.             su - $NagiosUser -c "touch $NagiosVarDir/nagios.log $NagiosRetentionFile"
  134.             rm -f $NagiosCommandFile
  135.             touch $NagiosRunFile
  136.             chown $NagiosUser:$NagiosGroup $NagiosRunFile
  137.             $NagiosBin -d $NagiosCfgFile
  138.             if [ -d $NagiosLockDir ]; then touch $NagiosLockDir/$NagiosLockFile; fi
  139.             echo " done."
  140.             exit 0
  141.         else
  142.             echo "CONFIG ERROR!  Start aborted.  Check your Nagios configuration."
  143.             exit 1
  144.         fi
  145.         ;;
  146.  
  147.     stop)
  148.         echo -n "Stopping nagios: "
  149.  
  150.         pid_nagios
  151.         killproc_nagios nagios
  152.  
  153.         # now we have to wait for nagios to exit and remove its
  154.         # own NagiosRunFile, otherwise a following "start" could
  155.         # happen, and then the exiting nagios will remove the
  156.         # new NagiosRunFile, allowing multiple nagios daemons
  157.         # to (sooner or later) run - John Sellens
  158.         #echo -n 'Waiting for nagios to exit .'
  159.         for i in 1 2 3 4 5 6 7 8 9 10 ; do
  160.             if status_nagios > /dev/null; then
  161.             echo -n '.'
  162.             sleep 1
  163.             else
  164.             break
  165.             fi
  166.         done
  167.         if status_nagios > /dev/null; then
  168.             echo ''
  169.             echo 'Warning - nagios did not exit in a timely manner'
  170.         else
  171.             echo 'done.'
  172.         fi
  173.  
  174.         rm -f $NagiosStatusFile $NagiosRunFile $NagiosLockDir/$NagiosLockFile $NagiosCommandFile
  175.         ;;
  176.  
  177.     status)
  178.         pid_nagios
  179.         printstatus_nagios nagios
  180.         ;;
  181.  
  182.     checkconfig)
  183.         printf "Running configuration check..."
  184.         $NagiosBin -v $NagiosCfgFile > /dev/null 2>&1;
  185.         if [ $? -eq 0 ]; then
  186.             echo " OK."
  187.         else
  188.             echo " CONFIG ERROR!  Check your Nagios configuration."
  189.             exit 1
  190.         fi
  191.         ;;
  192.  
  193.     restart)
  194.         printf "Running configuration check..."
  195.         $NagiosBin -v $NagiosCfgFile > /dev/null 2>&1;
  196.         if [ $? -eq 0 ]; then
  197.             echo "done."
  198.             $0 stop
  199.             $0 start
  200.         else
  201.             echo " CONFIG ERROR!  Restart aborted.  Check your Nagios configuration."
  202.             exit 1
  203.         fi
  204.         ;;
  205.  
  206.     reload|force-reload)
  207.         printf "Running configuration check..."
  208.         $NagiosBin -v $NagiosCfgFile > /dev/null 2>&1;
  209.         if [ $? -eq 0 ]; then
  210.             echo "done."
  211.             if test ! -f $NagiosRunFile; then
  212.                 $0 start
  213.             else
  214.                 pid_nagios
  215.                 if status_nagios > /dev/null; then
  216.                     printf "Reloading nagios configuration..."
  217.                     killproc_nagios nagios -HUP
  218.                     echo "done"
  219.                 else
  220.                     $0 stop
  221.                     $0 start
  222.                 fi
  223.             fi
  224.         else
  225.             echo " CONFIG ERROR!  Reload aborted.  Check your Nagios configuration."
  226.             exit 1
  227.         fi
  228.         ;;
  229.  
  230.     *)
  231.         echo "Usage: nagios {start|stop|restart|reload|force-reload|status|checkconfig}"
  232.         exit 1
  233.         ;;
  234.  
  235. esac
  236.  
  237. # End of this script
Add Comment
Please, Sign In to add comment