jansson: moved to github
[openwrt.org/packages.git] / utils / upsmon.init
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
143
144
145
#!/bin/sh /etc/rc.common
# Copyright © 2012 OpenWrt.org
#
# This is free software, licensed under the GNU General Public License v2.
# See /LICENSE for more information.
#
START=51

DEFAULT=/etc/default/nut
RUN_D=/var/run
PID_F=$RUN_D/upsmon.pid
UPSMON_C=$RUN_D/upsmon.conf

nut_upsmon_conf() {
	local cfg="$1"

	echo "# Config file automatically generated from UCI config" > $UPSMON_C

	config_get val "$cfg" runas
	[ -n "$val" ] && echo "RUN_AS_USER $val" >> $PSMON_C

	config_get val "$cfg" minsupplies 1
	echo "MINSUPPLIES $val" >> $UPSMON_C

	config_get val "$cfg" shutdowncmd "/sbin/halt"
	echo "SHUTDOWNCMD \"$val\"" >> $UPSMON_C

	config_get val "$cfg" notifycmd
	[ -n "$val" ] && echo "NOTIFYCMD \"$val\"" >> $UPSMON_C

	config_get val "$cfg" pollfreq 5
	echo "POLLFREQ $val" >> $UPSMON_C

	config_get val "$cfg" pollfreqalert 5
	echo "POLLFREQALERT $val" >> $UPSMON_C

	config_get val "$cfg" hostsync 15
	echo "HOSTSYNC $val" >> $UPSMON_C

	config_get val "$cfg" deadtime 15
	echo "DEADTIME $val" >> $UPSMON_C

	config_get val "$cfg" powerdownflag /var/run/killpower
	echo "POWERDOWNFLAG $val" >> $UPSMON_C

	config_get val "$cfg" onlinemsg
	[ -n "$val" ] && echo "NOTIFYMSG ONLINE \"$val\"" >> $UPSMON_C
	config_get val "$cfg" onbattmsg
	[ -n "$val" ] && echo "NOTIFYMSG ONBATT \"$val\"" >> $UPSMON_C
	config_get val "$cfg" lowbattmsg
	[ -n "$val" ] && echo "NOTIFYMSG LOWBATT \"$val\"" >> $UPSMON_C
	config_get val "$cfg" fsdmsg
	[ -n "$val" ] && echo "NOTIFYMSG FSD \"$val\"" >> $UPSMON_C
	config_get val "$cfg" commokmsg
	[ -n "$val" ] && echo "NOTIFYMSG COMMOK \"$val\"" >> $UPSMON_C
	config_get val "$cfg" commbadmsg
	[ -n "$val" ] && echo "NOTIFYMSG COMMBAD \"$val\"" >> $UPSMON_C
	config_get val "$cfg" shutdownmsg
	[ -n "$val" ] && echo "NOTIFYMSG SHUTDOWN \"$val\"" >> $UPSMON_C
	config_get val "$cfg" replbattmsg
	[ -n "$val" ] && echo "NOTIFYMSG REPLBATT \"$val\"" >> $UPSMON_C
	config_get val "$cfg" nocommmsg
	[ -n "$val" ] && echo "NOTIFYMSG NOCOMM \"$val\"" >> $UPSMON_C
	config_get val "$cfg" noparentmsg
	[ -n "$val" ] && echo "NOTIFYMSG NOPARENT \"$val\"" >> $UPSMON_C

	local default
	config_get default "$cfg" defaultnotify SYSLOG
	config_get val "$cfg" onlinenotify "$default"
	[ -n "$val" ] && echo "NOTIFYFLAG ONLINE $val" >> $UPSMON_C
	config_get val "$cfg" onbattnotify "$default"
	[ -n "$val" ] && echo "NOTIFYFLAG ONBATT $val" >> $UPSMON_C
	config_get val "$cfg" lowbattnotify "$default"
	[ -n "$val" ] && echo "NOTIFYFLAG LOWBATT $val" >> $UPSMON_C
	config_get val "$cfg" fsdnotify "$default"
	[ -n "$val" ] && echo "NOTIFYFLAG FSD $val" >> $UPSMON_C
	config_get val "$cfg" commoknotify "$default"
	[ -n "$val" ] && echo "NOTIFYFLAG COMMOK $val" >> $UPSMON_C
	config_get val "$cfg" commbadnotify "$default"
	[ -n "$val" ] && echo "NOTIFYFLAG COMMBAD $val" >> $UPSMON_C
	config_get val "$cfg" shutdownnotify "$default"
	[ -n "$val" ] && echo "NOTIFYFLAG SHUTDOWN $val" >> $UPSMON_C
	config_get val "$cfg" replbattnotify "$default"
	[ -n "$val" ] && echo "NOTIFYFLAG REPLBATT $val" >> $UPSMON_C
	config_get val "$cfg" nocommnotify "$default"
	[ -n "$val" ] && echo "NOTIFYFLAG NOCOMM $val" >> $UPSMON_C
	config_get val "$cfg" noparentnotify "$default"
	[ -n "$val" ] && echo "NOTIFYFLAG NOPARENT $val" >> $UPSMON_C

	config_get val "$cfg" rbwarntime 43200
	echo "RBWARNTIME $val" >> $UPSMON_C

	config_get val "$cfg" nocommwarntime 300
	echo "NOCOMMWARNTIME $val" >> $UPSMON_C

	config_get val "$cfg" finaldelay 5
	echo "FINALDELAY $val" >> $UPSMON_C
}

nut_upsmon_add() {
	local cfg="$1"
	local type="$2"
	local upsname
	local hostname
	local port
	local powervalue
	local user
	local pass
	local system

	# If no core config, use defaults
	[ -r $UPSMON_C ] || nut_upsmon_conf ""

	config_get upsname "$cfg" upsname
	config_get hostname "$cfg" hostname localhost
	config_get port "$cfg" port
	config_get powervalue "$cfg" powervalue 1
	config_get user "$cfg" user
	config_get pass "$cfg" password
	system="$upsname@$hostname"
	if [ -n "$port" ]; then
	    system="$system:$port";
	fi
	echo "MONITOR $system $powervalue $user $pass $type" >> $UPSMON_C
}

start() {
	[ -d $RUN_D ] || mkdir -p $RUN_D
	rm -f $UPSMON_C

	config_load upsmon

	config_foreach nut_upsmon_conf upsmon
	config_foreach nut_upsmon_add master master
	config_foreach nut_upsmon_add slave slave

	[ -f $DEFAULT ] && . $DEFAULT
	$DEBUG /usr/sbin/upsmon $UPSMON_OPTIONS
}

stop() {
	[ -r $PID_F ] && /usr/sbin/upsmon -c stop
	rm -f $UPSMON_C
}

comments