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.

158 lines
3.4 KiB

  1. #!/bin/sh /etc/rc.common
  2. # Copyright © 2012 OpenWrt.org
  3. #
  4. # This is free software, licensed under the GNU General Public License v2.
  5. # See /LICENSE for more information.
  6. #
  7. START=50
  8. RUN_D=/var/run
  9. PID_F=$RUN_D/upsd.pid
  10. UPS_C=/var/etc/nut/ups.conf
  11. USERS_C=/var/etc/nut/upsd.users
  12. UPSD_C=/var/etc/nut/upsd.conf
  13. USE_PROCD=1
  14. listen_address() {
  15. local cfg="$1"
  16. config_get address "$cfg" address "::1"
  17. config_get port "$cfg" port
  18. echo "LISTEN $address $port" >>$UPSD_C
  19. }
  20. upsd_statepath() {
  21. local cfg="$1"
  22. config_get statepath "$cfg" statepath
  23. }
  24. upsd_config() {
  25. local cfg="$1"
  26. local maxage maxconn certfile
  27. # Note runas support requires you make sure USB device file is readable by
  28. # the runas user
  29. config_get runas "$cfg" runas
  30. config_get maxage "$cfg" maxage
  31. [ -n "$maxage" ] && echo "MAXAGE $maxage" >>$UPSD_C
  32. config_get statepath "$cfg" statepath
  33. [ -n "$statepath" ] && echo "STATEPATH $statepath" >>$UPSD_C
  34. config_get maxconn "$cfg" maxconn
  35. [ -n "$maxconn" ] && echo "MAXCONN $maxconn" >>$UPSD_C
  36. #NOTE: certs only apply to SSL-enabled version
  37. config_get certfile "$cfg" certfile
  38. [ -n "$certfile" ] && echo "CERTFILE $certfile" >>$UPSD_C
  39. }
  40. nut_user_add() {
  41. local cfg="$1"
  42. local a
  43. local val
  44. config_get val "$cfg" username "$1"
  45. echo "[$val]" >> $USERS_C
  46. config_get val "$cfg" password
  47. echo " password = $val" >> $USERS_C
  48. config_get val "$cfg" actions
  49. for a in $val; do
  50. echo " actions = $a" >> $USERS_C
  51. done
  52. instcmd() {
  53. local val="$1"
  54. echo " instcmds = $val" >> $USERS_C
  55. }
  56. config_list_foreach "$cfg" instcmd instcmd
  57. config_get val "$cfg" upsmon
  58. if [ -n "$val" ]; then
  59. echo " upsmon $val" >> $USERS_C
  60. fi
  61. }
  62. start_service() {
  63. local runas statepath
  64. mkdir -p /var/etc/nut
  65. chmod -R 750 /var/etc/nut
  66. rm -f $UPSD_C
  67. rm -f $USERS_C
  68. rm -f $UPSD_C
  69. rm -f /var/etc/nut/nut.conf
  70. echo "# Config file automatically generated from UCI config" > $UPS_C
  71. echo "# Config file automatically generated from UCI config" > $USERS_C
  72. echo "# Config file automatically generated from UCI config" > $UPSD_C
  73. local in_driver have_drivers
  74. config_cb() {
  75. if [ "$1" != "driver" ]; then
  76. in_driver=
  77. else
  78. echo "[$2]" >> $UPS_C
  79. in_driver=true
  80. have_drivers=true
  81. fi
  82. }
  83. option_cb() {
  84. if [ "$in_driver" = "true" ]; then
  85. echo " $1 = $2" >> $UPS_C
  86. fi
  87. }
  88. config_load nut_server
  89. config_foreach nut_user_add user
  90. config_foreach upsd_config upsd
  91. config_foreach listen_address listen_address
  92. echo "MODE=netserver" >>/var/etc/nut/nut.conf
  93. chmod 0640 $USERS_C
  94. chmod 0640 $UPS_C
  95. chmod 0640 $UPSD_C
  96. chmod 0640 /var/etc/nut/nut.conf
  97. [ -d "${statepath:-/var/run/nut}" ] || {
  98. mkdir -m 0750 -p "${statepath:-/var/run/nut}"
  99. chown $runas:$(id -gn $runas) "${statepath:-/var/run/nut}"
  100. }
  101. if [ -n "$runas" ]; then
  102. chown -R $runas:$(id -gn $runas) /var/etc/nut
  103. fi
  104. if [ "$have_drivers" = "true" ]; then
  105. $DEBUG /usr/sbin/upsd ${runas:+-u $runas} $OPTIONS
  106. $DEBUG /usr/sbin/upsdrvctl ${runas:+-u $runas} start
  107. fi
  108. }
  109. nut_driver_stop() {
  110. local cfg="$1"
  111. local driver
  112. config_get driver "$cfg" driver
  113. [ -r ${statepath:-/var/run/nut}/$driver-$cfg ] && /usr/sbin/upsdrvctl stop $cfg
  114. }
  115. stop_service() {
  116. [ -r $PID_F ] && /usr/sbin/upsd -c stop
  117. config_load ups
  118. config_foreach upsd_statepath upsd
  119. config_foreach nut_driver_stop driver
  120. }
  121. reload_service() {
  122. upsd -c reload
  123. }