forked from naemon/naemon-core
-
Notifications
You must be signed in to change notification settings - Fork 0
/
daemon-init.in
219 lines (200 loc) · 5.12 KB
/
daemon-init.in
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
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
#!/bin/sh
# Naemon - Startup script for the Naemon monitoring daemon
#
# chkconfig: - 85 15
# description: Naemon is a service monitoring system
# processname: naemon
# config: @sysconfdir@/naemon.cfg
# pidfile: @lockfile@
#
### BEGIN INIT INFO
# Provides: naemon
# Required-Start: $local_fs $remote_fs $syslog $network
# Required-Stop: $local_fs $remote_fs $syslog $network
# Should-Start:
# Should-Stop:
# Default-Start: 3 5
# Default-Stop: 0 1 2 6
# Short-Description: start and stop Naemon monitoring server
# Description: Naemon is is a service monitoring system
### END INIT INFO
# Source function library.
if [ -e /etc/rc.d/init.d/functions ]; then
. /etc/rc.d/init.d/functions
fi
prefix="@prefix@"
exec_prefix="@exec_prefix@"
exec="@bindir@/naemon"
prog="naemon"
config="@sysconfdir@/naemon.cfg"
pidfile="@lockfile@"
pidfolder="`dirname $pidfile`"
user="naemon"
group="naemon"
checkconfig="false"
# Default nice for performance, change in sysconfig file not here!
NICELEVEL=0
test -e /etc/sysconfig/$prog && . /etc/sysconfig/$prog
lockfile=/var/lock/subsys/$prog
status() {
[ ! -e $pidfile ] && return 1;
PID=`cat $pidfile`;
kill -0 $PID
return $?
}
pidof_naemon() {
if [ -e "$pidfile" ]; then
if pidof $prog | tr ' ' '\n' | grep -w $(cat $pidfile); then
return 0
fi
fi
return 1
}
check_config() {
TMPFILE=$(mktemp /tmp/.configtest.XXXXXXXX)
$0 configtest > "$TMPFILE"
WARN=`grep ^"Total Warnings:" "$TMPFILE" |awk -F: '{print \$2}' |sed s/' '//g`
ERR=`grep ^"Total Errors:" "$TMPFILE" |awk -F: '{print \$2}' |sed s/' '//g`
if test "$WARN" = "0" && test "${ERR}" = "0"; then
echo "OK - Configuration check verified" > @TMPDIR@/naemon.configtest
chmod 0644 @TMPDIR@/naemon.configtest
/bin/rm "$TMPFILE"
return 0
else
# We'll write out the errors to a file we can have a
# script watching for
echo "WARNING: Errors in config files - see log for details: $TMPFILE" > @TMPDIR@/naemon.configtest
egrep -i "(^warning|^error)" "$TMPFILE" >> @TMPDIR@/naemon.configtest
chmod 0644 @TMPDIR@/naemon.configtest
cat "$TMPFILE"
exit 8
fi
}
naemon_wait_stop() {
pidtmp=$(pidof_naemon) || true
if kill -0 "${pidtmp:-}" 2> /dev/null; then
pid=$pidtmp
fi
stop
# wait until really stopped
if [ -n "${pid:-}" ]; then
i=0
while kill -0 "${pid:-}" 2> /dev/null; do
if [ $i = '60' ]; then
break;
else
if [ $i = '0' ]; then
echo -n " ... waiting "
else
echo -n "."
fi
i=$(($i+1))
sleep 1
fi
done
fi
}
start() {
test -x $exec || exit 5
test -f $config || exit 6
if [ "$checkconfig" = "false" ]; then
check_config
fi
echo -n "Starting $prog: "
# Create PID folder if it does not exist
if [ ! -d $pidfolder ]; then
mkdir $pidfolder
chown $user:$group $pidfolder
fi
# We need to _make sure_ the precache is there and verified
# Raise priority to make it run better
if type start-stop-daemon >/dev/null 2>&1; then
NICE_OPT=""
if [ $NICELEVEL != "0" ]; then NICE_OPT="--nicelevel $NICELEVEL"; fi
start-stop-daemon $NICE_OPT --start --user $user --name $prog --pidfile $pidfile --exec $exec -- -d $config
else
daemon --user=$user $exec -d $config
fi
retval=$?
echo
if [ -d /var/lock/subsys ]; then
test $retval -eq 0 && touch $lockfile
fi
return $retval
}
stop() {
echo -n "Stopping $prog: "
pid=$(pidof_naemon) || true
if [ "${pid}" ]; then
kill $pid
else
return 1
fi
retval=$?
echo
test $retval -eq 0 && rm -f $lockfile
return $retval
}
restart() {
check_config
checkconfig="true"
pid=$(pidof_naemon) || true
naemon_wait_stop
echo
start
}
reload() {
echo -n "Reloading $prog: "
pkill -HUP -u ${user} -f ${exec}
retval=$?
echo
}
force_reload() {
restart
}
case "$1" in
start)
status $prog && exit 0
$1
;;
stop)
status $prog|| exit 0
$1
;;
restart)
$1
;;
reload)
status $prog || exit 7
$1
;;
force-reload)
force_reload
;;
status)
if status $prog; then
PID=`cat $pidfile`;
echo "naemon (pid $PID) is running..."
exit 0
else
echo "naemon is stopped"
exit 1
fi
;;
condrestart|try-restart)
status $prog|| exit 0
restart
;;
configtest|check|checkconfig)
if type runuser >/dev/null 2>&1; then
runuser -s /bin/bash - $user -c "$corelimit >/dev/null 2>&1 ; $exec -vp $config"
else
/bin/su - -s /bin/sh $user -c "$corelimit >/dev/null 2>&1 ; $exec -vp $config"
fi
RETVAL=$?
;;
*)
echo "Usage: $0 {start|stop|status|restart|condrestart|try-restart|reload|force-reload|configtest}"
exit 2
esac
exit $?