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