Debian Codegraph @

agents/lindy_pdu/fence_lindypdu.py

raw symbols SCIP
1 #!@PYTHON@ -tt
2
3 # This library is free software; you can redistribute it and/or
4 # modify it under the terms of the GNU Lesser General Public
5 # License as published by the Free Software Foundation; either
6 # version 2.1 of the License, or (at your option) any later version.
7 #
8 # This library is distributed in the hope that it will be useful,
9 # but WITHOUT ANY WARRANTY; without even the implied warranty of
10 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11 # Lesser General Public License for more details.
12 #
13 # You should have received a copy of the GNU Lesser General Public
14 # License along with this library. If not, see
15 # <http://www.gnu.org/licenses/>.
16
17 # The Following agent has been tested on:
18 # Lindy PDU model 32657
19 # Firmware release s4.82-091012-1cb08s
20 # Probably works on different models with same MIB .. but is better test on them
21 #
22 # (C) 2021 Daimonlab -- Damiano Scaramuzza (cesello) [email protected]
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 ### CONSTANTS ###
33 # oid defining fence device
34 OID_SYS_OBJECT_ID = '.1.3.6.1.2.1.1.2.0'
35
36 ### GLOBAL VARIABLES ###
37 # Device - see Lindy PDU
38 device = None
39
40 # Port ID
41 port_id = None
42 # Switch ID
43 switch_id = None
44
45 # Classes describing Device params
46 # Here I follow the MIBS specs that use "switch" and "plug" concepts but
47 # the pdu really have one switch only and 8-16 plugs.
48 # Probably the "switch" term is used for future uses or more advanced pdus
49 class LindyPDU(object):
50 # PDU
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 ### FUNCTIONS ###
64 def lpdu_set_device(conn, options):
65 global device
66
67 agents_dir = {'.1.3.6.1.4.1.17420':LindyPDU}
68
69 # First resolve type of PDU device
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 # Now we resolv port_id/switch_id
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 # status is a comma separated string
118 # one line only as "1,1,1,0,1,1,1,1".
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 # status is a comma separated string
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 #search for all pdu
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 #I really don't know what to do with this case. I haven't a different lindy pdu to test
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 # Main agent method
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 # Operate the fencing device
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()