2016-10-31 13:03:16 +01:00
import pyroute2
import netifaces
import bumblebee . module
2016-10-31 16:08:03 +01:00
def usage ( ) :
return " nic "
def notes ( ) :
return " Interfaces starting with ' lo ' or ' virbr ' are ignored. Critical if the status of an interface is ' down ' , Warning if it is anything else but ' up ' . Interface status is derived from whether an IP address is available or not. "
def description ( ) :
return " Displays the names, IP addresses and status of each available interface. "
2016-10-31 13:03:16 +01:00
class Module ( bumblebee . module . Module ) :
2016-11-01 08:09:10 +01:00
def __init__ ( self , output , args ) :
2016-10-31 13:03:16 +01:00
super ( Module , self ) . __init__ ( args )
2016-11-03 19:45:12 +01:00
self . _exclude = ( " lo " , " virbr " , " docker " , " vboxnet " )
2016-10-31 13:03:16 +01:00
self . _interfaces = [ i for i in netifaces . interfaces ( ) if not i . startswith ( self . _exclude ) ]
self . _index = 0
self . _intf = self . _interfaces [ 0 ] if len ( self . _interfaces ) > 0 else None
self . _cache = { }
self . _state = " down "
def data ( self ) :
if len ( self . _interfaces ) < = self . _index :
return " n/a "
self . _intf = self . _interfaces [ self . _index ]
self . _state = " down "
addr = [ ]
2016-10-31 13:34:48 +01:00
try :
if netifaces . AF_INET in netifaces . ifaddresses ( self . _intf ) :
for ip in netifaces . ifaddresses ( self . _intf ) [ netifaces . AF_INET ] :
if " addr " in ip and ip [ " addr " ] != " " :
addr . append ( ip [ " addr " ] )
self . _state = " up "
except Exception as e :
self . _state = " down "
addr = [ ]
2016-10-31 13:03:16 +01:00
return " {} {} {} " . format ( self . _intf , self . _state , " , " . join ( addr ) )
def next ( self ) :
self . _index + = 1
if self . _index < len ( self . _interfaces ) :
return True
self . _index = 0
# reload to support hotplug
self . _interfaces = [ i for i in netifaces . interfaces ( ) if not i . startswith ( self . _exclude ) ]
return False
def _iswlan ( self , intf ) :
if not " wlan {} " . format ( intf ) in self . _cache :
iw = pyroute2 . IW ( )
ip = pyroute2 . IPRoute ( )
idx = ip . link_lookup ( ifname = intf ) [ 0 ]
try :
iw . get_interface_by_ifindex ( idx )
self . _cache [ " wlan {} " . format ( intf ) ] = True
2016-10-31 13:09:52 +01:00
except Exception as e :
2016-10-31 13:03:16 +01:00
self . _cache [ " wlan {} " . format ( intf ) ] = False
2016-10-31 13:09:52 +01:00
return self . _cache [ " wlan {} " . format ( intf ) ]
2016-10-31 13:03:16 +01:00
2016-10-31 13:34:48 +01:00
def _istunnel ( self , intf ) :
return intf . startswith ( " tun " )
2016-11-01 07:46:26 +01:00
def instance ( self ) :
return self . _intf
2016-10-31 13:03:16 +01:00
def state ( self ) :
t = " wireless " if self . _iswlan ( self . _intf ) else " wired "
2016-10-31 13:09:52 +01:00
2016-10-31 13:34:48 +01:00
t = " tunnel " if self . _istunnel ( self . _intf ) else t
2016-10-31 13:03:16 +01:00
return " {} - {} " . format ( t , self . _state )
def warning ( self ) :
return self . _state != " up "
def critical ( self ) :
return self . _state == " down "
# vim: tabstop=8 expandtab shiftwidth=4 softtabstop=4