forked from jsbillings/labstatsMQ-old
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcollectord
executable file
·146 lines (129 loc) · 2.96 KB
/
collectord
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
#!/bin/sh
#
# collectord: Starts/stops the labstats "collector" daemon
#
# chkconfig: 35 82 16
# description: Starts/stops the labstats "collector" daemon
# processname: collectord
# pidfile: /var/run/labstats/collector.pid
### BEGIN INIT INFO
# Provides: collectord
# Required-Start: $syslog $local_fs $network
# Required-Stop: $syslog $local_fs $network
# Default-Stop: 0 1 2 6
# Default-Start: 3 5
# Short-Description: Starts/stops the labstats "collector" daemon
# Description: Starts a daemonized version of the labstats collector script.
# Listens to port 5555 and publishes a JSON file containing the
# current machine's statistics (eg. CPU load, used memory,
# model etc.) to port 5556.
### END INIT INFO
# Source function library.
. /etc/rc.d/init.d/functions
# Storage of python script and its directory.
dir=/afs/umich.edu/user/a/d/adrikim/newLabStats # directory for now
prog="collector"
progdir=$dir$prog.py
# Storage of pidfile and its directory.
pidfile=/var/run/labstats/$prog.pid
pidname="$prog.pid"
PID=""
start() {
update_pid
# Checks your privilege.
if [ $UID -ne 0 ] ; then
failure
echo "User has insufficient privilege."
exit 4
elif [ ! -z "$PID" ] ; then
failure
echo "Instance of collector already seems to be running."
exit 4
fi
# Note: no need to try and process other flags
python $dir/collector.py -d
# check that it's running by check pid was created
if [ -e "$pidfile" ] ; then
success
echo "Started $prog"
else
failure
echo "$prog failed to start"
fi
}
stop() {
# checks sudo/root
if [ $UID -ne 0 ] ; then
failure
echo "Can't stop collector; user has insufficient privilege."
exit 4
fi
# Check that collector is actually running (check its pid)
update_pid
if [ -z $PID ] ; then
failure
echo "PID not found. Collector not running?"
exit 4
fi
# Stops collector
kill "$PID"
success
echo "Stopped $prog"
# Script handles erasure of the pidfile
}
restart() {
stop
start
}
reload() {
restart
}
force_reload() {
restart
}
rh_status() {
update_pid
if [ -z "$PID" ] ; then # PID is null; collector not running
echo "collector is not running"
else
echo "collector is running with pid" $PID
fi
}
update_pid() {
if [ -e "$pidfile" ] ; then
PID=$(head -n 1 $pidfile)
else
PID=`ps -ef | grep collector | grep python | awk '{print $2}'`
fi
}
rh_status_q() {
rh_status >/dev/null 2>&1
}
case "$1" in
start)
start
;;
stop)
stop
;;
restart)
restart
;;
reload)
reload
;;
force-reload)
force_reload
;;
status)
rh_status
;;
condrestart|try-restart)
rh_status_q || exit 0
restart
;;
*)
echo $"Usage: $0 {start|stop|status|restart|condrestart|try-restart|reload|force-reload}"
exit 2
esac
exit $?