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