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 mktemp || exit 1
45 # -----------------------------------------------------------------------------
47 # netdata exposes a few environment variables for us
49 pause_method="sleep" # use either "suspend" or "sleep"
50 # DO NOT USE SUSPEND - LINUX WILL SUSPEND NETDATA TOO
51 # THE WHOLE PROCESS GROUP - NOT JUST THE SHELL
53 pluginsd="${NETDATA_PLUGINS_DIR}"
54 [ -z "$pluginsd" ] && pluginsd="$( dirname $PROGRAM_FILE )"
56 confd="${NETDATA_CONFIG_DIR-/etc/netdata}"
57 chartsd="$pluginsd/../charts.d"
59 myconfig="$confd/$PROGRAM_NAME.conf"
61 minimum_update_frequency="${NETDATA_UPDATE_EVERY-1}"
62 update_every=${minimum_update_frequency} # this will be overwritten by the command line
64 # work around for non BASH shells
65 charts_create="_create"
66 charts_update="_update"
70 # when making iterations, charts.d can loop more frequently
71 # to prevent plugins missing iterations.
72 # this is a percentage relative to update_every to align its
74 # The minimum is 10%, the maximum 100%.
75 # So, if update_every is 1 second and time_divisor is 50,
76 # charts.d will iterate every 500ms.
77 # Charts will be called to collect data only if the time
78 # passes since the last time the collected data is equal or
79 # above their update_every.
82 # number of seconds to run without restart
83 # after this time, charts.d.plugin will exit
84 # netdata will restart it
87 # -----------------------------------------------------------------------------
102 if [ "$1" = "debug" -o "$1" = "all" ]
109 if [ -f "$chartsd/$1.chart.sh" ]
112 chart_only="$( echo $1.chart.sh | sed "s/\.chart\.sh$//g" )"
117 if [ -f "$chartsd/$1" ]
120 chart_only="$( echo $1 | sed "s/\.chart\.sh$//g" )"
132 [ $update_every -lt $minimum_update_frequency ] && update_every=$minimum_update_frequency
136 echo >&2 "Cannot understand parameter $1. Aborting."
142 # -----------------------------------------------------------------------------
143 # load my configuration
145 if [ -f "$myconfig" ]
150 echo >&2 "$PROGRAM_NAME: cannot load $myconfig"
154 time_divisor=$((time_divisor))
155 [ $time_divisor -lt 10 ] && time_divisor=10
156 [ $time_divisor -gt 100 ] && time_divisor=100
159 if [ "$pause_method" = "suspend" ]
161 # enable bash job control
162 # this is required for suspend to work
166 # -----------------------------------------------------------------------------
169 # netdata passes the requested update frequency as the first argument
170 update_every=$(( update_every + 1 - 1)) # makes sure it is a number
171 test $update_every -eq 0 && update_every=1 # if it is zero, make it 1
173 # check the charts.d directory
174 if [ ! -d "$chartsd" ]
176 echo >&2 "$PROGRAM_NAME: cannot find charts directory '$chartsd'"
181 # -----------------------------------------------------------------------------
184 # default sleep function
186 [ "$1" = "tellwork" ] && shift
192 now_ms="$(date +'%s')000"
195 # if found and included, this file overwrites loopsleepms()
196 # and current_time_ms() with a high resolution timer function
197 # for precise looping.
198 . "$pluginsd/loopsleepms.sh.inc"
201 # -----------------------------------------------------------------------------
206 tr -c "[A-Z][a-z][0-9]" "_" |\
207 sed -e "s|^_\+||g" -e "s|_\+$||g" -e "s|_\+|_|g" |\
212 # -----------------------------------------------------------------------------
213 # charts check functions
217 [ $? -ne 0 ] && echo >&2 "$PROGRAM_NAME: Cannot cd to $chartsd" && return 1
219 ls *.chart.sh | sed "s/\.chart\.sh$//g"
222 all_enabled_charts() {
225 # find all enabled charts
227 for chart in $( all_charts )
229 eval "enabled=\$$chart"
230 if [ "$enabled" = "yes" ]
232 [ $debug -eq 1 ] && echo >&2 "$PROGRAM_NAME: '$chart' is enabled."
233 local charts="$charts $chart"
235 echo >&2 "$PROGRAM_NAME: '$chart' is NOT enabled. Add a line with $chart=yes in $myconfig to enable it."
242 # check the enabled charts
243 local check="$( cat "$chartsd/$chart.chart.sh" | sed "s/^ \+//g" | grep "^$chart$charts_check()" )"
246 echo >&2 "$PROGRAM_NAME: chart '$chart' does not seem to have a $chart$charts_check() function. Disabling it."
250 local create="$( cat "$chartsd/$chart.chart.sh" | sed "s/^ \+//g" | grep "^$chart$charts_create()" )"
253 echo >&2 "$PROGRAM_NAME: chart '$chart' does not seem to have a $chart$charts_create() function. Disabling it."
257 local update="$( cat "$chartsd/$chart.chart.sh" | sed "s/^ \+//g" | grep "^$chart$charts_update()" )"
260 echo >&2 "$PROGRAM_NAME: chart '$chart' does not seem to have a $chart$charts_update() function. Disabling it."
265 #if [ -f "$confd/$chart.conf" ]
267 # if [ ! -z "$( cat "$confd/$chart.conf" | sed "s/^ \+//g" | grep -v "^$" | grep -v "^#" | grep -v "^$chart$charts_undescore" )" ]
269 # echo >&2 "$PROGRAM_NAME: chart's $chart config $confd/$chart.conf should only have lines starting with $chart$charts_undescore . Disabling it."
274 "$pluginsd/charts.d.dryrun-helper.sh" "$chart" "$chartsd/$chart.chart.sh" "$confd/$chart.conf" >/dev/null
277 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."
281 local charts2="$charts2 $chart"
285 [ $debug -eq 1 ] && echo >&2 "$PROGRAM_NAME: enabled charts: $charts2"
289 # -----------------------------------------------------------------------------
292 suffix_update_every="_update_every"
294 for chart in $( all_enabled_charts )
296 [ $debug -eq 1 ] && echo >&2 "$PROGRAM_NAME: loading chart: '$chartsd/$chart.chart.sh'"
297 . "$chartsd/$chart.chart.sh"
299 if [ -f "$confd/$chart.conf" ]
301 [ $debug -eq 1 ] && echo >&2 "$PROGRAM_NAME: loading chart options: '$confd/$chart.conf'"
302 . "$confd/$chart.conf"
305 eval "dt=\$$chart$suffix_update_every"
306 dt=$(( dt + 1 - 1 )) # make sure it is a number
307 if [ $dt -lt $update_every ]
309 eval "$chart$suffix_update_every=$update_every"
315 [ $debug -eq 1 ] && echo >&2 "$PROGRAM_NAME: '$chart' activated"
316 active_charts="$active_charts $chart"
318 echo >&2 "$PROGRAM_NAME: chart '$chart' check() function reports failure."
321 [ $debug -eq 1 ] && echo >&2 "$PROGRAM_NAME: activated charts: $active_charts"
324 # -----------------------------------------------------------------------------
327 # enable work time reporting
329 test $debug -eq 1 && debug_time=tellwork
331 # if we only need a specific chart, remove all the others
332 if [ ! -z "${chart_only}" ]
334 [ $debug -eq 1 ] && echo >&2 "$PROGRAM_NAME: requested to run only for: '${chart_only}'"
336 for chart in $active_charts
338 if [ "$chart" = "$chart_only" ]
340 check_charts="$chart"
344 active_charts="$check_charts"
346 [ $debug -eq 1 ] && echo >&2 "$PROGRAM_NAME: activated charts: $active_charts"
348 # stop if we just need a pre-check
351 echo >&2 "CHECK RESULT"
352 echo >&2 "Will run the charts: $active_charts"
356 # -----------------------------------------------------------------------------
362 if [ ! -z "$TMP_DIR" -a -d "$TMP_DIR" ]
364 [ $debug -eq 1 ] && echo >&2 "$PROGRAM_NAME: cleaning up temporary directory $TMP_DIR ..."
369 trap chartsd_cleanup EXIT
370 trap chartsd_cleanup SIGHUP
371 trap chartsd_cleanup INT
375 TMP_DIR="$( mktemp -d /var/run/netdata-${PROGRAM_NAME}-XXXXXXXXXX )"
377 TMP_DIR="$( mktemp -d /tmp/.netdata-${PROGRAM_NAME}-XXXXXXXXXX )"
380 cd "$TMP_DIR" || exit 1
382 # -----------------------------------------------------------------------------
386 for chart in $active_charts
388 [ $debug -eq 1 ] && echo >&2 "$PROGRAM_NAME: Calling '$chart$charts_create()'..."
392 run_charts="$run_charts $chart"
393 [ $debug -eq 1 ] && echo >&2 "$PROGRAM_NAME: '$chart' has initialized."
395 echo >&2 "$PROGRAM_NAME: chart '$chart' function '$chart$charts_create()' reports failure."
398 [ $debug -eq 1 ] && echo >&2 "$PROGRAM_NAME: run_charts='$run_charts'"
401 # -----------------------------------------------------------------------------
404 if [ -z "$run_charts" ]
406 echo >&2 "$PROGRAM_NAME: No charts to collect data from."
411 declare -A charts_last_update=() charts_min_dt=()
413 local exit_after=$((restart_timeout / update_every)) \
415 chart now_charts=() next_charts=($run_charts)
417 # return the current time in ms in $now_ms
420 for chart in $run_charts
422 eval "charts_min_dt[$chart]=\$$chart$suffix_update_every"
423 test -z "${charts_min_dt[$chart]}" && charts_min_dt[$charts]=$update_every
424 charts_last_update[$chart]=$((now_ms - (charts_min_dt[$chart] * 1000) ))
431 now_charts=("${next_charts[@]}")
434 for chart in "${now_charts[@]}"
436 # return the current time in ms in $now_ms
439 dt=$(( (now_ms - charts_last_update[$chart]) * 1000 ))
440 if [ $dt -ge $(( charts_min_dt[$chart] * 1000000 )) ]
442 charts_last_update[$chart]=$now_ms
444 # the first call should not give a duration
445 # so that netdata calibrates to current time
448 $chart$charts_update $dt
451 next_charts+=($chart)
453 echo >&2 "$PROGRAM_NAME: chart '$chart' update() function reports failure. Disabling it."
456 next_charts+=($chart)
460 if [ "$pause_method" = "suspend" ]
462 echo "STOPPING_WAKE_ME_UP_PLEASE"
463 suspend || ( echo >&2 "$PROGRAM_NAME: suspend returned error $?, falling back to sleep."; loopsleepms $debug_time $update_every $time_divisor)
465 # wait the time you are required to
466 loopsleepms $debug_time $update_every $time_divisor
469 test $c -gt $exit_after && exit 0