Eric Li | 7edb304 | 2011-01-06 17:57:17 -0800 | [diff] [blame^] | 1 | #!/usr/bin/python |
| 2 | |
| 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. |
| 12 | ## |
| 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 | ############################################################################# |
| 20 | |
| 21 | |
| 22 | |
| 23 | import getopt, sys |
| 24 | import os |
| 25 | import time |
| 26 | import select |
| 27 | import signal |
| 28 | from glob import glob |
| 29 | |
| 30 | #BEGIN_VERSION_GENERATION |
| 31 | FENCE_RELEASE_NAME="" |
| 32 | REDHAT_COPYRIGHT="" |
| 33 | BUILD_DATE="" |
| 34 | #END_VERSION_GENERATION |
| 35 | |
| 36 | POWER_ON="outletOn" |
| 37 | POWER_OFF="outletOff" |
| 38 | POWER_REBOOT="outletReboot" |
| 39 | |
| 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"; |
| 53 | |
| 54 | print sys.argv |
| 55 | sys.exit(0); |
| 56 | |
| 57 | |
| 58 | |
| 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." |
| 63 | |
| 64 | address = "" |
| 65 | output = "" |
| 66 | port = "" |
| 67 | action = "outletReboot" |
| 68 | status_check = False |
| 69 | verbose = False |
| 70 | |
| 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) |
| 74 | |
| 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) |
| 82 | |
| 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 |
| 113 | |
| 114 | if address == "": |
| 115 | usage() |
| 116 | sys.exit(1) |
| 117 | |
| 118 | if port == "": |
| 119 | usage() |
| 120 | sys.exit(1) |
| 121 | |
| 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() |
| 129 | |
| 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) |
| 140 | |
| 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) |
| 146 | |
| 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) |
| 152 | |
| 153 | |
| 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 |
| 164 | |
| 165 | ####End of stdin section |
| 166 | |
| 167 | apc_command = apc_base + apc_outletctl + port |
| 168 | |
| 169 | args_status = list() |
| 170 | args_off = list() |
| 171 | args_on = list() |
| 172 | |
| 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) |
| 183 | |
| 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") |
| 196 | |
| 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") |
| 209 | |
| 210 | cmdstr_status = ' '.join(args_status) |
| 211 | cmdstr_off = ' '.join(args_off) |
| 212 | cmdstr_on = ' '.join(args_on) |
| 213 | |
| 214 | ##This section issues the actual commands. Reboot is split into |
| 215 | ##Off, then On to make certain both actions work as planned. |
| 216 | ## |
| 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. |
| 221 | |
| 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() |
| 231 | |
| 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) |
| 251 | |
| 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) |
| 273 | |
| 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) |
| 290 | |
| 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) |
| 310 | |
| 311 | def execWithCaptureStatus(command, argv, searchPath = 0, root = '/', stdin = 0, |
| 312 | catchfd = 1, closefd = -1): |
| 313 | |
| 314 | if not os.access (root + command, os.X_OK): |
| 315 | raise RuntimeError, command + " cannot be run" |
| 316 | |
| 317 | (read, write) = os.pipe() |
| 318 | |
| 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) |
| 329 | |
| 330 | if closefd != -1: |
| 331 | os.close(closefd) |
| 332 | |
| 333 | if stdin: |
| 334 | os.dup2(stdin, 0) |
| 335 | os.close(stdin) |
| 336 | |
| 337 | if (searchPath): |
| 338 | os.execvp(command, argv) |
| 339 | else: |
| 340 | os.execv(command, argv) |
| 341 | |
| 342 | sys.exit(1) |
| 343 | |
| 344 | os.close(write) |
| 345 | |
| 346 | rc = "" |
| 347 | s = "1" |
| 348 | while (s): |
| 349 | select.select([read], [], []) |
| 350 | s = os.read(read, 1000) |
| 351 | rc = rc + s |
| 352 | |
| 353 | os.close(read) |
| 354 | |
| 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 |
| 361 | |
| 362 | if os.WIFEXITED(status) and (os.WEXITSTATUS(status) == 0): |
| 363 | status = os.WEXITSTATUS(status) |
| 364 | else: |
| 365 | status = -1 |
| 366 | |
| 367 | return (rc, status) |
| 368 | |
| 369 | if __name__ == "__main__": |
| 370 | main() |