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