How to Monitor Link state change, Routing table change, IP address changes events using pyroute2

162 views
Skip to first unread message

daren...@gmail.com

unread,
Mar 9, 2018, 4:17:07 PM3/9/18
to pyroute2-dev
Hi,

   I would like to monitor the 1. interface Link state events 2. Routing Table 3. IP address change events using IPDB pyroute2 functionality.

   How to register a call back function to monitor the events continuously (or polling) and print it on the console  when some thing happens in kernel.

   I am looking for your help to provide pseudo code for the above.


Regards
Daren

daren...@gmail.com

unread,
Mar 10, 2018, 12:45:18 PM3/10/18
to pyroute2-dev
Hi,

  I have tried this sample program to monitor net link events continuously and  endup with the errors as callback function is not callable. ipdb.register_callback('cb', mode='post')
  Could some one help me, how to fix this issue.

#!/usr/bin/python

import pyroute2

# Create Instance of IPDB
ipdb = pyroute2.IPDB()
ipdb.register_callback('cb', mode='post')

action = 'RTM_NEWLINK'

def cb(ipdb, msg, action):
  index = msg['index']
  interface = ipdb.interfaces[index]
  print interface


while(True):
  pass

Peter Saveliev

unread,
Mar 10, 2018, 1:42:56 PM3/10/18
to daren...@gmail.com, pyroute2-dev
The first argument must be not a string, but the function pointer:


#!/usr/bin/python

import pyroute2

# Create Instance of IPDB
with pyroute2.IPDB() as ipdb:

    action = 'RTM_NEWLINK'
    def cb(ipdb, msg, action):
        print(msg)

    ipdb.register_callback(cb, mode='post')

    while(True):
        pass


--
You received this message because you are subscribed to the Google Groups "pyroute2-dev" group.
To unsubscribe from this group and stop receiving emails from it, send an email to pyroute2-dev+unsubscribe@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

daren k

unread,
Mar 10, 2018, 3:32:05 PM3/10/18
to Peter Saveliev, pyroute2-dev
Peter,

  Bunch of thanks to your help. But i could see the exceptions while printing or fetching the interface index field. using  print(msg['index']).
index = msg['index']                    >>>>>>>>>>>>>>>>>>  Error.. Eventough msg field has index field, it is giving some expections.
interface = ipdb.interfaces[index]
Could you please help me to resolve this issue.



#!/usr/bin/python

import pyroute2

# Create Instance of IPDB
with pyroute2.IPDB() as ipdb:

    action = 'RTM_NEWLINK'
    def cb(ipdb, msg, action):
        print(msg['index'])

    ipdb.register_callback(cb, mode='post')

    while(True):
        pass

Exceptions are shown below:
4
Exception in thread IPDB callback 139948844673216:
Traceback (most recent call last):
  File "/usr/lib/python2.7/threading.py", line 801, in __bootstrap_inner
    self.run()
  File "/usr/lib/python2.7/threading.py", line 754, in run
    self.__target(*self.__args, **self.__kwargs)
  File "/usr/local/lib/python2.7/dist-packages/pyroute2/ipdb/main.py", line 968, in safe
    callback(*argv, **kwarg)
  File "peter.py", line 12, in cb
    print(msg['index'])
KeyError: 'index'

Exception in thread IPDB callback 139948844673216:
Traceback (most recent call last):
  File "/usr/lib/python2.7/threading.py", line 801, in __bootstrap_inner
    self.run()
  File "/usr/lib/python2.7/threading.py", line 754, in run
    self.__target(*self.__args, **self.__kwargs)
  File "/usr/local/lib/python2.7/dist-packages/pyroute2/ipdb/main.py", line 968, in safe
    callback(*argv, **kwarg)
  File "peter.py", line 12, in cb
    print(msg['index'])
KeyError: 'index'

2
Exception in thread IPDB callback 139948844673216:
Traceback (most recent call last):
  File "/usr/lib/python2.7/threading.py", line 801, in __bootstrap_inner
    self.run()
  File "/usr/lib/python2.7/threading.py", line 754, in run
    self.__target(*self.__args, **self.__kwargs)
  File "/usr/local/lib/python2.7/dist-packages/pyroute2/ipdb/main.py", line 968, in safe
    callback(*argv, **kwarg)
  File "peter.py", line 12, in cb
    print(msg['index'])
KeyError: 'index'

Exception in thread IPDB callback 139948844673216:
Traceback (most recent call last):
  File "/usr/lib/python2.7/threading.py", line 801, in __bootstrap_inner
    self.run()
  File "/usr/lib/python2.7/threading.py", line 754, in run
    self.__target(*self.__args, **self.__kwargs)
  File "/usr/local/lib/python2.7/dist-packages/pyroute2/ipdb/main.py", line 968, in safe
    callback(*argv, **kwarg)
  File "peter.py", line 12, in cb
    print(msg['index'])
KeyError: 'index'

Exception in thread IPDB callback 139948844673216:
Traceback (most recent call last):
  File "/usr/lib/python2.7/threading.py", line 801, in __bootstrap_inner
    self.run()
  File "/usr/lib/python2.7/threading.py", line 754, in run
    self.__target(*self.__args, **self.__kwargs)
  File "/usr/local/lib/python2.7/dist-packages/pyroute2/ipdb/main.py", line 968, in safe
    callback(*argv, **kwarg)
  File "peter.py", line 12, in cb
    print(msg['index'])
KeyError: 'index'

4

daren k

unread,
Mar 10, 2018, 4:28:15 PM3/10/18
to Peter Saveliev, pyroute2-dev
Hi Peter,

  This issue seems to be multiple events are coming from kernel. For some of the kernel events, there is no index parameter in msg. 
  Once the code is modified as below, the issue is resolved.

#!/usr/bin/python

import pyroute2

# Create Instance of IPDB
with pyroute2.IPDB() as ipdb:

    action = 'RTM_NEWLINK'
    def cb(ipdb, msg, action):
        if 'index' in msg:
          index = msg['index']
          interface = ipdb.interfaces[index]
          print interface.ifname


    ipdb.register_callback(cb, mode='post')

    while(True):
        pass



Peter Saveliev

unread,
Mar 11, 2018, 12:19:44 PM3/11/18
to daren k, pyroute2-dev
Sure. 'Cause not all the messages contain the «index» field. That's not an error, that's how different messages are assembled.

You may filter messages by the type, the interface messages always should have the index — but in some rare cases it's not defined (0)
Reply all
Reply to author
Forward
0 new messages