network_traffic: allow to hide down interfaces
Interfaces which are down can be hidden or formatted differently. The following options are added: hide_down - whether to not display a interface which is down format_down - format string if the interface is down (unless hide_down is set)
This commit is contained in:
parent
fbd44cd4dd
commit
9591de4461
@ -21,12 +21,16 @@ class NetworkTraffic(IntervalModule):
|
||||
interval = 1
|
||||
settings = (
|
||||
("format", "format string"),
|
||||
("format_down", "format string if the interface is down (unless hide_down is set)"),
|
||||
("hide_down", "whether to not display a interface which is down"),
|
||||
("interface", "network interface"),
|
||||
("divisor", "divide all byte values by this value"),
|
||||
("round_size", "defines number of digits in round"),
|
||||
)
|
||||
|
||||
format = "{interface} \u2197{bytes_sent}kB/s \u2198{bytes_recv}kB/s"
|
||||
format_down = "{interface} \u2013"
|
||||
hide_down = True
|
||||
interface = "eth0"
|
||||
divisor = 1024
|
||||
round_size = None
|
||||
@ -36,7 +40,8 @@ class NetworkTraffic(IntervalModule):
|
||||
|
||||
def update_counters(self):
|
||||
self.pnic_before = self.pnic
|
||||
self.pnic = psutil.net_io_counters(pernic=True)[self.interface]
|
||||
counters = psutil.net_io_counters(pernic=True)
|
||||
self.pnic = counters[self.interface] if self.interface in counters else None
|
||||
|
||||
def get_bytes_sent(self):
|
||||
return (self.pnic.bytes_sent - self.pnic_before.bytes_sent) / self.divisor
|
||||
@ -50,8 +55,18 @@ class NetworkTraffic(IntervalModule):
|
||||
def get_packets_received(self):
|
||||
return self.pnic.packets_recv - self.pnic_before.packets_recv
|
||||
|
||||
def sysfs_interface_up(self):
|
||||
try:
|
||||
sysfs = "/sys/class/net/{}/operstate".format(self.interface)
|
||||
with open(sysfs) as operstate:
|
||||
status = operstate.read().strip()
|
||||
return status == "up" or status == "unknown"
|
||||
except FileNotFoundError:
|
||||
return False
|
||||
|
||||
def run(self):
|
||||
self.update_counters()
|
||||
if self.sysfs_interface_up():
|
||||
if not self.pnic_before:
|
||||
return
|
||||
cdict = {
|
||||
@ -66,3 +81,14 @@ class NetworkTraffic(IntervalModule):
|
||||
"full_text": self.format.format(**cdict),
|
||||
"instance": self.interface,
|
||||
}
|
||||
elif self.hide_down:
|
||||
self.output = None
|
||||
return
|
||||
else:
|
||||
cdict = {
|
||||
"interface": self.interface,
|
||||
}
|
||||
self.output = {
|
||||
"full_text": self.format_down.format(**cdict),
|
||||
"instance": self.interface,
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user