This repository has been archived by the owner on Jul 8, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
containermon.sh
executable file
·329 lines (292 loc) · 8.81 KB
/
containermon.sh
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
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
#!/bin/bash -e
#
# Extract statistics from the cgroup filesystem for each LXC and Docker
# container and feed it to an RRD. The current working directory is
# used to find the RRDs.
#
# Known issues:
# - container names cannot contain a space (can they?)
#
CGROUPFS=/sys/fs/cgroup
DATADIR="$PWD/data"
HOSTDATADIR="$PWD/hostdata"
PLOTDIR="$PWD/plot"
mkdir -p "$DATADIR" "$HOSTDATADIR" "$PLOTDIR"
COLOR_ARRAY=(4488ee ee4488 88ee44 bb6622 6622bb 009922 002299 666666 990022 eeeeee)
COLOR_ARRAY_LEN=${#COLOR_ARRAY[@]}
#
# Find LXC and Docker containers
#
CONTAINERS=
for d in $CGROUPFS/cpu/{lxc,docker}/*/; do
[ -d "$d" ] && CONTAINERS+="$(basename $d) "
done
#echo Found containers: $CONTAINERS
#
# Create missing RRD databases
#
for c in $CONTAINERS; do
RRD="$DATADIR/$c.rrd"
if [ ! -f "$RRD" ]; then
#echo Creating "$RRD"
# 4 weeks with minute level data
HIGH_RES_SAMPLES=40320
# 365 days with ten minute level data
MED_RES_SAMPLES=52560
# ten years with hour level data
LOW_RES_SAMPLES=87600
# CPU cycles in jiffies (10ms)
DS="DS:user_jif:COUNTER:120:0:6400 "
DS+="DS:system_jif:COUNTER:120:0:6400 "
DS+="DS:rss:GAUGE:120:U:U "
rrdtool create "$RRD" --start 1300000000 --step 60 \
${DS} \
RRA:AVERAGE:0.5:1:${HIGH_RES_SAMPLES} \
RRA:AVERAGE:0.5:10:${MED_RES_SAMPLES} \
RRA:AVERAGE:0.5:60:${LOW_RES_SAMPLES} \
RRA:MAX:0.5:1:${HIGH_RES_SAMPLES} \
RRA:MAX:0.5:10:${MED_RES_SAMPLES} \
RRA:MAX:0.5:60:${LOW_RES_SAMPLES} \
RRA:MIN:0.5:1:${HIGH_RES_SAMPLES} \
RRA:MIN:0.5:10:${MED_RES_SAMPLES} \
RRA:MIN:0.5:60:${LOW_RES_SAMPLES}
fi
done
if [ ! -f "$HOSTDATADIR/disk.rrd" ]; then
RRD="$HOSTDATADIR/disk.rrd"
# 4 weeks with minute level data
HIGH_RES_SAMPLES=40320
# 365 days with ten minute level data
MED_RES_SAMPLES=52560
# ten years with hour level data
LOW_RES_SAMPLES=87600
# Disk reads and writes
DS="DS:rd_sectors:COUNTER:120:0:10000000 "
DS+="DS:rd_ms:COUNTER:120:0:500000 "
DS+="DS:wr_sectors:COUNTER:120:0:10000000 "
DS+="DS:wr_ms:COUNTER:120:0:500000 "
rrdtool create "$RRD" --start 1300000000 --step 60 \
${DS} \
RRA:AVERAGE:0.5:1:${HIGH_RES_SAMPLES} \
RRA:AVERAGE:0.5:10:${MED_RES_SAMPLES} \
RRA:AVERAGE:0.5:60:${LOW_RES_SAMPLES} \
RRA:MAX:0.5:1:${HIGH_RES_SAMPLES} \
RRA:MAX:0.5:10:${MED_RES_SAMPLES} \
RRA:MAX:0.5:60:${LOW_RES_SAMPLES} \
RRA:MIN:0.5:1:${HIGH_RES_SAMPLES} \
RRA:MIN:0.5:10:${MED_RES_SAMPLES} \
RRA:MIN:0.5:60:${LOW_RES_SAMPLES}
fi
if [ ! -f "$HOSTDATADIR/host.rrd" ]; then
RRD="$HOSTDATADIR/host.rrd"
# 4 weeks with minute level data
HIGH_RES_SAMPLES=40320
# 365 days with ten minute level data
MED_RES_SAMPLES=52560
# ten years with hour level data
LOW_RES_SAMPLES=87600
# CPU cycles in jiffies (10ms)
DS="DS:user_jif:COUNTER:120:0:6400 "
DS+="DS:system_jif:COUNTER:120:0:6400 "
DS+="DS:rss:GAUGE:120:U:U "
rrdtool create "$RRD" --start 1300000000 --step 60 \
${DS} \
RRA:AVERAGE:0.5:1:${HIGH_RES_SAMPLES} \
RRA:AVERAGE:0.5:10:${MED_RES_SAMPLES} \
RRA:AVERAGE:0.5:60:${LOW_RES_SAMPLES} \
RRA:MAX:0.5:1:${HIGH_RES_SAMPLES} \
RRA:MAX:0.5:10:${MED_RES_SAMPLES} \
RRA:MAX:0.5:60:${LOW_RES_SAMPLES} \
RRA:MIN:0.5:1:${HIGH_RES_SAMPLES} \
RRA:MIN:0.5:10:${MED_RES_SAMPLES} \
RRA:MIN:0.5:60:${LOW_RES_SAMPLES}
fi
# Make a list of databases, just the basename
RRDS=
for rrd in $(find $DATADIR/ -name *.rrd -ctime -1 | sort); do
RRDS+="$(basename -s .rrd $rrd) "
done
#
# Add current data for each container
#
for c in $CONTAINERS; do
RRD="$DATADIR/$c.rrd"
USER=0
SYSTEM=0
while read LINE; do
SPLIT=($LINE)
if [ "${SPLIT[0]}" == "user" ]; then
USER=${SPLIT[1]}
fi
if [ "${SPLIT[0]}" == "system" ]; then
SYSTEM=${SPLIT[1]}
fi
done < $CGROUPFS/cpuacct/*/$c/cpuacct.stat
RSS=0
while read LINE; do
SPLIT=($LINE)
if [ "${SPLIT[0]}" == "total_rss" ]; then
RSS=${SPLIT[1]}
fi
done < $CGROUPFS/memory/*/$c/memory.stat
#echo Container $c: user=$USER system=$SYSTEM RSS=$RSS
rrdtool update "$RRD" -t user_jif:system_jif:rss -- N:$USER:$SYSTEM:$RSS
done
# Add host disk I/O data
DISK=sda
RD_SECTORS=0
RD_MS=0
WR_SECTORS=0
WR_MS=0
while read LINE; do
SPLIT=($LINE)
if [ "${SPLIT[2]}" == "${DISK}" ]; then
RD_SECTORS="${SPLIT[5]}"
RD_MS="${SPLIT[6]}"
WR_SECTORS="${SPLIT[9]}"
WR_MS="${SPLIT[10]}"
fi
done < /proc/diskstats
rrdtool update "$HOSTDATADIR/disk.rrd" -t rd_sectors:rd_ms:wr_sectors:wr_ms -- \
N:$RD_SECTORS:$RD_MS:$WR_SECTORS:$WR_MS
# Add host CPU and RAM data
USER=0
SYSTEM=0
while read LINE; do
SPLIT=($LINE)
if [ "${SPLIT[0]}" == "user" ]; then
USER=${SPLIT[1]}
fi
if [ "${SPLIT[0]}" == "system" ]; then
SYSTEM=${SPLIT[1]}
fi
done < $CGROUPFS/cpuacct/cpuacct.stat
RSS=0
while read LINE; do
SPLIT=($LINE)
if [ "${SPLIT[0]}" == "total_rss" ]; then
RSS=${SPLIT[1]}
fi
done < $CGROUPFS/memory/memory.stat
rrdtool update "$HOSTDATADIR/host.rrd" -t user_jif:system_jif:rss -- N:$USER:$SYSTEM:$RSS
#
# Plot the RRDs
#
HTML=$(tempfile -m 644)
cat > $HTML <<EOF
<html><head><title>Container stats for $HOSTNAME</title></head>
<body style="background: black; color: white; width: 1560px;">
<p>Container stats for $HOSTNAME Updated at $(date)<br/>
Uptime: $(uptime)</p>
EOF
export LANG=en_US.UTF-8
WIDTH=768
HEIGHT=256
BG=000000
FG=ffffff
COMMON_OPTS="--color SHADEA#${BG} --color SHADEB#${BG} --color BACK#${BG} --color CANVAS#${BG} "
COMMON_OPTS+="--color FONT#${FG} --color AXIS#${FG} --color ARROW#${FG} "
COMMON_OPTS+="--color GRID#444444 --color MGRID#aaaaaa "
COMMON_OPTS+="--lazy "
for rrd in $RRDS; do
RRD="$DATADIR/$rrd.rrd"
# Jiffies (100Hz unit) neatly corresponds to % CPU load when graphed
rrdtool graph "$PLOTDIR/$rrd-cpu.png" \
-t "CPU load $rrd [%]" \
${COMMON_OPTS} \
--lower-limit 0 \
--rigid \
--full-size-mode \
-E --end now --start now-8h --width ${WIDTH} --height ${HEIGHT} \
DEF:user_jif=$RRD:user_jif:AVERAGE \
DEF:system_jif=$RRD:system_jif:AVERAGE \
AREA:system_jif#4488ee:"System" \
AREA:user_jif#bb6622:"User":STACK > /dev/null
rrdtool graph "$PLOTDIR/$rrd-ram.png" \
-t "RAM usage $rrd" \
${COMMON_OPTS} \
--lower-limit 0 \
--rigid \
--full-size-mode \
-E --end now --start now-8h --width ${WIDTH} --height ${HEIGHT} \
DEF:ram=$RRD:rss:MAX \
AREA:ram#6622bb:"User" > /dev/null
cat >> $HTML <<EOF
<img src="$rrd-cpu.png"/><img src="$rrd-ram.png"/>
EOF
done
# Draw a summary graph
CPUDEFS=
color=0
for rrd in $RRDS; do
escname=$(echo "$rrd" | tr - _)
RRD="$DATADIR/$rrd.rrd"
CPUDEFS+="DEF:${escname}_user=$RRD:user_jif:AVERAGE "
CPUDEFS+="DEF:${escname}_system=$RRD:system_jif:AVERAGE "
CPUDEFS+="CDEF:${escname}_cpu=${escname}_user,${escname}_system,+ "
CPULINES+="AREA:${escname}_cpu#${COLOR_ARRAY[color]}:$escname:STACK "
RAMDEFS+="DEF:${escname}_ram=$RRD:rss:MAX "
RAMLINES+="AREA:${escname}_ram#${COLOR_ARRAY[color]}:$escname:STACK "
((color=(color+1)%COLOR_ARRAY_LEN)) || true
done
# Add host statistics
HOSTRRD="$HOSTDATADIR/host.rrd"
CPUDEFS+="DEF:host_user=${HOSTRRD}:user_jif:AVERAGE "
CPUDEFS+="DEF:host_system=${HOSTRRD}:system_jif:AVERAGE "
CPUDEFS+="CDEF:host_cpu=host_user,host_system,+ "
CPULINES+="LINE:host_cpu#ffffffff:Host-CPU "
RAMDEFS+="DEF:host_ram=${HOSTRRD}:rss:MAX "
RAMLINES+="LINE:host_ram#ffffffff:Host-RSS "
rrdtool graph "$PLOTDIR/cpu-1d.png" \
-t "CPU load containers on $HOSTNAME [%]" \
${COMMON_OPTS} \
--lower-limit 0 \
--rigid \
--full-size-mode \
-E --end now --start now-24h --width ${WIDTH} --height $((HEIGHT+128)) \
$CPUDEFS \
$CPULINES > /dev/null
rrdtool graph "$PLOTDIR/ram-1d.png" \
-t "RAM usage containers on $HOSTNAME" \
${COMMON_OPTS} \
--lower-limit 0 \
--rigid \
--full-size-mode \
-E --end now --start now-24h --width ${WIDTH} --height $((HEIGHT+128)) \
$RAMDEFS \
$RAMLINES > /dev/null
cat >> $HTML <<EOF
<hr/>
<img src="cpu-1d.png"/><img src="ram-1d.png"/>
EOF
# Draw host disk I/O graph
RRD="$HOSTDATADIR/disk.rrd"
rrdtool graph "$PLOTDIR/__host-disk.png" \
-t "Disk I/O" \
${COMMON_OPTS} \
--lower-limit 0 --upper-limit 1000000 \
--rigid \
--full-size-mode \
-E --end now --start now-24h --width $((WIDTH+52)) --height ${HEIGHT} \
--right-axis 0.001:0 --right-axis-label "[ms]" \
DEF:rd_sectors=$RRD:rd_sectors:AVERAGE \
CDEF:rd_b=rd_sectors,512,* \
DEF:wr_sectors=$RRD:wr_sectors:AVERAGE \
CDEF:wr_b=wr_sectors,512,* \
DEF:rd_ms=$RRD:rd_ms:AVERAGE \
DEF:wr_ms=$RRD:wr_ms:AVERAGE \
CDEF:rd_rightaxis=rd_ms,1000,* \
CDEF:wr_rightaxis=wr_ms,1000,* \
AREA:wr_b#ff444466:"Write [B/s]" \
AREA:rd_b#4444ff66:"Read [B/s]" \
LINE:wr_rightaxis#ff8800:"Write [ms/s]" \
LINE:rd_rightaxis#0088ff:"Read [ms/s]" \
> /dev/null
cat >> $HTML <<EOF
<img src="__host-disk.png"/>
EOF
cat >> $HTML <<EOF
</body>
</html>
EOF
mv $HTML "$PLOTDIR/index.html"