]> arthur.barton.de Git - netdata.git/blob - plugins.d/charts.d.plugin
fix float2int()
[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 # the default enable/disable value for all charts
99 enable_all_charts="yes"
100
101 # -----------------------------------------------------------------------------
102 # parse parameters
103
104 debug=0
105 check=0
106 chart_only=
107 while [ ! -z "$1" ]
108 do
109         if [ "$1" = "check" ]
110         then
111                 check=1
112                 shift
113                 continue
114         fi
115
116         if [ "$1" = "debug" -o "$1" = "all" ]
117         then
118                 debug=1
119                 shift
120                 continue
121         fi
122
123         if [ -f "$chartsd/$1.chart.sh" ]
124         then
125                 debug=1
126                 chart_only="$( echo $1.chart.sh | sed "s/\.chart\.sh$//g" )"
127                 shift
128                 continue
129         fi
130
131         if [ -f "$chartsd/$1" ]
132         then
133                 debug=1
134                 chart_only="$( echo $1 | sed "s/\.chart\.sh$//g" )"
135                 shift
136                 continue
137         fi
138
139         # number check
140         n="$1"
141         x=$(( n ))
142         if [ "$x" = "$n" ]
143         then
144                 shift
145                 update_every=$x
146                 [ $update_every -lt $minimum_update_frequency ] && update_every=$minimum_update_frequency
147                 continue
148         fi
149
150         echo >&2 "Cannot understand parameter $1. Aborting."
151         echo "DISABLE"
152         exit 1
153 done
154
155
156 # -----------------------------------------------------------------------------
157 # load my configuration
158
159 if [ -f "$myconfig" ]
160         then
161         . "$myconfig"
162         if [ $? -ne 0 ]
163         then
164                 echo >&2 "$PROGRAM_NAME: cannot load $myconfig"
165                 echo "DISABLE"
166                 exit 1
167         fi
168         time_divisor=$((time_divisor))
169         [ $time_divisor -lt 10 ] && time_divisor=10
170         [ $time_divisor -gt 100 ] && time_divisor=100
171 else
172         echo >&2 "$PROGRAM_NAME: configuration file '$myconfig' not found. Using defaults."
173 fi
174
175 if [ "$pause_method" = "suspend" ]
176 then
177         # enable bash job control
178         # this is required for suspend to work
179         set -m
180 fi
181
182 # we check for the timeout command, after we load our
183 # configuration, so that the user may overwrite the
184 # timeout command we use, providing a function that
185 # can emulate the timeout command we need:
186 # > timeout SECONDS command ...
187 if [ $check_for_timeout -eq 1 ]
188         then
189         require_cmd timeout || exit 1
190 fi
191
192 # -----------------------------------------------------------------------------
193 # internal checks
194
195 # netdata passes the requested update frequency as the first argument
196 update_every=$(( update_every + 1 - 1)) # makes sure it is a number
197 test $update_every -eq 0 && update_every=1 # if it is zero, make it 1
198
199 # check the charts.d directory
200 if [ ! -d "$chartsd" ]
201         then
202         echo >&2 "$PROGRAM_NAME: cannot find charts directory '$chartsd'"
203         echo "DISABLE"
204 fi
205
206
207 # -----------------------------------------------------------------------------
208 # loop control
209
210 # default sleep function
211 LOOPSLEEPMS_HIGHRES=0
212 loopsleepms() {
213         [ "$1" = "tellwork" ] && shift
214         sleep $1
215 }
216
217 now_ms=
218 current_time_ms() {
219         now_ms="$(date +'%s')000"
220 }
221
222 # if found and included, this file overwrites loopsleepms()
223 # and current_time_ms() with a high resolution timer function
224 # for precise looping.
225 . "$pluginsd/loopsleepms.sh.inc"
226
227
228 # -----------------------------------------------------------------------------
229 # library functions
230
231 fixid() {
232         echo "$*" |\
233                 tr -c "[A-Z][a-z][0-9]" "_" |\
234                 sed -e "s|^_\+||g" -e "s|_\+$||g" -e "s|_\+|_|g" |\
235                 tr "[A-Z]" "[a-z]"
236 }
237
238 # convert any floating point number
239 # to integer, give a multiplier
240 # the result is stored in ${FLOAT2INT_RESULT}
241 # so that no fork is necessary
242 # the multiplier must be a power of 10
243 float2int() {
244         local f="$1" m="$2" a b l
245
246         #echo >&2 "f='${f}', m='${m}'"
247
248         # the length of the multiplier - 1
249         l=$[ ${#m} - 1 ]
250
251         # split the floating point number
252         # in integer (a) and decimal (b)
253         a=${f/.*/}
254         b=${f/*./}
255
256         # prepend a zero to the integer part
257         # if it is missing
258         [ -z "${a}" ] && a="0"
259         # strip leading zeros - base 10 convertion
260         a=$[10#$a]
261
262         # check the length of the decimal part
263         # against the length of the multiplier
264         if [ ${#b} -gt ${l} ]
265                 then
266                 # too many digits - take the most significant
267                 b=${b:0:${l}}
268         elif [ ${#b} -lt ${l} ]
269                 then
270                 # too few digits - pad with zero on the right
271                 local z="00000000000000000000000" r=$[l - ${#b}]
272                 b="${b}${z:0:${r}}"
273         fi
274         # strip leading zeros - base 10 convertion
275         b=$[10#$b]
276
277         # store the result
278         FLOAT2INT_RESULT=$[ (a * m) + b ]
279         #echo >&2 "FLOAT2INT_RESULT='${FLOAT2INT_RESULT}'"
280 }
281
282
283 # -----------------------------------------------------------------------------
284 # charts check functions
285
286 all_charts() {
287         cd "$chartsd"
288         [ $? -ne 0 ] && echo >&2 "$PROGRAM_NAME: Cannot cd to $chartsd" && return 1
289
290         ls *.chart.sh | sed "s/\.chart\.sh$//g"
291 }
292
293 all_enabled_charts() {
294         local charts= enabled=
295
296         # find all enabled charts
297
298         for chart in $( all_charts )
299         do
300                 eval "enabled=\$$chart"
301                 if [ -z "${enabled}" ]
302                         then
303                         enabled="${enable_all_charts}"
304                 fi
305
306                 if [ ! "${enabled}" = "yes" ]
307                 then
308                         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)."
309                 else
310                         [ $debug -eq 1 ] && echo >&2 "$PROGRAM_NAME: '$chart' is enabled."
311                         local charts="$charts $chart"
312                 fi
313         done
314
315         local charts2=
316         for chart in $charts
317         do
318                 # check the enabled charts
319                 local check="$( cat "$chartsd/$chart.chart.sh" | sed "s/^ \+//g" | grep "^$chart$charts_check()" )"
320                 if [ -z "$check" ]
321                 then
322                         echo >&2 "$PROGRAM_NAME: chart '$chart' does not seem to have a $chart$charts_check() function. Disabling it."
323                         continue
324                 fi
325
326                 local create="$( cat "$chartsd/$chart.chart.sh" | sed "s/^ \+//g" | grep "^$chart$charts_create()" )"
327                 if [ -z "$create" ]
328                 then
329                         echo >&2 "$PROGRAM_NAME: chart '$chart' does not seem to have a $chart$charts_create() function. Disabling it."
330                         continue
331                 fi
332
333                 local update="$( cat "$chartsd/$chart.chart.sh" | sed "s/^ \+//g" | grep "^$chart$charts_update()" )"
334                 if [ -z "$update" ]
335                 then
336                         echo >&2 "$PROGRAM_NAME: chart '$chart' does not seem to have a $chart$charts_update() function. Disabling it."
337                         continue
338                 fi
339
340                 # check its config
341                 #if [ -f "$confd/$chart.conf" ]
342                 #then
343                 #       if [ ! -z "$( cat "$confd/$chart.conf" | sed "s/^ \+//g" | grep -v "^$" | grep -v "^#" | grep -v "^$chart$charts_undescore" )" ]
344                 #       then
345                 #               echo >&2 "$PROGRAM_NAME: chart's $chart config $confd/$chart.conf should only have lines starting with $chart$charts_undescore . Disabling it."
346                 #               continue
347                 #       fi
348                 #fi
349
350                 #if [ $dryrunner -eq 1 ]
351                 #       then
352                 #       "$pluginsd/charts.d.dryrun-helper.sh" "$chart" "$chartsd/$chart.chart.sh" "$confd/$chart.conf" >/dev/null
353                 #       if [ $? -ne 0 ]
354                 #       then
355                 #               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."
356                 #               continue
357                 #       fi
358                 #fi
359
360                 local charts2="$charts2 $chart"
361         done
362
363         echo $charts2
364         [ $debug -eq 1 ] && echo >&2 "$PROGRAM_NAME: enabled charts: $charts2"
365 }
366
367
368 # -----------------------------------------------------------------------------
369 # load the charts
370
371 suffix_update_every="_update_every"
372 active_charts=
373 for chart in $( all_enabled_charts )
374 do
375         [ $debug -eq 1 ] && echo >&2 "$PROGRAM_NAME: loading chart: '$chartsd/$chart.chart.sh'"
376         . "$chartsd/$chart.chart.sh"
377
378         if [ -f "$confd/$chart.conf" ]
379         then
380                 [ $debug -eq 1 ] && echo >&2 "$PROGRAM_NAME: loading chart options: '$confd/$chart.conf'"
381                 . "$confd/$chart.conf"
382         else
383                 echo >&2 "$PROGRAM_NAME: $chart: configuration file '$confd/$chart.conf' not found. Using defaults."
384         fi
385
386         eval "dt=\$$chart$suffix_update_every"
387         dt=$(( dt + 1 - 1 )) # make sure it is a number
388         if [ $dt -lt $update_every ]
389         then
390                 eval "$chart$suffix_update_every=$update_every"
391         fi
392
393         $chart$charts_check
394         if [ $? -eq 0 ]
395         then
396                 [ $debug -eq 1 ] && echo >&2 "$PROGRAM_NAME: '$chart' activated"
397                 active_charts="$active_charts $chart"
398         else
399                 echo >&2 "$PROGRAM_NAME: chart '$chart' check() function reports failure."
400         fi
401 done
402 [ $debug -eq 1 ] && echo >&2 "$PROGRAM_NAME: activated charts: $active_charts"
403
404
405 # -----------------------------------------------------------------------------
406 # check overwrites
407
408 # enable work time reporting
409 debug_time=
410 test $debug -eq 1 && debug_time=tellwork
411
412 # if we only need a specific chart, remove all the others
413 if [ ! -z "${chart_only}" ]
414 then
415         [ $debug -eq 1 ] && echo >&2 "$PROGRAM_NAME: requested to run only for: '${chart_only}'"
416         check_charts=
417         for chart in $active_charts
418         do
419                 if [ "$chart" = "$chart_only" ]
420                 then
421                         check_charts="$chart"
422                         break
423                 fi
424         done
425         active_charts="$check_charts"
426 fi
427 [ $debug -eq 1 ] && echo >&2 "$PROGRAM_NAME: activated charts: $active_charts"
428
429 # stop if we just need a pre-check
430 if [ $check -eq 1 ]
431 then
432         echo >&2 "CHECK RESULT"
433         echo >&2 "Will run the charts: $active_charts"
434         exit 0
435 fi
436
437 # -----------------------------------------------------------------------------
438 # create temp dir
439
440 TMP_DIR=
441 chartsd_cleanup() {
442         cd /tmp
443         if [ ! -z "$TMP_DIR" -a -d "$TMP_DIR" ]
444         then
445                 [ $debug -eq 1 ] && echo >&2 "$PROGRAM_NAME: cleaning up temporary directory $TMP_DIR ..."
446                 rm -rf "$TMP_DIR"
447         fi
448         exit 0
449 }
450 trap chartsd_cleanup EXIT
451 trap chartsd_cleanup SIGHUP
452 trap chartsd_cleanup INT
453
454 if [ $UID = "0" ]
455 then
456         TMP_DIR="$( mktemp -d /var/run/netdata-${PROGRAM_NAME}-XXXXXXXXXX )"
457 else
458         TMP_DIR="$( mktemp -d /tmp/.netdata-${PROGRAM_NAME}-XXXXXXXXXX )"
459 fi
460
461 cd "$TMP_DIR" || exit 1
462
463 # -----------------------------------------------------------------------------
464 # create charts
465
466 run_charts=
467 for chart in $active_charts
468 do
469         [ $debug -eq 1 ] && echo >&2 "$PROGRAM_NAME: Calling '$chart$charts_create()'..."
470         $chart$charts_create
471         if [ $? -eq 0 ]
472         then
473                 run_charts="$run_charts $chart"
474                 [ $debug -eq 1 ] && echo >&2 "$PROGRAM_NAME: '$chart' has initialized."
475         else
476                 echo >&2 "$PROGRAM_NAME: chart '$chart' function '$chart$charts_create()' reports failure."
477         fi
478 done
479 [ $debug -eq 1 ] && echo >&2 "$PROGRAM_NAME: run_charts='$run_charts'"
480
481
482 # -----------------------------------------------------------------------------
483 # update dimensions
484
485 if [ -z "$run_charts" ]
486         then
487         echo >&2 "$PROGRAM_NAME: No charts to collect data from."
488         echo "DISABLE"
489         exit 1
490 fi
491
492 declare -A charts_last_update=() charts_update_every=() charts_next_update=() charts_run_counter=()
493 global_update() {
494         local exit_at \
495                 c=0 dt ret last_ms exec_start_ms exec_end_ms \
496                 chart now_charts=() next_charts=($run_charts)
497
498         # return the current time in ms in $now_ms
499         current_time_ms
500
501         exit_at=$(( now_ms + (restart_timeout * 1000) ))
502
503         for chart in $run_charts
504         do
505                 eval "charts_update_every[$chart]=\$$chart$suffix_update_every"
506                 test -z "${charts_update_every[$chart]}" && charts_update_every[$charts]=$update_every
507                 charts_last_update[$chart]=$((now_ms - (now_ms % (charts_update_every[$chart] * 1000) ) ))
508                 charts_next_update[$chart]=$(( charts_last_update[$chart] + (charts_update_every[$chart] * 1000) ))
509                 charts_run_counter[$chart]=0
510
511                 echo "CHART netdata.plugin_chartsd_$chart '' 'Execution time for $chart plugin' 'milliseconds / run' netdata netdata area 90000 ${charts_update_every[$chart]}"
512                 echo "DIMENSION run_time 'run time' absolute 1 1"
513         done
514
515         # the main loop
516         while [ 1 ]
517         do
518                 c=$((c + 1))
519                 now_charts=("${next_charts[@]}")
520                 next_charts=()
521
522                 # return the current time in ms in $now_ms
523                 current_time_ms
524
525                 for chart in "${now_charts[@]}"
526                 do
527                         # echo >&2 "DEBUG: chart: $chart last: ${charts_last_update[$chart]}, next: ${charts_next_update[$chart]}, now: ${now_ms}"
528                         if [ ${now_ms} -ge ${charts_next_update[$chart]} ]
529                         then
530                                 last_ms=${charts_last_update[$chart]}
531                                 dt=$(( (now_ms - last_ms) ))
532                                 # echo >&2 "DEBUG: chart: $chart last: ${charts_last_update[$chart]}, next: ${charts_next_update[$chart]}, now: ${now_ms}, dt: ${dt}"
533
534                                 charts_last_update[$chart]=${now_ms}
535
536                                 while [ ${charts_next_update[$chart]} -lt ${now_ms} ]
537                                 do
538                                         charts_next_update[$chart]=$(( charts_next_update[$chart] + (charts_update_every[$chart] * 1000) ))
539                                 done
540
541                                 # the first call should not give a duration
542                                 # so that netdata calibrates to current time
543                                 dt=$(( dt * 1000 ))
544                                 charts_run_counter[$chart]=$(( charts_run_counter[$chart] + 1 ))
545                                 if [ ${charts_run_counter[$chart]} -eq 1 ]
546                                         then
547                                         dt=
548                                 fi
549
550                                 exec_start_ms=$now_ms
551                                 $chart$charts_update $dt
552                                 ret=$?
553                                 
554                                 # return the current time in ms in $now_ms
555                                 current_time_ms; exec_end_ms=$now_ms
556
557                                 echo "BEGIN netdata.plugin_chartsd_$chart $dt"
558                                 if [ $ret -eq 0 ]
559                                 then
560                                         echo "SET run_time = $(( exec_end_ms - exec_start_ms ))"
561                                         next_charts+=($chart)
562                                 else
563                                         echo "SET run_time = $(( (exec_end_ms - exec_start_ms) * -1 ))"
564                                         echo >&2 "$PROGRAM_NAME: chart '$chart' update() function reports failure. Disabling it."
565                                 fi
566                                 echo "END"
567                         else
568                                 next_charts+=($chart)
569                         fi
570                 done
571
572                 if [ "$pause_method" = "suspend" ]
573                 then
574                         echo "STOPPING_WAKE_ME_UP_PLEASE"
575                         suspend || ( echo >&2 "$PROGRAM_NAME: suspend returned error $?, falling back to sleep."; loopsleepms $debug_time $update_every $time_divisor)
576                 else
577                         # wait the time you are required to
578                         #loopsleepms $debug_time $update_every $time_divisor
579                         if [ ${LOOPSLEEPMS_HIGHRES} -eq 1 ]
580                                 then
581                                 sleep 0.2
582                         else
583                                 sleep 1
584                         fi
585                 fi
586
587                 test ${now_ms} -ge ${exit_at} && exit 0
588         done
589 }
590
591 global_update