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 # -----------------------------------------------------------------------------
85 if [ "$1" = "debug" -o "$1" = "all" ]
92 if [ -f "$chartsd/$1.chart.sh" ]
95 chart_only="$( echo $1.chart.sh | sed "s/\.chart\.sh$//g" )"
100 if [ -f "$chartsd/$1" ]
103 chart_only="$( echo $1 | sed "s/\.chart\.sh$//g" )"
115 [ $update_every -lt $minimum_update_frequency ] && update_every=$minimum_update_frequency
119 echo >&2 "Cannot understand parameter $1. Aborting."
125 # -----------------------------------------------------------------------------
126 # load my configuration
128 if [ -f "$myconfig" ]
133 echo >&2 "$PROGRAM_NAME: cannot load $myconfig"
139 if [ "$pause_method" = "suspend" ]
141 # enable bash job control
142 # this is required for suspend to work
146 # -----------------------------------------------------------------------------
149 # netdata passes the requested update frequency as the first argument
150 update_every=$(( update_every + 1 - 1)) # makes sure it is a number
151 test $update_every -eq 0 && update_every=1 # if it is zero, make it 1
153 # check the charts.d directory
154 if [ ! -d "$chartsd" ]
156 echo >&2 "$PROGRAM_NAME: cannot find charts directory '$chartsd'"
161 # -----------------------------------------------------------------------------
164 # default sleep function
171 now_ms="$(date +'%s')000"
174 # if found and included, this file overwrites loopsleepms()
175 # and current_time_ms() with a high resolution timer function
176 # for precise looping.
177 . "$pluginsd/loopsleepms.sh.inc"
180 # -----------------------------------------------------------------------------
181 # charts check functions
185 [ $? -ne 0 ] && echo >&2 "$PROGRAM_NAME: Cannot cd to $chartsd" && return 1
187 ls *.chart.sh | sed "s/\.chart\.sh$//g"
190 all_enabled_charts() {
193 # find all enabled charts
195 for chart in $( all_charts )
197 eval "enabled=\$$chart"
198 if [ "$enabled" = "yes" ]
200 [ $debug -eq 1 ] && echo >&2 "$PROGRAM_NAME: '$chart' is enabled."
201 local charts="$charts $chart"
203 echo >&2 "$PROGRAM_NAME: '$chart' is NOT enabled. Add a line with $chart=yes in $myconfig to enable it."
210 # check the enabled charts
211 local check="$( cat "$chartsd/$chart.chart.sh" | sed "s/^ \+//g" | grep "^$chart$charts_check()" )"
214 echo >&2 "$PROGRAM_NAME: chart '$chart' does not seem to have a $chart$charts_check() function. Disabling it."
218 local create="$( cat "$chartsd/$chart.chart.sh" | sed "s/^ \+//g" | grep "^$chart$charts_create()" )"
221 echo >&2 "$PROGRAM_NAME: chart '$chart' does not seem to have a $chart$charts_create() function. Disabling it."
225 local update="$( cat "$chartsd/$chart.chart.sh" | sed "s/^ \+//g" | grep "^$chart$charts_update()" )"
228 echo >&2 "$PROGRAM_NAME: chart '$chart' does not seem to have a $chart$charts_update() function. Disabling it."
233 #if [ -f "$confd/$chart.conf" ]
235 # if [ ! -z "$( cat "$confd/$chart.conf" | sed "s/^ \+//g" | grep -v "^$" | grep -v "^#" | grep -v "^$chart$charts_undescore" )" ]
237 # echo >&2 "$PROGRAM_NAME: chart's $chart config $confd/$chart.conf should only have lines starting with $chart$charts_undescore . Disabling it."
242 "$pluginsd/charts.d.dryrun-helper.sh" "$chart" "$chartsd/$chart.chart.sh" "$confd/$chart.conf" >/dev/null
245 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."
249 local charts2="$charts2 $chart"
253 [ $debug -eq 1 ] && echo >&2 "$PROGRAM_NAME: enabled charts: $charts2"
257 # -----------------------------------------------------------------------------
260 suffix_update_every="_update_every"
262 for chart in $( all_enabled_charts )
264 [ $debug -eq 1 ] && echo >&2 "$PROGRAM_NAME: loading chart: '$chartsd/$chart.chart.sh'"
265 . "$chartsd/$chart.chart.sh"
267 if [ -f "$confd/$chart.conf" ]
269 [ $debug -eq 1 ] && echo >&2 "$PROGRAM_NAME: loading chart options: '$confd/$chart.conf'"
270 . "$confd/$chart.conf"
273 eval "dt=\$$chart$suffix_update_every"
274 dt=$(( dt + 1 - 1 )) # make sure it is a number
275 if [ $dt -lt $update_every ]
277 eval "$chart$suffix_update_every=$update_every"
283 [ $debug -eq 1 ] && echo >&2 "$PROGRAM_NAME: '$chart' activated"
284 active_charts="$active_charts $chart"
286 echo >&2 "$PROGRAM_NAME: chart '$chart' check() function reports failure."
289 [ $debug -eq 1 ] && echo >&2 "$PROGRAM_NAME: activated charts: $active_charts"
292 # -----------------------------------------------------------------------------
295 # enable work time reporting
297 test $debug -eq 1 && debug_time=tellwork
299 # if we only need a specific chart, remove all the others
300 if [ ! -z "${chart_only}" ]
302 [ $debug -eq 1 ] && echo >&2 "$PROGRAM_NAME: requested to run only for: '${chart_only}'"
304 for chart in $active_charts
306 if [ "$chart" = "$chart_only" ]
308 check_charts="$chart"
312 active_charts="$check_charts"
314 [ $debug -eq 1 ] && echo >&2 "$PROGRAM_NAME: activated charts: $active_charts"
316 # stop if we just need a pre-check
319 echo >&2 "CHECK RESULT"
320 echo >&2 "Will run the charts: $active_charts"
324 # -----------------------------------------------------------------------------
330 if [ ! -z "$TMP_DIR" -a -d "$TMP_DIR" ]
332 [ $debug -eq 1 ] && echo >&2 "$PROGRAM_NAME: cleaning up temporary directory $TMP_DIR ..."
337 trap chartsd_cleanup EXIT
338 trap chartsd_cleanup SIGHUP
339 trap chartsd_cleanup INT
343 TMP_DIR="$( mktemp -d /var/run/netdata-${PROGRAM_NAME}-XXXXXXXXXX )"
345 TMP_DIR="$( mktemp -d /tmp/.netdata-${PROGRAM_NAME}-XXXXXXXXXX )"
348 cd "$TMP_DIR" || exit 1
350 # -----------------------------------------------------------------------------
355 tr -c "[A-Z][a-z][0-9]" "_" |\
356 sed -e "s|^_\+||g" -e "s|_\+$||g" -e "s|_\+|_|g" |\
361 # -----------------------------------------------------------------------------
365 for chart in $active_charts
367 [ $debug -eq 1 ] && echo >&2 "$PROGRAM_NAME: Calling '$chart$charts_create()'..."
371 run_charts="$run_charts $chart"
372 [ $debug -eq 1 ] && echo >&2 "$PROGRAM_NAME: '$chart' has initialized."
374 echo >&2 "$PROGRAM_NAME: chart '$chart' function '$chart$charts_create()' reports failure."
377 [ $debug -eq 1 ] && echo >&2 "$PROGRAM_NAME: run_charts='$run_charts'"
380 # -----------------------------------------------------------------------------
383 declare -A charts_last_update=() charts_min_dt=()
385 local exit_after=$((3600 / update_every)) \
387 chart now_charts=() next_charts=($run_charts)
389 # return the current time in ms in $now_ms
392 for chart in $run_charts
394 eval "charts_min_dt[$chart]=\$$chart$suffix_update_every"
395 test -z "${charts_min_dt[$chart]}" && charts_min_dt[$charts]=$update_every
396 charts_last_update[$chart]=$((now_ms - (charts_min_dt[$chart] * 1000) ))
403 now_charts=("${next_charts[@]}")
406 for chart in "${now_charts[@]}"
408 # return the current time in ms in $now_ms
411 dt=$(( (now_ms - charts_last_update[$chart]) * 1000 ))
412 if [ $dt -ge $(( charts_min_dt[$chart] * 1000000 )) ]
414 charts_last_update[$chart]=$now_ms
416 # the first call should not give a duration
417 # so that netdata calibrates to current time
420 $chart$charts_update $dt
423 next_charts+=($chart)
425 echo >&2 "$PROGRAM_NAME: chart '$chart' update() function reports failure. Disabling it."
428 next_charts+=($chart)
432 if [ "$pause_method" = "suspend" ]
434 echo "STOPPING_WAKE_ME_UP_PLEASE"
435 suspend || ( echo >&2 "$PROGRAM_NAME: suspend returned error $?, falling back to sleep."; loopsleepms $debug_time $update_every )
437 # wait the time you are required to
438 loopsleepms $debug_time $update_every
441 test $c -gt $exit_after && exit 0