]> arthur.barton.de Git - netdata.git/blob - plugins.d/charts.d.plugin
new synchronization mechanism to align data collected to fixed steps
[netdata.git] / plugins.d / charts.d.plugin
1 #!/bin/bash
2
3 PROGRAM_FILE="$0"
4 PROGRAM_NAME="$(basename $0)"
5 PROGRAM_NAME="${PROGRAM_NAME/.plugin}"
6
7 # if you need to run parallel charts.d processes
8 # just link this files with a different name
9 # in the same directory, with a .plugin suffix
10 # netdata will start multiple of them
11 # each will have a different config file
12
13 echo >&2 "$PROGRAM_NAME: started from '$PROGRAM_FILE' with options: $*"
14
15 if [ $(( ${BASH_VERSINFO[0]} )) -lt 4 ]
16 then
17         echo >&2
18         echo >&2 "$PROGRAM_NAME: ERROR"
19         echo >&2 "BASH version 4 or later is required."
20         echo >&2 "You are running version: ${BASH_VERSION}"
21         echo >&2 "Please upgrade."
22         echo >&2
23         exit 1
24 fi
25
26 # check a few commands
27 require_cmd() {
28         which "$1" >/dev/null
29         if [ $? -ne 0 ]
30                 then
31                 echo >&2 "$PROGRAM_NAME: ERROR: Command '$1' is not found in the system path."
32                 return 1
33         fi
34         return 0
35 }
36
37 require_cmd date || exit 1
38 require_cmd sed || exit 1
39 require_cmd basename || exit 1
40 require_cmd dirname || exit 1
41 require_cmd cat || exit 1
42 require_cmd grep || exit 1
43 require_cmd egrep || exit 1
44 require_cmd mktemp || exit 1
45 require_cmd awk || exit 1
46
47 # -----------------------------------------------------------------------------
48 # insternal defaults
49 # netdata exposes a few environment variables for us
50
51 pause_method="sleep" # use either "suspend" or "sleep"
52                      # DO NOT USE SUSPEND - LINUX WILL SUSPEND NETDATA TOO
53                      # THE WHOLE PROCESS GROUP - NOT JUST THE SHELL
54
55 pluginsd="${NETDATA_PLUGINS_DIR}"
56 [ -z "$pluginsd" ] && pluginsd="$( dirname $PROGRAM_FILE )"
57
58 confd="${NETDATA_CONFIG_DIR-/etc/netdata}"
59 chartsd="$pluginsd/../charts.d"
60
61 myconfig="$confd/$PROGRAM_NAME.conf"
62
63 minimum_update_frequency="${NETDATA_UPDATE_EVERY-1}"
64 update_every=${minimum_update_frequency}        # this will be overwritten by the command line
65
66 # work around for non BASH shells
67 charts_create="_create"
68 charts_update="_update"
69 charts_check="_check"
70 charts_undescore="_"
71
72 # when making iterations, charts.d can loop more frequently
73 # to prevent plugins missing iterations.
74 # this is a percentage relative to update_every to align its
75 # iterations.
76 # The minimum is 10%, the maximum 100%.
77 # So, if update_every is 1 second and time_divisor is 50,
78 # charts.d will iterate every 500ms.
79 # Charts will be called to collect data only if the time
80 # passes since the last time the collected data is equal or
81 # above their update_every.
82 time_divisor=50
83
84 # number of seconds to run without restart
85 # after this time, charts.d.plugin will exit
86 # netdata will restart it
87 restart_timeout=1800
88
89 # check if the charts.d plugins are using global variables
90 # they should not.
91 # It does not currently support BASH v4 arrays, so it is
92 # disabled
93 dryrunner=0
94
95 # check for timeout command
96 check_for_timeout=1
97
98 # -----------------------------------------------------------------------------
99 # parse parameters
100
101 debug=0
102 check=0
103 chart_only=
104 while [ ! -z "$1" ]
105 do
106         if [ "$1" = "check" ]
107         then
108                 check=1
109                 shift
110                 continue
111         fi
112
113         if [ "$1" = "debug" -o "$1" = "all" ]
114         then
115                 debug=1
116                 shift
117                 continue
118         fi
119
120         if [ -f "$chartsd/$1.chart.sh" ]
121         then
122                 debug=1
123                 chart_only="$( echo $1.chart.sh | sed "s/\.chart\.sh$//g" )"
124                 shift
125                 continue
126         fi
127
128         if [ -f "$chartsd/$1" ]
129         then
130                 debug=1
131                 chart_only="$( echo $1 | sed "s/\.chart\.sh$//g" )"
132                 shift
133                 continue
134         fi
135
136         # number check
137         n="$1"
138         x=$(( n ))
139         if [ "$x" = "$n" ]
140         then
141                 shift
142                 update_every=$x
143                 [ $update_every -lt $minimum_update_frequency ] && update_every=$minimum_update_frequency
144                 continue
145         fi
146
147         echo >&2 "Cannot understand parameter $1. Aborting."
148         echo "DISABLE"
149         exit 1
150 done
151
152
153 # -----------------------------------------------------------------------------
154 # load my configuration
155
156 if [ -f "$myconfig" ]
157         then
158         . "$myconfig"
159         if [ $? -ne 0 ]
160         then
161                 echo >&2 "$PROGRAM_NAME: cannot load $myconfig"
162                 echo "DISABLE"
163                 exit 1
164         fi
165         time_divisor=$((time_divisor))
166         [ $time_divisor -lt 10 ] && time_divisor=10
167         [ $time_divisor -gt 100 ] && time_divisor=100
168 else
169         echo >&2 "$PROGRAM_NAME: configuration file '$myconfig' not found. Using defaults."
170 fi
171
172 if [ "$pause_method" = "suspend" ]
173 then
174         # enable bash job control
175         # this is required for suspend to work
176         set -m
177 fi
178
179 # we check for the timeout command, after we load our
180 # configuration, so that the user may overwrite the
181 # timeout command we use, providing a function that
182 # can emulate the timeout command we need:
183 # > timeout SECONDS command ...
184 if [ $check_for_timeout -eq 1 ]
185         then
186         require_cmd timeout || exit 1
187 fi
188
189 # -----------------------------------------------------------------------------
190 # internal checks
191
192 # netdata passes the requested update frequency as the first argument
193 update_every=$(( update_every + 1 - 1)) # makes sure it is a number
194 test $update_every -eq 0 && update_every=1 # if it is zero, make it 1
195
196 # check the charts.d directory
197 if [ ! -d "$chartsd" ]
198         then
199         echo >&2 "$PROGRAM_NAME: cannot find charts directory '$chartsd'"
200         echo "DISABLE"
201 fi
202
203
204 # -----------------------------------------------------------------------------
205 # loop control
206
207 # default sleep function
208 LOOPSLEEPMS_HIGHRES=0
209 loopsleepms() {
210         [ "$1" = "tellwork" ] && shift
211         sleep $1
212 }
213
214 now_ms=
215 current_time_ms() {
216         now_ms="$(date +'%s')000"
217 }
218
219 # if found and included, this file overwrites loopsleepms()
220 # and current_time_ms() with a high resolution timer function
221 # for precise looping.
222 . "$pluginsd/loopsleepms.sh.inc"
223
224
225 # -----------------------------------------------------------------------------
226 # library functions
227
228 fixid() {
229         echo "$*" |\
230                 tr -c "[A-Z][a-z][0-9]" "_" |\
231                 sed -e "s|^_\+||g" -e "s|_\+$||g" -e "s|_\+|_|g" |\
232                 tr "[A-Z]" "[a-z]"
233 }
234
235
236 # -----------------------------------------------------------------------------
237 # charts check functions
238
239 all_charts() {
240         cd "$chartsd"
241         [ $? -ne 0 ] && echo >&2 "$PROGRAM_NAME: Cannot cd to $chartsd" && return 1
242
243         ls *.chart.sh | sed "s/\.chart\.sh$//g"
244 }
245
246 all_enabled_charts() {
247         local charts=
248
249         # find all enabled charts
250
251         for chart in $( all_charts )
252         do
253                 eval "enabled=\$$chart"
254                 if [ "$enabled" = "no" ]
255                 then
256                         echo >&2 "$PROGRAM_NAME: '$chart' is NOT enabled. Add a line with $chart=yes in $myconfig to enable it (or remove the line that disables it)."
257                 else
258                         [ $debug -eq 1 ] && echo >&2 "$PROGRAM_NAME: '$chart' is enabled."
259                         local charts="$charts $chart"
260                 fi
261         done
262
263         local charts2=
264         for chart in $charts
265         do
266                 # check the enabled charts
267                 local check="$( cat "$chartsd/$chart.chart.sh" | sed "s/^ \+//g" | grep "^$chart$charts_check()" )"
268                 if [ -z "$check" ]
269                 then
270                         echo >&2 "$PROGRAM_NAME: chart '$chart' does not seem to have a $chart$charts_check() function. Disabling it."
271                         continue
272                 fi
273
274                 local create="$( cat "$chartsd/$chart.chart.sh" | sed "s/^ \+//g" | grep "^$chart$charts_create()" )"
275                 if [ -z "$create" ]
276                 then
277                         echo >&2 "$PROGRAM_NAME: chart '$chart' does not seem to have a $chart$charts_create() function. Disabling it."
278                         continue
279                 fi
280
281                 local update="$( cat "$chartsd/$chart.chart.sh" | sed "s/^ \+//g" | grep "^$chart$charts_update()" )"
282                 if [ -z "$update" ]
283                 then
284                         echo >&2 "$PROGRAM_NAME: chart '$chart' does not seem to have a $chart$charts_update() function. Disabling it."
285                         continue
286                 fi
287
288                 # check its config
289                 #if [ -f "$confd/$chart.conf" ]
290                 #then
291                 #       if [ ! -z "$( cat "$confd/$chart.conf" | sed "s/^ \+//g" | grep -v "^$" | grep -v "^#" | grep -v "^$chart$charts_undescore" )" ]
292                 #       then
293                 #               echo >&2 "$PROGRAM_NAME: chart's $chart config $confd/$chart.conf should only have lines starting with $chart$charts_undescore . Disabling it."
294                 #               continue
295                 #       fi
296                 #fi
297
298                 #if [ $dryrunner -eq 1 ]
299                 #       then
300                 #       "$pluginsd/charts.d.dryrun-helper.sh" "$chart" "$chartsd/$chart.chart.sh" "$confd/$chart.conf" >/dev/null
301                 #       if [ $? -ne 0 ]
302                 #       then
303                 #               echo >&2 "$PROGRAM_NAME: chart's $chart did not pass the dry run check. This means it uses global variables not starting with $chart. Disabling it."
304                 #               continue
305                 #       fi
306                 #fi
307
308                 local charts2="$charts2 $chart"
309         done
310
311         echo $charts2
312         [ $debug -eq 1 ] && echo >&2 "$PROGRAM_NAME: enabled charts: $charts2"
313 }
314
315
316 # -----------------------------------------------------------------------------
317 # load the charts
318
319 suffix_update_every="_update_every"
320 active_charts=
321 for chart in $( all_enabled_charts )
322 do
323         [ $debug -eq 1 ] && echo >&2 "$PROGRAM_NAME: loading chart: '$chartsd/$chart.chart.sh'"
324         . "$chartsd/$chart.chart.sh"
325
326         if [ -f "$confd/$chart.conf" ]
327         then
328                 [ $debug -eq 1 ] && echo >&2 "$PROGRAM_NAME: loading chart options: '$confd/$chart.conf'"
329                 . "$confd/$chart.conf"
330         else
331                 echo >&2 "$PROGRAM_NAME: $chart: configuration file '$confd/$chart.conf' not found. Using defaults."
332         fi
333
334         eval "dt=\$$chart$suffix_update_every"
335         dt=$(( dt + 1 - 1 )) # make sure it is a number
336         if [ $dt -lt $update_every ]
337         then
338                 eval "$chart$suffix_update_every=$update_every"
339         fi
340
341         $chart$charts_check
342         if [ $? -eq 0 ]
343         then
344                 [ $debug -eq 1 ] && echo >&2 "$PROGRAM_NAME: '$chart' activated"
345                 active_charts="$active_charts $chart"
346         else
347                 echo >&2 "$PROGRAM_NAME: chart '$chart' check() function reports failure."
348         fi
349 done
350 [ $debug -eq 1 ] && echo >&2 "$PROGRAM_NAME: activated charts: $active_charts"
351
352
353 # -----------------------------------------------------------------------------
354 # check overwrites
355
356 # enable work time reporting
357 debug_time=
358 test $debug -eq 1 && debug_time=tellwork
359
360 # if we only need a specific chart, remove all the others
361 if [ ! -z "${chart_only}" ]
362 then
363         [ $debug -eq 1 ] && echo >&2 "$PROGRAM_NAME: requested to run only for: '${chart_only}'"
364         check_charts=
365         for chart in $active_charts
366         do
367                 if [ "$chart" = "$chart_only" ]
368                 then
369                         check_charts="$chart"
370                         break
371                 fi
372         done
373         active_charts="$check_charts"
374 fi
375 [ $debug -eq 1 ] && echo >&2 "$PROGRAM_NAME: activated charts: $active_charts"
376
377 # stop if we just need a pre-check
378 if [ $check -eq 1 ]
379 then
380         echo >&2 "CHECK RESULT"
381         echo >&2 "Will run the charts: $active_charts"
382         exit 0
383 fi
384
385 # -----------------------------------------------------------------------------
386 # create temp dir
387
388 TMP_DIR=
389 chartsd_cleanup() {
390         cd /tmp
391         if [ ! -z "$TMP_DIR" -a -d "$TMP_DIR" ]
392         then
393                 [ $debug -eq 1 ] && echo >&2 "$PROGRAM_NAME: cleaning up temporary directory $TMP_DIR ..."
394                 rm -rf "$TMP_DIR"
395         fi
396         exit 0
397 }
398 trap chartsd_cleanup EXIT
399 trap chartsd_cleanup SIGHUP
400 trap chartsd_cleanup INT
401
402 if [ $UID = "0" ]
403 then
404         TMP_DIR="$( mktemp -d /var/run/netdata-${PROGRAM_NAME}-XXXXXXXXXX )"
405 else
406         TMP_DIR="$( mktemp -d /tmp/.netdata-${PROGRAM_NAME}-XXXXXXXXXX )"
407 fi
408
409 cd "$TMP_DIR" || exit 1
410
411 # -----------------------------------------------------------------------------
412 # create charts
413
414 run_charts=
415 for chart in $active_charts
416 do
417         [ $debug -eq 1 ] && echo >&2 "$PROGRAM_NAME: Calling '$chart$charts_create()'..."
418         $chart$charts_create
419         if [ $? -eq 0 ]
420         then
421                 run_charts="$run_charts $chart"
422                 [ $debug -eq 1 ] && echo >&2 "$PROGRAM_NAME: '$chart' has initialized."
423         else
424                 echo >&2 "$PROGRAM_NAME: chart '$chart' function '$chart$charts_create()' reports failure."
425         fi
426 done
427 [ $debug -eq 1 ] && echo >&2 "$PROGRAM_NAME: run_charts='$run_charts'"
428
429
430 # -----------------------------------------------------------------------------
431 # update dimensions
432
433 if [ -z "$run_charts" ]
434         then
435         echo >&2 "$PROGRAM_NAME: No charts to collect data from."
436         echo "DISABLE"
437         exit 1
438 fi
439
440 declare -A charts_last_update=() charts_update_every=() charts_next_update=() chart_run_counter=()
441 global_update() {
442         local exit_at \
443                 c=0 dt ret exec_start_ms exec_end_ms \
444                 chart now_charts=() next_charts=($run_charts)
445
446         # return the current time in ms in $now_ms
447         current_time_ms
448
449         exit_at=$(( now_ms + (restart_timeout * 1000) ))
450
451         for chart in $run_charts
452         do
453                 eval "charts_update_every[$chart]=\$$chart$suffix_update_every"
454                 test -z "${charts_update_every[$chart]}" && charts_update_every[$charts]=$update_every
455                 charts_last_update[$chart]=$((now_ms - (now_ms % (charts_update_every[$chart] * 1000) ) ))
456                 charts_next_update[$chart]=$(( charts_last_update[$chart] + (charts_update_every[$chart] * 1000) ))
457                 charts_run_counter[$chart]=0
458
459                 echo "CHART netdata.plugin_$chart '' 'Execution time for $chart plugin' 'milliseconds / run' netdata netdata area 90000 ${charts_update_every[$chart]}"
460                 echo "DIMENSION run_time 'run time' absolute 1 1"
461         done
462
463         # the main loop
464         while [ 1 ]
465         do
466                 c=$((c + 1))
467                 now_charts=("${next_charts[@]}")
468                 next_charts=()
469
470                 # return the current time in ms in $now_ms
471                 current_time_ms
472
473                 for chart in "${now_charts[@]}"
474                 do
475                         # echo >&2 "DEBUG: chart: $chart last: ${charts_last_update[$chart]}, next: ${charts_next_update[$chart]}, now: ${now_ms}"
476                         if [ ${now_ms} -ge ${charts_next_update[$chart]} ]
477                         then
478                                 dt=$(( (now_ms - charts_last_update[$chart]) ))
479                                 #echo >&2 "DEBUG: chart: $chart last: ${charts_last_update[$chart]}, next: ${charts_next_update[$chart]}, now: ${now_ms}, dt: ${dt}"
480
481                                 charts_last_update[$chart]=$now_ms
482
483                                 while [ ${charts_next_update[$chart]} -lt ${now_ms} ]
484                                 do
485                                         charts_next_update[$chart]=$(( charts_next_update[$chart] + (charts_update_every[$chart] * 1000) ))
486                                 done
487
488                                 # the first call should not give a duration
489                                 # so that netdata calibrates to current time
490                                 charts_run_counter[$chart]=$(( charts_run_counter[$chart] + 1 ))
491                                 test ${charts_run_counter[$chart]} -eq 1 && dt=
492                                 dt=$(( dt * 1000 ))
493
494                                 exec_start_ms=$now_ms
495                                 $chart$charts_update $dt
496                                 ret=$?
497                                 
498                                 # return the current time in ms in $now_ms
499                                 current_time_ms; exec_end_ms=$now_ms
500
501                                 echo "BEGIN netdata.plugin_$chart $dt"
502                                 if [ $ret -eq 0 ]
503                                 then
504                                         echo "SET run_time = $(( exec_end_ms - exec_start_ms ))"
505                                         next_charts+=($chart)
506                                 else
507                                         echo "SET run_time = $(( (exec_end_ms - exec_start_ms) * -1 ))"
508                                         echo >&2 "$PROGRAM_NAME: chart '$chart' update() function reports failure. Disabling it."
509                                 fi
510                                 echo "END"
511                         else
512                                 next_charts+=($chart)
513                         fi
514                 done
515
516                 if [ "$pause_method" = "suspend" ]
517                 then
518                         echo "STOPPING_WAKE_ME_UP_PLEASE"
519                         suspend || ( echo >&2 "$PROGRAM_NAME: suspend returned error $?, falling back to sleep."; loopsleepms $debug_time $update_every $time_divisor)
520                 else
521                         # wait the time you are required to
522                         #loopsleepms $debug_time $update_every $time_divisor
523                         if [ ${LOOPSLEEPMS_HIGHRES} -eq 1 ]
524                                 then
525                                 sleep 0.2
526                         else
527                                 sleep 1
528                         fi
529                 fi
530
531                 test ${now_ms} -ge ${exit_at} && exit 0
532         done
533 }
534
535 global_update