| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
import sys |
| 25 |
import atexit |
| 26 |
import logging |
| 27 |
sys.path.append("@FENCEAGENTSLIBDIR@") |
| 28 |
from fencing import * |
| 29 |
from fencing import fail_usage |
| 30 |
from fencing_snmp import FencingSnmp |
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
OID_SYS_OBJECT_ID = '.1.3.6.1.2.1.1.2.0' |
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
device = None |
| 39 |
|
| 40 |
|
| 41 |
port_id = None |
| 42 |
|
| 43 |
switch_id = None |
| 44 |
|
| 45 |
|
| 46 |
|
| 47 |
|
| 48 |
|
| 49 |
class LindyPDU(object): |
| 50 |
|
| 51 |
status_oid = '.1.3.6.1.4.1.17420.1.2.9.%d.13.0' |
| 52 |
control_oid = '.1.3.6.1.4.1.17420.1.2.9.%d.13.0' |
| 53 |
outlet_table_oid = '.1.3.6.1.4.1.17420.1.2.9.%d.14' |
| 54 |
pdu_table_oid = '.1.3.6.1.4.1.17420.1.2.9' |
| 55 |
attached_pdus = '.1.3.6.1.4.1.17420.1.2.5.0' |
| 56 |
ident_str = "Lindy 32657 PDU" |
| 57 |
state_on = 1 |
| 58 |
state_off = 0 |
| 59 |
turn_on = 1 |
| 60 |
turn_off = 0 |
| 61 |
has_switches = True |
| 62 |
|
| 63 |
|
| 64 |
def lpdu_set_device(conn, options): |
| 65 |
global device |
| 66 |
|
| 67 |
agents_dir = {'.1.3.6.1.4.1.17420':LindyPDU} |
| 68 |
|
| 69 |
|
| 70 |
pdu_type = conn.walk(OID_SYS_OBJECT_ID) |
| 71 |
|
| 72 |
if not ((len(pdu_type) == 1) and (pdu_type[0][1] in agents_dir)): |
| 73 |
pdu_type = [[None, None]] |
| 74 |
|
| 75 |
device = agents_dir[pdu_type[0][1]] |
| 76 |
|
| 77 |
logging.debug("Trying %s"%(device.ident_str)) |
| 78 |
|
| 79 |
def lpdu_resolv_port_id(conn, options): |
| 80 |
|
| 81 |
if device == None: |
| 82 |
lpdu_set_device(conn, options) |
| 83 |
|
| 84 |
port_id=switch_id=None |
| 85 |
|
| 86 |
if options["--plug"].isdigit() and ((not device.has_switches) or (options["--switch"].isdigit())): |
| 87 |
port_id = int(options["--plug"]) |
| 88 |
|
| 89 |
if device.has_switches: |
| 90 |
switch_id = int(options["--switch"]) |
| 91 |
else: |
| 92 |
table = conn.walk(device.pdu_table_oid, 30) |
| 93 |
|
| 94 |
for x in table: |
| 95 |
if x[1].strip('"').split(',')[0] == options["--plug"]: |
| 96 |
t = x[0].split('.') |
| 97 |
if device.has_switches: |
| 98 |
port_id = int(t[len(t)-1]) |
| 99 |
switch_id = int(t[len(t)-3]) |
| 100 |
else: |
| 101 |
port_id = int(t[len(t)-1]) |
| 102 |
|
| 103 |
if port_id == None: |
| 104 |
fail_usage("Can't find port with name %s!"%(options["--plug"])) |
| 105 |
|
| 106 |
return (switch_id,port_id) |
| 107 |
|
| 108 |
def get_power_status(conn, options): |
| 109 |
|
| 110 |
(switch_id,port_id)=lpdu_resolv_port_id(conn, options) |
| 111 |
|
| 112 |
oid = ((device.has_switches) and device.status_oid%(switch_id) or device.status_oid%(port_id)) |
| 113 |
|
| 114 |
|
| 115 |
try: |
| 116 |
(oid, status) = conn.get(oid) |
| 117 |
|
| 118 |
|
| 119 |
state=status.strip('"').split(',')[port_id-1] |
| 120 |
if state == str(device.state_on): |
| 121 |
return "on" |
| 122 |
elif state == str(device.state_off): |
| 123 |
return "off" |
| 124 |
else: |
| 125 |
return None |
| 126 |
except Exception: |
| 127 |
return None |
| 128 |
|
| 129 |
def set_power_status(conn, options): |
| 130 |
|
| 131 |
(switch_id,port_id)=lpdu_resolv_port_id(conn, options) |
| 132 |
|
| 133 |
oid = ((device.has_switches) and device.control_oid%(switch_id) or device.control_oid%(port_id)) |
| 134 |
|
| 135 |
(oid, status) = conn.get(oid) |
| 136 |
|
| 137 |
state=status.strip('"').split(',') |
| 138 |
state[port_id-1]=str((options["--action"] == "on" and device.turn_on or device.turn_off)) |
| 139 |
conn.set(oid, ",".join(state)) |
| 140 |
|
| 141 |
|
| 142 |
def get_outlets_status(conn, options): |
| 143 |
result = {} |
| 144 |
pdu_id=[] |
| 145 |
|
| 146 |
if device == None: |
| 147 |
lpdu_set_device(conn, options) |
| 148 |
|
| 149 |
if (device.has_switches and options["--switch"].isdigit()): |
| 150 |
pdu_id.append(options["--switch"]) |
| 151 |
|
| 152 |
elif (device.has_switches): |
| 153 |
|
| 154 |
pdus=conn.walk(device.attached_pdus, 30) |
| 155 |
pdus_info=pdus[0][1].strip('"').split(',') |
| 156 |
pdu_id=pdus_info[1:] |
| 157 |
else: |
| 158 |
|
| 159 |
table_oid=device.pdu_table_oid |
| 160 |
|
| 161 |
|
| 162 |
for switch in pdu_id: |
| 163 |
table_oid = device.outlet_table_oid % int(switch) |
| 164 |
res_ports = conn.walk(table_oid, 30) |
| 165 |
status_oid=device.status_oid % int(switch) |
| 166 |
port_status=conn.walk(status_oid, 30) |
| 167 |
state=port_status[0][1].strip('"').split(',') |
| 168 |
for x in res_ports: |
| 169 |
t = x[0].split('.') |
| 170 |
port_num = ((device.has_switches) and "%s:%s"%(t[len(t)-4], t[len(t)-2]) or "%s"%(t[len(t)-2])) |
| 171 |
port_name = x[1].strip('"').split(',')[0] |
| 172 |
result[port_num] = (port_name, "on" if state[int(t[len(t)-2])-1]=='1' else "off") |
| 173 |
|
| 174 |
return result |
| 175 |
|
| 176 |
|
| 177 |
def main(): |
| 178 |
global device |
| 179 |
|
| 180 |
device_opt = ["ipaddr", "login", "passwd", "no_login", "no_password", \ |
| 181 |
"port", "snmp_version", "snmp","switch"] |
| 182 |
|
| 183 |
atexit.register(atexit_handler) |
| 184 |
|
| 185 |
all_opt["snmp_version"]["default"] = "1" |
| 186 |
all_opt["community"]["default"] = "public" |
| 187 |
all_opt["switch"]["default"] = "1" |
| 188 |
device = LindyPDU |
| 189 |
|
| 190 |
options = check_input(device_opt, process_input(device_opt)) |
| 191 |
|
| 192 |
docs = {} |
| 193 |
docs["shortdesc"] = "Fence agent for Lindy over SNMP" |
| 194 |
docs["longdesc"] = "fence_lindypdu is a Power Fencing agent \ |
| 195 |
which can be used with the Lindy PDU network power switch. It logs \ |
| 196 |
into a device via SNMP and reboots a specified outlet. It supports \ |
| 197 |
SNMP v1 with all combinations of authenticity/privacy settings." |
| 198 |
docs["vendorurl"] = "http://www.lindy.co.uk" |
| 199 |
show_docs(options, docs) |
| 200 |
|
| 201 |
|
| 202 |
result = fence_action(FencingSnmp(options), options, set_power_status, get_power_status, get_outlets_status) |
| 203 |
|
| 204 |
sys.exit(result) |
| 205 |
if __name__ == "__main__": |
| 206 |
main() |