0

I am trying to create a pysnmp daemon. I want to have the ability to start, stop, and restart the thread that the daemon is running on. I am having trouble cleaning the socket, notification receiver, and transport dispatcher.

I am using a pysnmp v1/2c trap receiver

class trapReceiverThread(threading.Thread):
def __init__(self):
    try:
        trapworking = snmpEngine.transportDispatcher.jobsArePending()
    except:
        trapworking = -1

    if trapworking == 0:
        snmpEngine.transportDispatcher.jobStarted(1)

    elif trapworking == -1:
        print "starting"
        # UDP over IPv4, first listening interface/port
        config.addV1System(snmpEngine, 'my-area', 'public')

        # SecurityName <-> CommunityName mapping
        print "d0"
        config.addSocketTransport(
                                  snmpEngine,
                                  udp.domainName + (1,),
                                  udp.UdpTransport().openServerMode(( 'localhost', 162 ))
                                  )
        ntfrcv.NotificationReceiver(snmpEngine, cbFun)
        snmpEngine.transportDispatcher.jobStarted(1)
    else:
        print "Trap receiver already started."

def run(self):
    try:
        snmpEngine.transportDispatcher.runDispatcher()
    except:
        print "fail"
        snmpEngine.transportDispatcher.closeDispatcher()
        raise

def cbFun(snmpEngine,
          stateReference,
          contextEngineId, contextName,
          varBinds,
          cbCtx):
    transportDomain, transportAddress = snmpEngine.msgAndPduDsp.getTransportInfo(stateReference)
    print('Notification from %s, ContextEngineId "%s", ContextName "%s"' % (
        transportAddress, contextEngineId.prettyPrint(),
        contextName.prettyPrint()
        )
    )
    for obj in varBinds:
         print obj

trapStatus = threading.Thread(target = trapReceiverThread().run)
trapStatus.deamon = True

def start():
    global trapStatus
    if trapStatus.isAlive() == False:
        try:
            trapStatus.start();
        except:
            trapStatus = threading.Thread(target = trapReceiverThread().run)
            trapStatus.start();           

def stop():
    if snmpEngine.transportDispatcher.jobsArePending():
        print "stopping"
        """
        CODE to stop SocketTransport, transportDispatcher, and NotificationReceiver
        """
        snmpEngine.transportDispatcher.jobFinished(1)
        trapStatus.join()

def restart():
    stop()
    start()
KVL
  • 31
  • 1
  • 5
  • It's unclear what exactly happens and what kind of code is involved. The code you have posted looks like just a basic single-threaded, asynchronous TRAP receiver. – Pooh Feb 20 '14 at 21:29
  • Your code is still incomplete. Could you make it runnable as-is? It looks like you use a single snmpEngine instance across all of your threads. If that's really the case, it won't work that way. You need to keep a dedicated snmpEngine instance per each thread you are running. – Pooh Feb 24 '14 at 07:15

1 Answers1

1

Since the trap is defined the the local variable transportDispater, the process can be stopped by finishing job #1 and releasing the port.

transportDispatcher.jobFinished(1)                
transportDispatcher.unregisterRecvCbFun(recvId=None)
transportDispatcher.unregisterTransport(udp.domainName)
Kumar V
  • 8,810
  • 9
  • 39
  • 58
KVL
  • 31
  • 1
  • 5