228 | | sys.stderr.write("pysnmp doesn't seem to be installed. SNMP checks will be ignored !\n") |
229 | | return 0 |
230 | | |
231 | | pageCounterOID = ".1.3.6.1.2.1.43.10.2.1.4.1.1" # SNMPv2-SMI::mib-2.43.10.2.1.4.1.1 |
232 | | def retrieveSNMPValues(hostname, community) : |
233 | | """Retrieves a printer's internal page counter and status via SNMP.""" |
234 | | ver = alpha.protoVersions[alpha.protoVersionId1] |
235 | | req = ver.Message() |
236 | | req.apiAlphaSetCommunity(community) |
237 | | req.apiAlphaSetPdu(ver.GetRequestPdu()) |
238 | | req.apiAlphaGetPdu().apiAlphaSetVarBindList((pageCounterOID, ver.Null())) |
239 | | tsp = Manager() |
| 227 | hasV4 = False |
241 | | tsp.sendAndReceive(req.berEncode(), \ |
242 | | (hostname, 161), \ |
243 | | (handleAnswer, req)) |
244 | | except : |
245 | | raise "No SNMP !" |
246 | | tsp.close() |
247 | | |
248 | | def handleAnswer(wholemsg, notusedhere, req): |
249 | | """Decodes and handles the SNMP answer.""" |
250 | | ver = alpha.protoVersions[alpha.protoVersionId1] |
251 | | rsp = ver.Message() |
252 | | try : |
253 | | rsp.berDecode(wholemsg) |
254 | | except TypeMismatchError, msg : |
255 | | raise "No SNMP !" |
256 | | else : |
257 | | if req.apiAlphaMatch(rsp): |
258 | | errorStatus = rsp.apiAlphaGetPdu().apiAlphaGetErrorStatus() |
259 | | if errorStatus: |
260 | | raise "No SNMP !" |
261 | | else: |
262 | | self.values = [] |
263 | | for varBind in rsp.apiAlphaGetPdu().apiAlphaGetVarBindList(): |
264 | | self.values.append(varBind.apiAlphaGetOidVal()[1].rawAsn1Value) |
265 | | try : |
266 | | pagecounter = self.values[0] |
267 | | except : |
| 229 | from pysnmp.asn1.encoding.ber.error import TypeMismatchError |
| 230 | from pysnmp.mapping.udp.role import Manager |
| 231 | from pysnmp.proto.api import alpha |
| 232 | except ImportError : |
| 233 | sys.stderr.write("pysnmp doesn't seem to be installed. SNMP checks will be ignored !\n") |
| 234 | return 0 |
| 235 | else : |
| 236 | hasV4 = True |
| 237 | |
| 238 | if hasV4 : |
| 239 | def retrieveSNMPValues(hostname, community) : |
| 240 | """Retrieves a printer's internal page counter and status via SNMP.""" |
| 241 | errorIndication, errorStatus, errorIndex, varBinds = \ |
| 242 | cmdgen.CommandGenerator().getCmd(cmdgen.CommunityData("pykota", community, 0), \ |
| 243 | cmdgen.UdpTransportTarget((hostname, 161)), \ |
| 244 | tuple([int(i) for i in pageCounterOID.split('.')])) |
| 245 | if errorIndication : |
| 246 | raise "No SNMP !" |
| 247 | elif errorStatus : |
| 248 | raise "No SNMP !" |
| 249 | else : |
| 250 | self.SNMPOK = True |
| 251 | else : |
| 252 | def retrieveSNMPValues(hostname, community) : |
| 253 | """Retrieves a printer's internal page counter and status via SNMP.""" |
| 254 | ver = alpha.protoVersions[alpha.protoVersionId1] |
| 255 | req = ver.Message() |
| 256 | req.apiAlphaSetCommunity(community) |
| 257 | req.apiAlphaSetPdu(ver.GetRequestPdu()) |
| 258 | req.apiAlphaGetPdu().apiAlphaSetVarBindList((pageCounterOID, ver.Null())) |
| 259 | tsp = Manager() |
| 260 | try : |
| 261 | tsp.sendAndReceive(req.berEncode(), \ |
| 262 | (hostname, 161), \ |
| 263 | (handleAnswer, req)) |
| 264 | except : |
| 265 | raise "No SNMP !" |
| 266 | tsp.close() |
| 267 | |
| 268 | def handleAnswer(wholemsg, notusedhere, req): |
| 269 | """Decodes and handles the SNMP answer.""" |
| 270 | ver = alpha.protoVersions[alpha.protoVersionId1] |
| 271 | rsp = ver.Message() |
| 272 | try : |
| 273 | rsp.berDecode(wholemsg) |
| 274 | except TypeMismatchError, msg : |
| 275 | raise "No SNMP !" |
| 276 | else : |
| 277 | if req.apiAlphaMatch(rsp): |
| 278 | errorStatus = rsp.apiAlphaGetPdu().apiAlphaGetErrorStatus() |
| 279 | if errorStatus: |