From 73b383155bca355d9a216b60cb445dfc091ccc69 Mon Sep 17 00:00:00 2001 From: nicolargo Date: Sat, 18 Jun 2016 18:49:25 +0200 Subject: [PATCH] Add default gateway option --- conf/glances.conf | 26 +++++++++++--------------- glances/plugins/glances_ports.py | 15 ++++++++++----- glances/ports_list.py | 29 +++++++++++++++++++++++++++++ 3 files changed, 50 insertions(+), 20 deletions(-) diff --git a/conf/glances.conf b/conf/glances.conf index 68e71c70..97b3688e 100644 --- a/conf/glances.conf +++ b/conf/glances.conf @@ -185,23 +185,19 @@ mem_critical=90 refresh=30 # Set the default timeout for a scan (can be overwrite in the scan list) timeout=3 +# If True, add the default gateway on top of the port list +port_default_gateway=True # Define the scan list # host (name or IP) is mandatory -# port is optional (set to 0 = ICMP check by default) -# description is optional (set to host:port) -port_1_host=www.google.com -port_1_port=443 -port_1_description=Internet Web -port_2_host=www.google.com -port_2_description=Internet Ping -port_3_host=www.free.fr -port_3_description=Free ISP -port_4_host=192.168.0.1 -port_4_port=80 -port_5_description=Home Box -port_5_host=192.168.5.1 -port_5_port=80 -port_5_timeout=2 +# port (TCP port number) is optional (if not set, use ICMP) +# description is optional (if not set, define to host:port) +port_1_host=192.168.0.1 +port_1_port=80 +port_1_description=Home Box +port_2_host=www.free.fr +port_2_description=My ISP +port_3_host=www.google.com +port_3_description=Internet ############################################################################## # Client/server diff --git a/glances/plugins/glances_ports.py b/glances/plugins/glances_ports.py index a9a233d0..1c577d8f 100644 --- a/glances/plugins/glances_ports.py +++ b/glances/plugins/glances_ports.py @@ -141,12 +141,17 @@ class Plugin(GlancesPlugin): fnull = open(os.devnull, 'w') counter = Counter() - ret = subprocess.check_call(cmd, stdout=fnull, stderr=fnull, close_fds=True) - if ret == 0: - # Ping return RTT. - port['status'] = counter.get() / 2.0 + try: + ret = subprocess.check_call(cmd, stdout=fnull, stderr=fnull, close_fds=True) + except Exception as e: + logger.debug("{0}: Error while pinging host ({2})".format(self.plugin_name, port['host'], e)) + return 1 else: - port['status'] = False + if ret == 0: + # Ping return RTT. + port['status'] = counter.get() / 2.0 + else: + port['status'] = False return ret diff --git a/glances/ports_list.py b/glances/ports_list.py index c5783105..8d17743c 100644 --- a/glances/ports_list.py +++ b/glances/ports_list.py @@ -21,6 +21,18 @@ from glances.compat import range from glances.logger import logger +from glances.globals import BSD + +# XXX *BSDs: Segmentation fault (core dumped) +# -- https://bitbucket.org/al45tair/netifaces/issues/15 +if not BSD: + try: + import netifaces + netifaces_tag = True + except ImportError: + netifaces_tag = False +else: + netifaces_tag = False class GlancesPortsList(object): @@ -47,8 +59,25 @@ class GlancesPortsList(object): logger.warning("No [%s] section in the configuration file. Cannot load ports list." % self._section) else: logger.debug("Start reading the [%s] section in the configuration file" % self._section) + refresh = config.get_value(self._section, 'refresh', default=self._default_refresh) timeout = config.get_value(self._section, 'timeout', default=self._default_timeout) + + # Add default gateway on top of the ports_list lits + default_gateway = config.get_value(self._section, 'port_default_gateway', default='False') + if default_gateway.lower().startswith('true') and netifaces_tag: + new_port = {} + new_port['host'] = netifaces.gateways()['default'][netifaces.AF_INET][0] + # ICMP + new_port['port'] = 0 + new_port['description'] = 'DefaultGateway' + new_port['refresh'] = refresh + new_port['timeout'] = timeout + new_port['status'] = None + logger.debug("Add default gateway %s to the static list" % (new_port['host'])) + ports_list.append(new_port) + + # Read the scan list for i in range(1, 256): new_port = {} postfix = 'port_%s_' % str(i)