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.

181 lines
5.2 KiB

  1. #!/bin/sh /etc/rc.common
  2. START=60
  3. USE_PROCD=1
  4. UPSMON_C=/var/etc/nut/upsmon.conf
  5. nut_upsmon_conf() {
  6. local cfg="$1"
  7. echo "# Config file automatically generated from UCI config" > $UPSMON_C
  8. config_get runas "$cfg" runas "nut"
  9. [ -n "$runas" ] && echo "RUN_AS_USER $runas" >> $UPSMON_C
  10. config_get val "$cfg" minsupplies 1
  11. echo "MINSUPPLIES $val" >> $UPSMON_C
  12. config_get val "$cfg" shutdowncmd "/sbin/halt"
  13. echo "SHUTDOWNCMD \"$val\"" >> $UPSMON_C
  14. config_get val "$cfg" notifycmd
  15. [ -n "$val" ] && echo "NOTIFYCMD \"$val\"" >> $UPSMON_C
  16. config_get val "$cfg" pollfreq 5
  17. echo "POLLFREQ $val" >> $UPSMON_C
  18. config_get val "$cfg" pollfreqalert 5
  19. echo "POLLFREQALERT $val" >> $UPSMON_C
  20. config_get val "$cfg" hostsync 15
  21. echo "HOSTSYNC $val" >> $UPSMON_C
  22. config_get val "$cfg" deadtime 15
  23. echo "DEADTIME $val" >> $UPSMON_C
  24. config_get val "$cfg" powerdownflag /var/run/killpower
  25. echo "POWERDOWNFLAG $val" >> $UPSMON_C
  26. config_get val "$cfg" onlinemsg
  27. [ -n "$val" ] && echo "NOTIFYMSG ONLINE \"$val\"" >> $UPSMON_C
  28. config_get val "$cfg" onbattmsg
  29. [ -n "$val" ] && echo "NOTIFYMSG ONBATT \"$val\"" >> $UPSMON_C
  30. config_get val "$cfg" lowbattmsg
  31. [ -n "$val" ] && echo "NOTIFYMSG LOWBATT \"$val\"" >> $UPSMON_C
  32. config_get val "$cfg" fsdmsg
  33. [ -n "$val" ] && echo "NOTIFYMSG FSD \"$val\"" >> $UPSMON_C
  34. config_get val "$cfg" commokmsg
  35. [ -n "$val" ] && echo "NOTIFYMSG COMMOK \"$val\"" >> $UPSMON_C
  36. config_get val "$cfg" commbadmsg
  37. [ -n "$val" ] && echo "NOTIFYMSG COMMBAD \"$val\"" >> $UPSMON_C
  38. config_get val "$cfg" shutdownmsg
  39. [ -n "$val" ] && echo "NOTIFYMSG SHUTDOWN \"$val\"" >> $UPSMON_C
  40. config_get val "$cfg" replbattmsg
  41. [ -n "$val" ] && echo "NOTIFYMSG REPLBATT \"$val\"" >> $UPSMON_C
  42. config_get val "$cfg" nocommmsg
  43. [ -n "$val" ] && echo "NOTIFYMSG NOCOMM \"$val\"" >> $UPSMON_C
  44. config_get val "$cfg" noparentmsg
  45. [ -n "$val" ] && echo "NOTIFYMSG NOPARENT \"$val\"" >> $UPSMON_C
  46. notifylist() {
  47. local value="$1"
  48. append optval "$value" "+"
  49. }
  50. setnotify() {
  51. local cfg="$1"
  52. local optname="$2"
  53. local optval
  54. config_list_foreach "$cfg" "$optname" notifylist
  55. if [ -z "$optval" ]; then
  56. # If no list fallback to trying option, fallback to default
  57. config_get optval "$cfg" "$optname" "$default"
  58. if [ -n "$optval" ]; then
  59. echo "$optval"
  60. else
  61. # No default, so do the NUT default
  62. echo "SYSLOG"
  63. fi
  64. else
  65. echo "$optval"
  66. fi
  67. }
  68. local default optval
  69. val=""
  70. config_list_foreach "$cfg" defaultnotify notifylist
  71. default="$optval"
  72. echo "NOTIFYFLAG ONLINE $(setnotify "$cfg" onlinenotify)" >> $UPSMON_C
  73. echo "NOTIFYFLAG ONBATT $(setnotify "$cfg" onbattnotify)" >> $UPSMON_C
  74. echo "NOTIFYFLAG LOWBATT $(setnotify "$cfg" lowbatnotify)" >> $UPSMON_C
  75. echo "NOTIFYFLAG FSD $(setnotify "$cfg" fsdnotify)" >> $UPSMON_C
  76. echo "NOTIFYFLAG COMMOK $(setnotify "$cfg" commoknotify)" >> $UPSMON_C
  77. echo "NOTIFYFLAG COMMBAD $(setnotify "$cfg" commbadnotify)" >> $UPSMON_C
  78. echo "NOTIFYFLAG SHUTDOWN $(setnotify "$cfg" shutdownnotify)" >> $UPSMON_C
  79. echo "NOTIFYFLAG REPLBATT $(setnotify "$cfg" repolbattnotify)" >> $UPSMON_C
  80. echo "NOTIFYFLAG NOCOMM $(setnotify "$cfg" nocommnotify)" >> $UPSMON_C
  81. echo "NOTIFYFLAG NOPARENT $(setnotify "$cfg" noparentnotify)" >> $UPSMON_C
  82. config_get val "$cfg" rbwarntime 43200
  83. echo "RBWARNTIME $val" >> $UPSMON_C
  84. config_get val "$cfg" nocommwarntime 300
  85. echo "NOCOMMWARNTIME $val" >> $UPSMON_C
  86. config_get val "$cfg" finaldelay 5
  87. echo "FINALDELAY $val" >> $UPSMON_C
  88. config_get val "$cfg" certpath
  89. if [ -n "$val" ]; then echo "CERTPATH $val" >> $UPSMON_C; fi
  90. config_get_bool val "$cfg" certverify 0
  91. if [ -n "$val" ]; then echo "CERTVERIFY $val" >> $UPSMON_C; fi
  92. config_get_bool val "$cfg" forcessl 0
  93. if [ -n "$val" ]; then echo "FORCESSL $val" >> $UPSMON_C; fi
  94. }
  95. nut_upsmon_add() {
  96. local cfg="$1"
  97. local type="$2"
  98. local upsname
  99. local hostname
  100. local port
  101. local powervalue
  102. local username
  103. local password
  104. local system
  105. # if UPSMON_C is a symlink we're only doing generated config
  106. [ -L $UPSMON_C ] && {
  107. rm -f $UPSMON_C
  108. nut_upsmon_conf ""
  109. }
  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. }
  122. start_service() {
  123. mkdir -p "$(dirname "$UPSMON_C")"
  124. chmod 750 "$(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. [ -z "$(cat /var/etc/nut/nut.conf)" ] && echo "MODE=netclient" >>/var/etc/nut/nut.conf
  130. chmod 640 $UPSMON_C
  131. chmod 640 /var/etc/nut/nut.conf
  132. chown ${runas:-root}:$(id -gn ${runas:-root}) /var/etc/nut
  133. chown ${runas:-root}:$(id -gn ${runas:-root}) /var/etc/nut/nut.conf
  134. chown ${runas:-root}:$(id -gn ${runas:-root}) $UPSMON_C
  135. [ -d /var/run/nut ] || {
  136. mkdir -m 0750 -p /var/run/nut
  137. chown ${runas:-root}:$(id -gn ${runas:-root}) /var/run/nut
  138. }
  139. exec $DEBUG /usr/sbin/upsmon $UPSMON_OPTIONS
  140. }
  141. stop_service() {
  142. exec /usr/sbin/upsmon -c stop
  143. }
  144. reload_service() {
  145. exec /usr/sbin/upsmon -c reload
  146. }