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"
268 # -----------------------------------------------------------------------------
272 for chart in $active_charts
277 run_charts="$run_charts $chart"
279 echo >&2 "charts.d: chart '$chart' create() function reports failure."
284 # -----------------------------------------------------------------------------
288 local exit_after=$((3600 / update_every))
290 # return the current time in ms in $now_ms
294 for chart in $now_charts
296 eval "local last_update_$chart=\$((now_ms - ($chart$suffix_update_every * 1000) ))"
304 local now_charts="$run_charts"
308 for chart in $now_charts
310 # return the current time in ms in $now_ms
313 eval "local chart_min_dt=\$$chart$suffix_update_every"
314 test -z "$chart_min_dt" && local chart_min_dt=$update_every
315 local chart_min_dt=$((chart_min_dt * 1000000))
317 eval "local last=\$last_update_$chart"
318 test -z "$last" && local last=$((now_ms - (chart_min_dt / 1000) ))
320 local dt=$(( (now_ms - last) * 1000 ))
321 if [ $dt -ge $chart_min_dt ]
323 eval "last_update_$chart=$now_ms"
325 # the first call should not give a duration
326 # so that netdata calibrates to current time
327 test $c -eq 1 && local dt=
329 $chart$charts_update $dt
332 run_charts="$run_charts $chart"
334 echo >&2 "charts.d: chart '$chart' update() function reports failure. Disabling it."
337 run_charts="$run_charts $chart"
341 if [ "$pause_method" = "suspend" ]
343 echo "STOPPING_WAKE_ME_UP_PLEASE"
344 suspend || ( echo >&2 "suspend returned error $?, falling back to sleep."; loopsleepms $debug_time $update_every )
346 # wait the time you are required to
347 loopsleepms $debug_time $update_every
350 test $c -gt $exit_after && exit 0