apcupsd.chart.sh 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223
  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. apcupsd_ip=
  9. apcupsd_port=
  10. declare -A apcupsd_sources=(
  11. ["local"]="127.0.0.1:3551"
  12. )
  13. # how frequently to collect UPS data
  14. apcupsd_update_every=10
  15. apcupsd_timeout=3
  16. # the priority of apcupsd related to other charts
  17. apcupsd_priority=90000
  18. apcupsd_get() {
  19. run -t $apcupsd_timeout apcaccess status "$1"
  20. }
  21. is_ups_alive() {
  22. local status
  23. status="$(apcupsd_get "$1" | sed -e 's/STATUS.*: //' -e 't' -e 'd')"
  24. case "$status" in
  25. "" | "COMMLOST" | "SHUTTING DOWN") return 1 ;;
  26. *) return 0 ;;
  27. esac
  28. }
  29. apcupsd_check() {
  30. # this should return:
  31. # - 0 to enable the chart
  32. # - 1 to disable the chart
  33. require_cmd apcaccess || return 1
  34. # backwards compatibility
  35. if [ "${apcupsd_ip}:${apcupsd_port}" != ":" ]; then
  36. apcupsd_sources["local"]="${apcupsd_ip}:${apcupsd_port}"
  37. fi
  38. local host working=0 failed=0
  39. for host in "${!apcupsd_sources[@]}"; do
  40. apcupsd_get "${apcupsd_sources[${host}]}" > /dev/null
  41. # shellcheck disable=2181
  42. if [ $? -ne 0 ]; then
  43. error "cannot get information for apcupsd server ${host} on ${apcupsd_sources[${host}]}."
  44. failed=$((failed + 1))
  45. else
  46. if ! is_ups_alive ${apcupsd_sources[${host}]}; then
  47. error "APC UPS ${host} on ${apcupsd_sources[${host}]} is not online."
  48. failed=$((failed + 1))
  49. else
  50. working=$((working + 1))
  51. fi
  52. fi
  53. done
  54. if [ ${working} -eq 0 ]; then
  55. error "No APC UPSes found available."
  56. return 1
  57. fi
  58. return 0
  59. }
  60. apcupsd_create() {
  61. local host
  62. for host in "${!apcupsd_sources[@]}"; do
  63. # create the charts
  64. cat << EOF
  65. CHART apcupsd_${host}.charge '' "UPS Charge" "percentage" ups apcupsd.charge area $((apcupsd_priority + 2)) $apcupsd_update_every '' '' 'apcupsd'
  66. DIMENSION battery_charge charge absolute 1 100
  67. CHART apcupsd_${host}.battery_voltage '' "UPS Battery Voltage" "Volts" ups apcupsd.battery.voltage line $((apcupsd_priority + 4)) $apcupsd_update_every '' '' 'apcupsd'
  68. DIMENSION battery_voltage voltage absolute 1 100
  69. DIMENSION battery_voltage_nominal nominal absolute 1 100
  70. CHART apcupsd_${host}.input_voltage '' "UPS Input Voltage" "Volts" input apcupsd.input.voltage line $((apcupsd_priority + 5)) $apcupsd_update_every '' '' 'apcupsd'
  71. DIMENSION input_voltage voltage absolute 1 100
  72. DIMENSION input_voltage_min min absolute 1 100
  73. DIMENSION input_voltage_max max absolute 1 100
  74. CHART apcupsd_${host}.input_frequency '' "UPS Input Frequency" "Hz" input apcupsd.input.frequency line $((apcupsd_priority + 6)) $apcupsd_update_every '' '' 'apcupsd'
  75. DIMENSION input_frequency frequency absolute 1 100
  76. CHART apcupsd_${host}.output_voltage '' "UPS Output Voltage" "Volts" output apcupsd.output.voltage line $((apcupsd_priority + 7)) $apcupsd_update_every '' '' 'apcupsd'
  77. DIMENSION output_voltage voltage absolute 1 100
  78. DIMENSION output_voltage_nominal nominal absolute 1 100
  79. CHART apcupsd_${host}.load '' "UPS Load" "percentage" ups apcupsd.load area $((apcupsd_priority)) $apcupsd_update_every '' '' 'apcupsd'
  80. DIMENSION load load absolute 1 100
  81. CHART apcupsd_${host}.load_usage '' "UPS Load Usage" "Watts" ups apcupsd.load_usage area $((apcupsd_priority + 1)) $apcupsd_update_every '' '' 'apcupsd'
  82. DIMENSION load_usage load absolute 1 100
  83. CHART apcupsd_${host}.temp '' "UPS Temperature" "Celsius" ups apcupsd.temperature line $((apcupsd_priority + 8)) $apcupsd_update_every '' '' 'apcupsd'
  84. DIMENSION temp temp absolute 1 100
  85. CHART apcupsd_${host}.time '' "UPS Time Remaining" "Minutes" ups apcupsd.time area $((apcupsd_priority + 3)) $apcupsd_update_every '' '' 'apcupsd'
  86. DIMENSION time time absolute 1 100
  87. CHART apcupsd_${host}.online '' "UPS ONLINE flag" "boolean" ups apcupsd.online line $((apcupsd_priority + 9)) $apcupsd_update_every '' '' 'apcupsd'
  88. DIMENSION online online absolute 0 1
  89. EOF
  90. done
  91. return 0
  92. }
  93. apcupsd_update() {
  94. # the first argument to this function is the microseconds since last update
  95. # pass this parameter to the BEGIN statement (see below).
  96. # do all the work to collect / calculate the values
  97. # for each dimension
  98. # remember: KEEP IT SIMPLE AND SHORT
  99. local host working=0 failed=0
  100. for host in "${!apcupsd_sources[@]}"; do
  101. apcupsd_get "${apcupsd_sources[${host}]}" | awk "
  102. BEGIN {
  103. battery_charge = 0;
  104. battery_voltage = 0;
  105. battery_voltage_nominal = 0;
  106. input_voltage = 0;
  107. input_voltage_min = 0;
  108. input_voltage_max = 0;
  109. input_frequency = 0;
  110. output_voltage = 0;
  111. output_voltage_nominal = 0;
  112. load = 0;
  113. temp = 0;
  114. time = 0;
  115. nompower = 0;
  116. load_usage = 0;
  117. }
  118. /^BCHARGE.*/ { battery_charge = \$3 * 100 };
  119. /^BATTV.*/ { battery_voltage = \$3 * 100 };
  120. /^NOMBATTV.*/ { battery_voltage_nominal = \$3 * 100 };
  121. /^LINEV.*/ { input_voltage = \$3 * 100 };
  122. /^MINLINEV.*/ { input_voltage_min = \$3 * 100 };
  123. /^MAXLINEV.*/ { input_voltage_max = \$3 * 100 };
  124. /^LINEFREQ.*/ { input_frequency = \$3 * 100 };
  125. /^OUTPUTV.*/ { output_voltage = \$3 * 100 };
  126. /^NOMOUTV.*/ { output_voltage_nominal = \$3 * 100 };
  127. /^LOADPCT.*/ { load = \$3 * 100 };
  128. /^ITEMP.*/ { temp = \$3 * 100 };
  129. /^NOMPOWER.*/ { nompower = \$3 };
  130. /^TIMELEFT.*/ { time = \$3 * 100 };
  131. /^STATUS.*/ { online=(\$3 != \"COMMLOST\" && !(\$3 == \"SHUTTING\" && \$4 == \"DOWN\"))?1:0 };
  132. END {
  133. { load_usage = nompower * load / 100 };
  134. print \"BEGIN apcupsd_${host}.online $1\";
  135. print \"SET online = \" online;
  136. print \"END\"
  137. if (online == 1) {
  138. print \"BEGIN apcupsd_${host}.charge $1\";
  139. print \"SET battery_charge = \" battery_charge;
  140. print \"END\"
  141. print \"BEGIN apcupsd_${host}.battery_voltage $1\";
  142. print \"SET battery_voltage = \" battery_voltage;
  143. print \"SET battery_voltage_nominal = \" battery_voltage_nominal;
  144. print \"END\"
  145. print \"BEGIN apcupsd_${host}.input_voltage $1\";
  146. print \"SET input_voltage = \" input_voltage;
  147. print \"SET input_voltage_min = \" input_voltage_min;
  148. print \"SET input_voltage_max = \" input_voltage_max;
  149. print \"END\"
  150. print \"BEGIN apcupsd_${host}.input_frequency $1\";
  151. print \"SET input_frequency = \" input_frequency;
  152. print \"END\"
  153. print \"BEGIN apcupsd_${host}.output_voltage $1\";
  154. print \"SET output_voltage = \" output_voltage;
  155. print \"SET output_voltage_nominal = \" output_voltage_nominal;
  156. print \"END\"
  157. print \"BEGIN apcupsd_${host}.load $1\";
  158. print \"SET load = \" load;
  159. print \"END\"
  160. print \"BEGIN apcupsd_${host}.load_usage $1\";
  161. print \"SET load_usage = \" load_usage;
  162. print \"END\"
  163. print \"BEGIN apcupsd_${host}.temp $1\";
  164. print \"SET temp = \" temp;
  165. print \"END\"
  166. print \"BEGIN apcupsd_${host}.time $1\";
  167. print \"SET time = \" time;
  168. print \"END\"
  169. }
  170. }"
  171. # shellcheck disable=SC2181
  172. if [ $? -ne 0 ]; then
  173. failed=$((failed + 1))
  174. error "failed to get values for APC UPS ${host} on ${apcupsd_sources[${host}]}" && return 1
  175. else
  176. working=$((working + 1))
  177. fi
  178. done
  179. [ $working -eq 0 ] && error "failed to get values from all APC UPSes" && return 1
  180. return 0
  181. }