summaryrefslogtreecommitdiffstats
path: root/etc/initsystem/icinga2.init.d.cmake
blob: 56588a737f848ee1d6bda3783af28c2e04e251af (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
#!/bin/sh
#
# chkconfig: 35 90 12
# description: Icinga 2
#
### BEGIN INIT INFO
# Provides:          icinga2
# Required-Start:    $remote_fs $syslog $network
# Required-Stop:     $remote_fs $syslog $network
# Should-Start:      mysql postgresql
# Should-Stop:       mysql postgresql
# Default-Start:     2 3 5
# Default-Stop:      0 1 6
# Short-Description: icinga2 host/service/network monitoring and management system
# Description:       Icinga 2 is a monitoring and management system for hosts, services and networks.
### END INIT INFO

# Get function from functions library
if [ -f /etc/rc.d/init.d/functions ]; then
	. /etc/rc.d/init.d/functions
elif [ -f /etc/init.d/functions ]; then
	. /etc/init.d/functions
fi

# load system specific defines
SYSCONFIGFILE=@ICINGA2_SYSCONFIGFILE@
if [ -f "$SYSCONFIGFILE" ]; then
	. "$SYSCONFIGFILE"
else
	echo "Couldn't load system specific defines from $SYSCONFIGFILE. Using defaults."
fi

# Set defaults, to overwrite see "@ICINGA2_SYSCONFIGFILE@"

: "${ICINGA2_USER:="@ICINGA2_USER@"}"
: "${ICINGA2_GROUP:="@ICINGA2_GROUP@"}"
: "${ICINGA2_COMMAND_GROUP:="@ICINGA2_COMMAND_GROUP@"}"
: "${DAEMON:="@CMAKE_INSTALL_FULL_SBINDIR@/icinga2"}"
: "${ICINGA2_CONFIG_FILE:="@ICINGA2_CONFIGDIR@/icinga2.conf"}"
: "${ICINGA2_ERROR_LOG:=@ICINGA2_LOGDIR@/error.log}"
: "${ICINGA2_STARTUP_LOG:=@ICINGA2_LOGDIR@/startup.log}"
: "${ICINGA2_PID_FILE:="@ICINGA2_INITRUNDIR@/icinga2.pid"}"

# Load extra environment variables
if [ -f /etc/default/icinga2 ]; then
	. /etc/default/icinga2
fi

test -x "$DAEMON" || exit 5

if [ ! -e "$ICINGA2_CONFIG_FILE" ]; then
	echo "Config file '$ICINGA2_CONFIG_FILE' does not exist."
	exit 6
fi

if ! getent passwd "$ICINGA2_USER" >/dev/null 2>&1; then
	echo "Icinga user '$ICINGA2_USER' does not exist. Exiting."
	exit 6
fi

if ! getent group "$ICINGA2_GROUP" >/dev/null 2>&1; then
	echo "Icinga group '$ICINGA2_GROUP' does not exist. Exiting."
	exit 6
fi

if ! getent group "$ICINGA2_COMMAND_GROUP" >/dev/null 2>&1; then
	echo "Icinga command group '$ICINGA2_COMMAND_GROUP' does not exist. Exiting."
	exit 6
fi

# Start Icinga 2
start() {
	printf "Starting Icinga 2: "
	@CMAKE_INSTALL_PREFIX@/lib/icinga2/prepare-dirs "$SYSCONFIGFILE"

	if ! "$DAEMON" daemon -c "$ICINGA2_CONFIG_FILE" -d -e "$ICINGA2_ERROR_LOG" > "$ICINGA2_STARTUP_LOG" 2>&1; then
		echo "Error starting Icinga. Check '$ICINGA2_STARTUP_LOG' for details."
		exit 1
	else
		echo "Done"
	fi
}

# Restart Icinga 2
stop() {
	printf "Stopping Icinga 2: "

	if [ ! -e "$ICINGA2_PID_FILE" ]; then
		echo "The PID file '$ICINGA2_PID_FILE' does not exist."
		if [ "x$1" = "xnofail" ]; then
			return
		else
			exit 7
		fi
	fi

	pid=`cat "$ICINGA2_PID_FILE"`

	if icinga2 internal signal -s SIGINT -p "$pid" >/dev/null 2>&1; then
		for i in 1 2 3 4 5 6 7 8 9 10; do
		if ! icinga2 internal signal -s SIGCHLD -p "$pid" >/dev/null 2>&1; then
				break
			fi

			printf '.'
			sleep 3
		done
	fi

	if icinga2 internal signal -s SIGCHLD -p "$pid" >/dev/null 2>&1; then
		icinga2 internal signal -s SIGKILL -p "$pid" >/dev/null 2>&1
	fi

	echo "Done"
}

# Reload Icinga 2
reload() {
	exec @CMAKE_INSTALL_PREFIX@/lib/icinga2/safe-reload "$SYSCONFIGFILE"
}

# Check the Icinga 2 configuration
checkconfig() {
	printf "Checking configuration: "

	if ! "$DAEMON" daemon -c "$ICINGA2_CONFIG_FILE" -C > "$ICINGA2_STARTUP_LOG" 2>&1; then
		if [ "x$1" = "x" ]; then
			cat "$ICINGA2_STARTUP_LOG"
			echo "Icinga 2 detected configuration errors. Check '$ICINGA2_STARTUP_LOG' for details."
			exit 1
		else
			echo "Not ${1}ing Icinga 2 due to configuration errors. Check '$ICINGA2_STARTUP_LOG' for details."
			if [ "x$2" = "xfail" ]; then
				exit 1
			fi
		fi
	fi

	echo "Done"
	# no arguments requires full output
	if [ "x$1" = "x" ]; then
		cat "$ICINGA2_STARTUP_LOG"
	fi
}

# Print status for Icinga 2
status() {
	printf "Icinga 2 status: "

	if [ ! -e "$ICINGA2_PID_FILE" ]; then
		echo "Not running"
		exit 3
	fi

	pid=`cat "$ICINGA2_PID_FILE"`
	if icinga2 internal signal -s SIGCHLD -p "$pid" >/dev/null 2>&1; then
		echo "Running"
	else
		echo "Not running"
		exit 3
	fi
}

### main logic ###
case "$1" in
	start)
		checkconfig start fail
		start
	;;
	stop)
		stop
	;;
	status)
		status
	;;
	restart)
		checkconfig restart fail
		stop nofail
		start
	;;
	condrestart)
		status > /dev/null 2>&1 || exit 0
		checkconfig restart fail
		stop nofail
		start
	;;
	reload)
		reload
	;;
	checkconfig)
		checkconfig
	;;
	*)
		echo "Usage: $0 {start|stop|restart|reload|checkconfig|status}"
		exit 3
esac

exit 0