]> git.donarmstrong.com Git - dsa-puppet.git/blob - modules/nagios/files/nagios3.init
the debian init script has a bug
[dsa-puppet.git] / modules / nagios / files / nagios3.init
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/sbin/nagios3
27 NAME="nagios3"
28 DESC="nagios3 monitoring daemon"
29 NAGIOSCFG="/etc/nagios3/nagios.cfg"
30 CGICFG="/etc/nagios3/cgi.cfg"
31 NICENESS=5
32
33 [ -x "$DAEMON" ] || exit 0
34 [ -r /etc/default/nagios3 ] && . /etc/default/nagios3
35
36
37 # this is from madduck on IRC, 2006-07-06
38 # There should be a better possibility to give daemon error messages
39 # and/or to log things
40 log()
41 {
42   case "$1" in
43     [[:digit:]]*) success=$1; shift;;
44     *) :;;
45   esac
46   log_action_begin_msg "$1"; shift
47   log_action_end_msg ${success:-0} "$*"
48 }
49
50 check_started () {
51         #nagios3-core can be installed without -cgi
52         if [ -e $CGICFG ];
53         then
54                 check_cmd=$(get_config nagios_check_command $CGICFG)
55                 if [ ! "$check_cmd" ]; then
56                         log 6 "unable to determine nagios_check_command from $CGICFG!" 
57                         return 6
58                 fi
59         else 
60                 #use hardcoded default version
61                 check_cmd="/usr/lib/nagios/plugins/check_nagios /var/cache/nagios3/status.dat 5 '/usr/sbin/nagios3'"
62         fi
63
64   eval $check_cmd >/dev/null
65                 
66   if [ -f "$THEPIDFILE" ]; then
67     pid="$(cat $THEPIDFILE)"
68     if [ "$pid" ] && kill -0 $pid >/dev/null 2>/dev/null; then
69       return 0    # Is started
70     fi
71   fi
72   return 1      # Isn't started
73 }
74
75 #
76 #       get_config()
77 #
78 #       grab a config option from nagios.cfg (or possibly another nagios config
79 #       file if specified).  everything after the '=' is echo'd out, making
80 #       this a nice generalized way to get requested settings.
81 #
82 get_config () {
83   if [ "$2" ]; then
84     set -- `grep ^$1 $2 | sed 's@=@ @'`
85   else
86     set -- `grep ^$1 $NAGIOSCFG | sed 's@=@ @'`
87   fi
88   shift
89   echo $*
90 }
91
92 check_config () {
93   if $DAEMON -v $NAGIOSCFG >/dev/null 2>&1 ; then
94     # First get the user/group etc Nagios is running as
95     nagios_user="$(get_config nagios_user)"
96     nagios_group="$(get_config nagios_group)"
97     log_file="$(get_config log_file)"
98     log_dir="$(dirname $log_file)"
99
100     return 0    # Config is ok
101   else
102     # config is not okay, so let's barf the error to the user
103     $DAEMON -v $NAGIOSCFG
104   fi
105 }
106
107 check_named_pipe () {
108   nagiospipe="$(get_config command_file)"
109   if [ -p "$nagiospipe" ]; then
110     return 1   # a named pipe exists
111   elif [ -e "$nagiospipe" ];then
112     return 1
113   else
114     return 0   # no named pipe exists
115   fi
116 }
117
118 if [ ! -f "$NAGIOSCFG" ]; then
119   log_failure_msg "There is no configuration file for Nagios 3."
120   exit 6
121 fi
122
123 THEPIDFILE=$(get_config "lock_file")
124 [ -n "$THEPIDFILE" ] || THEPIDFILE='/var/run/nagios3/nagios.pid'
125
126 start () {
127   DIRECTORY=$(dirname $THEPIDFILE)
128   [ ! -d $DIRECTORY ] && mkdir -p $DIRECTORY
129   chown nagios:nagios $DIRECTORY
130
131   if ! check_started; then
132     if ! check_named_pipe; then
133       log_action_msg "named pipe exists - removing"
134       rm -f $nagiospipe
135     fi
136     if check_config; then
137       start_daemon -n $NICENESS -p $THEPIDFILE $DAEMON -d $NAGIOSCFG
138       ret=$?
139     else
140       log_failure_msg "errors in config!"
141       log_end_msg 1
142       exit 1
143     fi
144   else
145     log_warning_msg "already running!"
146   fi
147   return $ret
148 }
149
150 stop () {
151     killproc -p $THEPIDFILE
152     ret=$?
153     if [ `pidof nagios3 | wc -l ` -gt 0 ]; then
154         echo -n "Waiting for $NAME daemon to die.."
155         cnt=0
156         while [ `pidof nagios3 | wc -l ` -gt 0 ]; do
157             cnt=`expr "$cnt" + 1`
158             if [ "$cnt" -gt 15 ]; then
159                 kill -9 `pidof nagios3`
160                 break
161             fi
162             sleep 1
163             echo -n "."
164         done
165     fi
166     echo
167     if ! check_named_pipe; then
168       rm -f $nagiospipe
169     fi
170     if [ -n "$ret" ]; then
171       return $ret
172     else
173       return $?
174     fi
175 }
176
177 status()
178 {
179   log_action_begin_msg "checking $DAEMON"
180   if check_started; then
181     log_action_end_msg 0 "running"
182   else
183     if [ -e "$THEPIDFILE" ]; then
184       log_action_end_msg 1 "$DAEMON failed"
185       exit 1
186     else
187       log_action_end_msg 1 "not running"
188       exit 3
189     fi
190   fi
191 }
192
193
194 reload () {
195   # Check first
196   if check_config; then
197     if check_started; then
198       killproc -p $THEPIDFILE $DAEMON 1 
199     else
200       log_warning_msg "Not running."
201     fi
202   else
203     log_failure_msg "errors in config!"
204     log_end_msg 6
205     exit 6
206  fi
207 }
208
209 check () {
210    $DAEMON -v $NAGIOSCFG
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