example.chart.sh 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. # shellcheck shell=bash
  2. # no need for shebang - this file is loaded from charts.d.plugin
  3. # SPDX-License-Identifier: GPL-3.0-or-later
  4. # netdata
  5. # real-time performance and health monitoring, done right!
  6. # (C) 2016 Costa Tsaousis <costa@tsaousis.gr>
  7. #
  8. # if this chart is called X.chart.sh, then all functions and global variables
  9. # must start with X_
  10. # _update_every is a special variable - it holds the number of seconds
  11. # between the calls of the _update() function
  12. example_update_every=
  13. # the priority is used to sort the charts on the dashboard
  14. # 1 = the first chart
  15. example_priority=150000
  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. # global variables to store our collected data
  20. # remember: they need to start with the module name example_
  21. example_value1=
  22. example_value2=
  23. example_value3=
  24. example_value4=
  25. example_last=0
  26. example_count=0
  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. example_value1=$RANDOM
  37. example_value2=$RANDOM
  38. example_value3=$RANDOM
  39. example_value4=$((8192 + (RANDOM * 16383 / 32767)))
  40. if [ $example_count -gt 0 ]; then
  41. example_count=$((example_count - 1))
  42. [ $example_last -gt 16383 ] && example_value4=$((example_last + (RANDOM * ((32767 - example_last) / 2) / 32767)))
  43. [ $example_last -le 16383 ] && example_value4=$((example_last - (RANDOM * (example_last / 2) / 32767)))
  44. else
  45. example_count=$((1 + (RANDOM * 5 / 32767)))
  46. if [ $example_last -gt 16383 ] && [ $example_value4 -gt 16383 ]; then
  47. example_value4=$((example_value4 - 16383))
  48. fi
  49. if [ $example_last -le 16383 ] && [ $example_value4 -lt 16383 ]; then
  50. example_value4=$((example_value4 + 16383))
  51. fi
  52. fi
  53. example_last=$example_value4
  54. # this should return:
  55. # - 0 to send the data to netdata
  56. # - 1 to report a failure to collect the data
  57. return 0
  58. }
  59. # _check is called once, to find out if this chart should be enabled or not
  60. example_check() {
  61. # this should return:
  62. # - 0 to enable the chart
  63. # - 1 to disable the chart
  64. # check something
  65. [ "${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
  66. # check that we can collect data
  67. example_get || return 1
  68. return 0
  69. }
  70. # _create is called once, to create the charts
  71. example_create() {
  72. # create the chart with 3 dimensions
  73. cat << EOF
  74. CHART example.random '' "Random Numbers Stacked Chart" "% of random numbers" random random stacked $((example_priority)) $example_update_every '' '' 'example'
  75. DIMENSION random1 '' percentage-of-absolute-row 1 1
  76. DIMENSION random2 '' percentage-of-absolute-row 1 1
  77. DIMENSION random3 '' percentage-of-absolute-row 1 1
  78. CHART example.random2 '' "A random number" "random number" random random area $((example_priority + 1)) $example_update_every '' '' 'example'
  79. DIMENSION random '' absolute 1 1
  80. EOF
  81. return 0
  82. }
  83. # _update is called continuously, to collect the values
  84. example_update() {
  85. # the first argument to this function is the microseconds since last update
  86. # pass this parameter to the BEGIN statement (see below).
  87. example_get || return 1
  88. # write the result of the work.
  89. cat << VALUESEOF
  90. BEGIN example.random $1
  91. SET random1 = $example_value1
  92. SET random2 = $example_value2
  93. SET random3 = $example_value3
  94. END
  95. BEGIN example.random2 $1
  96. SET random = $example_value4
  97. END
  98. VALUESEOF
  99. return 0
  100. }