better example chart for generic use
Costa Tsaousis (ktsaou) committed
Jul 14, 2016 at 21:10 UTC
5ab13fd2672c0c4d6904f7d5225f5d3952a0516c
1 file changed
+56
-31
charts.d/example.chart.sh
+56
-31
@@ -15,13 +15,63 @@ example_priority=150000
15
# (just a demonstration for something that needs to be checked)
16
example_magic_number=
17
18
+# global variables to store our collected data
19
+# remember: they need to start with the module name example_
20
+example_value1=
21
+example_value2=
22
+example_value3=
23
+example_value4=
24
+example_last=0
25
+example_count=0
26
+
27
+example_get() {
28
+ # do all the work to collect / calculate the values
29
+ # for each dimension
30
+ #
31
+ # Remember:
32
+ # 1. KEEP IT SIMPLE AND SHORT
33
+ # 2. AVOID FORKS (avoid piping commands)
34
+ # 3. AVOID CALLING TOO MANY EXTERNAL PROGRAMS
35
+ # 4. USE LOCAL VARIABLES (global variables may overlap with other modules)
36
+
37
+ example_value1=$RANDOM
38
+ example_value2=$RANDOM
39
+ example_value3=$RANDOM
40
+ example_value4=$((8192 + (RANDOM * 16383 / 32767) ))
41
+
42
+ if [ $example_count -gt 0 ]
43
+ then
44
+ example_count=$((example_count - 1))
45
+
46
+ [ $example_last -gt 16383 ] && example_value4=$((example_last + (RANDOM * ( (32767 - example_last) / 2) / 32767)))
47
+ [ $example_last -le 16383 ] && example_value4=$((example_last - (RANDOM * (example_last / 2) / 32767)))
48
+ else
49
+ example_count=$((1 + (RANDOM * 5 / 32767) ))
50
+
51
+ [ $example_last -gt 16383 -a $value4 -gt 16383 ] && example_value4=$((value4 - 16383))
52
+ [ $example_last -le 16383 -a $value4 -lt 16383 ] && example_value4=$((value4 + 16383))
53
+ fi
54
+ example_last=$example_value4
55
+
56
+ # this should return:
57
+ # - 0 to send the data to netdata
58
+ # - 1 to report a failure to collect the data
59
+
60
+ return 0
61
+}
62
+
63
# _check is called once, to find out if this chart should be enabled or not
64
example_check() {
65
# this should return:
66
# - 0 to enable the chart
67
# - 1 to disable the chart
68
24
- [ "${example_magic_number}" != "12345" ] && return 1
69
+ # check something
70
+ [ "${example_magic_number}" != "12345" ] && echo >&2 "example: you have to set example_magic_number=$example_magic_number in example.conf to start example chart." && return 1
71
+
72
+ # check that we can collect data
73
+ example_get || return 1
74
+
75
return 0
76
}
77
@@ -41,46 +91,21 @@ EOF
91
}
92
93
# _update is called continiously, to collect the values
44
-example_last=0
45
-example_count=0
94
example_update() {
47
- local value1 value2 value3 value4 mode
48
-
95
# the first argument to this function is the microseconds since last update
96
# pass this parameter to the BEGIN statement (see bellow).
97
52
- # do all the work to collect / calculate the values
53
- # for each dimension
54
- # remember: KEEP IT SIMPLE AND SHORT
55
-
56
- value1=$RANDOM
57
- value2=$RANDOM
58
- value3=$RANDOM
59
- value4=$((8192 + (RANDOM * 16383 / 32767) ))
60
-
61
- if [ $example_count -gt 0 ]
62
- then
63
- example_count=$((example_count - 1))
64
-
65
- [ $example_last -gt 16383 ] && value4=$((example_last + (RANDOM * ( (32767 - example_last) / 2) / 32767)))
66
- [ $example_last -le 16383 ] && value4=$((example_last - (RANDOM * (example_last / 2) / 32767)))
67
- else
68
- example_count=$((1 + (RANDOM * 5 / 32767) ))
69
-
70
- [ $example_last -gt 16383 -a $value4 -gt 16383 ] && value4=$((value4 - 16383))
71
- [ $example_last -le 16383 -a $value4 -lt 16383 ] && value4=$((value4 + 16383))
72
- fi
73
- example_last=$value4
98
+ example_get || return 1
99
100
# write the result of the work.
101
cat <<VALUESEOF
102
BEGIN example.random $1
78
-SET random1 = $value1
79
-SET random2 = $value2
80
-SET random3 = $value3
103
+SET random1 = $example_value1
104
+SET random2 = $example_value2
105
+SET random3 = $example_value3
106
END
107
BEGIN example.random2 $1
83
-SET random = $value4
108
+SET random = $example_value4
109
END
110
VALUESEOF
111
# echo >&2 "example_count = $example_count value = $value4"