Show
Ignore:
Timestamp:
10/11/09 08:41:33 (14 years ago)
Author:
jerome
Message:

Removed all support for pysnmp v3.x
Applied the patch from Ilya Etingof and Börje Sennung to fix #47.

Files:
1 modified

Legend:

Unmodified
Added
Removed
  • pykota/trunk/bin/pkturnkey

    r3490 r3506  
    163163            from pysnmp.entity.rfc3413.oneliner import cmdgen 
    164164        except ImportError : 
    165             hasV4 = False 
    166             try : 
    167                 from pysnmp.asn1.encoding.ber.error import TypeMismatchError 
    168                 from pysnmp.mapping.udp.role import Manager 
    169                 from pysnmp.proto.api import alpha 
    170             except ImportError : 
    171                 logerr("pysnmp doesn't seem to be installed. SNMP checks will be ignored !\n") 
    172                 return False 
    173         else : 
    174             hasV4 = True 
    175  
    176         if hasV4 : 
    177             def retrieveSNMPValues(hostname, community) : 
    178                 """Retrieves a printer's internal page counter and status via SNMP.""" 
    179                 errorIndication, errorStatus, errorIndex, varBinds = \ 
    180                      cmdgen.CommandGenerator().getCmd(cmdgen.CommunityData("pykota", community, 0), \ 
    181                                                       cmdgen.UdpTransportTarget((hostname, 161)), \ 
    182                                                       tuple([int(i) for i in pageCounterOID.split('.')])) 
    183                 if errorIndication : 
    184                     raise "No SNMP !" 
    185                 elif errorStatus : 
    186                     raise "No SNMP !" 
    187                 else : 
    188                     self.SNMPOK = True 
    189         else : 
    190             def retrieveSNMPValues(hostname, community) : 
    191                 """Retrieves a printer's internal page counter and status via SNMP.""" 
    192                 ver = alpha.protoVersions[alpha.protoVersionId1] 
    193                 req = ver.Message() 
    194                 req.apiAlphaSetCommunity(community) 
    195                 req.apiAlphaSetPdu(ver.GetRequestPdu()) 
    196                 req.apiAlphaGetPdu().apiAlphaSetVarBindList((pageCounterOID, ver.Null())) 
    197                 tsp = Manager() 
    198                 try : 
    199                     tsp.sendAndReceive(req.berEncode(), \ 
    200                                        (hostname, 161), \ 
    201                                        (handleAnswer, req)) 
    202                 except : 
    203                     raise "No SNMP !" 
    204                 tsp.close() 
    205  
    206             def handleAnswer(wholemsg, notusedhere, req): 
    207                 """Decodes and handles the SNMP answer.""" 
    208                 ver = alpha.protoVersions[alpha.protoVersionId1] 
    209                 rsp = ver.Message() 
    210                 try : 
    211                     rsp.berDecode(wholemsg) 
    212                 except TypeMismatchError, msg : 
    213                     raise "No SNMP !" 
    214                 else : 
    215                     if req.apiAlphaMatch(rsp): 
    216                         errorStatus = rsp.apiAlphaGetPdu().apiAlphaGetErrorStatus() 
    217                         if errorStatus: 
    218                             raise "No SNMP !" 
    219                         else: 
    220                             self.values = [] 
    221                             for varBind in rsp.apiAlphaGetPdu().apiAlphaGetVarBindList(): 
    222                                 self.values.append(varBind.apiAlphaGetOidVal()[1].rawAsn1Value) 
    223                             try : 
    224                                 pagecounter = self.values[0] 
    225                             except : 
    226                                 raise "No SNMP !" 
    227                             else : 
    228                                 self.SNMPOK = True 
    229                                 return True 
    230  
    231         self.SNMPOK = False 
     165            logerr("pysnmp doesn't seem to be installed. PyKota needs pysnmp v4.x, otherwise SNMP checks will be ignored !\n") 
     166            return False 
    232167        try : 
    233             retrieveSNMPValues(hostname, community) 
     168            errorIndication, errorStatus, errorIndex, varBinds = \ 
     169                cmdgen.CommandGenerator().getCmd(cmdgen.CommunityData("pykota", community, 0), \ 
     170                                                     cmdgen.UdpTransportTarget((hostname, 161)), \ 
     171                                                     tuple([int(i) for i in pageCounterOID.split('.')])) 
     172            if errorIndication : 
     173                raise "No SNMP !" 
     174            elif errorStatus : 
     175                raise "No SNMP !" 
     176            else : 
     177                self.SNMPOK = True 
    234178        except : 
    235179            self.SNMPOK = False