Added unknown_up parameter. Closes #103.

This commit is contained in:
Alexis Lahouze 2014-08-24 23:58:17 +02:00
parent 1dbbc01cc6
commit f95624d801

View File

@ -59,13 +59,13 @@ def get_bonded_slaves():
return slaves return slaves
def sysfs_interface_up(interface): def sysfs_interface_up(interface, unknown_up = False):
try: try:
with open("/sys/class/net/{}/operstate".format(interface)) as f: with open("/sys/class/net/{}/operstate".format(interface)) as f:
status = f.read().strip() status = f.read().strip()
except FileNotFoundError: except FileNotFoundError:
raise RuntimeError("Unknown interface {iface}!".format(iface=interface)) raise RuntimeError("Unknown interface {iface}!".format(iface=interface))
return status == "up" return status == "up" or unknown_up and status == "unknown"
class Network(IntervalModule): class Network(IntervalModule):
@ -94,6 +94,7 @@ class Network(IntervalModule):
"format_up", "color_up", "format_up", "color_up",
"format_down", "color_down", "format_down", "color_down",
("detached_down", "If the interface doesn't exist, display it as if it were down"), ("detached_down", "If the interface doesn't exist, display it as if it were down"),
("unknown_up", "If the interface is in unknown state, display it as if it were up"),
"name", "name",
) )
@ -103,6 +104,7 @@ class Network(IntervalModule):
color_up = "#00FF00" color_up = "#00FF00"
color_down = "#FF0000" color_down = "#FF0000"
detached_down = True detached_down = True
unknown_up = False
def init(self): def init(self):
if self.interface not in netifaces.interfaces() and not self.detached_down: if self.interface not in netifaces.interfaces() and not self.detached_down:
@ -122,14 +124,14 @@ class Network(IntervalModule):
except KeyError: except KeyError:
pass pass
else: else:
if sysfs_interface_up(self.interface): if sysfs_interface_up(self.interface, self.unknown_up):
master_info = netifaces.ifaddresses(master) master_info = netifaces.ifaddresses(master)
for af in (netifaces.AF_INET, netifaces.AF_INET6): for af in (netifaces.AF_INET, netifaces.AF_INET6):
try: try:
info[af] = master_info[af] info[af] = master_info[af]
except KeyError: except KeyError:
pass pass
up = sysfs_interface_up(self.interface) up = sysfs_interface_up(self.interface, self.unknown_up)
fdict = dict( fdict = dict(
zip_longest(["v4", "v4mask", "v4cidr", "v6", "v6mask", "v6cidr"], [], fillvalue="")) zip_longest(["v4", "v4mask", "v4cidr", "v6", "v6mask", "v6cidr"], [], fillvalue=""))