]> arthur.barton.de Git - netdata.git/blob - plugins.d/charts.d.plugin
compatibility fixes
[netdata.git] / plugins.d / charts.d.plugin
1 #!/bin/sh
2
3 # -----------------------------------------------------------------------------
4 # insternal defaults
5
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
9
10 pluginsd="plugins.d"
11 confd="conf.d"
12 chartsd="charts.d"
13 myconfig="$confd/charts.d.conf"
14 minimum_update_frequency=1
15 update_every=1  # this is overwritten by the command line
16
17 # work around for non BASH shells
18 charts_create="_create"
19 charts_update="_update"
20 charts_check="_check"
21 charts_undescore="_"
22
23 # -----------------------------------------------------------------------------
24 # parse parameters
25
26 debug=0
27 check=0
28 chart_only=
29 while [ ! -z "$1" ]
30 do
31         if [ "$1" = "check" ]
32         then
33                 check=1
34                 shift
35                 continue
36         fi
37
38         if [ "$1" = "debug" -o "$1" = "all" ]
39         then
40                 debug=1
41                 shift
42                 continue
43         fi
44
45         if [ -f "$chartsd/$1.chart.sh" ]
46         then
47                 debug=1
48                 chart_only="`echo $1.chart.sh | sed "s/\.chart\.sh$//g"`"
49                 shift
50                 continue
51         fi
52
53         if [ -f "$chartsd/$1" ]
54         then
55                 debug=1
56                 chart_only="`echo $1 | sed "s/\.chart\.sh$//g"`"
57                 shift
58                 continue
59         fi
60
61         if [ -f "$1" ]
62         then
63                 debug=1
64                 chart_only="`basename "$1" | sed "s/\.chart\.sh$//g"`"
65                 shift
66                 continue
67         fi
68
69         # number check
70         n="$1"
71         x=$((n + 1 - 1))
72         if [ "$x" = "$n" ]
73         then
74                 update_every=$x
75                 shift
76                 continue
77         fi
78
79         echo >&2 "Cannot understand parameter $1. Aborting."
80         echo "DISABLE"
81         exit 1
82 done
83
84
85 # -----------------------------------------------------------------------------
86 # load my configuration
87
88 if [ -f "$myconfig" ]
89         then
90         . "$myconfig"
91         if [ $? -ne 0 ]
92         then
93                 echo >&2 "charts.d: cannot load $myconfig"
94                 echo "DISABLE"
95                 exit 1
96         fi
97 fi
98
99 if [ "$pause_method" = "suspend" ]
100 then
101         # enable bash job control
102         # this is required for suspend to work
103         set -m
104 fi
105
106 # -----------------------------------------------------------------------------
107 # internal checks
108
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
112
113 # check the charts.d directory
114 if [ ! -d "$chartsd" ]
115         then
116         echo >&2 "charts.d: cannot find charts directory '$chartsd'"
117         echo "DISABLE"
118 fi
119
120
121 # -----------------------------------------------------------------------------
122 # loop control
123
124 # default sleep function
125 loopsleepms() {
126         sleep $1
127 }
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"
131
132
133 # -----------------------------------------------------------------------------
134 # charts check functions
135
136 all_charts() {
137         cd "$chartsd"
138         ls *.chart.sh | sed "s/\.chart\.sh$//g"
139 }
140
141 all_enabled_charts() {
142         local charts=
143
144         # find all enabled charts
145
146         for chart in `all_charts`
147         do
148                 eval "enabled=\$$chart"
149                 if [ "$enabled" = "yes" ]
150                 then
151                         local charts="$charts $chart"
152                 else
153                         echo >&2 "charts.d: chart '$chart' is NOT enabled. Add a line with $chart=yes in $myconfig to enable it."
154                 fi
155         done
156
157         local charts2=
158         for chart in $charts
159         do
160                 # check the enabled charts
161                 local check="`cat "$chartsd/$chart.chart.sh" | sed "s/^ \+//g" | grep "^$chart$charts_check()"`"
162                 if [ -z "$check" ]
163                 then
164                         echo >&2 "charts.d: chart '$chart' does not seem to have a $chart$charts_check() function. Disabling it."
165                         continue
166                 fi
167
168                 local create="`cat "$chartsd/$chart.chart.sh" | sed "s/^ \+//g" | grep "^$chart$charts_create()"`"
169                 if [ -z "$create" ]
170                 then
171                         echo >&2 "charts.d: chart '$chart' does not seem to have a $chart$charts_create() function. Disabling it."
172                         continue
173                 fi
174
175                 local update="`cat "$chartsd/$chart.chart.sh" | sed "s/^ \+//g" | grep "^$chart$charts_update()"`"
176                 if [ -z "$update" ]
177                 then
178                         echo >&2 "charts.d: chart '$chart' does not seem to have a $chart$charts_update() function. Disabling it."
179                         continue
180                 fi
181
182                 # check its config
183                 if [ -f "$confd/$chart.conf" ]
184                 then
185                         if [ ! -z "`cat "$confd/$chart.conf" | sed "s/^ \+//g" | grep -v "^$" | grep -v "^#" | grep -v "^$chart$charts_undescore"`" ]
186                         then
187                                 echo >&2 "charts.d: chart's $chart config $confd/$chart.conf should only have lines starting with $chart$charts_undescore . Disabling it."
188                                 continue
189                         fi
190                 fi
191
192                 "$pluginsd/charts.d.dryrun-helper.sh" "$chart" "$chartsd/$chart.chart.sh" "$confd/$chart.conf" >/dev/null
193                 if [ $? -ne 0 ]
194                 then
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."
196                         continue
197                 fi
198
199                 local charts2="$charts2 $chart"
200         done
201
202         echo $charts2
203 }
204
205
206 # -----------------------------------------------------------------------------
207 # load the charts
208
209 active_charts=
210 for chart in `all_enabled_charts`
211 do
212         . "$chartsd/$chart.chart.sh"
213
214         if [ -f "$confd/$chart.conf" ]
215         then
216                 . "$confd/$chart.conf"
217         fi
218
219         $chart$charts_check
220         if [ $? -eq 0 ]
221         then
222                 active_charts="$active_charts $chart"
223         else
224                 echo >&2 "charts.d: chart '$chart' check() function reports failure."
225         fi
226 done
227
228
229 # -----------------------------------------------------------------------------
230 # check overwrites
231
232 # enable work time reporting
233 debug_time=
234 test $debug -eq 1 && debug_time=tellwork
235
236 # if we only need a specific chart, remove all the others
237 if [ ! -z "$chart_only" ]
238 then
239         check_charts=
240         for chart in $active_charts
241         do
242                 if [ "$chart" = "$chart_only" ]
243                 then
244                         check_charts="$chart"
245                         break
246                 fi
247         done
248         active_charts="$check_charts"
249 fi
250
251 # stop if we just need a pre-check
252 if [ $check -eq 1 ]
253 then
254         echo "CHECK RESULT"
255         echo "Will run the charts: $active_charts"
256         exit 0
257 fi
258
259
260 # -----------------------------------------------------------------------------
261 # create charts
262
263 run_charts=
264 for chart in $active_charts
265 do
266         $chart$charts_create
267         if [ $? -eq 0 ]
268         then
269                 run_charts="$run_charts $chart"
270         else
271                 echo >&2 "charts.d: chart '$chart' create() function reports failure."
272         fi
273 done
274
275
276 # -----------------------------------------------------------------------------
277 # update dimensions
278 global_update() {
279         while [ 1 ]
280         do
281                 local now_charts="$run_charts"
282                 local run_charts=
283
284                 local chart=
285                 for chart in $now_charts
286                 do
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)
291
292                         eval "local last=\$last_update_$chart"
293                         test -z "$last" && local last=$((now - 1000))
294
295                         local dt=$(( (now - last) * 1000 ))
296                         eval "last_update_$chart=$now"
297
298                         $chart$charts_update $dt
299                         if [ $? -eq 0 ]
300                         then
301                                 run_charts="$run_charts $chart"
302                         else
303                                 echo >&2 "charts.d: chart '$chart' update() function reports failure. Disabling it."
304                         fi
305                 done
306
307                 if [ "$pause_method" = "suspend" ]
308                 then
309                         echo "STOPPING_WAKE_ME_UP_PLEASE"
310                         suspend || ( echo >&2 "suspend returned error $?, falling back to sleep."; loopsleepms $debug_time $update_every )
311                 else
312                         # wait the time you are required to
313                         loopsleepms $debug_time $update_every
314                 fi
315         done
316 }
317
318 global_update