nut.chart.sh 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241
  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-2017 Costa Tsaousis <costa@tsaousis.gr>
  7. #
  8. # a space separated list of UPS names
  9. # if empty, the list returned by 'upsc -l' will be used
  10. nut_ups=
  11. # how frequently to collect UPS data
  12. nut_update_every=2
  13. # how much time in seconds, to wait for nut to respond
  14. nut_timeout=2
  15. # set this to 1, to enable another chart showing the number
  16. # of UPS clients connected to upsd
  17. nut_clients_chart=0
  18. # the priority of nut related to other charts
  19. nut_priority=90000
  20. declare -A nut_ids=()
  21. declare -A nut_names=()
  22. nut_get_all() {
  23. run -t $nut_timeout upsc -l
  24. }
  25. nut_get() {
  26. run -t $nut_timeout upsc "$1"
  27. if [ "${nut_clients_chart}" -eq "1" ]
  28. then
  29. printf "ups.connected_clients: "
  30. run -t $nut_timeout upsc -c "$1" | wc -l
  31. fi
  32. }
  33. nut_check() {
  34. # this should return:
  35. # - 0 to enable the chart
  36. # - 1 to disable the chart
  37. local x
  38. require_cmd upsc || return 1
  39. [ -z "$nut_ups" ] && nut_ups="$( nut_get_all )"
  40. for x in $nut_ups
  41. do
  42. nut_get "$x" >/dev/null
  43. # shellcheck disable=SC2181
  44. if [ $? -eq 0 ]
  45. then
  46. if [ ! -z "${nut_names[${x}]}" ]
  47. then
  48. nut_ids[$x]="$( fixid "${nut_names[${x}]}" )"
  49. else
  50. nut_ids[$x]="$( fixid "$x" )"
  51. fi
  52. continue
  53. fi
  54. error "cannot get information for NUT UPS '$x'."
  55. done
  56. if [ ${#nut_ids[@]} -eq 0 ]
  57. then
  58. # shellcheck disable=SC2154
  59. error "Cannot find UPSes - please set nut_ups='ups_name' in $confd/nut.conf"
  60. return 1
  61. fi
  62. return 0
  63. }
  64. nut_create() {
  65. # create the charts
  66. local x
  67. for x in "${nut_ids[@]}"
  68. do
  69. cat <<EOF
  70. CHART nut_$x.charge '' "UPS Charge" "percentage" ups nut.charge area $((nut_priority + 1)) $nut_update_every
  71. DIMENSION battery_charge charge absolute 1 100
  72. CHART nut_$x.runtime '' "UPS Runtime" "seconds" ups nut.runtime area $((nut_priority + 2)) $nut_update_every
  73. DIMENSION battery_runtime runtime absolute 1 100
  74. CHART nut_$x.battery_voltage '' "UPS Battery Voltage" "Volts" ups nut.battery.voltage line $((nut_priority + 3)) $nut_update_every
  75. DIMENSION battery_voltage voltage absolute 1 100
  76. DIMENSION battery_voltage_high high absolute 1 100
  77. DIMENSION battery_voltage_low low absolute 1 100
  78. DIMENSION battery_voltage_nominal nominal absolute 1 100
  79. CHART nut_$x.input_voltage '' "UPS Input Voltage" "Volts" input nut.input.voltage line $((nut_priority + 4)) $nut_update_every
  80. DIMENSION input_voltage voltage absolute 1 100
  81. DIMENSION input_voltage_fault fault absolute 1 100
  82. DIMENSION input_voltage_nominal nominal absolute 1 100
  83. CHART nut_$x.input_current '' "UPS Input Current" "Ampere" input nut.input.current line $((nut_priority + 5)) $nut_update_every
  84. DIMENSION input_current_nominal nominal absolute 1 100
  85. CHART nut_$x.input_frequency '' "UPS Input Frequency" "Hz" input nut.input.frequency line $((nut_priority + 6)) $nut_update_every
  86. DIMENSION input_frequency frequency absolute 1 100
  87. DIMENSION input_frequency_nominal nominal absolute 1 100
  88. CHART nut_$x.output_voltage '' "UPS Output Voltage" "Volts" output nut.output.voltage line $((nut_priority + 7)) $nut_update_every
  89. DIMENSION output_voltage voltage absolute 1 100
  90. CHART nut_$x.load '' "UPS Load" "percentage" ups nut.load area $((nut_priority)) $nut_update_every
  91. DIMENSION load load absolute 1 100
  92. CHART nut_$x.temp '' "UPS Temperature" "temperature" ups nut.temperature line $((nut_priority + 8)) $nut_update_every
  93. DIMENSION temp temp absolute 1 100
  94. EOF
  95. if [ "${nut_clients_chart}" = "1" ]
  96. then
  97. cat <<EOF2
  98. CHART nut_$x.clients '' "UPS Connected Clients" "clients" ups nut.clients area $((nut_priority + 9)) $nut_update_every
  99. DIMENSION clients '' absolute 1 1
  100. EOF2
  101. fi
  102. done
  103. return 0
  104. }
  105. nut_update() {
  106. # the first argument to this function is the microseconds since last update
  107. # pass this parameter to the BEGIN statement (see bellow).
  108. # do all the work to collect / calculate the values
  109. # for each dimension
  110. # remember: KEEP IT SIMPLE AND SHORT
  111. local i x
  112. for i in "${!nut_ids[@]}"
  113. do
  114. x="${nut_ids[$i]}"
  115. nut_get "$i" | awk "
  116. BEGIN {
  117. battery_charge = 0;
  118. battery_runtime = 0;
  119. battery_voltage = 0;
  120. battery_voltage_high = 0;
  121. battery_voltage_low = 0;
  122. battery_voltage_nominal = 0;
  123. input_voltage = 0;
  124. input_voltage_fault = 0;
  125. input_voltage_nominal = 0;
  126. input_current_nominal = 0;
  127. input_frequency = 0;
  128. input_frequency_nominal = 0;
  129. output_voltage = 0;
  130. load = 0;
  131. temp = 0;
  132. client = 0;
  133. do_clients = ${nut_clients_chart};
  134. }
  135. /^battery.charge: .*/ { battery_charge = \$2 * 100 };
  136. /^battery.runtime: .*/ { battery_runtime = \$2 * 100 };
  137. /^battery.voltage: .*/ { battery_voltage = \$2 * 100 };
  138. /^battery.voltage.high: .*/ { battery_voltage_high = \$2 * 100 };
  139. /^battery.voltage.low: .*/ { battery_voltage_low = \$2 * 100 };
  140. /^battery.voltage.nominal: .*/ { battery_voltage_nominal = \$2 * 100 };
  141. /^input.voltage: .*/ { input_voltage = \$2 * 100 };
  142. /^input.voltage.fault: .*/ { input_voltage_fault = \$2 * 100 };
  143. /^input.voltage.nominal: .*/ { input_voltage_nominal = \$2 * 100 };
  144. /^input.current.nominal: .*/ { input_current_nominal = \$2 * 100 };
  145. /^input.frequency: .*/ { input_frequency = \$2 * 100 };
  146. /^input.frequency.nominal: .*/ { input_frequency_nominal = \$2 * 100 };
  147. /^output.voltage: .*/ { output_voltage = \$2 * 100 };
  148. /^ups.load: .*/ { load = \$2 * 100 };
  149. /^ups.temperature: .*/ { temp = \$2 * 100 };
  150. /^ups.connected_clients: .*/ { clients = \$2 };
  151. END {
  152. print \"BEGIN nut_$x.charge $1\";
  153. print \"SET battery_charge = \" battery_charge;
  154. print \"END\"
  155. print \"BEGIN nut_$x.runtime $1\";
  156. print \"SET battery_runtime = \" battery_runtime;
  157. print \"END\"
  158. print \"BEGIN nut_$x.battery_voltage $1\";
  159. print \"SET battery_voltage = \" battery_voltage;
  160. print \"SET battery_voltage_high = \" battery_voltage_high;
  161. print \"SET battery_voltage_low = \" battery_voltage_low;
  162. print \"SET battery_voltage_nominal = \" battery_voltage_nominal;
  163. print \"END\"
  164. print \"BEGIN nut_$x.input_voltage $1\";
  165. print \"SET input_voltage = \" input_voltage;
  166. print \"SET input_voltage_fault = \" input_voltage_fault;
  167. print \"SET input_voltage_nominal = \" input_voltage_nominal;
  168. print \"END\"
  169. print \"BEGIN nut_$x.input_current $1\";
  170. print \"SET input_current_nominal = \" input_current_nominal;
  171. print \"END\"
  172. print \"BEGIN nut_$x.input_frequency $1\";
  173. print \"SET input_frequency = \" input_frequency;
  174. print \"SET input_frequency_nominal = \" input_frequency_nominal;
  175. print \"END\"
  176. print \"BEGIN nut_$x.output_voltage $1\";
  177. print \"SET output_voltage = \" output_voltage;
  178. print \"END\"
  179. print \"BEGIN nut_$x.load $1\";
  180. print \"SET load = \" load;
  181. print \"END\"
  182. print \"BEGIN nut_$x.temp $1\";
  183. print \"SET temp = \" temp;
  184. print \"END\"
  185. if(do_clients) {
  186. print \"BEGIN nut_$x.clients $1\";
  187. print \"SET clients = \" clients;
  188. print \"END\"
  189. }
  190. }"
  191. # shellcheck disable=2181
  192. [ $? -ne 0 ] && unset "nut_ids[$i]" && error "failed to get values for '$i', disabling it."
  193. done
  194. [ ${#nut_ids[@]} -eq 0 ] && error "no UPSes left active." && return 1
  195. return 0
  196. }