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 # -----------------------------------------------------------------------------
210 for chart in `all_enabled_charts`
212 . "$chartsd/$chart.chart.sh"
214 if [ -f "$confd/$chart.conf" ]
216 . "$confd/$chart.conf"
222 active_charts="$active_charts $chart"
224 echo >&2 "charts.d: chart '$chart' check() function reports failure."
229 # -----------------------------------------------------------------------------
232 # enable work time reporting
234 test $debug -eq 1 && debug_time=tellwork
236 # if we only need a specific chart, remove all the others
237 if [ ! -z "$chart_only" ]
240 for chart in $active_charts
242 if [ "$chart" = "$chart_only" ]
244 check_charts="$chart"
248 active_charts="$check_charts"
251 # stop if we just need a pre-check
255 echo "Will run the charts: $active_charts"
260 # -----------------------------------------------------------------------------
264 for chart in $active_charts
269 run_charts="$run_charts $chart"
271 echo >&2 "charts.d: chart '$chart' create() function reports failure."
276 # -----------------------------------------------------------------------------
281 local now_charts="$run_charts"
285 for chart in $now_charts
287 local d="`date +'%s.%N'`"
288 local s="`echo $d | cut -d '.' -f 1`"
289 local m="`echo $d | cut -d '.' -f 2 | cut -b 1-3`"
290 local now="$s$m" # milliseconds since epoch (1-1-1970)
292 eval "local last=\$last_update_$chart"
293 test -z "$last" && local last=$((now - 1000))
295 local dt=$(( (now - last) * 1000 ))
296 eval "last_update_$chart=$now"
298 $chart$charts_update $dt
301 run_charts="$run_charts $chart"
303 echo >&2 "charts.d: chart '$chart' update() function reports failure. Disabling it."
307 if [ "$pause_method" = "suspend" ]
309 echo "STOPPING_WAKE_ME_UP_PLEASE"
310 suspend || ( echo >&2 "suspend returned error $?, falling back to sleep."; loopsleepms $debug_time $update_every )
312 # wait the time you are required to
313 loopsleepms $debug_time $update_every