-
Notifications
You must be signed in to change notification settings - Fork 90
/
mackerel-agent.initd
173 lines (143 loc) · 3.29 KB
/
mackerel-agent.initd
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
#!/bin/sh
#
# /etc/rc.d/init.d/mackerel-agent
#
# Daemon for mackerel.io
#
# chkconfig: 2345 95 5
# description: Daemon for mackerel.io
### BEGIN INIT INFO
# Provides: mackerel-agent
# Short-Description: start and stop mackerel-agent
# Description: Daemon for mackerel.io
### END INIT INFO
# Source function library.
. /etc/rc.d/init.d/functions
prog="mackerel-agent"
[ -e /etc/sysconfig/$prog ] && . /etc/sysconfig/$prog
BIN=${BIN:="/usr/bin/$prog"}
LOGFILE=${LOGILE:="/var/log/$prog.log"}
PIDFILE=${PIDFILE:="/var/run/$prog.pid"}
ROOT=${ROOT:="/var/lib/$prog"}
MACKEREL_PLUGIN_WORKDIR=${MACKEREL_PLUGIN_WORKDIR:="/var/tmp/$prog"}
if [ "$HTTP_PROXY" != "" ]; then
export HTTP_PROXY=$HTTP_PROXY
fi
lockfile=/var/lock/subsys/$prog
# if matched return 0, else return 1
prefix_match() {
[ "${1#$2*}" = "$1" ] && return 1
return 0
}
start() {
[ -x $exec ] || exit 5
echo -n $"Starting $prog:"
mkdir -m 777 -p $MACKEREL_PLUGIN_WORKDIR
MACKEREL_PLUGIN_WORKDIR=$MACKEREL_PLUGIN_WORKDIR $BIN ${APIBASE:+--apibase=$APIBASE} ${APIKEY:+--apikey=$APIKEY} --pidfile=$PIDFILE --root=$ROOT $OTHER_OPTS >>$LOGFILE 2>&1 &
sleep 3
if ! kill -0 $(cat $PIDFILE 2>/dev/null) >/dev/null 2>&1; then
failure
echo
return 1
fi
touch $lockfile
success
echo
return 0
}
configtest_q() {
$BIN configtest ${APIBASE:+--apibase=$APIBASE} ${APIKEY:+--apikey=$APIKEY} --pidfile=$PIDFILE --root=$ROOT $OTHER_OPTS >>$LOGFILE 2>&1
}
configtest() {
echo -n $"Testing configuration of $prog: "
configtest_q
if [ "$?" -eq 0 ]; then
success
echo
else
failure
echo
return 1
fi
}
stop() {
echo -n $"Stopping $prog: "
if [ ! -f $PIDFILE ]; then
echo
echo "pid file ($PIDFILE) not found" >&2
return 0
fi
pid=$(cat $PIDFILE)
process_name=$(ps -p $pid -o comm= )
if [ -z $process_name ]; then
failure
echo
echo "No such process (pid: $pid)" >&2
return 1
fi
prefix_match $prog $process_name
retval=$?
if [ $retval -eq 1 ]; then
failure
echo
echo "pid file ($PIDFILE) is invalid" >&2
return 1
fi
killproc -p $PIDFILE -QUIT
retval=$?
echo
[ $retval -eq 0 ] && rm -f $lockfile
return $retval
}
restart() {
stop
start
}
retire() {
echo -n $"Retiring the Host:"
$BIN retire -force ${APIBASE:+--apibase=$APIBASE} ${APIKEY:+--apikey=$APIKEY} --root=$ROOT $OTHER_OPTS >>$LOGFILE 2>&1
retval=$?
if [ $retval -ne 0 ]; then
failure
echo
return $retval
fi
success
echo
}
rh_status() {
status -p $PIDFILE $prog
}
rh_status_q() {
rh_status >/dev/null 2>&1
}
case "$1" in
start)
rh_status_q && exit 0
$1
;;
stop)
rh_status_q || exit 0
$1
retval=$?
[ "$AUTO_RETIREMENT" != "" ] && [ "$AUTO_RETIREMENT" != "0" ] && retire
exit $retval
;;
reload)
configtest_q || exit 1
restart
;;
restart)
$1
;;
status)
rh_status
;;
configtest)
configtest
;;
*)
echo $"Usage: $0 {start|stop|restart|reload|status|configtest}"
exit 2
esac
exit $?