]> arthur.barton.de Git - netdata.git/blob - plugins.d/charts.d.plugin
Now there are 2 kinds of plugins:
[netdata.git] / plugins.d / charts.d.plugin
1 #!/bin/sh
2
3 check=0
4 if [ "$1" = "check" ]
5 then
6         check=1
7         shift
8 fi
9
10 # default sleep function
11 loopsleepms() {
12         sleep $1
13 }
14 # if found and included, this file overwrites loopsleepms()
15 # with a high resolution timer function for precise looping.
16 . "`dirname $0`/loopsleepms.sh.inc"
17
18 # netdata passes the requested update frequency as the first argument
19 update_every=$1
20 update_every=$(( update_every + 1 - 1)) # makes sure it is a number
21 test $update_every -eq 0 && update_every=1 # if it is zero, make it 1
22
23 pluginsd="plugins.d"
24 confd="conf.d"
25 chartsd="charts.d"
26 myconfig="$confd/charts.d.conf"
27 minimum_update_frequency=1
28
29 # load the configuration
30 if [ -f "$myconfig" ]
31         then
32         . "$myconfig"
33         if [ $? -ne 0 ]
34         then
35                 echo >&2 "charts.d: cannot load $myconfig"
36                 echo "DISABLE"
37                 exit 1
38         fi
39 fi
40
41 if [ ! -d "$chartsd" ]
42         then
43         echo >&2 "charts.d: cannot find charts directory '$chartsd'"
44         echo "DISABLE"
45 fi
46
47 all_charts() {
48         cd "$chartsd"
49         ls *.chart.sh | sed "s/\.chart\.sh$//g"
50 }
51
52 all_enabled_charts() {
53         local charts=
54
55         # find all enabled charts
56
57         for x in `all_charts`
58         do
59                 eval "enabled=\$$x"
60                 if [ "$enabled" = "yes" ]
61                 then
62                         local charts="$charts $x"
63                 else
64                         echo >&2 "charts.d: chart '$x' is NOT enabled. Add a line with $x=yes in $myconfig to enable it."
65                 fi
66         done
67
68         local charts2=
69         for x in $charts
70         do
71                 # check the enabled charts
72                 local check=`cat "$chartsd/$x.chart.sh" | sed "s/^ \+//g" | grep "^${x}_check()"`
73                 if [ -z "$check" ]
74                 then
75                         echo >&2 "charts.d: chart '$x' does not seem to have a ${x}_check() function. Disabling it."
76                         continue
77                 fi
78
79                 local create=`cat "$chartsd/$x.chart.sh" | sed "s/^ \+//g" | grep "^${x}_create()"`
80                 if [ -z "$create" ]
81                 then
82                         echo >&2 "charts.d: chart '$x' does not seem to have a ${x}_create() function. Disabling it."
83                         continue
84                 fi
85
86                 local update=`cat "$chartsd/$x.chart.sh" | sed "s/^ \+//g" | grep "^${x}_update()"`
87                 if [ -z "$update" ]
88                 then
89                         echo >&2 "charts.d: chart '$x' does not seem to have a ${x}_update() function. Disabling it."
90                         continue
91                 fi
92
93                 # check its config
94                 if [ -f "$confd/$x.conf" ]
95                 then
96                         if [ ! -z "`cat "$confd/$x.conf" | sed "s/^ \+//g" | grep -v "^$" | grep -v "^#" | grep -v "^${x}_"`" ]
97                         then
98                                 echo >&2 "charts.d: chart's $x config $confd/$x.conf should only have lines starting with ${x}_ . Disabling it."
99                                 continue
100                         fi
101                 fi
102
103                 "$pluginsd/charts.d.dryrun-helper.sh" "$x" "$chartsd/$x.chart.sh" "$confd/$x.conf" >/dev/null
104                 if [ $? -ne 0 ]
105                 then
106                         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."
107                         continue
108                 fi
109
110                 local charts2="$charts2 $x"
111         done
112
113         echo $charts2
114 }
115
116
117 active_charts=
118 for x in `all_enabled_charts`
119 do
120         . "$chartsd/$x.chart.sh"
121
122         if [ -f "$confd/$x.conf" ]
123         then
124                 . "$confd/$x.conf"
125         fi
126
127         ${x}_check
128         if [ $? -eq 0 ]
129         then
130                 active_charts="$active_charts $x"
131         else
132                 echo >&2 "charts.d: chart '$x' check() function reports failure."
133         fi
134 done
135
136 if [ $check -eq 1 ]
137 then
138         echo "CHECK RESULT"
139         echo "Will run the charts: $active_charts"
140         exit 0
141 fi
142
143 run_charts=
144 for x in $active_charts
145 do
146         ${x}_create
147         if [ $? -eq 0 ]
148         then
149                 run_charts="$run_charts $x"
150         else
151                 echo >&2 "charts.d: chart '$x' create() function reports failure."
152         fi
153 done
154
155 # work forever
156 while [ 1 ]
157 do
158         now_charts=$run_charts
159         run_charts=
160
161         for x in $now_charts
162         do
163                 ${x}_update
164                 if [ $? -eq 0 ]
165                 then
166                         run_charts="$run_charts $x"
167                 else
168                         echo >&2 "charts.d: chart '$x' update() function reports failure. Disabling it."
169                 fi
170         done
171
172         # wait the time you are required to
173         loopsleepms $update_every
174 done