]> arthur.barton.de Git - netdata.git/blob - charts.d/example.chart.sh
Merge pull request #2021 from ktsaou/master
[netdata.git] / charts.d / example.chart.sh
1 # no need for shebang - this file is loaded from charts.d.plugin
2
3 # netdata
4 # real-time performance and health monitoring, done right!
5 # (C) 2016 Costa Tsaousis <costa@tsaousis.gr>
6 # GPL v3+
7 #
8
9 # if this chart is called X.chart.sh, then all functions and global variables
10 # must start with X_
11
12 # _update_every is a special variable - it holds the number of seconds
13 # between the calls of the _update() function
14 example_update_every=
15
16 # the priority is used to sort the charts on the dashboard
17 # 1 = the first chart
18 example_priority=150000
19
20 # to enable this chart, you have to set this to 12345
21 # (just a demonstration for something that needs to be checked)
22 example_magic_number=
23
24 # global variables to store our collected data
25 # remember: they need to start with the module name example_
26 example_value1=
27 example_value2=
28 example_value3=
29 example_value4=
30 example_last=0
31 example_count=0
32
33 example_get() {
34         # do all the work to collect / calculate the values
35         # for each dimension
36         #
37         # Remember:
38         # 1. KEEP IT SIMPLE AND SHORT
39         # 2. AVOID FORKS (avoid piping commands)
40         # 3. AVOID CALLING TOO MANY EXTERNAL PROGRAMS
41         # 4. USE LOCAL VARIABLES (global variables may overlap with other modules)
42
43         example_value1=$RANDOM
44         example_value2=$RANDOM
45         example_value3=$RANDOM
46         example_value4=$((8192 + (RANDOM * 16383 / 32767) ))
47
48         if [ $example_count -gt 0 ]
49                 then
50                 example_count=$((example_count - 1))
51
52                 [ $example_last -gt 16383 ] && example_value4=$((example_last + (RANDOM * ( (32767 - example_last) / 2) / 32767)))
53                 [ $example_last -le 16383 ] && example_value4=$((example_last - (RANDOM * (example_last / 2) / 32767)))
54         else
55                 example_count=$((1 + (RANDOM * 5 / 32767) ))
56
57                 [ $example_last -gt 16383 -a $example_value4 -gt 16383 ] && example_value4=$((example_value4 - 16383))
58                 [ $example_last -le 16383 -a $example_value4 -lt 16383 ] && example_value4=$((example_value4 + 16383))
59         fi
60         example_last=$example_value4
61
62         # this should return:
63         #  - 0 to send the data to netdata
64         #  - 1 to report a failure to collect the data
65
66         return 0
67 }
68
69 # _check is called once, to find out if this chart should be enabled or not
70 example_check() {
71         # this should return:
72         #  - 0 to enable the chart
73         #  - 1 to disable the chart
74
75         # check something
76         [ "${example_magic_number}" != "12345" ] && error "manual configuration required: you have to set example_magic_number=$example_magic_number in example.conf to start example chart." && return 1
77
78         # check that we can collect data
79         example_get || return 1
80
81         return 0
82 }
83
84 # _create is called once, to create the charts
85 example_create() {
86         # create the chart with 3 dimensions
87         cat <<EOF
88 CHART example.random '' "Random Numbers Stacked Chart" "% of random numbers" random random stacked $((example_priority)) $example_update_every
89 DIMENSION random1 '' percentage-of-absolute-row 1 1
90 DIMENSION random2 '' percentage-of-absolute-row 1 1
91 DIMENSION random3 '' percentage-of-absolute-row 1 1
92 CHART example.random2 '' "A random number" "random number" random random area $((example_priority + 1)) $example_update_every
93 DIMENSION random '' absolute 1 1
94 EOF
95
96         return 0
97 }
98
99 # _update is called continiously, to collect the values
100 example_update() {
101         # the first argument to this function is the microseconds since last update
102         # pass this parameter to the BEGIN statement (see bellow).
103
104         example_get || return 1
105
106         # write the result of the work.
107         cat <<VALUESEOF
108 BEGIN example.random $1
109 SET random1 = $example_value1
110 SET random2 = $example_value2
111 SET random3 = $example_value3
112 END
113 BEGIN example.random2 $1
114 SET random = $example_value4
115 END
116 VALUESEOF
117
118         return 0
119 }