@cryptotaxi247 / netdata-1 / commits / 648e4ae95

Now there are 2 kinds of plugins:

- plugins.d plugins that are expected to always be in memory and execute continiously - charts.d scripts that are sourced by the charts.d plugin and are all executed together, lowering significantly the resources they require (compared to plugins)

Costa Tsaousis (ktsaou) committed May 3, 2014 at 20:08 UTC 648e4ae9540b2b6bed15839b319d456e8ba01674
7 files changed +299
charts.d/example.chart.sh renamed
charts.d/load_average.chart.sh new
+47
@@ -0,0 +1,47 @@
1 +#!/bin/sh
2 +
3 +load_average_check() {
4 + # this should return:
5 + # - 0 to enable the chart
6 + # - 1 to disable the chart
7 +
8 + return 0
9 +}
10 +
11 +load_average_create() {
12 + # create a chart with 3 dimensions
13 +cat <<EOF
14 +CHART example.load '' "System Load Average" "load" load load line 500 $update_every
15 +DIMENSION load1 '1 min' absolute 1 100
16 +DIMENSION load5 '5 mins' absolute 1 100
17 +DIMENSION load15 '15 mins' absolute 1 100
18 +EOF
19 +
20 + return 0
21 +}
22 +
23 +load_average_update() {
24 + # do all the work to collect / calculate the values
25 + # for each dimension
26 + # remember: KEEP IT SIMPLE AND SHORT
27 +
28 + # here we parse the system average load
29 + # it is decimal (with 2 decimal digits), so we remove the dot and
30 + # at the definition we have divisor = 100, to have the graph show the right value
31 + loadavg="`cat /proc/loadavg | sed -e "s/\.//g"`"
32 + load1=`echo $loadavg | cut -d ' ' -f 1`
33 + load5=`echo $loadavg | cut -d ' ' -f 2`
34 + load15=`echo $loadavg | cut -d ' ' -f 3`
35 +
36 + # write the result of the work.
37 + cat <<VALUESEOF
38 +BEGIN example.load
39 +SET load1 = $load1
40 +SET load5 = $load5
41 +SET load15 = $load15
42 +END
43 +VALUESEOF
44 +
45 + return 0
46 +}
47 +
charts.d/squid.chart.sh renamed
conf.d/charts.d.conf new
+4
@@ -0,0 +1,4 @@
1 +squid=yes
2 +test=yes
3 +example=yes
4 +load_average=yes
conf.d/squid.conf new
+1
@@ -0,0 +1 @@
1 +squid_url="http://127.0.0.1:3128/squid-internal-mgr/counters"
plugins.d/charts.d.dryrun-helper.sh new
+73
@@ -0,0 +1,73 @@
1 +#!/bin/sh
2 +
3 +# will stop the script for any error
4 +set -e
5 +
6 +me="$0"
7 +name="$1"
8 +chart="$2"
9 +conf="$3"
10 +
11 +can_diff=1
12 +
13 +tmp1=`mktemp`
14 +tmp2=`mktemp`
15 +
16 +myset() {
17 + set | grep -v "^_=" | grep -v "^PIPESTATUS=" | grep -v "^BASH_LINENO="
18 +}
19 +
20 +# save 2 'set'
21 +myset >"$tmp1"
22 +myset >"$tmp2"
23 +
24 +# make sure they don't differ
25 +diff "$tmp1" "$tmp2" >/dev/null 2>&1
26 +if [ $? -ne 0 ]
27 +then
28 + # they differ, we cannot do the check
29 + echo >&2 "$me: cannot check with diff."
30 + can_diff=0
31 +fi
32 +
33 +# do it again, now including the script
34 +myset >"$tmp1"
35 +
36 +# include the plugin and its config
37 +if [ -f "$conf" ]
38 +then
39 + . "$conf"
40 + if [ $? -ne 0 ]
41 + then
42 + echo >&2 "$me: cannot load config file $conf"
43 + rm "$tmp1" "$tmp2"
44 + exit 1
45 + fi
46 +fi
47 +
48 +. "$chart"
49 +if [ $? -ne 0 ]
50 +then
51 + echo >&2 "$me: cannot load chart file $chart"
52 + rm "$tmp1" "$tmp2"
53 + exit 1
54 +fi
55 +
56 +# remove all variables starting with the plugin name
57 +myset | grep -v "^$name" >"$tmp2"
58 +
59 +if [ $can_diff -eq 1 ]
60 +then
61 + # check if they are different
62 + # make sure they don't differ
63 + diff "$tmp1" "$tmp2" >&2
64 + if [ $? -ne 0 ]
65 + then
66 + # they differ
67 + rm "$tmp1" "$tmp2"
68 + exit 1
69 + fi
70 +fi
71 +
72 +rm "$tmp1" "$tmp2"
73 +exit 0
plugins.d/charts.d.plugin new
+174
@@ -0,0 +1,174 @@
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