From 098c8b043777ea8118217e9d96308590c2d32a5c Mon Sep 17 00:00:00 2001 From: Eugeni Dodonov Date: Wed, 30 Sep 2009 09:59:50 -0300 Subject: displaying global system info (gw, dns, ...). reduced code duplication --- src/monitor.py | 49 ++++++++++++++++++++++++++++++++++++++++++++++--- src/net_monitor | 26 ++++++++++++++++++++++++++ 2 files changed, 72 insertions(+), 3 deletions(-) (limited to 'src') diff --git a/src/monitor.py b/src/monitor.py index b2eebd9..cef0771 100644 --- a/src/monitor.py +++ b/src/monitor.py @@ -121,16 +121,16 @@ class Monitor: return os.access("/sys/class/net/%s/wireless" % iface, os.R_OK) def get_address(self, ifname): - s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM) mac=_("No physical address") # ip address try: - addr=socket.inet_ntoa(fcntl.ioctl( s.fileno(), 0x8915, struct.pack('256s', ifname[:15]))[20:24]) + res = self.ioctl(0x8915, struct.pack('256s', ifname[:15]))[20:24] + addr=socket.inet_ntoa(res) except: addr=_("No address assigned") # mac address try: - mac_struct=fcntl.ioctl( s.fileno(), 0x8927, struct.pack('256s', ifname[:15]))[18:24] + mac_struct=self.ioctl(0x8927, struct.pack('256s', ifname[:15]))[18:24] mac=":".join(["%02x" % ord(char) for char in mac_struct]) except: addr=_("No address assigned") @@ -165,3 +165,46 @@ class Monitor: """Pretty-Formats size""" return size + def get_dns(self): + """Returns list of DNS servers""" + servers = [] + try: + with open("/etc/resolv.conf") as fd: + data = fd.readlines() + for l in data: + l = l.strip() + if not l: + continue + fields = l.split() + if fields[0] == 'nameserver': + servers.append(fields[1]) + except: + traceback.print_exc() + return servers + + def get_routes(self): + """Read network routes""" + routes = [] + default_routes = [] + try: + with open("/proc/net/route") as fd: + data = fd.readlines()[1:] + for l in data: + l = l.strip() + if not l: + continue + params = l.split() + iface = params[0] + dst = int(params[1], 16) + gw = int(params[2], 16) + gw_str = socket.inet_ntoa(struct.pack("L", gw)) + metric = int(params[6], 16) + mask = int(params[7], 16) + routes.append((iface, dst, mask, gw, metric)) + if dst == 0 and mask == 0: + default_routes.append((gw_str, iface)) + except: + traceback.print_exc() + pass + return routes, default_routes + diff --git a/src/net_monitor b/src/net_monitor index 42524e7..9ec4b92 100755 --- a/src/net_monitor +++ b/src/net_monitor @@ -296,16 +296,42 @@ class MonitorGui: if self.check_network_accounting(iface): self.enabled_ifaces.append(iface) + # global statusbar + self.statusbar = gtk.Statusbar() + self.context_id = self.statusbar.get_context_id("Statusbar") + self.main_vbox.pack_start(self.statusbar, False, False, padding=1) + + # add initial message + self.statusbar.push(self.context_id, _("Please wait..")) + # configure timer gobject.timeout_add(1000, self.update) self.window.show_all() + def update_tray_info(self): + """Updates tray information""" + dns_servers = self.monitor.get_dns() + if dns_servers: + dns_message = ", ".join(dns_servers) + else: + dns_message = _("Not found") + # routes + routes, default_routes = self.monitor.get_routes() + if default_routes: + gw_message = ", ".join(["%s (%s)" % (gw, iface) for gw, iface in default_routes]) + else: + gw_message = _("Not found") + tray_message = _("Default routes: %s; DNS: %s") % (gw_message, dns_message) + self.statusbar.pop(self.context_id) + self.statusbar.push(self.context_id, tray_message) + def update(self, interval=1): """Updates traffic counters (interval is in seconds)""" # TODO: move it to Monitor()? net=self.monitor.readnet() wifi_stats = self.monitor.wireless_stats() + self.update_tray_info() for iface in self.ifaces: status = self.monitor.get_status(iface) old_data_in = self.ifaces[iface]['data_in'] -- cgit v1.2.1