]> arthur.barton.de Git - netdata.git/blobdiff - charts.d/example.chart.sh
update nfacct plugin to match current netdata development
[netdata.git] / charts.d / example.chart.sh
old mode 100755 (executable)
new mode 100644 (file)
index 60607fd..86fde49
-#!/bin/sh
+# no need for shebang - this file is loaded from charts.d.plugin
 
-# report our PID back to netdata
-# this is required for netdata to kill this process when it exits
-echo "MYPID $$"
+# netdata
+# real-time performance and health monitoring, done right!
+# (C) 2016 Costa Tsaousis <costa@tsaousis.gr>
+# GPL v3+
+#
 
-# default sleep function
-loopsleepms() {
-       sleep $1
+# if this chart is called X.chart.sh, then all functions and global variables
+# must start with X_
+
+# _update_every is a special variable - it holds the number of seconds
+# between the calls of the _update() function
+example_update_every=
+
+# the priority is used to sort the charts on the dashboard
+# 1 = the first chart
+example_priority=150000
+
+# to enable this chart, you have to set this to 12345
+# (just a demonstration for something that needs to be checked)
+example_magic_number=
+
+# global variables to store our collected data
+# remember: they need to start with the module name example_
+example_value1=
+example_value2=
+example_value3=
+example_value4=
+example_last=0
+example_count=0
+
+example_get() {
+       # do all the work to collect / calculate the values
+       # for each dimension
+       #
+       # Remember:
+       # 1. KEEP IT SIMPLE AND SHORT
+       # 2. AVOID FORKS (avoid piping commands)
+       # 3. AVOID CALLING TOO MANY EXTERNAL PROGRAMS
+       # 4. USE LOCAL VARIABLES (global variables may overlap with other modules)
+
+       example_value1=$RANDOM
+       example_value2=$RANDOM
+       example_value3=$RANDOM
+       example_value4=$((8192 + (RANDOM * 16383 / 32767) ))
+
+       if [ $example_count -gt 0 ]
+               then
+               example_count=$((example_count - 1))
+
+               [ $example_last -gt 16383 ] && example_value4=$((example_last + (RANDOM * ( (32767 - example_last) / 2) / 32767)))
+               [ $example_last -le 16383 ] && example_value4=$((example_last - (RANDOM * (example_last / 2) / 32767)))
+       else
+               example_count=$((1 + (RANDOM * 5 / 32767) ))
+
+               [ $example_last -gt 16383 -a $example_value4 -gt 16383 ] && example_value4=$((example_value4 - 16383))
+               [ $example_last -le 16383 -a $example_value4 -lt 16383 ] && example_value4=$((example_value4 + 16383))
+       fi
+       example_last=$example_value4
+
+       # this should return:
+       #  - 0 to send the data to netdata
+       #  - 1 to report a failure to collect the data
+
+       return 0
 }
-# if found and included, this file overwrites loopsleepms()
-# with a high resolution timer function for precise looping.
-. "`dirname $0`/loopsleepms.sh.inc"
 
-# netdata passes the requested update frequency as the first argument
-update_every=$1
-update_every=$(( update_every + 1 - 1))        # makes sure it is a number
-test $update_every -eq 0 && update_every=1 # if it is zero, make it 1
+# _check is called once, to find out if this chart should be enabled or not
+example_check() {
+       # this should return:
+       #  - 0 to enable the chart
+       #  - 1 to disable the chart
+
+       # check something
+       [ "${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
 
+       # check that we can collect data
+       example_get || return 1
 
-# create the chart with 3 dimensions
-cat <<EOF
-CHART example.load '' "System Load Average" "load" load load line 500 $update_every
-DIMENSION load1 '1 min' absolute 1 100
-DIMENSION load5 '5 mins' absolute 1 100
-DIMENSION load15 '15 mins' absolute 1 100
+       return 0
+}
 
-CHART example.random '' "Random Numbers Stacked Chart" "% of random numbers" random random stacked 5000 $update_every
+# _create is called once, to create the charts
+example_create() {
+       # create the chart with 3 dimensions
+       cat <<EOF
+CHART example.random '' "Random Numbers Stacked Chart" "% of random numbers" random random stacked $((example_priority)) $example_update_every
 DIMENSION random1 '' percentage-of-absolute-row 1 1
 DIMENSION random2 '' percentage-of-absolute-row 1 1
 DIMENSION random3 '' percentage-of-absolute-row 1 1
+CHART example.random2 '' "A random number" "random number" random random area $((example_priority + 1)) $example_update_every
+DIMENSION random '' absolute 1 1
 EOF
 
-# You can create more charts if you like.
-# Just add more chart definitions.
-
-# work forever
-while [ 1 ]
-do
-       # do all the work to collect / calculate the values
-       # for each dimension
+       return 0
+}
 
-       # here we parse the system average load
-       # it is decimal (with 2 decimal digits), so we remove the dot and
-       # at the definition we have divisor = 100, to have the graph show the right value
-       loadavg="`cat /proc/loadavg | sed -e "s/\.//g"`"
-       load1=`echo $loadavg | cut -d ' ' -f 1`
-       load5=`echo $loadavg | cut -d ' ' -f 2`
-       load15=`echo $loadavg | cut -d ' ' -f 3`
+# _update is called continiously, to collect the values
+example_update() {
+       # the first argument to this function is the microseconds since last update
+       # pass this parameter to the BEGIN statement (see bellow).
 
-       value1=$RANDOM
-       value2=$RANDOM
-       value3=$RANDOM
+       example_get || return 1
 
        # write the result of the work.
        cat <<VALUESEOF
-BEGIN example.load
-SET load1 = $load1
-SET load5 = $load5
-SET load15 = $load15
+BEGIN example.random $1
+SET random1 = $example_value1
+SET random2 = $example_value2
+SET random3 = $example_value3
 END
-
-BEGIN example.random
-SET random1 = $value1
-SET random2 = $value2
-SET random3 = $value3
+BEGIN example.random2 $1
+SET random = $example_value4
 END
 VALUESEOF
 
-       # if you have more charts, add BEGIN->END statements here
-
-       # wait the time you are required to
-       loopsleepms $update_every
-done
+       return 0
+}