]> granicus.if.org Git - icinga2/blob - debian/icinga2-common.icinga2.init
Fix the .bundle validation.
[icinga2] / debian / icinga2-common.icinga2.init
1 #! /bin/sh
2 ### BEGIN INIT INFO
3 # Provides:          icinga2
4 # Required-Start:    $remote_fs $syslog
5 # Required-Stop:     $remote_fs $syslog
6 # Default-Start:     2 3 4 5
7 # Default-Stop:      0 1 6
8 # Short-Description: icinga2 host/service/network monitoring and management system
9 # Description:       icinga2 is a monitoring and management system for hosts, services and networks.
10 ### END INIT INFO
11
12 # Author: Alexander Wirt <formorer@debian.org>
13 #         Markus Frosch <markus@lazyfrosch.de>
14
15 # PATH should only include /usr/* if it runs after the mountnfs.sh script
16 PATH=/sbin:/usr/sbin:/bin:/usr/bin
17 DESC="icinga2 monitoring daemon"
18 NAME=icinga2
19 DAEMON=/usr/sbin/icinga2
20 DAEMON_CONFIG="/etc/icinga2/icinga2.conf"
21 DAEMON_USER=nagios
22 DAEMON_GROUP=nagios
23 DAEMON_CMDGROUP=www-data
24 DAEMON_ARGS="-e /var/log/icinga2/icinga2.err"
25 PIDFILE=/var/run/icinga2/$NAME.pid
26 SCRIPTNAME=/etc/init.d/$NAME
27
28 # Exit if the package is not installed
29 [ -x "$DAEMON" ] || exit 0
30
31 # Read configuration variable file if it is present
32 [ -r /etc/default/$NAME ] && . /etc/default/$NAME
33
34 # Load the VERBOSE setting and other rcS variables
35 . /lib/init/vars.sh
36
37 # Define LSB log_* functions.
38 # Depend on lsb-base (>= 3.2-14) to ensure that this file is present
39 # and status_of_proc is working.
40 . /lib/lsb/init-functions
41
42 # check run directory
43 check_run () {
44   test -d '/var/run/icinga2' || mkdir /var/run/icinga2
45   test -d '/var/run/icinga2/cmd' || mkdir /var/run/icinga2/cmd
46
47   chown "$DAEMON_USER":"$DAEMON_GROUP" /var/run/icinga2
48   chmod 0755 /var/run/icinga2
49
50   chown "$DAEMON_USER":"$DAEMON_CMDGROUP" /var/run/icinga2/cmd
51   chmod 2710 /var/run/icinga2/cmd
52 }
53
54 check_config () {
55   $DAEMON --validate -u "$DAEMON_USER" -g "$DAEMON_GROUP" -c "$DAEMON_CONFIG"
56 }
57
58 #
59 # Function that starts the daemon/service
60 #
61 do_start()
62 {
63   # Return
64   #   0 if daemon has been started
65   #   1 if daemon was already running
66   #   2 if daemon could not be started
67   start-stop-daemon --start --quiet --pidfile $PIDFILE --exec $DAEMON --test > /dev/null \
68     || return 1
69   start-stop-daemon --start --quiet --pidfile $PIDFILE --exec $DAEMON -- \
70     -c "$DAEMON_CONFIG" -u "$DAEMON_USER" -g "$DAEMON_GROUP" -d $DAEMON_ARGS \
71     || return 2
72   # Add code here, if necessary, that waits for the process to be ready
73   # to handle requests from services started subsequently which depend
74   # on this one.  As a last resort, sleep for some time.
75 }
76
77 #
78 # Function that starts the daemon/service in foreground for debugging
79 #
80 do_foreground()
81 {
82   start-stop-daemon --start --quiet --pidfile $PIDFILE --exec $DAEMON --test \
83     || return 1
84   start-stop-daemon --start --pidfile $PIDFILE --exec $DAEMON -- \
85     -c "$DAEMON_CONFIG" -u "$DAEMON_USER" -g "$DAEMON_GROUP" $DAEMON_ARGS \
86     || return 2
87 }
88
89 #
90 # Function that stops the daemon/service
91 #
92 do_stop()
93 {
94   # Return
95   #   0 if daemon has been stopped
96   #   1 if daemon was already stopped
97   #   2 if daemon could not be stopped
98   #   other if a failure occurred
99   start-stop-daemon --stop --quiet --retry=TERM/30/KILL/5 --pidfile $PIDFILE --name $NAME
100   RETVAL="$?"
101   [ "$RETVAL" = 2 ] && return 2
102   # Wait for children to finish too if this is a daemon that forks
103   # and if the daemon is only ever run from this initscript.
104   # If the above conditions are not satisfied then add some other code
105   # that waits for the process to drop all resources that could be
106   # needed by services started subsequently.  A last resort is to
107   # sleep for some time.
108   start-stop-daemon --stop --quiet --oknodo --retry=0/30/KILL/5 --exec $DAEMON
109   [ "$?" = 2 ] && return 2
110   # Many daemons don't delete their pidfiles when they exit.
111   rm -f $PIDFILE
112   return "$RETVAL"
113 }
114
115 #
116 # Function that sends a SIGHUP to the daemon/service
117 #
118 do_reload() {
119   #
120   # If the daemon can reload its configuration without
121   # restarting (for example, when it is sent a SIGHUP),
122   # then implement that here.
123   #
124   start-stop-daemon --stop --signal 1 --quiet --pidfile $PIDFILE --name $NAME
125   return 0
126 }
127
128 do_check_config () {
129   DOEXITONOK="$1"
130   log_begin_msg "checking Icinga2 configuration"
131   if ! check_config >/dev/null 2>&1; then
132     echo
133     check_config
134     log_failure_msg "checking Icinga2 configuration"
135     exit 1
136   else
137     log_end_msg 0
138     [ -n "$DOEXITONOK" ] && exit 0
139   fi
140 }
141
142 check_run
143
144 case "$1" in
145   start)
146     do_check_config
147     log_daemon_msg "Starting $DESC" "$NAME"
148     do_start
149     case "$?" in
150       0|1) log_end_msg 0 ;;
151       2) log_end_msg 1 ;;
152     esac
153     ;;
154   foreground)
155     do_check_config
156     log_action_msg "Starting $DESC in foreground"
157     status=0
158     trap 'status=2;' INT # handle intr here
159     for i in 1 2; do
160       do_foreground || status="$?"
161       case "$status" in
162         0)
163           log_success_msg "$NAME ended graceful"
164           break
165           ;;
166         1)
167           if [ "$i" -eq 1 ]; then
168             log_warning_msg "$NAME already running!"
169             log_begin_msg "Do you want to stop $NAME before going foreground? [y/N] "
170             read -r question
171             if [ "$question" = "y" ] || [ "$question" = "yes" ]; then
172               do_stop
173               continue
174             fi
175           fi
176           log_failure_msg "$NAME already running! Please stop before starting in foreground!"
177           break
178           ;;
179         2)
180           log_failure_msg "$NAME ended"
181           break
182           ;;
183       esac
184     done
185     exit $status
186     ;;
187   stop)
188     log_daemon_msg "Stopping $DESC" "$NAME"
189     do_stop
190     case "$?" in
191       0|1) log_end_msg 0 ;;
192       2) log_end_msg 1 ;;
193     esac
194     ;;
195   status)
196     status_of_proc "$DAEMON" "$NAME" && exit 0 || exit $?
197     ;;
198   reload|force-reload)
199     do_check_config
200     log_daemon_msg "Reloading $DESC" "$NAME"
201     do_reload
202     log_end_msg $?
203     ;;
204   restart)
205     do_check_config
206     log_daemon_msg "Restarting $DESC" "$NAME"
207     do_stop
208     case "$?" in
209       0|1)
210         do_start
211         case "$?" in
212           0) log_end_msg 0 ;;
213           1) log_end_msg 1 ;; # Old process is still running
214           *) log_end_msg 1 ;; # Failed to start
215         esac
216         ;;
217       *)
218         # Failed to stop
219         log_end_msg 1
220         ;;
221     esac
222   ;;
223   checkconfig)
224     do_check_config 1
225     ;;
226   *)
227     echo "Usage: $SCRIPTNAME {start|stop|status|reload|force-reload|restart|checkconfig|foreground}" >&2
228     exit 3
229     ;;
230 esac
231
232 # vi: ts=2 sw=2 expandtab :
233 :