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