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 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
55 pluginsd="${NETDATA_PLUGINS_DIR}"
56 [ -z "$pluginsd" ] && pluginsd="$( dirname $PROGRAM_FILE )"
58 confd="${NETDATA_CONFIG_DIR-/etc/netdata}"
59 chartsd="$pluginsd/../charts.d"
61 myconfig="$confd/$PROGRAM_NAME.conf"
63 minimum_update_frequency="${NETDATA_UPDATE_EVERY-1}"
64 update_every=${minimum_update_frequency} # this will be overwritten by the command line
66 # work around for non BASH shells
67 charts_create="_create"
68 charts_update="_update"
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
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.
84 # number of seconds to run without restart
85 # after this time, charts.d.plugin will exit
86 # netdata will restart it
89 # check if the charts.d plugins are using global variables
91 # It does not currently support BASH v4 arrays, so it is
95 # check for timeout command
98 # -----------------------------------------------------------------------------
106 if [ "$1" = "check" ]
113 if [ "$1" = "debug" -o "$1" = "all" ]
120 if [ -f "$chartsd/$1.chart.sh" ]
123 chart_only="$( echo $1.chart.sh | sed "s/\.chart\.sh$//g" )"
128 if [ -f "$chartsd/$1" ]
131 chart_only="$( echo $1 | sed "s/\.chart\.sh$//g" )"
143 [ $update_every -lt $minimum_update_frequency ] && update_every=$minimum_update_frequency
147 echo >&2 "Cannot understand parameter $1. Aborting."
153 # -----------------------------------------------------------------------------
154 # load my configuration
156 if [ -f "$myconfig" ]
161 echo >&2 "$PROGRAM_NAME: cannot load $myconfig"
165 time_divisor=$((time_divisor))
166 [ $time_divisor -lt 10 ] && time_divisor=10
167 [ $time_divisor -gt 100 ] && time_divisor=100
169 echo >&2 "$PROGRAM_NAME: configuration file '$myconfig' not found. Using defaults."
172 if [ "$pause_method" = "suspend" ]
174 # enable bash job control
175 # this is required for suspend to work
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 ]
186 require_cmd timeout || exit 1
189 # -----------------------------------------------------------------------------
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
196 # check the charts.d directory
197 if [ ! -d "$chartsd" ]
199 echo >&2 "$PROGRAM_NAME: cannot find charts directory '$chartsd'"
204 # -----------------------------------------------------------------------------
207 # default sleep function
209 [ "$1" = "tellwork" ] && shift
215 now_ms="$(date +'%s')000"
218 # if found and included, this file overwrites loopsleepms()
219 # and current_time_ms() with a high resolution timer function
220 # for precise looping.
221 . "$pluginsd/loopsleepms.sh.inc"
224 # -----------------------------------------------------------------------------
229 tr -c "[A-Z][a-z][0-9]" "_" |\
230 sed -e "s|^_\+||g" -e "s|_\+$||g" -e "s|_\+|_|g" |\
235 # -----------------------------------------------------------------------------
236 # charts check functions
240 [ $? -ne 0 ] && echo >&2 "$PROGRAM_NAME: Cannot cd to $chartsd" && return 1
242 ls *.chart.sh | sed "s/\.chart\.sh$//g"
245 all_enabled_charts() {
248 # find all enabled charts
250 for chart in $( all_charts )
252 eval "enabled=\$$chart"
253 if [ "$enabled" = "no" ]
255 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 [ $debug -eq 1 ] && echo >&2 "$PROGRAM_NAME: '$chart' is enabled."
258 local charts="$charts $chart"
265 # check the enabled charts
266 local check="$( cat "$chartsd/$chart.chart.sh" | sed "s/^ \+//g" | grep "^$chart$charts_check()" )"
269 echo >&2 "$PROGRAM_NAME: chart '$chart' does not seem to have a $chart$charts_check() function. Disabling it."
273 local create="$( cat "$chartsd/$chart.chart.sh" | sed "s/^ \+//g" | grep "^$chart$charts_create()" )"
276 echo >&2 "$PROGRAM_NAME: chart '$chart' does not seem to have a $chart$charts_create() function. Disabling it."
280 local update="$( cat "$chartsd/$chart.chart.sh" | sed "s/^ \+//g" | grep "^$chart$charts_update()" )"
283 echo >&2 "$PROGRAM_NAME: chart '$chart' does not seem to have a $chart$charts_update() function. Disabling it."
288 #if [ -f "$confd/$chart.conf" ]
290 # if [ ! -z "$( cat "$confd/$chart.conf" | sed "s/^ \+//g" | grep -v "^$" | grep -v "^#" | grep -v "^$chart$charts_undescore" )" ]
292 # echo >&2 "$PROGRAM_NAME: chart's $chart config $confd/$chart.conf should only have lines starting with $chart$charts_undescore . Disabling it."
297 #if [ $dryrunner -eq 1 ]
299 # "$pluginsd/charts.d.dryrun-helper.sh" "$chart" "$chartsd/$chart.chart.sh" "$confd/$chart.conf" >/dev/null
302 # 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."
307 local charts2="$charts2 $chart"
311 [ $debug -eq 1 ] && echo >&2 "$PROGRAM_NAME: enabled charts: $charts2"
315 # -----------------------------------------------------------------------------
318 suffix_update_every="_update_every"
320 for chart in $( all_enabled_charts )
322 [ $debug -eq 1 ] && echo >&2 "$PROGRAM_NAME: loading chart: '$chartsd/$chart.chart.sh'"
323 . "$chartsd/$chart.chart.sh"
325 if [ -f "$confd/$chart.conf" ]
327 [ $debug -eq 1 ] && echo >&2 "$PROGRAM_NAME: loading chart options: '$confd/$chart.conf'"
328 . "$confd/$chart.conf"
330 echo >&2 "$PROGRAM_NAME: $chart: configuration file '$confd/$chart.conf' not found. Using defaults."
333 eval "dt=\$$chart$suffix_update_every"
334 dt=$(( dt + 1 - 1 )) # make sure it is a number
335 if [ $dt -lt $update_every ]
337 eval "$chart$suffix_update_every=$update_every"
343 [ $debug -eq 1 ] && echo >&2 "$PROGRAM_NAME: '$chart' activated"
344 active_charts="$active_charts $chart"
346 echo >&2 "$PROGRAM_NAME: chart '$chart' check() function reports failure."
349 [ $debug -eq 1 ] && echo >&2 "$PROGRAM_NAME: activated charts: $active_charts"
352 # -----------------------------------------------------------------------------
355 # enable work time reporting
357 test $debug -eq 1 && debug_time=tellwork
359 # if we only need a specific chart, remove all the others
360 if [ ! -z "${chart_only}" ]
362 [ $debug -eq 1 ] && echo >&2 "$PROGRAM_NAME: requested to run only for: '${chart_only}'"
364 for chart in $active_charts
366 if [ "$chart" = "$chart_only" ]
368 check_charts="$chart"
372 active_charts="$check_charts"
374 [ $debug -eq 1 ] && echo >&2 "$PROGRAM_NAME: activated charts: $active_charts"
376 # stop if we just need a pre-check
379 echo >&2 "CHECK RESULT"
380 echo >&2 "Will run the charts: $active_charts"
384 # -----------------------------------------------------------------------------
390 if [ ! -z "$TMP_DIR" -a -d "$TMP_DIR" ]
392 [ $debug -eq 1 ] && echo >&2 "$PROGRAM_NAME: cleaning up temporary directory $TMP_DIR ..."
397 trap chartsd_cleanup EXIT
398 trap chartsd_cleanup SIGHUP
399 trap chartsd_cleanup INT
403 TMP_DIR="$( mktemp -d /var/run/netdata-${PROGRAM_NAME}-XXXXXXXXXX )"
405 TMP_DIR="$( mktemp -d /tmp/.netdata-${PROGRAM_NAME}-XXXXXXXXXX )"
408 cd "$TMP_DIR" || exit 1
410 # -----------------------------------------------------------------------------
414 for chart in $active_charts
416 [ $debug -eq 1 ] && echo >&2 "$PROGRAM_NAME: Calling '$chart$charts_create()'..."
420 run_charts="$run_charts $chart"
421 [ $debug -eq 1 ] && echo >&2 "$PROGRAM_NAME: '$chart' has initialized."
423 echo >&2 "$PROGRAM_NAME: chart '$chart' function '$chart$charts_create()' reports failure."
426 [ $debug -eq 1 ] && echo >&2 "$PROGRAM_NAME: run_charts='$run_charts'"
429 # -----------------------------------------------------------------------------
432 if [ -z "$run_charts" ]
434 echo >&2 "$PROGRAM_NAME: No charts to collect data from."
439 declare -A charts_last_update=() charts_min_dt=()
441 local exit_after=$((restart_timeout / update_every)) \
442 c=0 dt ret exec_start_ms exec_end_ms \
443 chart now_charts=() next_charts=($run_charts)
445 # return the current time in ms in $now_ms
448 for chart in $run_charts
450 eval "charts_min_dt[$chart]=\$$chart$suffix_update_every"
451 test -z "${charts_min_dt[$chart]}" && charts_min_dt[$charts]=$update_every
452 charts_last_update[$chart]=$((now_ms - (charts_min_dt[$chart] * 1000) ))
454 echo "CHART netdata.plugin_$chart '' 'Execution time for $chart plugin' 'milliseconds / run' netdata netdata area 90000 ${charts_min_dt[$chart]}"
455 echo "DIMENSION run_time 'run time' absolute 1 1"
462 now_charts=("${next_charts[@]}")
465 for chart in "${now_charts[@]}"
467 # return the current time in ms in $now_ms
469 dt=$(( (now_ms - charts_last_update[$chart]) * 1000 ))
470 if [ $dt -ge $(( charts_min_dt[$chart] * 1000000 )) ]
472 charts_last_update[$chart]=$now_ms
474 # the first call should not give a duration
475 # so that netdata calibrates to current time
478 exec_start_ms=$now_ms
479 $chart$charts_update $dt
481 current_time_ms; exec_end_ms=$now_ms
483 echo "BEGIN netdata.plugin_$chart $dt"
486 echo "SET run_time = $(( exec_end_ms - exec_start_ms ))"
487 next_charts+=($chart)
489 echo "SET run_time = $(( (exec_end_ms - exec_start_ms) * -1 ))"
490 echo >&2 "$PROGRAM_NAME: chart '$chart' update() function reports failure. Disabling it."
494 next_charts+=($chart)
498 if [ "$pause_method" = "suspend" ]
500 echo "STOPPING_WAKE_ME_UP_PLEASE"
501 suspend || ( echo >&2 "$PROGRAM_NAME: suspend returned error $?, falling back to sleep."; loopsleepms $debug_time $update_every $time_divisor)
503 # wait the time you are required to
504 loopsleepms $debug_time $update_every $time_divisor
507 test $c -gt $exit_after && exit 0