You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

231 lines
6.1 KiB

  1. #!/bin/sh /etc/rc.common
  2. START=82
  3. STOP=28
  4. USE_PROCD=1
  5. UPSMON_C=/var/etc/nut/upsmon.conf
  6. nut_upsmon_conf() {
  7. local cfg="$1"
  8. local RUNAS val optval
  9. echo "# Config file automatically generated from UCI config" > "$UPSMON_C"
  10. config_get RUNAS "$cfg" runas "nutmon"
  11. [ -n "$RUNAS" ] && echo "RUN_AS_USER $RUNAS" >> "$UPSMON_C"
  12. runas="$RUNAS"
  13. config_get val "$cfg" minsupplies 1
  14. echo "MINSUPPLIES $val" >> "$UPSMON_C"
  15. config_get val "$cfg" shutdowncmd "/usr/sbin/nutshutdown"
  16. echo "SHUTDOWNCMD \"$val\"" >> "$UPSMON_C"
  17. config_get val "$cfg" notifycmd
  18. [ -n "$val" ] && echo "NOTIFYCMD \"$val\"" >> "$UPSMON_C"
  19. config_get val "$cfg" pollfreq 5
  20. echo "POLLFREQ $val" >> "$UPSMON_C"
  21. config_get val "$cfg" pollfreqalert 5
  22. echo "POLLFREQALERT $val" >> "$UPSMON_C"
  23. config_get val "$cfg" hostsync 15
  24. echo "HOSTSYNC $val" >> "$UPSMON_C"
  25. config_get val "$cfg" deadtime 15
  26. echo "DEADTIME $val" >> "$UPSMON_C"
  27. echo "POWERDOWNFLAG /var/run/killpower" >> "$UPSMON_C"
  28. config_get val "$cfg" onlinemsg
  29. [ -n "$val" ] && echo "NOTIFYMSG ONLINE \"$val\"" >> "$UPSMON_C"
  30. config_get val "$cfg" onbattmsg
  31. [ -n "$val" ] && echo "NOTIFYMSG ONBATT \"$val\"" >> "$UPSMON_C"
  32. config_get val "$cfg" lowbattmsg
  33. [ -n "$val" ] && echo "NOTIFYMSG LOWBATT \"$val\"" >> "$UPSMON_C"
  34. config_get val "$cfg" fsdmsg
  35. [ -n "$val" ] && echo "NOTIFYMSG FSD \"$val\"" >> "$UPSMON_C"
  36. config_get val "$cfg" commokmsg
  37. [ -n "$val" ] && echo "NOTIFYMSG COMMOK \"$val\"" >> "$UPSMON_C"
  38. config_get val "$cfg" commbadmsg
  39. [ -n "$val" ] && echo "NOTIFYMSG COMMBAD \"$val\"" >> "$UPSMON_C"
  40. config_get val "$cfg" shutdownmsg
  41. [ -n "$val" ] && echo "NOTIFYMSG SHUTDOWN \"$val\"" >> "$UPSMON_C"
  42. config_get val "$cfg" replbattmsg
  43. [ -n "$val" ] && echo "NOTIFYMSG REPLBATT \"$val\"" >> "$UPSMON_C"
  44. config_get val "$cfg" nocommmsg
  45. [ -n "$val" ] && echo "NOTIFYMSG NOCOMM \"$val\"" >> "$UPSMON_C"
  46. config_get val "$cfg" noparentmsg
  47. [ -n "$val" ] && echo "NOTIFYMSG NOPARENT \"$val\"" >> "$UPSMON_C"
  48. notifylist() {
  49. local value="$1"
  50. append optval "$value" "+"
  51. }
  52. setnotify() {
  53. local cfg="$1"
  54. local optname="$2"
  55. local optval
  56. config_list_foreach "$cfg" "$optname" notifylist
  57. if [ -z "$optval" ]; then
  58. # If no list fallback to trying option, fallback to default
  59. config_get optval "$cfg" "$optname" "$default"
  60. if [ -n "$optval" ]; then
  61. echo "$optval"
  62. else
  63. # No default, so do the NUT default
  64. echo "SYSLOG"
  65. fi
  66. else
  67. echo "$optval"
  68. fi
  69. }
  70. local default optval
  71. val=""
  72. config_list_foreach "$cfg" defaultnotify notifylist
  73. default="$optval"
  74. {
  75. echo "NOTIFYFLAG ONLINE $(setnotify "$cfg" onlinenotify)" ; \
  76. echo "NOTIFYFLAG ONBATT $(setnotify "$cfg" onbattnotify)" ; \
  77. echo "NOTIFYFLAG LOWBATT $(setnotify "$cfg" lowbatnotify)" ; \
  78. echo "NOTIFYFLAG FSD $(setnotify "$cfg" fsdnotify)" ; \
  79. echo "NOTIFYFLAG COMMOK $(setnotify "$cfg" commoknotify)" ; \
  80. echo "NOTIFYFLAG COMMBAD $(setnotify "$cfg" commbadnotify)" ; \
  81. echo "NOTIFYFLAG SHUTDOWN $(setnotify "$cfg" shutdownnotify)" ; \
  82. echo "NOTIFYFLAG REPLBATT $(setnotify "$cfg" repolbattnotify)" ; \
  83. echo "NOTIFYFLAG NOCOMM $(setnotify "$cfg" nocommnotify)" ; \
  84. echo "NOTIFYFLAG NOPARENT $(setnotify "$cfg" noparentnotify)" ; \
  85. } >> "$UPSMON_C"
  86. config_get val "$cfg" rbwarntime 43200
  87. echo "RBWARNTIME $val" >> "$UPSMON_C"
  88. config_get val "$cfg" nocommwarntime 300
  89. echo "NOCOMMWARNTIME $val" >> "$UPSMON_C"
  90. config_get val "$cfg" finaldelay 5
  91. echo "FINALDELAY $val" >> "$UPSMON_C"
  92. config_get val "$cfg" certpath
  93. if [ -n "$val" ]; then echo "CERTPATH $val" >> "$UPSMON_C"; fi
  94. config_get_bool val "$cfg" certverify 0
  95. if [ -n "$val" ]; then echo "CERTVERIFY $val" >> "$UPSMON_C"; fi
  96. config_get_bool val "$cfg" forcessl 0
  97. if [ -n "$val" ]; then echo "FORCESSL $val" >> "$UPSMON_C"; fi
  98. havemon=1
  99. }
  100. nut_upsmon_add() {
  101. local cfg="$1"
  102. local type="$2"
  103. local upsname
  104. local hostname
  105. local port
  106. local powervalue
  107. local username
  108. local password
  109. local system
  110. config_get upsname "$cfg" upsname
  111. config_get hostname "$cfg" hostname localhost
  112. config_get port "$cfg" port
  113. config_get powervalue "$cfg" powervalue 1
  114. config_get username "$cfg" username
  115. config_get password "$cfg" password
  116. system="$upsname@$hostname"
  117. if [ -n "$port" ]; then
  118. system="$system:$port";
  119. fi
  120. echo "MONITOR $system $powervalue $username $password $type" >> "$UPSMON_C"
  121. havems=1
  122. }
  123. build_config() {
  124. mkdir -m 0750 -p "$(dirname "$UPSMON_C")"
  125. config_load nut_monitor
  126. config_foreach nut_upsmon_conf upsmon
  127. config_foreach nut_upsmon_add master master
  128. config_foreach nut_upsmon_add slave slave
  129. [ ! -s /var/etc/nut/nut.conf ] && {
  130. echo "MODE=netclient" >>/var/etc/nut/nut.conf
  131. chmod 640 /var/etc/nut/nut.conf
  132. chgrp "$(id -gn "${runas:-nutmon}")" /var/etc/nut/nut.conf
  133. }
  134. [ -s "$UPSMON_C" ] && chmod 640 "$UPSMON_C"
  135. [ -s "$UPSMON_C" ] && chgrp "$(id -gn "${runas:-nutmon}")" "$UPSMON_C"
  136. }
  137. interface_triggers() {
  138. local action="$1"
  139. local triggerlist trigger
  140. config_get triggerlist "upsmon" triggerlist
  141. . "${IPKG_INSTROOT}"/lib/functions/network.sh
  142. if [ -n "$triggerlist" ]; then
  143. for trigger in $triggerlist; do
  144. if [ "$action" = "add_trigger" ]; then
  145. procd_add_interface_trigger "interface.*" "$trigger" /etc/init.d/nut-monitor restart
  146. else
  147. network_is_up "$trigger" && return 0
  148. fi
  149. done
  150. else
  151. if [ "$action" = "add_trigger" ]; then
  152. procd_add_raw_trigger "interface.*.up" 2000 /etc/init.d/nut-monitor restart
  153. else
  154. ubus call network.device status | grep -q '"up": true' && return 0
  155. fi
  156. fi
  157. [ "$action" = "add_trigger" ] || return 1
  158. }
  159. start_service() {
  160. local runas=nutmon
  161. local havemon havems
  162. build_config
  163. [ "$havemon" != 1 ] && return 1
  164. [ "$havems" != 1 ] && return 1
  165. interface_triggers "check_interface_up" || return 0
  166. procd_open_instance "upsmon"
  167. procd_set_param respawn 10 20 6
  168. procd_set_param stderr 1
  169. procd_set_param stdout 0
  170. procd_set_param command /usr/sbin/upsmon -D
  171. procd_close_instance
  172. return 0
  173. }
  174. reload_service() {
  175. if pgrep upsmon >/dev/null 2>/dev/null; then
  176. local runas=nutmon
  177. build_config
  178. /usr/sbin/upsmon -c reload
  179. else
  180. restart
  181. fi
  182. }
  183. stop_service() {
  184. upsmon -c stop
  185. }
  186. service_triggers() {
  187. config_load nut_monitor
  188. interface_triggers "add_trigger"
  189. procd_add_reload_trigger "nut_monitor"
  190. }