From 66ed3041276ca368968d732a558fad2fcadb66e5 Mon Sep 17 00:00:00 2001 From: Eugeni Dodonov Date: Tue, 29 Sep 2009 13:16:31 -0300 Subject: detecting interface status. updated TODO --- TODO | 6 ++++-- net_monitor.py | 35 ++++++++++++++++++++++++++++++++++- 2 files changed, 38 insertions(+), 3 deletions(-) diff --git a/TODO b/TODO index bc3c7fc..a981d2a 100644 --- a/TODO +++ b/TODO @@ -3,10 +3,12 @@ TODO for net_monitor: - show advanced values (netmask, broadcast, MTU, flags, ...) - implement advanced logging (start and stop counting network transfers, accounting number of packets, bytes, and so on) - - correctly detect when device is active (/sys/class/net/iface/operstate) - highlight interfaces with higher transmission rate - - wireless: show essid and signal level when available (/proc/net/wireless) + - wireless: show essid, signal level and rate when available (/proc/net/wireless) - detect if it is wireless: /sys/class/net/iface/wireless + - detect essid: ioctl SIOCGIWESSID + - detect mode: SIOCGIWMODE + - detect rate: SIOCGIWRATE - network info: show gateway, routes and dns info (/proc/net/routes) - auto-detect new and removed interfaces (ppp0, etc) - advanced fields: diff --git a/net_monitor.py b/net_monitor.py index 16709c6..fc05f2d 100755 --- a/net_monitor.py +++ b/net_monitor.py @@ -31,6 +31,35 @@ except IOError: ifaces = {} HISTOGRAM_SIZE=50 +def get_status(ifname): + try: + fd = open("/sys/class/net/%s/operstate" % ifname) + status = fd.readline().strip() + fd.close() + except: + status="unknown" + if status == "unknown": + # pretty-format interface status + status = _("Unknown") + return status + +def readwireless(): + """Check if device is wireless and get its details if necessary""" + try: + with open("/proc/net/wireless") as fd: + ifaces = fd.readlines()[2:] + for line in ifaces: + line = line.strip() + if not line: + continue + iface, params = line.split(":", 1) + iface = iface.strip() + params = params.replace(".", "").split() + return {} + except: + # something bad happened + traceback.print_exc() + return {} def get_address(ifname): s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM) @@ -327,7 +356,9 @@ class Monitor: def update(self, interval=1): """Updates traffic counters (interval is in seconds)""" net=readnet() + readwireless() for iface in self.ifaces: + status = get_status(iface) old_data_in = self.ifaces[iface]['data_in'] old_data_out = self.ifaces[iface]['data_out'] total_in = self.ifaces[iface]['total_in'] @@ -375,6 +406,7 @@ class Monitor: ('widget_histo_in', histo_in), ('widget_histo_out', histo_out), ('widget_ip_address', ip), + ('widget_status', status), ('widget_hw_address', mac), ]: if widget in self.ifaces[iface]: @@ -471,7 +503,8 @@ class Monitor: # interface iface_h, iface_p = self.build_value_pair(sizegroup, _("Network interface:"), iface) vbox_global.pack_start(iface_h, False, False) - iface_s, iface_status = self.build_value_pair(sizegroup, _("Device status:"), _("Up")) + iface_s, iface_status = self.build_value_pair(sizegroup, _("Device status:")) + self.ifaces[iface]["widget_status"] = iface_status vbox_global.pack_start(iface_s, False, False) iface_addr_s, iface_addr = self.build_value_pair(sizegroup, _("IP Address:")) self.ifaces[iface]["widget_ip_address"] = iface_addr -- cgit v1.2.1