Show
Ignore:
Timestamp:
05/03/06 19:19:53 (18 years ago)
Author:
jerome
Message:

Added support for pysnmp v4.x in addition to v3.4.x

Files:
1 modified

Legend:

Unmodified
Added
Removed
  • pykota/trunk/bin/pkturnkey

    r2831 r2877  
    221221    def supportsSNMP(self, hostname, community) : 
    222222        """Returns 1 if the printer accepts SNMP queries, else 0.""" 
     223        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 
    223224        try : 
    224             from pysnmp.asn1.encoding.ber.error import TypeMismatchError 
    225             from pysnmp.mapping.udp.role import Manager 
    226             from pysnmp.proto.api import alpha 
     225            from pysnmp.entity.rfc3413.oneliner import cmdgen 
    227226        except ImportError :     
    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 
    240228            try : 
    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: 
    268280                            raise "No SNMP !" 
    269                         else :     
    270                             self.SNMPOK = 1 
    271                             return 1 
     281                        else: 
     282                            self.values = [] 
     283                            for varBind in rsp.apiAlphaGetPdu().apiAlphaGetVarBindList(): 
     284                                self.values.append(varBind.apiAlphaGetOidVal()[1].rawAsn1Value) 
     285                            try :     
     286                                pagecounter = self.values[0] 
     287                            except : 
     288                                raise "No SNMP !" 
     289                            else :     
     290                                self.SNMPOK = 1 
     291                                return 1 
    272292             
    273293        self.SNMPOK = 0