apcupsd.chart.sh 6.5 KB

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