apcupsd.chart.sh 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213
  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 src
  62. for host in "${!apcupsd_sources[@]}"; do
  63. src=${apcupsd_sources[${host}]}
  64. # create the charts
  65. cat << EOF
  66. CHART apcupsd_${host}.charge '' "UPS Charge for ${host} on ${src}" "percentage" ups apcupsd.charge area $((apcupsd_priority + 1)) $apcupsd_update_every '' '' 'apcupsd'
  67. DIMENSION battery_charge charge absolute 1 100
  68. CHART apcupsd_${host}.battery_voltage '' "UPS Battery Voltage for ${host} on ${src}" "Volts" ups apcupsd.battery.voltage line $((apcupsd_priority + 3)) $apcupsd_update_every '' '' 'apcupsd'
  69. DIMENSION battery_voltage voltage absolute 1 100
  70. DIMENSION battery_voltage_nominal nominal absolute 1 100
  71. CHART apcupsd_${host}.input_voltage '' "UPS Input Voltage for ${host} on ${src}" "Volts" input apcupsd.input.voltage line $((apcupsd_priority + 4)) $apcupsd_update_every '' '' 'apcupsd'
  72. DIMENSION input_voltage voltage absolute 1 100
  73. DIMENSION input_voltage_min min absolute 1 100
  74. DIMENSION input_voltage_max max absolute 1 100
  75. CHART apcupsd_${host}.input_frequency '' "UPS Input Frequency for ${host} on ${src}" "Hz" input apcupsd.input.frequency line $((apcupsd_priority + 5)) $apcupsd_update_every '' '' 'apcupsd'
  76. DIMENSION input_frequency frequency absolute 1 100
  77. CHART apcupsd_${host}.output_voltage '' "UPS Output Voltage for ${host} on ${src}" "Volts" output apcupsd.output.voltage line $((apcupsd_priority + 6)) $apcupsd_update_every '' '' 'apcupsd'
  78. DIMENSION output_voltage voltage absolute 1 100
  79. DIMENSION output_voltage_nominal nominal absolute 1 100
  80. CHART apcupsd_${host}.load '' "UPS Load for ${host} on ${src}" "percentage" ups apcupsd.load area $((apcupsd_priority)) $apcupsd_update_every '' '' 'apcupsd'
  81. DIMENSION load load absolute 1 100
  82. CHART apcupsd_${host}.temp '' "UPS Temperature for ${host} on ${src}" "Celsius" ups apcupsd.temperature line $((apcupsd_priority + 7)) $apcupsd_update_every '' '' 'apcupsd'
  83. DIMENSION temp temp absolute 1 100
  84. CHART apcupsd_${host}.time '' "UPS Time Remaining for ${host} on ${src}" "Minutes" ups apcupsd.time area $((apcupsd_priority + 2)) $apcupsd_update_every '' '' 'apcupsd'
  85. DIMENSION time time absolute 1 100
  86. CHART apcupsd_${host}.online '' "UPS ONLINE flag for ${host} on ${src}" "boolean" ups apcupsd.online line $((apcupsd_priority + 8)) $apcupsd_update_every '' '' 'apcupsd'
  87. DIMENSION online online absolute 0 1
  88. EOF
  89. done
  90. return 0
  91. }
  92. apcupsd_update() {
  93. # the first argument to this function is the microseconds since last update
  94. # pass this parameter to the BEGIN statement (see below).
  95. # do all the work to collect / calculate the values
  96. # for each dimension
  97. # remember: KEEP IT SIMPLE AND SHORT
  98. local host working=0 failed=0
  99. for host in "${!apcupsd_sources[@]}"; do
  100. apcupsd_get "${apcupsd_sources[${host}]}" | awk "
  101. BEGIN {
  102. battery_charge = 0;
  103. battery_voltage = 0;
  104. battery_voltage_nominal = 0;
  105. input_voltage = 0;
  106. input_voltage_min = 0;
  107. input_voltage_max = 0;
  108. input_frequency = 0;
  109. output_voltage = 0;
  110. output_voltage_nominal = 0;
  111. load = 0;
  112. temp = 0;
  113. time = 0;
  114. }
  115. /^BCHARGE.*/ { battery_charge = \$3 * 100 };
  116. /^BATTV.*/ { battery_voltage = \$3 * 100 };
  117. /^NOMBATTV.*/ { battery_voltage_nominal = \$3 * 100 };
  118. /^LINEV.*/ { input_voltage = \$3 * 100 };
  119. /^MINLINEV.*/ { input_voltage_min = \$3 * 100 };
  120. /^MAXLINEV.*/ { input_voltage_max = \$3 * 100 };
  121. /^LINEFREQ.*/ { input_frequency = \$3 * 100 };
  122. /^OUTPUTV.*/ { output_voltage = \$3 * 100 };
  123. /^NOMOUTV.*/ { output_voltage_nominal = \$3 * 100 };
  124. /^LOADPCT.*/ { load = \$3 * 100 };
  125. /^ITEMP.*/ { temp = \$3 * 100 };
  126. /^TIMELEFT.*/ { time = \$3 * 100 };
  127. /^STATUS.*/ { online=(\$3 != \"COMMLOST\" && !(\$3 == \"SHUTTING\" && \$4 == \"DOWN\"))?1:0 };
  128. END {
  129. print \"BEGIN apcupsd_${host}.online $1\";
  130. print \"SET online = \" online;
  131. print \"END\"
  132. if (online == 1) {
  133. print \"BEGIN apcupsd_${host}.charge $1\";
  134. print \"SET battery_charge = \" battery_charge;
  135. print \"END\"
  136. print \"BEGIN apcupsd_${host}.battery_voltage $1\";
  137. print \"SET battery_voltage = \" battery_voltage;
  138. print \"SET battery_voltage_nominal = \" battery_voltage_nominal;
  139. print \"END\"
  140. print \"BEGIN apcupsd_${host}.input_voltage $1\";
  141. print \"SET input_voltage = \" input_voltage;
  142. print \"SET input_voltage_min = \" input_voltage_min;
  143. print \"SET input_voltage_max = \" input_voltage_max;
  144. print \"END\"
  145. print \"BEGIN apcupsd_${host}.input_frequency $1\";
  146. print \"SET input_frequency = \" input_frequency;
  147. print \"END\"
  148. print \"BEGIN apcupsd_${host}.output_voltage $1\";
  149. print \"SET output_voltage = \" output_voltage;
  150. print \"SET output_voltage_nominal = \" output_voltage_nominal;
  151. print \"END\"
  152. print \"BEGIN apcupsd_${host}.load $1\";
  153. print \"SET load = \" load;
  154. print \"END\"
  155. print \"BEGIN apcupsd_${host}.temp $1\";
  156. print \"SET temp = \" temp;
  157. print \"END\"
  158. print \"BEGIN apcupsd_${host}.time $1\";
  159. print \"SET time = \" time;
  160. print \"END\"
  161. }
  162. }"
  163. # shellcheck disable=SC2181
  164. if [ $? -ne 0 ]; then
  165. failed=$((failed + 1))
  166. error "failed to get values for APC UPS ${host} on ${apcupsd_sources[${host}]}" && return 1
  167. else
  168. working=$((working + 1))
  169. fi
  170. done
  171. [ $working -eq 0 ] && error "failed to get values from all APC UPSes" && return 1
  172. return 0
  173. }