Advertisement
Guest User

Untitled

a guest
Oct 31st, 2014
151
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Bash 5.88 KB | None | 0 0
  1. #! /bin/sh
  2. #       Written by Miquel van Smoorenburg miquels@cistron.nl
  3. #       Modified for Debian GNU/Linux
  4. #       by Ian Murdock imurdock@gnu.ai.mit.edu.
  5. #               Clamav version by Magnus Ekdahl magnus@debian.org
  6. #       Nagios version by Sean Finney seanius@debian.org and probably others
  7. #       nagios2 version by Marc Haber mh+debian-packages@zugschlus.de
  8. #       nagios3 version by Alexander Wirt formorer@debian.org
  9.  
  10. ### BEGIN INIT INFO
  11. # Provides:          nagios3
  12. # Required-Start:    $local_fs $remote_fs $syslog $named $network $time
  13. # Required-Stop:     $local_fs $remote_fs $syslog $named $network
  14. # Should-Start:      
  15. # Should-Stop:      
  16. # Default-Start:     2 3 4 5
  17. # Default-Stop:      0 1 6
  18. # Short-Description: nagios host/service/network monitoring and management system
  19. # Description:       nagios is a monitoring and management system for hosts, services and networks.
  20. ### END INIT INFO
  21.  
  22. set -e
  23.  
  24. . /lib/lsb/init-functions
  25.  
  26. DAEMON=/usr/local/nagios/bin/nagios
  27. NAME="nagios"
  28. DESC="nagios 3.99 monitoring daemon"
  29. NAGIOSCFG="/etc/nagios/nagios.cfg"
  30. CGICFG="/etc/nagios/cgi.cfg"
  31. NICENESS=5
  32.  
  33. [ -x "$DAEMON" ] || exit 0
  34. [ -r /etc/default/nagios ] && . /etc/default/nagios
  35.  
  36. # this is from madduck on IRC, 2006-07-06
  37. # There should be a better possibility to give daemon error messages
  38. # and/or to log things
  39. log()
  40. {
  41.   case "$1" in
  42.     [[:digit:]]*) success=$1; shift;;
  43.     *) :;;
  44.   esac
  45.   log_action_begin_msg "$1"; shift
  46.   log_action_end_msg ${success:-0} "$*"
  47. }
  48.  
  49. check_started () {
  50.     #nagios3-core can be installed without -cgi
  51.     if [ -e $CGICFG ];
  52.     then
  53.         check_cmd=$(get_config nagios_check_command $CGICFG)
  54.         if [ ! "$check_cmd" ]; then
  55.             log 6 "unable to determine nagios_check_command from $CGICFG!"
  56.             return 6
  57.         fi
  58.     else
  59.         #use hardcoded default version
  60.         check_cmd="/usr/lib/nagios/plugins/check_nagios /var/cache/nagios3/status.dat 5 '/usr/local/nagios/bin/nagios'"
  61.     fi
  62.  
  63.   eval $check_cmd >/dev/null
  64.        
  65.   if [ -f "$THEPIDFILE" ]; then
  66.     pid="$(cat $THEPIDFILE)"
  67.     if [ "$pid" ] && kill -0 $pid >/dev/null 2>/dev/null; then
  68.       return 0    # Is started
  69.     fi
  70.   fi
  71.   return 1  # Isn't started
  72. }
  73.  
  74. #
  75. #   get_config()
  76. #
  77. #   grab a config option from nagios.cfg (or possibly another nagios config
  78. #   file if specified).  everything after the '=' is echo'd out, making
  79. #   this a nice generalized way to get requested settings.
  80. #
  81. get_config () {
  82.   if [ "$2" ]; then
  83.     set -- `grep ^$1 $2 | sed 's@=@ @'`
  84.   else
  85.     set -- `grep ^$1 $NAGIOSCFG | sed 's@=@ @'`
  86.   fi
  87.   shift
  88.   echo $*
  89. }
  90.  
  91. check_config () {
  92.   if $DAEMON -v $NAGIOSCFG >/dev/null 2>&1 ; then
  93.     # First get the user/group etc Nagios is running as
  94.     nagios_user="$(get_config nagios_user)"
  95.     nagios_group="$(get_config nagios_group)"
  96.     log_file="$(get_config log_file)"
  97.     log_dir="$(dirname $log_file)"
  98.  
  99.     return 0    # Config is ok
  100.   else
  101.     # config is not okay, so let's barf the error to the user
  102.     $DAEMON -v $NAGIOSCFG
  103.   fi
  104. }
  105.  
  106. check_named_pipe () {
  107.   nagiospipe="$(get_config command_file)"
  108.   if [ -p "$nagiospipe" ]; then
  109.     return 1   # a named pipe exists
  110.   elif [ -e "$nagiospipe" ];then
  111.     return 1
  112.   else
  113.     return 0   # no named pipe exists
  114.   fi
  115. }
  116.  
  117. if [ ! -f "$NAGIOSCFG" ]; then
  118.   log_failure_msg "There is no configuration file for Nagios 3."
  119.   exit 6
  120. fi
  121.  
  122. THEPIDFILE=$(get_config "lock_file")
  123. [ -n "$THEPIDFILE" ] || THEPIDFILE='/var/run/nagios.pid'
  124.  
  125. start () {
  126.  
  127.   if [ "$ENABLED" = "no"  ]; then
  128.       log_warning_msg "Not starting Nagios3 - set ENABLED to yes in /etc/default/nagios"
  129.       exit 0
  130.   fi
  131.  
  132.   DIRECTORY=$(dirname $THEPIDFILE)
  133.   [ ! -d $DIRECTORY ] && mkdir -p $DIRECTORY
  134.   chown nagios:nagios $DIRECTORY
  135.  
  136.   if ! check_started; then
  137.     if ! check_named_pipe; then
  138.       log_action_msg "named pipe exists - removing"
  139.       rm -f $nagiospipe
  140.     fi
  141.     if check_config; then
  142.       start_daemon -n $NICENESS -p $THEPIDFILE $DAEMON -d $NAGIOSCFG
  143.       ret=$?
  144.     else
  145.       log_failure_msg "errors in config!"
  146.       log_end_msg 1
  147.       exit 1
  148.     fi
  149.   else
  150.     log_warning_msg "already running!"
  151.   fi
  152.   return $ret
  153. }
  154.  
  155. stop () {
  156.     killproc -p $THEPIDFILE
  157.     ret=$?
  158.     if [ `pidof nagios3 | wc -l ` -gt 0 ]; then
  159.         echo -n "Waiting for $NAME daemon to die.."
  160.         cnt=0
  161.         while [ `pidof nagios3 | wc -l ` -gt 0 ]; do
  162.             cnt=`expr "$cnt" + 1`
  163.             if [ "$cnt" -gt 15 ]; then
  164.                 kill -9 `pidof nagios`
  165.                 break
  166.             fi
  167.             sleep 1
  168.             echo -n "."
  169.         done
  170.     fi
  171.     echo
  172.     if ! check_named_pipe; then
  173.       rm -f $nagiospipe
  174.     fi
  175.     if [ -n "$ret" ]; then
  176.       return $ret
  177.     else
  178.       return $?
  179.     fi
  180. }
  181.  
  182. status()
  183. {
  184.   log_action_begin_msg "checking $DAEMON"
  185.   if check_started; then
  186.     log_action_end_msg 0 "running"
  187.   else
  188.     if [ -e "$THEPIDFILE" ]; then
  189.       log_action_end_msg 1 "$DAEMON failed"
  190.       exit 1
  191.     else
  192.       log_action_end_msg 1 "not running"
  193.       exit 3
  194.     fi
  195.   fi
  196. }
  197.  
  198. reload () {
  199.   # Check first
  200.   if check_config; then
  201.     if check_started; then
  202.       killproc -p $THEPIDFILE $DAEMON 1
  203.     else
  204.       log_warning_msg "Not running."
  205.     fi
  206.   else
  207.     log_failure_msg "errors in config!"
  208.     log_end_msg 6
  209.     exit 6
  210.  fi
  211. }
  212.  
  213. case "$1" in
  214.   start)
  215.     log_daemon_msg "Starting $DESC" "$NAME"
  216.     start
  217.     log_end_msg $?
  218.     ;;
  219.   stop)
  220.     log_daemon_msg "Stopping $DESC" "$NAME"
  221.     stop
  222.     log_end_msg $?
  223.   ;;
  224.   restart)
  225.     log_daemon_msg "Restarting $DESC" "$NAME"
  226.     stop
  227.     if [ -z "$?" -o "$?" = "0" ]; then
  228.       start
  229.     fi
  230.     log_end_msg $?
  231.   ;;
  232.   reload|force-reload)
  233.     log_daemon_msg "Reloading $DESC configuration files" "$NAME"
  234.     reload
  235.     log_end_msg $?
  236.   ;;
  237.   status)
  238.     status
  239.     ;;
  240.   check)
  241.     check
  242.     ;;
  243.   *)
  244.     log_failure_msg "Usage: $0 {start|stop|restart|reload|force-reload|status}" >&2
  245.     exit 1
  246.   ;;
  247. esac
  248.  
  249. exit 0
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement