-
Notifications
You must be signed in to change notification settings - Fork 814
/
Copy pathdatadog-agent.init
executable file
·165 lines (147 loc) · 5.36 KB
/
datadog-agent.init
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
#!/bin/sh
### BEGIN INIT INFO
# Provides: dd-agent
# Short-Description: Start and start dd-agent
# Description: dd-agent is the monitoring Agent component for Datadog
# Required-Start: $remote_fs
# Required-Stop: $remote_fs
# Default-Start: 2 3 4 5
# Default-Stop: 0 1 6
### END INIT INFO
. /lib/lsb/init-functions
PATH=$PATH:/sbin # add the location of start-stop-daemon on Debian
AGENTPATH="/usr/bin/dd-agent"
AGENTCONF="/etc/dd-agent/datadog.conf"
DOGSTATSDPATH="/usr/bin/dogstatsd"
AGENTUSER="dd-agent"
USE_SUPERVISOR="/usr/bin/dd-forwarder"
NAME="datadog-agent"
DESC="Datadog Agent"
DDAGENT_PID_PATH="/var/run/dd-agent"
SUPERVISOR_PIDFILE="/var/run/datadog-supervisord.pid"
SUPERVISOR_FILE="/etc/dd-agent/supervisor.conf"
SUPERVISOR_SOCK="/var/tmp/datadog-supervisor.sock"
SUPERVISORD="/usr/bin/supervisord"
# This script is considered a configuration file and will not be
# removed by dpkg unless the --purge option it set. Therefore we
# make sure that the Agent is actually installed before we try to do anything:
if [ ! -x $AGENTPATH ]; then
echo "$AGENTPATH not found. Exiting."
exit 0
fi
check_status() {
if [ -f $USE_SUPERVISOR ]; then
# If the socket exists, we can use supervisorctl
if [ -e $SUPERVISOR_SOCK ]; then
# If we're using supervisor, check the number of datadog processes
# supervisor is currently controlling, and make sure that it's the
# same as the number of programs specified in the supervisor config
# file:
supervisor_processes=$(supervisorctl -c $SUPERVISOR_FILE status)
datadog_supervisor_processes=$(echo "$supervisor_processes" |
grep -v pup |
grep $NAME |
grep -c RUNNING)
supervisor_config_programs=$(grep -v pup $SUPERVISOR_FILE |
grep -c '\[program:')
if [ "$datadog_supervisor_processes" -ne "$supervisor_config_programs" ]; then
echo "$supervisor_processes"
echo "$DESC (supervisor) is NOT running all child processes"
return 1
else
echo "$DESC (supervisor) is running all child processes"
return 0
fi
else
echo "$DESC (supervisor) is not running"
return 1
fi
else
# If we're not using supervisor, use the Agent and dogstatsd status
# commands:
su $AGENTUSER -c "$AGENTPATH status > /dev/null 2>&1"
agent_exit_code=$?
su $AGENTUSER -c "$DOGSTATSDPATH status > /dev/null 2>&1"
dogstatsd_exit_code=$?
if [ $agent_exit_code -eq 1 ] && [ $dogstatsd_exit_code -eq 1 ]; then
echo "$DESC is NOT running all child processes"
return 1
else
echo "$DESC is running all child processes"
return 0
fi
fi
}
# Action to take
case "$1" in
start)
if [ ! -f $AGENTCONF ]; then
echo "$AGENTCONF not found. Exiting."
exit 3
fi
check_status > /dev/null
if [ $? -eq 0 ]; then
echo "$DESC is already running"
exit 0
fi
if [ -f $USE_SUPERVISOR ]; then
log_daemon_msg "Starting $DESC (using supervisord)" "$NAME"
start-stop-daemon --start --quiet --oknodo --exec $SUPERVISORD -- -c $SUPERVISOR_FILE --pidfile $SUPERVISOR_PIDFILE
if [ $? -ne 0 ]; then
log_end_msg 1
fi
else
log_daemon_msg "Starting $DESC" "$NAME"
if [ ! -f $DDAGENT_PID_PATH ]; then
mkdir -p $DDAGENT_PID_PATH
fi
chown $AGENTUSER $DDAGENT_PID_PATH
su $AGENTUSER -c "env LANG=POSIX $AGENTPATH start init --clean > /dev/null 2>&1" && \
su $AGENTUSER -c "env LANG=POSIX $DOGSTATSDPATH start > /dev/null 2>&1"
fi
sleep 4 # wait for the services to start up
if check_status > /dev/null; then
log_end_msg 0
else
log_end_msg 1
fi
;;
stop)
if [ -f $USE_SUPERVISOR ]; then
log_daemon_msg "Stopping $DESC (stopping supervisord)" "$NAME"
start-stop-daemon --stop --retry 30 --quiet --oknodo --pidfile $SUPERVISOR_PIDFILE
else
log_daemon_msg "Stopping $DESC" "$NAME"
su $AGENTUSER -c "$AGENTPATH stop init > /dev/null 2>&1" && \
su $AGENTUSER -c "$DOGSTATSDPATH stop > /dev/null 2>&1"
fi
log_end_msg $?
;;
info)
shift # Shift 'info' out of args so we can pass any
# addtional options to the real command
# (right now only dd-agent supports additional flags)
su $AGENTUSER -c "$AGENTPATH info $@"
RETURN_VALUE=$?
su $AGENTUSER -c "$DOGSTATSDPATH info"
RETURN_VALUE=$(($RETURN_VALUE || $?))
if [ -f $USE_SUPERVISOR ]; then
su $AGENTUSER -c "$USE_SUPERVISOR info"
RETURN_VALUE=$(($RETURN_VALUE || $?))
fi
exit $RETURN_VALUE
;;
status)
check_status
;;
restart|force-reload)
$0 stop
$0 start
;;
*)
N=/etc/init.d/$NAME
echo "Usage: $N {start|stop|restart|info|status}"
exit 1
;;
esac
exit $?