Small adjustments on toplevel README file
[autotest-zwu.git] / conmux / drivers / fence_apc_snmp.py
blob3595071f94c9d6aa0f482f41b05b964f290c332f
1 #!/usr/bin/python
3 #############################################################################
4 #############################################################################
5 ##
6 ## Copyright (C) Sistina Software, Inc. 1997-2003 All rights reserved.
7 ## Copyright (C) 2004-2006 Red Hat, Inc. All rights reserved.
8 ##
9 ## This copyrighted material is made available to anyone wishing to use,
10 ## modify, copy, or redistribute it subject to the terms and conditions
11 ## of the GNU General Public License v.2.
13 #############################################################################
14 ## This APC Fence script uses snmp to control the APC power
15 ## switch. This script requires that net-snmp-utils be installed
16 ## on all nodes in the cluster, and that the powernet369.mib file be
17 ## located in /usr/share/snmp/mibs/
18 #############################################################################
19 #############################################################################
23 import getopt, sys
24 import os
25 import time
26 import select
27 import signal
28 from glob import glob
30 #BEGIN_VERSION_GENERATION
31 FENCE_RELEASE_NAME=""
32 REDHAT_COPYRIGHT=""
33 BUILD_DATE=""
34 #END_VERSION_GENERATION
36 POWER_ON="outletOn"
37 POWER_OFF="outletOff"
38 POWER_REBOOT="outletReboot"
40 def usage():
41 print "Usage:";
42 print "";
43 print "Options:";
44 print " -a <ip> IP address or hostname of MasterSwitch";
45 print " -h usage";
46 print " -l <name> Login name";
47 print " -n <num> Outlet number to change";
48 print " -o <string> Action: Reboot (default), Off or On";
49 print " -p <string> Login password";
50 print " -q quiet mode";
51 print " -V version";
52 print " -v Log to file /tmp/apclog";
54 print sys.argv
55 sys.exit(0);
59 def main():
60 apc_base = "enterprises.apc.products.hardware."
61 apc_outletctl = "masterswitch.sPDUOutletControl.sPDUOutletControlTable.sPDUOutletControlEntry.sPDUOutletCtl."
62 apc_outletstatus = "masterswitch.sPDUOutletStatus.sPDUOutletStatusMSPTable.sPDUOutletStatusMSPEntry.sPDUOutletStatusMSP."
64 address = ""
65 output = ""
66 port = ""
67 action = "outletReboot"
68 status_check = False
69 verbose = False
71 if not glob('/usr/share/snmp/mibs/powernet*.mib'):
72 sys.stderr.write('This APC Fence script uses snmp to control the APC power switch. This script requires that net-snmp-utils be installed on all nodes in the cluster, and that the powernet369.mib file be located in /usr/share/snmp/mibs/\n')
73 sys.exit(1)
75 if len(sys.argv) > 1:
76 try:
77 opts, args = getopt.getopt(sys.argv[1:], "a:hl:p:n:o:vV", ["help", "output="])
78 except getopt.GetoptError:
79 #print help info and quit
80 usage()
81 sys.exit(2)
83 for o, a in opts:
84 if o == "-v":
85 verbose = True
86 if o == "-V":
87 print "%s\n" % FENCE_RELEASE_NAME
88 print "%s\n" % REDHAT_COPYRIGHT
89 print "%s\n" % BUILD_DATE
90 sys.exit(0)
91 if o in ("-h", "--help"):
92 usage()
93 sys.exit(0)
94 if o == "-n":
95 port = a
96 if o == "-o":
97 lcase = a.lower() #Lower case string
98 if lcase == "off":
99 action = "outletOff"
100 elif lcase == "on":
101 action = "outletOn"
102 elif lcase == "reboot":
103 action = "outletReboot"
104 elif lcase == "status":
105 #action = "sPDUOutletStatusMSPOutletState"
106 action = ""
107 status_check = True
108 else:
109 usage()
110 sys.exit()
111 if o == "-a":
112 address = a
114 if address == "":
115 usage()
116 sys.exit(1)
118 if port == "":
119 usage()
120 sys.exit(1)
122 else: #Get opts from stdin
123 params = {}
124 #place params in dict
125 for line in sys.stdin:
126 val = line.split("=")
127 if len(val) == 2:
128 params[val[0].strip()] = val[1].strip()
130 try:
131 address = params["ipaddr"]
132 except KeyError, e:
133 sys.stderr.write("FENCE: Missing ipaddr param for fence_apc...exiting")
134 sys.exit(1)
135 try:
136 login = params["login"]
137 except KeyError, e:
138 sys.stderr.write("FENCE: Missing login param for fence_apc...exiting")
139 sys.exit(1)
141 try:
142 passwd = params["passwd"]
143 except KeyError, e:
144 sys.stderr.write("FENCE: Missing passwd param for fence_apc...exiting")
145 sys.exit(1)
147 try:
148 port = params["port"]
149 except KeyError, e:
150 sys.stderr.write("FENCE: Missing port param for fence_apc...exiting")
151 sys.exit(1)
154 try:
155 a = params["option"]
156 if a == "Off" or a == "OFF" or a == "off":
157 action = POWER_OFF
158 elif a == "On" or a == "ON" or a == "on":
159 action = POWER_ON
160 elif a == "Reboot" or a == "REBOOT" or a == "reboot":
161 action = POWER_REBOOT
162 except KeyError, e:
163 action = POWER_REBOOT
165 ####End of stdin section
167 apc_command = apc_base + apc_outletctl + port
169 args_status = list()
170 args_off = list()
171 args_on = list()
173 args_status.append("/usr/bin/snmpget")
174 args_status.append("-Oqu") #sets printing options
175 args_status.append("-v")
176 args_status.append("1")
177 args_status.append("-c")
178 args_status.append("private")
179 args_status.append("-m")
180 args_status.append("ALL")
181 args_status.append(address)
182 args_status.append(apc_command)
184 args_off.append("/usr/bin/snmpset")
185 args_off.append("-Oqu") #sets printing options
186 args_off.append("-v")
187 args_off.append("1")
188 args_off.append("-c")
189 args_off.append("private")
190 args_off.append("-m")
191 args_off.append("ALL")
192 args_off.append(address)
193 args_off.append(apc_command)
194 args_off.append("i")
195 args_off.append("outletOff")
197 args_on.append("/usr/bin/snmpset")
198 args_on.append("-Oqu") #sets printing options
199 args_on.append("-v")
200 args_on.append("1")
201 args_on.append("-c")
202 args_on.append("private")
203 args_on.append("-m")
204 args_on.append("ALL")
205 args_on.append(address)
206 args_on.append(apc_command)
207 args_on.append("i")
208 args_on.append("outletOn")
210 cmdstr_status = ' '.join(args_status)
211 cmdstr_off = ' '.join(args_off)
212 cmdstr_on = ' '.join(args_on)
214 ##This section issues the actual commands. Reboot is split into
215 ##Off, then On to make certain both actions work as planned.
217 ##The status command just dumps the outlet status to stdout.
218 ##The status checks that are made when turning an outlet on or off, though,
219 ##use the execWithCaptureStatus so that the stdout from snmpget can be
220 ##examined and the desired operation confirmed.
222 if status_check:
223 if verbose:
224 fd = open("/tmp/apclog", "w")
225 fd.write("Attempting the following command: %s\n" % cmdstr_status)
226 strr = os.system(cmdstr_status)
227 print strr
228 if verbose:
229 fd.write("Result: %s\n" % strr)
230 fd.close()
232 else:
233 if action == POWER_OFF:
234 if verbose:
235 fd = open("/tmp/apclog", "w")
236 fd.write("Attempting the following command: %s\n" % cmdstr_off)
237 strr = os.system(cmdstr_off)
238 time.sleep(1)
239 strr,code = execWithCaptureStatus("/usr/bin/snmpget",args_status)
240 if verbose:
241 fd.write("Result: %s\n" % strr)
242 fd.close()
243 if strr.find(POWER_OFF) >= 0:
244 print "Success. Outlet off"
245 sys.exit(0)
246 else:
247 if verbose:
248 fd.write("Unable to power off apc outlet")
249 fd.close()
250 sys.exit(1)
252 elif action == POWER_ON:
253 if verbose:
254 fd = open("/tmp/apclog", "w")
255 fd.write("Attempting the following command: %s\n" % cmdstr_on)
256 strr = os.system(cmdstr_on)
257 time.sleep(1)
258 strr,code = execWithCaptureStatus("/usr/bin/snmpget",args_status)
259 #strr = os.system(cmdstr_status)
260 if verbose:
261 fd.write("Result: %s\n" % strr)
262 if strr.find(POWER_ON) >= 0:
263 if verbose:
264 fd.close()
265 print "Success. Outlet On."
266 sys.exit(0)
267 else:
268 print "Unable to power on apc outlet"
269 if verbose:
270 fd.write("Unable to power on apc outlet")
271 fd.close()
272 sys.exit(1)
274 elif action == POWER_REBOOT:
275 if verbose:
276 fd = open("/tmp/apclog", "w")
277 fd.write("Attempting the following command: %s\n" % cmdstr_off)
278 strr = os.system(cmdstr_off)
279 time.sleep(1)
280 strr,code = execWithCaptureStatus("/usr/bin/snmpget",args_status)
281 #strr = os.system(cmdstr_status)
282 if verbose:
283 fd.write("Result: %s\n" % strr)
284 if strr.find(POWER_OFF) < 0:
285 print "Unable to power off apc outlet"
286 if verbose:
287 fd.write("Unable to power off apc outlet")
288 fd.close()
289 sys.exit(1)
291 if verbose:
292 fd.write("Attempting the following command: %s\n" % cmdstr_on)
293 strr = os.system(cmdstr_on)
294 time.sleep(1)
295 strr,code = execWithCaptureStatus("/usr/bin/snmpget",args_status)
296 #strr = os.system(cmdstr_status)
297 if verbose:
298 fd.write("Result: %s\n" % strr)
299 if strr.find(POWER_ON) >= 0:
300 if verbose:
301 fd.close()
302 print "Success: Outlet Rebooted."
303 sys.exit(0)
304 else:
305 print "Unable to power on apc outlet"
306 if verbose:
307 fd.write("Unable to power on apc outlet")
308 fd.close()
309 sys.exit(1)
311 def execWithCaptureStatus(command, argv, searchPath = 0, root = '/', stdin = 0,
312 catchfd = 1, closefd = -1):
314 if not os.access (root + command, os.X_OK):
315 raise RuntimeError, command + " cannot be run"
317 (read, write) = os.pipe()
319 childpid = os.fork()
320 if (not childpid):
321 if (root and root != '/'): os.chroot (root)
322 if isinstance(catchfd, tuple):
323 for fd in catchfd:
324 os.dup2(write, fd)
325 else:
326 os.dup2(write, catchfd)
327 os.close(write)
328 os.close(read)
330 if closefd != -1:
331 os.close(closefd)
333 if stdin:
334 os.dup2(stdin, 0)
335 os.close(stdin)
337 if (searchPath):
338 os.execvp(command, argv)
339 else:
340 os.execv(command, argv)
342 sys.exit(1)
344 os.close(write)
346 rc = ""
347 s = "1"
348 while (s):
349 select.select([read], [], [])
350 s = os.read(read, 1000)
351 rc = rc + s
353 os.close(read)
355 pid = -1
356 status = -1
357 try:
358 (pid, status) = os.waitpid(childpid, 0)
359 except OSError, (errno, msg):
360 print __name__, "waitpid:", msg
362 if os.WIFEXITED(status) and (os.WEXITSTATUS(status) == 0):
363 status = os.WEXITSTATUS(status)
364 else:
365 status = -1
367 return (rc, status)
369 if __name__ == "__main__":
370 main()