added mem.apps and improved error handling and debugging for charts.d
Costa Tsaousis (ktsaou) committed
May 4, 2014 at 03:45 UTC
5ca16e50c378b7e79455dc9ec97620e33e5f330d
4 files changed
+198
-31
charts.d/mem_apps.chart.sh
new
+60
@@ -0,0 +1,60 @@
1
+#!/bin/sh
2
+
3
+mem_apps_apps="netdata asterisk squid apache2 mysqld dovecot cupsd sshd named clamd smbd"
4
+
5
+# these are required for computing memory in bytes and cpu in seconds
6
+#mem_apps_pagesize="`getconf PAGESIZE`"
7
+#mem_apps_clockticks="`getconf CLK_TCK`"
8
+
9
+mem_apps_check() {
10
+ # this should return:
11
+ # - 0 to enable the chart
12
+ # - 1 to disable the chart
13
+
14
+ if [ -z "$mem_apps_apps" ]
15
+ then
16
+ echo >&2 "mem_apps: Please set mem_apps_apps='command1 command2 ...' in $confd/mem_apps_apps.conf"
17
+ return 1
18
+ fi
19
+ return 0
20
+}
21
+
22
+mem_apps_bc_finalze=
23
+
24
+mem_apps_create() {
25
+
26
+ cat <<EOF1
27
+CHART system.apps '' "Apps Memory" "MB" "mem" "mem" stacked 20000 $update_every
28
+EOF1
29
+
30
+ local x=
31
+ for x in $mem_apps_apps
32
+ do
33
+
34
+cat <<EOF1
35
+DIMENSION $x $x absolute 1 1024
36
+EOF1
37
+ # this string is needed later in the update() function
38
+ # to finalize the instructions for the bc command
39
+ mem_apps_bc_finalze="$mem_apps_bc_finalze \"SET $x = \"; $x;"
40
+ done
41
+ return 0
42
+}
43
+
44
+mem_apps_egrep="(`echo "$mem_apps_apps" | sed -e "s/^ \+//g" -e "s/ \+$//g" -e "s/ /|/g"`)"
45
+mem_apps_update() {
46
+ # do all the work to collect / calculate the values
47
+ # for each dimension
48
+ # remember: KEEP IT SIMPLE AND SHORT
49
+
50
+ echo "BEGIN system.apps"
51
+ ps -e -o comm,rss |\
52
+ egrep "^$mem_apps_egrep " |\
53
+ ( sed -e "s/ \+/ /g" -e "s/ /+=/g";
54
+ echo "$mem_apps_bc_finalze"
55
+ ) | bc
56
+ echo "END"
57
+
58
+ return 0
59
+}
60
+
charts.d/squid.chart.sh
+6
-6
@@ -17,21 +17,21 @@ squid_check() {
17
squid_create() {
18
# create the charts
19
cat <<EOF
20
-CHART squid.client_bandwidth '' "Squid Client Bandwidth" "kilobits/s" squid squid area 1 $update_every
20
+CHART net.squid_clients '' "Squid Client Bandwidth" "kilobits/s" squid '' area 20001 $update_every
21
DIMENSION client_http_kbytes_in in incremental 8 1
22
DIMENSION client_http_kbytes_out out incremental -8 1
23
DIMENSION client_http_hit_kbytes_out hits incremental -8 1
24
25
-CHART squid.client_requests '' "Squid Client Requests" "requests/s" squid squid line 3 $update_every
25
+CHART squid.client_requests '' "Squid Client Requests" "requests/s" squid 'none' line 20003 $update_every
26
DIMENSION client_http_requests requests incremental 1 1
27
DIMENSION client_http_hits hits incremental 1 1
28
DIMENSION client_http_errors errors incremental -1 1
29
30
-CHART squid.server_bandwidth '' "Squid Server Bandwidth" "kilobits/s" squid squid area 2 $update_every
30
+CHART net.squid_servers '' "Squid Server Bandwidth" "kilobits/s" squid '' area 20002 $update_every
31
DIMENSION server_all_kbytes_in in incremental 8 1
32
DIMENSION server_all_kbytes_out out incremental -8 1
33
34
-CHART squid.server_requests '' "Squid Server Requests" "requests/s" squid squid line 4 $update_every
34
+CHART squid.server_requests '' "Squid Server Requests" "requests/s" squid 'none' line 20004 $update_every
35
DIMENSION server_all_requests requests incremental 1 1
36
DIMENSION server_all_errors errors incremental -1 1
37
EOF
@@ -49,7 +49,7 @@ squid_update() {
49
50
# write the result of the work.
51
cat <<VALUESEOF
52
-BEGIN squid.client_bandwidth
52
+BEGIN net.squid_clients
53
SET client_http_kbytes_in = $client_http_kbytes_in
54
SET client_http_kbytes_out = $client_http_kbytes_out
55
SET client_http_hit_kbytes_out = $client_http_hit_kbytes_out
@@ -61,7 +61,7 @@ SET client_http_hits = $client_http_hits
61
SET client_http_errors = $client_http_errors
62
END
63
64
-BEGIN squid.server_bandwidth
64
+BEGIN net.squid_servers
65
SET server_all_kbytes_in = $server_all_kbytes_in
66
SET server_all_kbytes_out = $server_all_kbytes_out
67
END
plugins.d/charts.d.plugin
+119
-22
@@ -1,32 +1,73 @@
1
#!/bin/sh
2
3
-check=0
4
-if [ "$1" = "check" ]
5
-then
6
- check=1
7
- shift
8
-fi
9
-
10
-# default sleep function
11
-loopsleepms() {
12
- sleep $1
13
-}
14
-# if found and included, this file overwrites loopsleepms()
15
-# with a high resolution timer function for precise looping.
16
-. "`dirname $0`/loopsleepms.sh.inc"
17
-
18
-# netdata passes the requested update frequency as the first argument
19
-update_every=$1
20
-update_every=$(( update_every + 1 - 1)) # makes sure it is a number
21
-test $update_every -eq 0 && update_every=1 # if it is zero, make it 1
3
+# -----------------------------------------------------------------------------
4
+# insternal defaults
5
6
pluginsd="plugins.d"
7
confd="conf.d"
8
chartsd="charts.d"
9
myconfig="$confd/charts.d.conf"
10
minimum_update_frequency=1
11
+update_every=1 # this is overwritten by the command line
12
+
13
+# -----------------------------------------------------------------------------
14
+# parse parameters
15
+
16
+debug=0
17
+check=0
18
+chart_only=
19
+while [ ! -z "$1" ]
20
+do
21
+ if [ "$1" = "check" ]
22
+ then
23
+ check=1
24
+ shift
25
+ continue
26
+ fi
27
+
28
+ if [ -f "$chartsd/$1.chart.sh" ]
29
+ then
30
+ debug=1
31
+ chart_only="`echo $1.chart.sh | sed "s/\.chart\.sh$//g"`"
32
+ shift
33
+ continue
34
+ fi
35
+
36
+ if [ -f "$chartsd/$1" ]
37
+ then
38
+ debug=1
39
+ chart_only="`echo $1 | sed "s/\.chart\.sh$//g"`"
40
+ shift
41
+ continue
42
+ fi
43
+
44
+ if [ -f "$1" ]
45
+ then
46
+ debug=1
47
+ chart_only="`basename "$1" | sed "s/\.chart\.sh$//g"`"
48
+ shift
49
+ continue
50
+ fi
51
+
52
+ # number check
53
+ n="$1"
54
+ x=$((n + 1 - 1))
55
+ if [ "$x" = "$n" ]
56
+ then
57
+ update_every=$x
58
+ shift
59
+ continue
60
+ fi
61
+
62
+ echo >&2 "Cannot understand parameter $1. Aborting."
63
+ echo "DISABLE"
64
+ exit 1
65
+done
66
+
67
+
68
+# -----------------------------------------------------------------------------
69
+# load my configuration
70
29
-# load the configuration
71
if [ -f "$myconfig" ]
72
then
73
. "$myconfig"
@@ -38,12 +79,37 @@ if [ -f "$myconfig" ]
79
fi
80
fi
81
82
+
83
+# -----------------------------------------------------------------------------
84
+# internal checks
85
+
86
+# netdata passes the requested update frequency as the first argument
87
+update_every=$(( update_every + 1 - 1)) # makes sure it is a number
88
+test $update_every -eq 0 && update_every=1 # if it is zero, make it 1
89
+
90
+# check the charts.d directory
91
if [ ! -d "$chartsd" ]
92
then
93
echo >&2 "charts.d: cannot find charts directory '$chartsd'"
94
echo "DISABLE"
95
fi
96
97
+
98
+# -----------------------------------------------------------------------------
99
+# loop control
100
+
101
+# default sleep function
102
+loopsleepms() {
103
+ sleep $1
104
+}
105
+# if found and included, this file overwrites loopsleepms()
106
+# with a high resolution timer function for precise looping.
107
+. "`dirname $0`/loopsleepms.sh.inc"
108
+
109
+
110
+# -----------------------------------------------------------------------------
111
+# charts check functions
112
+
113
all_charts() {
114
cd "$chartsd"
115
ls *.chart.sh | sed "s/\.chart\.sh$//g"
@@ -114,6 +180,9 @@ all_enabled_charts() {
180
}
181
182
183
+# -----------------------------------------------------------------------------
184
+# load the charts
185
+
186
active_charts=
187
for x in `all_enabled_charts`
188
do
@@ -133,6 +202,28 @@ do
202
fi
203
done
204
205
+
206
+# -----------------------------------------------------------------------------
207
+# check overwrites
208
+
209
+# if we only need a specific chart, remove all the others
210
+debug_time=
211
+if [ ! -z "$chart_only" ]
212
+then
213
+ debug_time=tellwork
214
+ check_charts=
215
+ for x in $active_charts
216
+ do
217
+ if [ "$x" = "$chart_only" ]
218
+ then
219
+ check_charts="$x"
220
+ break
221
+ fi
222
+ done
223
+ active_charts="$check_charts"
224
+fi
225
+
226
+# stop if we just need a pre-check
227
if [ $check -eq 1 ]
228
then
229
echo "CHECK RESULT"
@@ -140,6 +231,10 @@ then
231
exit 0
232
fi
233
234
+
235
+# -----------------------------------------------------------------------------
236
+# create charts
237
+
238
run_charts=
239
for x in $active_charts
240
do
@@ -152,7 +247,9 @@ do
247
fi
248
done
249
155
-# work forever
250
+
251
+# -----------------------------------------------------------------------------
252
+# update dimensions
253
while [ 1 ]
254
do
255
now_charts=$run_charts
@@ -170,5 +267,5 @@ do
267
done
268
269
# wait the time you are required to
173
- loopsleepms $update_every
270
+ loopsleepms $debug_time $update_every
271
done
plugins.d/loopsleepms.sh.inc
+13
-3
@@ -8,7 +8,15 @@
8
LOOPSLEEPMS_HIGHRES=2
9
LOOPSLEEPMS_LASTRUN=0
10
LOOPSLEEPMS_LASTSLEEP=0
11
+LOOPSLEEPMS_LASTWORK=0
12
loopsleepms() {
13
+ tellwork=0
14
+ if [ "$1" = "tellwork" ]
15
+ then
16
+ local tellwork=1
17
+ shift
18
+ fi
19
+
20
# the time in seconds to wait, as the first argument
21
local t=$1
22
@@ -41,11 +49,13 @@ loopsleepms() {
49
test $LOOPSLEEPMS_LASTRUN -eq 0 && LOOPSLEEPMS_LASTRUN=$now
50
51
# calculate ms since last run
44
- local workms=$((now - LOOPSLEEPMS_LASTRUN - LOOPSLEEPMS_LASTSLEEP))
45
- # echo "# last loop's work took $workms ms"
52
+ LOOPSLEEPMS_LASTWORK=$((now - LOOPSLEEPMS_LASTRUN - LOOPSLEEPMS_LASTSLEEP))
53
+ # echo "# last loop's work took $LOOPSLEEPMS_LASTWORK ms"
54
+
55
+ test $tellwork -eq 1 && echo >&2 " >>> DEBUG >>> WORK TOOK $LOOPSLEEPMS_LASTWORK ms"
56
57
# calculate ms to sleep
48
- local mstosleep=$((t - workms))
58
+ local mstosleep=$((t - LOOPSLEEPMS_LASTWORK))
59
# echo "# mstosleep is $mstosleep ms"
60
61
# if we are too slow, sleep some time