Work at SourceForge, help us to make it a better place! We have an immediate need for a Support Technician in our San Francisco or Denver office.

Close

Transport already registered?

Anonymous
2013-01-08
2013-05-20

  • Anonymous
    2013-01-08

    When attempting to send packet I receive the following error:

    Traceback (most recent call last):
      File "./check_tps.py", line 62, in <module>
        udp.domainName, udp.UdpSocketTransport().openClientMode()
      File "/usr/local/lib/python2.7/site-packages/pysnmp/carrier/asynsock/dispatch.py", line 18, in registerTransport
        AbstractTransportDispatcher.registerTransport(self, tDomain, t)
      File "/usr/local/lib/python2.7/site-packages/pysnmp/carrier/base.py", line 77, in registerTransport
        'Transport %s already registered' % (tDomain,)
    pysnmp.carrier.error.CarrierError: Transport (1, 3, 6, 1, 6, 1, 1) already registered
    

    I'm confused as to what this means - is. This machine already has Net-SNMP running on it (just the agent). Is this caused by pySNMP attempting to send from Port 161 which is already occupied?

    Here's my Trap PDU:

    trapPDU = pMod.TrapPDU()
    pMod.apiTrapPDU.setDefaults(trapPDU)
    pMod.apiTrapPDU.setEnterprise(trapPDU, (1,3,6,1,4,1,36577,1,2,1,0))
    

    Cheers,
    Lukasz

     
  • That error means that you have already configured UDP transport target in your script but later  on tried to configure it again.

    That error has nothing to do with Net-SNMP daemon running on the same machine.

    If you'd show your script it would be easier to pinpoint an error.

    Have you looked into pysnmp examples at http://pysnmp.sourceforge.net?

     

  • Anonymous
    2013-01-09

    Hmm alright. I copied the SNMPv1 trap sending code almost directly from the PySNMP website. Here's the script:

     1
     2
     3
     4
     5
     6
     7
     8
     9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    #!/usr/local/bin/python
    import urllib2
    import json
    import sched
    import time
    from datetime import datetime
    from pysnmp.carrier.asynsock.dispatch import AsynsockDispatcher
    from pysnmp.carrier.asynsock.dgram import udp
    from pyasn1.codec.ber import encoder
    from pysnmp.proto import api
    #Declare variables
    last = 0
    max_tps = 120 #Specifies maximum permitted TPS
    alert_tps = 100 #Specifies warning treshold
    loop = 1
    community = "callmetraps" #Specifies SNMP community string
    destination = "172.16.37.37" #Specifies SNMP destination
    #Set protocol verison
    pMod = api.protoModules[api.protoVersion1]
    #Build PDU
    trapPDU = pMod.TrapPDU()
    pMod.apiTrapPDU.setDefaults(trapPDU)
    pMod.apiTrapPDU.setEnterprise(trapPDU, (1,3,6,1,4,1,36577,1,2,1,0))
    #Build message
    trap_message = pMod.Message()
    pMod.apiMessage.setDefaults(trap_message)
    pMod.apiMessage.setCommunity(trap_message, community)
    pMod.apiMessage.setPDU(trap_message, trapPDU)
    #Create dispatcher
    trap_sender = AsynsockDispatcher()
    #Create infinite loop to pool the system
    while loop == 1:
            #Grab start time for the loop
            start_time = datetime.now()
            #Grab and parse total sent messages from the management interface
            query = urllib2.urlopen("http://localhost:8000/management/SmsCounters").read()
            parsed = json.loads(query)
            current = parsed[u'total']
            #First check if this is the first time the program is run
            if last == 0:
                    last = current
            else:
                    #Check TPS
                    tps = current - last
                    last = current
                    #Get current time for the message
                    current_time = datetime.now()
                    current_time = str(current_time)
                    if tps >= alert_tps:
                            if tps >= max_tps:
                                    #Critical TPS (above max_tps). Send trap and print message.
                                    print current_time + " CRIT, " + str(tps)
                                    #Register transport
                                    trap_sender.registerTransport(udp.domainName, udp.UdpSocketTransport().openClientMode())
                                    #Send message
                                    trap_sender.sendMessage(encoder.encode(trap_message), udp.domainName, (destination, 162))
                            else:
                                    #Alert TPS. Print message.
                                    print current_time + " WARN, " + str(tps)
                    else:
                            #Normal TPS. Print message.
                            print current_time + " NORM, " + str(tps)
            #Calculate end time for loop
            end_time = datetime.now()
            query_time = end_time - start_time
            query_time = str(query_time)
            query_time = query_time[6:]
            query_time = float(query_time)
            #Sleep until loop is ready for next iteration (approx every one second).
            time.sleep(1 - query_time - 0.0005)
    #Close dispatcher
    trap_sender.closeDispatcher()
    

    I am by no means a Python expert - barely learning as it is.

     

  • Anonymous
    2013-01-09

    Ok I got this figured out. registerTransport is in the while loop so it gets run multiple times. Thanks that tip put me on the right track. Now to open another question - how to send SNMPv2 traps with custom OID and custom VarBinds…

     
  • BTW, you are using the low-level API to pysnmp. Unless you have strong reasons for it, I'd use the "oneliner" version which is much more compact and thus easier to use.