4 PROGRAM_NAME="$(basename $0)"
5 PROGRAM_NAME="${PROGRAM_NAME/.plugin}"
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
13 echo >&2 "$PROGRAM_NAME: started from '$PROGRAM_FILE' with options: $*"
15 if [ $(( ${BASH_VERSINFO[0]} )) -lt 4 ]
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."
26 # check a few commands
31 echo >&2 "$PROGRAM_NAME: ERROR: Command '$1' is not found in the system path."
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
47 # -----------------------------------------------------------------------------
49 # netdata exposes a few environment variables for us
51 pluginsd="${NETDATA_PLUGINS_DIR}"
52 [ -z "$pluginsd" ] && pluginsd="$( dirname $PROGRAM_FILE )"
54 confd="${NETDATA_CONFIG_DIR-/etc/netdata}"
55 chartsd="$pluginsd/../charts.d"
57 myconfig="$confd/$PROGRAM_NAME.conf"
59 minimum_update_frequency="${NETDATA_UPDATE_EVERY-1}"
60 update_every=${minimum_update_frequency} # this will be overwritten by the command line
62 # work around for non BASH shells
63 charts_create="_create"
64 charts_update="_update"
68 # when making iterations, charts.d can loop more frequently
69 # to prevent plugins missing iterations.
70 # this is a percentage relative to update_every to align its
72 # The minimum is 10%, the maximum 100%.
73 # So, if update_every is 1 second and time_divisor is 50,
74 # charts.d will iterate every 500ms.
75 # Charts will be called to collect data only if the time
76 # passed since the last time the collected data is equal or
77 # above their update_every.
80 # number of seconds to run without restart
81 # after this time, charts.d.plugin will exit
82 # netdata will restart it
83 restart_timeout=$((3600 * 4))
85 # check if the charts.d plugins are using global variables
87 # It does not currently support BASH v4 arrays, so it is
91 # check for timeout command
94 # the default enable/disable value for all charts
95 enable_all_charts="yes"
97 # -----------------------------------------------------------------------------
105 if [ "$1" = "check" ]
112 if [ "$1" = "debug" -o "$1" = "all" ]
119 if [ -f "$chartsd/$1.chart.sh" ]
122 chart_only="$( echo $1.chart.sh | sed "s/\.chart\.sh$//g" )"
127 if [ -f "$chartsd/$1" ]
130 chart_only="$( echo $1 | sed "s/\.chart\.sh$//g" )"
142 [ $update_every -lt $minimum_update_frequency ] && update_every=$minimum_update_frequency
146 echo >&2 "Cannot understand parameter $1. Aborting."
152 # -----------------------------------------------------------------------------
155 # default sleep function
156 LOOPSLEEPMS_HIGHRES=0
158 current_time_ms_default() {
159 now_ms="$(date +'%s')000"
161 current_time_ms="current_time_ms_default"
162 current_time_ms_accuracy=1
165 # if found and included, this file overwrites loopsleepms()
166 # and current_time_ms() with a high resolution timer function
167 # for precise looping.
168 . "$pluginsd/loopsleepms.sh.inc"
170 # -----------------------------------------------------------------------------
171 # load my configuration
173 if [ -f "$myconfig" ]
178 echo >&2 "$PROGRAM_NAME: cannot load $myconfig"
182 time_divisor=$((time_divisor))
183 [ $time_divisor -lt 10 ] && time_divisor=10
184 [ $time_divisor -gt 100 ] && time_divisor=100
186 echo >&2 "$PROGRAM_NAME: configuration file '$myconfig' not found. Using defaults."
189 # we check for the timeout command, after we load our
190 # configuration, so that the user may overwrite the
191 # timeout command we use, providing a function that
192 # can emulate the timeout command we need:
193 # > timeout SECONDS command ...
194 if [ $check_for_timeout -eq 1 ]
196 require_cmd timeout || exit 1
199 # -----------------------------------------------------------------------------
202 # netdata passes the requested update frequency as the first argument
203 update_every=$(( update_every + 1 - 1)) # makes sure it is a number
204 test $update_every -eq 0 && update_every=1 # if it is zero, make it 1
206 # check the charts.d directory
207 if [ ! -d "$chartsd" ]
209 echo >&2 "$PROGRAM_NAME: cannot find charts directory '$chartsd'"
214 # -----------------------------------------------------------------------------
219 tr -c "[A-Z][a-z][0-9]" "_" |\
220 sed -e "s|^_\+||g" -e "s|_\+$||g" -e "s|_\+|_|g" |\
224 # convert any floating point number
225 # to integer, give a multiplier
226 # the result is stored in ${FLOAT2INT_RESULT}
227 # so that no fork is necessary
228 # the multiplier must be a power of 10
230 local f m="$2" a b l v=($1)
233 # echo >&2 "value='${1}' f='${f}', m='${m}'"
235 # the length of the multiplier - 1
238 # check if the number is in scientific notation
239 if [[ ${f} =~ ^[[:space:]]*(-)?[0-9.]+(e|E)(\+|-)[0-9]+ ]]
241 # convert it to decimal
242 # unfortunately, this fork cannot be avoided
243 # if you know of a way to avoid it, please let me know
244 f=$(printf "%0.${l}f" ${f})
247 # split the floating point number
248 # in integer (a) and decimal (b)
252 # if the integer part is missing
254 [ -z "${a}" ] && a="0"
256 # strip leading zeros from the integer part
260 # check the length of the decimal part
261 # against the length of the multiplier
262 if [ ${#b} -gt ${l} ]
264 # too many digits - take the most significant
267 elif [ ${#b} -lt ${l} ]
269 # too few digits - pad with zero on the right
270 local z="00000000000000000000000" r=$((l - ${#b}))
274 # strip leading zeros from the decimal part
279 FLOAT2INT_RESULT=$(( (a * m) + b ))
280 #echo >&2 "FLOAT2INT_RESULT='${FLOAT2INT_RESULT}'"
284 # -----------------------------------------------------------------------------
285 # charts check functions
289 [ $? -ne 0 ] && echo >&2 "$PROGRAM_NAME: Cannot cd to $chartsd" && return 1
291 ls *.chart.sh | sed "s/\.chart\.sh$//g"
294 declare -A charts_enable_keyword=(
301 ['load_average']="force"
312 all_enabled_charts() {
313 local charts= enabled= required=
315 # find all enabled charts
317 for chart in $( all_charts )
319 eval "enabled=\$$chart"
320 if [ -z "${enabled}" ]
322 enabled="${enable_all_charts}"
325 required="${charts_enable_keyword[${chart}]}"
326 [ -z "${required}" ] && required="yes"
328 if [ ! "${enabled}" = "${required}" ]
330 echo >&2 "$PROGRAM_NAME: '$chart' is NOT enabled. Add a line with $chart=$required in $myconfig to enable it (or remove the line that disables it)."
332 [ $debug -eq 1 ] && echo >&2 "$PROGRAM_NAME: '$chart' is enabled."
333 local charts="$charts $chart"
340 # check the enabled charts
341 local check="$( cat "$chartsd/$chart.chart.sh" | sed "s/^ \+//g" | grep "^$chart$charts_check()" )"
344 echo >&2 "$PROGRAM_NAME: chart '$chart' does not seem to have a $chart$charts_check() function. Disabling it."
348 local create="$( cat "$chartsd/$chart.chart.sh" | sed "s/^ \+//g" | grep "^$chart$charts_create()" )"
351 echo >&2 "$PROGRAM_NAME: chart '$chart' does not seem to have a $chart$charts_create() function. Disabling it."
355 local update="$( cat "$chartsd/$chart.chart.sh" | sed "s/^ \+//g" | grep "^$chart$charts_update()" )"
358 echo >&2 "$PROGRAM_NAME: chart '$chart' does not seem to have a $chart$charts_update() function. Disabling it."
363 #if [ -f "$confd/$chart.conf" ]
365 # if [ ! -z "$( cat "$confd/$chart.conf" | sed "s/^ \+//g" | grep -v "^$" | grep -v "^#" | grep -v "^$chart$charts_undescore" )" ]
367 # echo >&2 "$PROGRAM_NAME: chart's $chart config $confd/$chart.conf should only have lines starting with $chart$charts_undescore . Disabling it."
372 #if [ $dryrunner -eq 1 ]
374 # "$pluginsd/charts.d.dryrun-helper.sh" "$chart" "$chartsd/$chart.chart.sh" "$confd/$chart.conf" >/dev/null
377 # 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."
382 local charts2="$charts2 $chart"
386 [ $debug -eq 1 ] && echo >&2 "$PROGRAM_NAME: enabled charts: $charts2"
390 # -----------------------------------------------------------------------------
393 suffix_update_every="_update_every"
395 for chart in $( all_enabled_charts )
397 [ $debug -eq 1 ] && echo >&2 "$PROGRAM_NAME: loading chart: '$chartsd/$chart.chart.sh'"
398 . "$chartsd/$chart.chart.sh"
400 if [ -f "$confd/charts.d/$chart.conf" ]
402 [ $debug -eq 1 ] && echo >&2 "$PROGRAM_NAME: loading chart options: '$confd/charts.d/$chart.conf'"
403 . "$confd/charts.d/$chart.conf"
404 elif [ -f "$confd/$chart.conf" ]
406 [ $debug -eq 1 ] && echo >&2 "$PROGRAM_NAME: loading chart options: '$confd/$chart.conf'"
407 . "$confd/$chart.conf"
409 echo >&2 "$PROGRAM_NAME: $chart: configuration file '$confd/charts.d/$chart.conf' not found. Using defaults."
412 eval "dt=\$$chart$suffix_update_every"
413 dt=$(( dt + 1 - 1 )) # make sure it is a number
414 if [ $dt -lt $update_every ]
416 eval "$chart$suffix_update_every=$update_every"
422 [ $debug -eq 1 ] && echo >&2 "$PROGRAM_NAME: '$chart' activated"
423 active_charts="$active_charts $chart"
425 echo >&2 "$PROGRAM_NAME: chart '$chart' check() function reports failure."
428 [ $debug -eq 1 ] && echo >&2 "$PROGRAM_NAME: activated charts: $active_charts"
431 # -----------------------------------------------------------------------------
434 # enable work time reporting
436 test $debug -eq 1 && debug_time=tellwork
438 # if we only need a specific chart, remove all the others
439 if [ ! -z "${chart_only}" ]
441 [ $debug -eq 1 ] && echo >&2 "$PROGRAM_NAME: requested to run only for: '${chart_only}'"
443 for chart in $active_charts
445 if [ "$chart" = "$chart_only" ]
447 check_charts="$chart"
451 active_charts="$check_charts"
453 [ $debug -eq 1 ] && echo >&2 "$PROGRAM_NAME: activated charts: $active_charts"
455 # stop if we just need a pre-check
458 echo >&2 "CHECK RESULT"
459 echo >&2 "Will run the charts: $active_charts"
463 # -----------------------------------------------------------------------------
469 if [ ! -z "$TMP_DIR" -a -d "$TMP_DIR" ]
471 [ $debug -eq 1 ] && echo >&2 "$PROGRAM_NAME: cleaning up temporary directory $TMP_DIR ..."
476 trap chartsd_cleanup EXIT
477 trap chartsd_cleanup SIGHUP
478 trap chartsd_cleanup INT
482 TMP_DIR="$( mktemp -d /var/run/netdata-${PROGRAM_NAME}-XXXXXXXXXX )"
484 TMP_DIR="$( mktemp -d /tmp/.netdata-${PROGRAM_NAME}-XXXXXXXXXX )"
487 cd "$TMP_DIR" || exit 1
489 # -----------------------------------------------------------------------------
493 for chart in $active_charts
495 [ $debug -eq 1 ] && echo >&2 "$PROGRAM_NAME: Calling '$chart$charts_create()'..."
499 run_charts="$run_charts $chart"
500 [ $debug -eq 1 ] && echo >&2 "$PROGRAM_NAME: '$chart' has initialized."
502 echo >&2 "$PROGRAM_NAME: chart '$chart' function '$chart$charts_create()' reports failure."
505 [ $debug -eq 1 ] && echo >&2 "$PROGRAM_NAME: run_charts='$run_charts'"
508 # -----------------------------------------------------------------------------
511 if [ -z "$run_charts" ]
513 echo >&2 "$PROGRAM_NAME: No charts to collect data from."
518 declare -A charts_last_update=() charts_update_every=() charts_next_update=() charts_run_counter=() charts_serial_failures=()
521 c=0 dt ret last_ms exec_start_ms exec_end_ms \
522 chart now_charts=() next_charts=($run_charts) \
523 next_ms x seconds millis
525 # return the current time in ms in $now_ms
528 exit_at=$(( now_ms + (restart_timeout * 1000) ))
530 for chart in $run_charts
532 eval "charts_update_every[$chart]=\$$chart$suffix_update_every"
533 test -z "${charts_update_every[$chart]}" && charts_update_every[$charts]=$update_every
534 charts_last_update[$chart]=$((now_ms - (now_ms % (charts_update_every[$chart] * 1000) ) ))
535 charts_next_update[$chart]=$(( charts_last_update[$chart] + (charts_update_every[$chart] * 1000) ))
536 charts_run_counter[$chart]=0
537 charts_serial_failures[$chart]=0
539 echo "CHART netdata.plugin_chartsd_$chart '' 'Execution time for $chart plugin' 'milliseconds / run' charts.d netdata.plugin_charts area 145000 ${charts_update_every[$chart]}"
540 echo "DIMENSION run_time 'run time' absolute 1 1"
544 while [ "${#next_charts[@]}" -gt 0 ]
547 now_charts=("${next_charts[@]}")
550 # return the current time in ms in $now_ms
553 for chart in "${now_charts[@]}"
555 #echo >&2 " DEBUG: chart: $chart last: ${charts_last_update[$chart]}, next: ${charts_next_update[$chart]}, now: ${now_ms}"
556 if [ ${now_ms} -ge ${charts_next_update[$chart]} ]
558 last_ms=${charts_last_update[$chart]}
559 dt=$(( (now_ms - last_ms) ))
560 #echo >&2 " DEBUG: chart: $chart last: ${charts_last_update[$chart]}, next: ${charts_next_update[$chart]}, now: ${now_ms}, dt: ${dt}"
562 charts_last_update[$chart]=${now_ms}
564 while [ ${charts_next_update[$chart]} -lt ${now_ms} ]
566 charts_next_update[$chart]=$(( charts_next_update[$chart] + (charts_update_every[$chart] * 1000) ))
569 # the first call should not give a duration
570 # so that netdata calibrates to current time
572 charts_run_counter[$chart]=$(( charts_run_counter[$chart] + 1 ))
573 if [ ${charts_run_counter[$chart]} -eq 1 ]
578 exec_start_ms=$now_ms
579 #echo >&2 " EXEC: $chart$charts_update $dt"
580 $chart$charts_update $dt
583 # return the current time in ms in $now_ms
584 ${current_time_ms}; exec_end_ms=$now_ms
586 echo "BEGIN netdata.plugin_chartsd_$chart $dt"
587 echo "SET run_time = $(( exec_end_ms - exec_start_ms ))"
592 charts_serial_failures[$chart]=0
593 next_charts+=($chart)
595 charts_serial_failures[$chart]=$(( charts_serial_failures[$chart] + 1 ))
597 if [ ${charts_serial_failures[$chart]} -gt 10 ]
599 echo >&2 "$PROGRAM_NAME: chart '$chart' update() function reported failure ${charts_serial_failures[$chart]} times. Disabling it."
601 echo >&2 "$PROGRAM_NAME: chart '$chart' update() function reports failure. Will keep trying for a while."
602 next_charts+=($chart)
606 next_charts+=($chart)
610 # wait the time you are required to
611 next_ms=$((now_ms + (update_every * 1000 * 100) ))
612 for x in "${charts_next_update[@]}"; do [ ${x} -lt ${next_ms} ] && next_ms=${x}; done
613 next_ms=$((next_ms - now_ms))
615 if [ ${LOOPSLEEPMS_HIGHRES} -eq 1 -a ${next_ms} -gt 0 ]
617 next_ms=$(( next_ms + current_time_ms_accuracy ))
618 seconds=$(( next_ms / 1000 ))
619 millis=$(( next_ms % 1000 ))
620 if [ ${millis} -lt 10 ]
623 elif [ ${millis} -lt 100 ]
628 [ $debug -eq 1 ] && echo >&2 "$PROGRAM_NAME: sleeping for ${seconds}.${millis} seconds."
629 ${mysleep} ${seconds}.${millis}
631 [ $debug -eq 1 ] && echo >&2 "$PROGRAM_NAME: sleeping for ${update_every} seconds."
632 ${mysleep} $update_every
635 test ${now_ms} -ge ${exit_at} && exit 0
638 echo >&2 "$PROGRAM_NAME: Nothing left to do. Disabling charts.d.plugin."