206 | | s.connect((hostname, socket.getservbyname("snmp", "udp"))) |
207 | | for i in range(2) : |
208 | | s.send("") |
209 | | except : |
210 | | pass |
211 | | else : |
212 | | snmpsupport = 1 |
213 | | s.close() |
214 | | return snmpsupport |
| 202 | from pysnmp.asn1.encoding.ber.error import TypeMismatchError |
| 203 | from pysnmp.mapping.udp.error import SnmpOverUdpError |
| 204 | from pysnmp.mapping.udp.role import Manager |
| 205 | from pysnmp.proto.api import alpha |
| 206 | except ImportError : |
| 207 | sys.stderr.write("pysnmp doesn't seem to be installed. SNMP checks will be ignored !\n") |
| 208 | return 0 |
| 209 | |
| 210 | 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 |
| 211 | def retrieveSNMPValues(hostname, community) : |
| 212 | """Retrieves a printer's internal page counter and status via SNMP.""" |
| 213 | ver = alpha.protoVersions[alpha.protoVersionId1] |
| 214 | req = ver.Message() |
| 215 | req.apiAlphaSetCommunity(community) |
| 216 | req.apiAlphaSetPdu(ver.GetRequestPdu()) |
| 217 | req.apiAlphaGetPdu().apiAlphaSetVarBindList((pageCounterOID, ver.Null())) |
| 218 | tsp = Manager() |
| 219 | try : |
| 220 | tsp.sendAndReceive(req.berEncode(), \ |
| 221 | (hostname, 161), \ |
| 222 | (handleAnswer, req)) |
| 223 | except : |
| 224 | raise "No SNMP !" |
| 225 | tsp.close() |
| 226 | |
| 227 | def handleAnswer(wholeMsg, notusedhere, req): |
| 228 | """Decodes and handles the SNMP answer.""" |
| 229 | ver = alpha.protoVersions[alpha.protoVersionId1] |
| 230 | rsp = ver.Message() |
| 231 | try : |
| 232 | rsp.berDecode(wholeMsg) |
| 233 | except TypeMismatchError, msg : |
| 234 | raise "No SNMP !" |
| 235 | else : |
| 236 | if req.apiAlphaMatch(rsp): |
| 237 | errorStatus = rsp.apiAlphaGetPdu().apiAlphaGetErrorStatus() |
| 238 | if errorStatus: |
| 239 | raise "No SNMP !" |
| 240 | else: |
| 241 | self.values = [] |
| 242 | for varBind in rsp.apiAlphaGetPdu().apiAlphaGetVarBindList(): |
| 243 | self.values.append(varBind.apiAlphaGetOidVal()[1].rawAsn1Value) |
| 244 | try : |
| 245 | pagecounter = self.values[0] |
| 246 | except : |
| 247 | raise "No SNMP !" |
| 248 | else : |
| 249 | self.SNMPOK = 1 |
| 250 | return 1 |
| 251 | |
| 252 | self.SNMPOK = 0 |
| 253 | try : |
| 254 | retrieveSNMPValues(hostname, community) |
| 255 | except : |
| 256 | self.SNMPOK = 0 |
| 257 | return self.SNMPOK |