File: init

package info (click to toggle)
nut 0.45.5-rel-1
  • links: PTS
  • area: main
  • in suites: woody
  • size: 1,984 kB
  • ctags: 2,146
  • sloc: ansic: 22,216; sh: 1,138; makefile: 405
file content (142 lines) | stat: -rw-r--r-- 3,528 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
#! /bin/sh
#
# nut    Script to start and stop Network UPS Tools daemon(s)

PATH=/sbin:/usr/sbin:/bin:/usr/bin:/usr/local/sbin:/usr/local/bin
NAME=nut
DESC="Network UPS Tools"

UPSD_CONF=/etc/nut/upsd.conf
UPSMON_CONF=/etc/nut/upsmon.conf

start_stop_model_daemons () {
  (cat $UPSD_CONF | grep "^UPS") |
  while read directive identifier statepath model args ; do
    # note that the following construction of the pidfile assumes
    # that the user has configured their upsd.conf CORRECTLY!
    pidfile="/var/run/nut/${statepath#/var/lib/nut/}.pid"
    case "$1" in
      start)
        eval start-stop-daemon -S -q -p $pidfile \
            -x /lib/nut/$model -- $args 2>/dev/null >/dev/null
        echo -n " ${statepath#/var/lib/nut/}"
        ;;
      stop)
        eval start-stop-daemon -K -o -q -p $pidfile \
            -n $model 2>/dev/null >/dev/null
        echo -n " ${statepath#/var/lib/nut/}"
        ;;
    esac
  done
}

start_stop_upsd () {
  (cat $UPSD_CONF | grep "^UPS") |
  while read directive identifier statepath model args ; do
    case "$1" in
      start)
        eval start-stop-daemon -S -q -p /var/run/nut/upsd.pid \
            -x /sbin/upsd 2>/dev/null >/dev/null
        echo -n " upsd"
        break # only start upsd once
        ;;
      stop)
        eval start-stop-daemon -K -o -q -p /var/run/nut/upsd.pid \
            -n upsd 2>/dev/null >/dev/null
        echo -n " upsd"
        break # only stop upsd once
        ;;
    esac
  done
}

start_stop_upsmon () {
  (cat $UPSMON_CONF | grep "^MONITOR" ) |
  while read directive system powervalue password master_slave ; do
    case "$1" in
      start)
        eval start-stop-daemon -S -q -p /var/run/nut/upsmon.pid \
            -x /sbin/upsmon 2>/dev/null >/dev/null
        echo -n " upsmon"
        break # only start upsmon once
        ;;
      stop)
        eval start-stop-daemon -K -o -q -p /var/run/nut/upsmon.pid \
            -n upsmon 2>/dev/null >/dev/null
        echo -n " upsmon"
        break # only stop upsmon once
        ;;
    esac
  done
}

word_count() {
    echo $#
}

poweroff() {
  (cat $UPSMON_CONF | grep "^POWERDOWNFLAG") |
  while read directive flagfile ; do
    if [ -f $flagfile ] ; then
      (cat $UPSD_CONF | grep "^UPS") |
      while read directive identifier statepath model args ; do
        case $(word_count $args) in
          0)
            # ok... this is bad
            echo "unable to poweroff $identifier because upsd.conf is incorrect"
            ;;
          1)
            # 'args' contains only one item (the port)
            eval /lib/nut/$model -k $args
            ;;
          *)
            # 'args' contains at least two items; need to extract the port
            the_port=`echo $args | sed -ne 's#^.* \([^ ]*\)$#\1#p'`
            the_args=`echo $args | sed -ne 's#^\(.*\) [^ ]*$#\1#p'`
            eval /lib/nut/$model $the_args -k $the_port
            ;;
        esac
      done
    fi
  done
}


case "$1" in

  start)
    echo -n "Starting $DESC:"
    start_stop_model_daemons start
    start_stop_upsd start
    start_stop_upsmon start
    echo "."
    ;;

  stop)
    echo -n "Stopping $DESC:"
    start_stop_upsmon stop
    start_stop_upsd stop
    start_stop_model_daemons stop
    echo "."
    ;;

  restart|force-reload)
    $0 stop
    sleep 1
    $0 start
    ;;

  poweroff)
    echo "Powering off $DESC."
    poweroff
    ;;

  *)
    N=/etc/init.d/$NAME
    echo "Usage: $N {start|stop|restart|force-reload|poweroff}" >&2
    exit 1
    ;;

esac

exit 0