summaryrefslogtreecommitdiffstats
path: root/lib/plugins/stonith/external/ippower9258.in
blob: 6ae7e0235345c3375569fc94f00e9320f13d03b3 (plain)
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
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
#!/bin/sh
#
# External STONITH module using IP Power 9258 or compatible devices.
#
# Copyright (c) 2010 Helmut Weymann (Helmut (at) h-weymann (dot) de)
# 
# This program is free software; you can redistribute it and/or modify
# it under the terms of version 2 of the GNU General Public License as
# published by the Free Software Foundation.
#
# This program is distributed in the hope that it would be useful, but
# WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
#
# Further, this software is distributed without any warranty that it is
# free of the rightful claim of any third person regarding infringement
# or the like. Any license provided herein, whether implied or
# otherwise, applies only to this software file. Patent licenses, if
# any, provided herein do not apply to combinations of this program with
# other software, or any other product whatsoever.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write the Free Software Foundation,
# Inc., 59 Temple Place - Suite 330, Boston MA 02111-1307, USA.
#

#
# Basic commands & parameters independent from individual device 

DEVICE="IP Power 9258"
IPPowerOn="1"
IPPowerOff="0"
IPGetPower="Set.cmd?CMD=GetPower"
IPSetPower="Set.cmd?CMD=SetPower"
IPPort_name="P"
IPPort0=60
HTTP_COMMAND="wget -q -O - --"
LOG_ERROR="ha_log.sh err"
LOG_WARNING="ha_log.sh warn"
LOG_INFO="ha_log.sh info"
LOG_DEBUG="ha_log.sh debug"
MY_COOKIES="cookies.txt"
MY_TEMPFILE="temp.htm"
PORT_STATUS="iocontrol.htm"
UNDEFINED_HOSTNAME="*not-defined*"

#
# check MY_ROOT_PATH for IP Power 9258 and create it if necessary
MY_ROOT_PATH="@GLUE_STATE_DIR@/heartbeat/rsctmp/ippower9258"

#
# script functions
#

get_challenge() {
	#
	# device sends a challenge for md5 encryption of username, password and challenge
	send_web_command - "http://$deviceip/" | grep Challenge | grep input | cut -d '"' -f 6
}

get_cookie_from_device(){
	# the form on the login page has these fields:
	# Username, Password, Challenge, Response, ScreenWidth
	#
    challenge=`get_challenge`
	response=`echo -n "$username$password$challenge" | md5sum | cut -b -32`
	postdata="Username=$username&Password=&Challenge=&Response=$response&ScreenWidth=1024"
	send_web_command " $MY_PATH/$MY_TEMPFILE --post-data=$postdata" "http://$deviceip/tgi/login.tgi"
	if grep -qs "Invalid User name or Password" $MY_PATH/$MY_TEMPFILE
	then
		$LOG_ERROR "Login to device $deviceip failed."
		$LOG_ERROR "Received Challenge = <<<$challenge>>>."
		$LOG_ERROR "Sent postdata = <<<$postdata>>>."
		exit 1
	fi
}

get_data_from_device() {
	# If successful all device info is available in MY_PATH
	rm -f "$MY_PATH/$PORT_STATUS"
	send_web_command "$MY_PATH/$PORT_STATUS" "http://$deviceip/$PORT_STATUS"
	if grep -qs "Cookie Time Out" $MY_PATH/$PORT_STATUS
	then
		$LOG_ERROR "received no port data from $deviceip (Cookie Time Out)"
		exit 1
	fi
}

send_http_request() {
	# ececution of http commands supported by the device
	$HTTP_COMMAND "http://$username:$password@$deviceip/$1"
}

send_web_command(){
	# ececution of web commands through the web-interface
	WEB_COMMAND="wget -q --keep-session-cookies"
	WEB_COMMAND="$WEB_COMMAND --load-cookies $MY_PATH/$MY_COOKIES"
	WEB_COMMAND="$WEB_COMMAND --save-cookies $MY_PATH/$MY_COOKIES"
	$WEB_COMMAND -O $1 -- $2
}

name2port() {
	local name=$1
	local i=$IPPort0
	for h in $device_hostlist ; do
		if [ $h = $name ]; then
			echo $IPPort_name$i
			return
		fi
		i=`expr $i + 1`
	done
	echo "invalid"
}

set_port() {
	#
	# port status is always set. Even if requested status is current status.
	# host status is not considered.
	local host=$1
	local requested_status=$2 # 0 or 1
	local port=`name2port $host`
	if [ "$port" = "invalid" ]
	then
		$LOG_ERROR "Host $host is not in hostlist ($hostlist) for $deviceip."
		exit 1
	fi
	ret=`send_http_request "$IPSetPower+$port=$requested_status" | cut -b 11`
	if [ "$ret" != "$requested_status" ]
	then
		$LOG_ERROR "$DEVICE at $deviceip responds with wrong status $ret for host $host at port $port."
		exit 1
	fi
	return 0
}

