3 # -----------------------------------------------------------------------------
6 pause_method="sleep" # use either "suspend" or "sleep"
7 # DO NOT USE SUSPEND - LINUX WILL SUSPEND NETDATA TOO
8 # THE WHOLE PROCESS GROUP - NOT JUST THE SHELL
13 myconfig="$confd/charts.d.conf"
14 minimum_update_frequency=1
15 update_every=1 # this is overwritten by the command line
17 # work around for non BASH shells
18 charts_create="_create"
19 charts_update="_update"
23 # -----------------------------------------------------------------------------
38 if [ "$1" = "debug" -o "$1" = "all" ]
45 if [ -f "$chartsd/$1.chart.sh" ]
48 chart_only="$( echo $1.chart.sh | sed "s/\.chart\.sh$//g" )"
53 if [ -f "$chartsd/$1" ]
56 chart_only="$( echo $1 | sed "s/\.chart\.sh$//g" )"
64 chart_only="$( basename "$1" | sed "s/\.chart\.sh$//g" )"
79 echo >&2 "Cannot understand parameter $1. Aborting."
85 # -----------------------------------------------------------------------------
86 # load my configuration
93 echo >&2 "charts.d: cannot load $myconfig"
99 if [ "$pause_method" = "suspend" ]
101 # enable bash job control
102 # this is required for suspend to work
106 # -----------------------------------------------------------------------------
109 # netdata passes the requested update frequency as the first argument
110 update_every=$(( update_every + 1 - 1)) # makes sure it is a number
111 test $update_every -eq 0 && update_every=1 # if it is zero, make it 1
113 # check the charts.d directory
114 if [ ! -d "$chartsd" ]
116 echo >&2 "charts.d: cannot find charts directory '$chartsd'"
121 # -----------------------------------------------------------------------------
124 # default sleep function
128 # if found and included, this file overwrites loopsleepms()
129 # with a high resolution timer function for precise looping.
130 . "$( dirname $0 )/loopsleepms.sh.inc"
133 # -----------------------------------------------------------------------------
134 # charts check functions
138 ls *.chart.sh | sed "s/\.chart\.sh$//g"
141 all_enabled_charts() {
144 # find all enabled charts
146 for chart in $( all_charts )
148 eval "enabled=\$$chart"
149 if [ "$enabled" = "yes" ]
151 local charts="$charts $chart"
153 echo >&2 "charts.d: chart '$chart' is NOT enabled. Add a line with $chart=yes in $myconfig to enable it."
160 # check the enabled charts
161 local check="$( cat "$chartsd/$chart.chart.sh" | sed "s/^ \+//g" | grep "^$chart$charts_check()" )"
164 echo >&2 "charts.d: chart '$chart' does not seem to have a $chart$charts_check() function. Disabling it."
168 local create="$( cat "$chartsd/$chart.chart.sh" | sed "s/^ \+//g" | grep "^$chart$charts_create()" )"
171 echo >&2 "charts.d: chart '$chart' does not seem to have a $chart$charts_create() function. Disabling it."
175 local update="$( cat "$chartsd/$chart.chart.sh" | sed "s/^ \+//g" | grep "^$chart$charts_update()" )"
178 echo >&2 "charts.d: chart '$chart' does not seem to have a $chart$charts_update() function. Disabling it."
183 if [ -f "$confd/$chart.conf" ]
185 if [ ! -z "$( cat "$confd/$chart.conf" | sed "s/^ \+//g" | grep -v "^$" | grep -v "^#" | grep -v "^$chart$charts_undescore" )" ]
187 echo >&2 "charts.d: chart's $chart config $confd/$chart.conf should only have lines starting with $chart$charts_undescore . Disabling it."
192 "$pluginsd/charts.d.dryrun-helper.sh" "$chart" "$chartsd/$chart.chart.sh" "$confd/$chart.conf" >/dev/null
195 echo >&2 "charts.d: chart's $chart did not pass the dry run check. This means it uses global variables not starting with $chart. Disabling it."
199 local charts2="$charts2 $chart"
206 # -----------------------------------------------------------------------------
209 suffix_update_every="_update_every"
211 for chart in $( all_enabled_charts )
213 . "$chartsd/$chart.chart.sh"
215 if [ -f "$confd/$chart.conf" ]
217 . "$confd/$chart.conf"
220 eval "dt=\$$chart$suffix_update_every"
221 dt=$(( dt + 1 - 1 )) # make sure it is a number
222 if [ $dt -lt $update_every ]
224 eval "$chart$suffix_update_every=$update_every"
230 active_charts="$active_charts $chart"
232 echo >&2 "charts.d: chart '$chart' check() function reports failure."
237 # -----------------------------------------------------------------------------
240 # enable work time reporting
242 test $debug -eq 1 && debug_time=tellwork
244 # if we only need a specific chart, remove all the others
245 if [ ! -z "$chart_only" ]
248 for chart in $active_charts
250 if [ "$chart" = "$chart_only" ]
252 check_charts="$chart"
256 active_charts="$check_charts"
259 # stop if we just need a pre-check
263 echo "Will run the charts: $active_charts"
267 # -----------------------------------------------------------------------------
272 if [ ! -z "$TMP_DIR" -a -d "$TMP_DIR" ]
274 echo >&2 "charts.d: cleaning up temporary directory $TMP_DIR ..."
279 trap chartsd_cleanup EXIT
280 trap chartsd_cleanup SIGHUP
281 trap chartsd_cleanup INT
285 TMP_DIR="$( mktemp -d /var/run/netdata-charts.d-XXXXXXXXXX )"
287 TMP_DIR="$( mktemp -d /tmp/.netdata-charts.d-XXXXXXXXXX )"
290 # -----------------------------------------------------------------------------
295 tr -c "[A-Z][a-z][0-9]" "_" |\
296 sed -e "s|^_\+||g" -e "s|_\+$||g" -e "s|_\+|_|g" |\
301 # -----------------------------------------------------------------------------
305 for chart in $active_charts
310 run_charts="$run_charts $chart"
312 echo >&2 "charts.d: chart '$chart' create() function reports failure."
317 # -----------------------------------------------------------------------------
321 local exit_after=$((3600 / update_every))
323 # return the current time in ms in $now_ms
327 for chart in $now_charts
329 eval "local last_update_$chart=\$((now_ms - ($chart$suffix_update_every * 1000) ))"
337 local now_charts="$run_charts"
341 for chart in $now_charts
343 # return the current time in ms in $now_ms
346 eval "local chart_min_dt=\$$chart$suffix_update_every"
347 test -z "$chart_min_dt" && local chart_min_dt=$update_every
348 local chart_min_dt=$((chart_min_dt * 1000000))
350 eval "local last=\$last_update_$chart"
351 test -z "$last" && local last=$((now_ms - (chart_min_dt / 1000) ))
353 local dt=$(( (now_ms - last) * 1000 ))
354 if [ $dt -ge $chart_min_dt ]
356 eval "last_update_$chart=$now_ms"
358 # the first call should not give a duration
359 # so that netdata calibrates to current time
360 test $c -eq 1 && local dt=
362 $chart$charts_update $dt
365 run_charts="$run_charts $chart"
367 echo >&2 "charts.d: chart '$chart' update() function reports failure. Disabling it."
370 run_charts="$run_charts $chart"
374 if [ "$pause_method" = "suspend" ]
376 echo "STOPPING_WAKE_ME_UP_PLEASE"
377 suspend || ( echo >&2 "suspend returned error $?, falling back to sleep."; loopsleepms $debug_time $update_every )
379 # wait the time you are required to
380 loopsleepms $debug_time $update_every
383 test $c -gt $exit_after && exit 0