2020-05-03 11:15:52 +02:00
|
|
|
# pylint: disable=C0111,R0903
|
2020-03-01 14:36:12 +01:00
|
|
|
|
2020-04-02 16:30:31 +02:00
|
|
|
"""Displays the name, IP address(es) and status of each available network interface.
|
2020-03-01 14:36:12 +01:00
|
|
|
|
|
|
|
Requires the following python module:
|
|
|
|
* netifaces
|
|
|
|
|
2020-07-18 08:23:28 +02:00
|
|
|
Requires the following executable:
|
|
|
|
* iw
|
2020-11-13 14:56:31 +01:00
|
|
|
* (until and including 2.0.5: iwgetid)
|
2020-07-18 08:23:28 +02:00
|
|
|
|
2020-03-01 14:36:12 +01:00
|
|
|
Parameters:
|
2020-11-30 17:21:17 +01:00
|
|
|
* nic.exclude: Comma-separated list of interface prefixes (supporting regular expressions) to exclude (defaults to 'lo,virbr,docker,vboxnet,veth,br,.*:avahi')
|
2020-03-01 14:36:12 +01:00
|
|
|
* nic.include: Comma-separated list of interfaces to include
|
|
|
|
* nic.states: Comma-separated list of states to show (prefix with '^' to invert - i.e. ^down -> show all devices that are not in state down)
|
2022-02-12 11:06:10 +01:00
|
|
|
* nic.format: Format string (defaults to '{intf} {state} {ip} {ssid} {strength}')
|
|
|
|
* nic.strength_warning: Integer to set the threshold for warning state (defaults to 50)
|
|
|
|
* nic.strength_critical: Integer to set the threshold for critical state (defaults to 30)
|
2020-04-02 16:30:31 +02:00
|
|
|
"""
|
2020-03-01 14:36:12 +01:00
|
|
|
|
2020-06-30 14:49:43 +02:00
|
|
|
import re
|
2020-03-01 14:36:12 +01:00
|
|
|
import shutil
|
|
|
|
import netifaces
|
|
|
|
import subprocess
|
|
|
|
|
|
|
|
import core.module
|
2020-04-02 16:30:31 +02:00
|
|
|
import core.decorators
|
2020-03-01 14:36:12 +01:00
|
|
|
import util.cli
|
|
|
|
import util.format
|
|
|
|
|
2020-05-03 11:15:52 +02:00
|
|
|
|
2020-03-01 14:36:12 +01:00
|
|
|
class Module(core.module.Module):
|
2022-02-12 11:06:10 +01:00
|
|
|
@core.decorators.every(seconds=5)
|
2020-04-26 16:39:24 +02:00
|
|
|
def __init__(self, config, theme):
|
2020-03-01 14:36:12 +01:00
|
|
|
widgets = []
|
2020-04-26 16:39:24 +02:00
|
|
|
super().__init__(config, theme, widgets)
|
2020-11-30 17:21:17 +01:00
|
|
|
self._exclude = util.format.aslist(
|
|
|
|
self.parameter("exclude", "lo,virbr,docker,vboxnet,veth,br,.*:avahi")
|
2020-05-03 11:15:52 +02:00
|
|
|
)
|
2020-11-30 17:21:17 +01:00
|
|
|
self._include = util.format.aslist(self.parameter("include", ""))
|
2020-05-03 11:15:52 +02:00
|
|
|
|
|
|
|
self._states = {"include": [], "exclude": []}
|
|
|
|
for state in tuple(
|
|
|
|
filter(len, util.format.aslist(self.parameter("states", "")))
|
|
|
|
):
|
|
|
|
if state[0] == "^":
|
|
|
|
self._states["exclude"].append(state[1:])
|
2020-03-01 14:36:12 +01:00
|
|
|
else:
|
2020-05-03 11:15:52 +02:00
|
|
|
self._states["include"].append(state)
|
2022-02-12 11:06:10 +01:00
|
|
|
self._format = self.parameter("format", "{intf} {state} {ip} {ssid} {strength}")
|
|
|
|
|
|
|
|
self._strength_threshold_critical = self.parameter("strength_critical", 30)
|
|
|
|
self._strength_threshold_warning = self.parameter("strength_warning", 50)
|
|
|
|
|
|
|
|
# Limits for the accepted dBm values of wifi strength
|
|
|
|
self.__strength_dbm_lower_bound = -110
|
|
|
|
self.__strength_dbm_upper_bound = -30
|
|
|
|
|
2020-06-30 14:49:43 +02:00
|
|
|
self.iw = shutil.which("iw")
|
2020-03-01 14:36:12 +01:00
|
|
|
self._update_widgets(widgets)
|
|
|
|
|
|
|
|
def update(self):
|
|
|
|
self._update_widgets(self.widgets())
|
|
|
|
|
|
|
|
def state(self, widget):
|
|
|
|
states = []
|
|
|
|
|
2020-05-03 11:15:52 +02:00
|
|
|
if widget.get("state") == "down":
|
|
|
|
states.append("critical")
|
|
|
|
elif widget.get("state") != "up":
|
|
|
|
states.append("warning")
|
2020-03-01 14:36:12 +01:00
|
|
|
|
2020-05-03 11:15:52 +02:00
|
|
|
intf = widget.get("intf")
|
|
|
|
iftype = "wireless" if self._iswlan(intf) else "wired"
|
|
|
|
iftype = "tunnel" if self._istunnel(intf) else iftype
|
2020-03-01 14:36:12 +01:00
|
|
|
|
2022-02-12 11:06:10 +01:00
|
|
|
# "strength" is none if interface type is not wlan
|
|
|
|
if self._iswlan(intf):
|
|
|
|
if widget.get("strength") < self._strength_threshold_critical:
|
|
|
|
states.append("critical")
|
|
|
|
elif widget.get("strength") < self._strength_threshold_warning:
|
|
|
|
states.append("warning")
|
|
|
|
|
2020-05-03 11:15:52 +02:00
|
|
|
states.append("{}-{}".format(iftype, widget.get("state")))
|
2020-03-01 14:36:12 +01:00
|
|
|
|
|
|
|
return states
|
|
|
|
|
|
|
|
def _iswlan(self, intf):
|
|
|
|
# wifi, wlan, wlp, seems to work for me
|
2020-05-03 11:15:52 +02:00
|
|
|
if intf.startswith("w"):
|
|
|
|
return True
|
2020-03-01 14:36:12 +01:00
|
|
|
return False
|
|
|
|
|
|
|
|
def _istunnel(self, intf):
|
2020-05-03 11:15:52 +02:00
|
|
|
return intf.startswith("tun") or intf.startswith("wg")
|
2020-03-01 14:36:12 +01:00
|
|
|
|
|
|
|
def get_addresses(self, intf):
|
|
|
|
retval = []
|
|
|
|
try:
|
|
|
|
for ip in netifaces.ifaddresses(intf).get(netifaces.AF_INET, []):
|
2020-05-03 11:15:52 +02:00
|
|
|
if ip.get("addr", "") != "":
|
|
|
|
retval.append(ip.get("addr"))
|
2020-03-01 14:36:12 +01:00
|
|
|
except Exception:
|
|
|
|
return []
|
|
|
|
return retval
|
|
|
|
|
2020-11-30 17:21:17 +01:00
|
|
|
def _excluded(self, intf):
|
|
|
|
for e in self._exclude:
|
|
|
|
if re.match(e, intf):
|
|
|
|
return True
|
|
|
|
return False
|
|
|
|
|
2020-03-01 14:36:12 +01:00
|
|
|
def _update_widgets(self, widgets):
|
2020-05-09 10:57:44 +02:00
|
|
|
self.clear_widgets()
|
2020-11-30 17:21:17 +01:00
|
|
|
interfaces = []
|
|
|
|
for i in netifaces.interfaces():
|
|
|
|
if not self._excluded(i):
|
|
|
|
interfaces.append(i)
|
2020-03-01 14:36:12 +01:00
|
|
|
interfaces.extend([i for i in netifaces.interfaces() if i in self._include])
|
|
|
|
|
|
|
|
for intf in interfaces:
|
|
|
|
addr = []
|
2020-05-03 11:15:52 +02:00
|
|
|
state = "down"
|
2020-03-01 14:36:12 +01:00
|
|
|
for ip in self.get_addresses(intf):
|
|
|
|
addr.append(ip)
|
2020-05-03 11:15:52 +02:00
|
|
|
state = "up"
|
2020-03-01 14:36:12 +01:00
|
|
|
|
2020-05-03 11:15:52 +02:00
|
|
|
if len(self._states["exclude"]) > 0 and state in self._states["exclude"]:
|
|
|
|
continue
|
|
|
|
if (
|
|
|
|
len(self._states["include"]) > 0
|
|
|
|
and state not in self._states["include"]
|
|
|
|
):
|
|
|
|
continue
|
2020-03-01 14:36:12 +01:00
|
|
|
|
2022-02-12 11:06:10 +01:00
|
|
|
strength_dbm = self.get_strength_dbm(intf)
|
|
|
|
strength_percent = self.convert_strength_dbm_percent(strength_dbm)
|
|
|
|
|
2020-03-01 14:36:12 +01:00
|
|
|
widget = self.widget(intf)
|
|
|
|
if not widget:
|
2020-05-09 10:57:44 +02:00
|
|
|
widget = self.add_widget(name=intf)
|
2020-03-01 14:36:12 +01:00
|
|
|
# join/split is used to get rid of multiple whitespaces (in case SSID is not available, for instance
|
2020-05-03 11:15:52 +02:00
|
|
|
widget.full_text(
|
|
|
|
" ".join(
|
|
|
|
self._format.format(
|
|
|
|
ip=", ".join(addr),
|
|
|
|
intf=intf,
|
|
|
|
state=state,
|
2022-02-12 11:06:10 +01:00
|
|
|
strength=str(strength_percent) + "%" if strength_percent else "",
|
2020-05-03 11:15:52 +02:00
|
|
|
ssid=self.get_ssid(intf),
|
|
|
|
).split()
|
|
|
|
)
|
|
|
|
)
|
|
|
|
widget.set("intf", intf)
|
|
|
|
widget.set("state", state)
|
2022-02-12 11:06:10 +01:00
|
|
|
widget.set("strength", strength_percent)
|
2020-03-01 14:36:12 +01:00
|
|
|
|
|
|
|
def get_ssid(self, intf):
|
2020-11-30 23:21:21 +01:00
|
|
|
if not self._iswlan(intf) or self._istunnel(intf) or not self.iw:
|
|
|
|
return ""
|
|
|
|
|
|
|
|
iw_info = util.cli.execute("{} dev {} info".format(self.iw, intf))
|
|
|
|
for line in iw_info.split("\n"):
|
2020-12-01 15:58:55 +01:00
|
|
|
match = re.match(r"^\s+ssid\s(.+)$", line)
|
2020-11-30 23:21:21 +01:00
|
|
|
if match:
|
|
|
|
return match.group(1)
|
|
|
|
|
2020-05-03 11:15:52 +02:00
|
|
|
return ""
|
|
|
|
|
2022-02-12 11:06:10 +01:00
|
|
|
def get_strength_dbm(self, intf):
|
|
|
|
if not self._iswlan(intf) or self._istunnel(intf) or not self.iw:
|
|
|
|
return None
|
|
|
|
|
|
|
|
with open("/proc/net/wireless", "r") as file:
|
|
|
|
for line in file:
|
|
|
|
if intf in line:
|
|
|
|
# Remove trailing . by slicing it off ;)
|
|
|
|
strength_dbm = line.split()[3][:-1]
|
|
|
|
return util.format.asint(strength_dbm,
|
|
|
|
minium=self.__strength_dbm_lower_bound,
|
|
|
|
maximum=self.__strength_dbm_upper_bound)
|
|
|
|
|
|
|
|
return None
|
|
|
|
|
|
|
|
def convert_strength_dbm_percent(self, signal):
|
|
|
|
return int(100 * ((signal + 100) / 70.0)) if signal else None
|
|
|
|
|
2020-03-01 14:36:12 +01:00
|
|
|
|
|
|
|
# vim: tabstop=8 expandtab shiftwidth=4 softtabstop=4
|