build_device_hostlist() {
	# 
	# hostnames are available from http://$deviceip/iocontrol.htm"
	# check for number of ports
	#
	device_hostlist=$(
	w3m -dump $MY_PATH/$PORT_STATUS | grep 'Power[1-8]' |
	sed 's/[^[]*\[//;s/\].*//;s/ *//' |
	while read h; do
		[ -z "$h" ] &&
			echo $UNDEFINED_HOSTNAME ||
			echo $h
	done
	)
	if [ x = x"$device_hostlist" ]; then
		$LOG_ERROR "cannot get hostlist for $deviceip"
		exit 1
	fi
	$LOG_DEBUG "Got new hostlist ($device_hostlist) from $deviceip"
}

filter_device_hostlist() {
	# check the given hostlist against the device hostlist
	local host
	for host in $device_hostlist; do
		[ "$host" != "$UNDEFINED_HOSTNAME" ] &&
			echo $host
	done
}

check_hostlist() {
	# check the given hostlist against the device hostlist
	local cnt=`echo "$hostlist" | wc -w`
	local cnt2=0
	local host
	for host in $hostlist; do
		if [ `name2port $host` != "invalid" ]; then
			cnt2=$((cnt2+1))
		else
			$LOG_ERROR "host $host not defined at $deviceip"
		fi
	done
	[ $cnt -ne $cnt2 ] &&
		exit 1
}

get_http_status() {
	pattern="P60=[01],P61=[01],P62=[01],P63=[01],P64=[01],P65=[01],P66=[01],P67=[01]"
	ret=`send_http_request "$IPGetPower" | grep $pattern`
	if [ "X$ret" = "X" ]
	then
		$LOG_ERROR "$DEVICE at $deviceip returns invalid or no string."
		exit 1
	fi
}

hostlist=`echo $hostlist | tr ',' ' '`
case $1 in
gethosts|on|off|reset|status)
	# need environment from stonithd
	# and device information from individual device
	#
	# default device username is admin
	# IP Power 9258 does not allow user management.
	#
	if [ "X$username" = "X" ]
	then
		username="admin"
	fi

	mkdir -p $MY_ROOT_PATH
	tmp_path="$deviceip" # ensure a simple unique pathname
	MY_PATH="$MY_ROOT_PATH/$tmp_path"
	mkdir -p $MY_PATH
	get_cookie_from_device
	get_data_from_device
	build_device_hostlist
	if [ "X$hostlist" = "X" ]; then
		hostlist="`filter_device_hostlist`"
	else
		check_hostlist
	fi
	;;
*)
	# the client is asking for meta-data
	;;
esac

target=`echo $2 | sed 's/[.].*//'`
# the necessary actions for stonithd
case $1 in
gethosts)
	echo $hostlist
	;;
on)
	set_port $target $IPPowerOn
	;;
off)
	set_port $target $IPPowerOff
	;;
reset)
	set_port $target $IPPowerOff
	sleep 5
	set_port $target $IPPowerOn
	;;
status)
	# werify http command interface
	get_http_status
	;;
getconfignames)
	# return all the config names
	for ipparam in deviceip username password hostlist
	do
		echo $ipparam
	done
	;;
getinfo-devid)
	echo "IP Power 9258"
	;;
getinfo-devname)
	echo "IP Power 9258 power switch"
	;;
getinfo-devdescr)
	echo "Power switch IP Power 9258 with 4 or 8 power outlets."
	echo "WARNING: It is different from IP Power 9258 HP"
	;;
getinfo-devurl)
	echo "http://www.aviosys.com/manual.htm"
	;;
getinfo-xml)
	cat << IPPOWERXML
<parameters>
<parameter name="deviceip" unique="1" required="1">
<content type="string" />
<shortdesc lang="en">
IP address or hostname of the device.
</shortdesc>
<longdesc lang="en">
The IP Address or the hostname of the device.
</longdesc>
</parameter>

<parameter name="password" unique="0" required="1">
<content type="string" />
<shortdesc lang="en">
Password
</shortdesc>
<longdesc lang="en">
The password to log in with.
</longdesc>
</parameter>

<parameter name="hostlist" unique="0" required="0">
<content type="string" />
<shortdesc lang="en">
Hostlist
</shortdesc>
<longdesc lang="en">
The list of hosts that the device controls.
If you leave this list empty, we will retrieve the hostnames from the device.
</longdesc>
</parameter>

<parameter name="username" unique="0" required="0">
<content type="string" default="admin"/>
<shortdesc lang="en">
Account Name
</shortdesc>
<longdesc lang="en">
The user to log in with.
</longdesc>
</parameter>

</parameters>
IPPOWERXML
	;;
*)
	$LOG_ERROR "Unexpected command $1 for $DEVICE at $deviceip."
	exit 1;
	;;
esac