From 5f52c03f44eb8477a4add3c44f8787fd7fc58e2a Mon Sep 17 00:00:00 2001 From: nicolargo Date: Sun, 28 Jan 2018 21:52:30 +0100 Subject: [PATCH] Display debug message if dep lib is not found #1224 --- NEWS | 1 + glances/amps_list.py | 4 ++-- glances/plugins/glances_cloud.py | 25 ++++++++++++++----------- glances/plugins/glances_docker.py | 13 ++++++------- glances/plugins/glances_fs.py | 1 - glances/plugins/glances_gpu.py | 10 +++++----- glances/plugins/glances_ip.py | 20 ++++++++------------ glances/plugins/glances_ports.py | 13 +++++++------ glances/plugins/glances_quicklook.py | 10 +++++----- glances/plugins/glances_raid.py | 12 +++++++++--- glances/plugins/glances_wifi.py | 12 ++++++------ 11 files changed, 63 insertions(+), 58 deletions(-) diff --git a/NEWS b/NEWS index 731d4971..6c215a9c 100644 --- a/NEWS +++ b/NEWS @@ -19,6 +19,7 @@ Enhancements and new features: * Context switches bottleneck identification #1212 * Take advantage of the PSUtil issue #1025 (Add process_iter(attrs, ad_value)) #1105 * Nice Process Priority Configuration #1218 + * Display debug message if dep lib is not found #1224 Bugs corrected: diff --git a/glances/amps_list.py b/glances/amps_list.py index 3669da66..3e1152be 100644 --- a/glances/amps_list.py +++ b/glances/amps_list.py @@ -74,9 +74,9 @@ class AmpsList(object): try: amp = __import__(os.path.basename(amp_script)[:-3]) except ImportError as e: - logger.warning("Cannot load {}, you need to install an external Python package ({})".format(os.path.basename(amp_script), e)) + logger.warning("Missing Python Lib ({}), cannot load {} AMP".format(e, amp_conf_name)) except Exception as e: - logger.warning("Cannot load {} ({})".format(os.path.basename(amp_script), e)) + logger.warning("Cannot load {} AMP ({})".format(amp_conf_name, e)) else: # Add the AMP to the dictionary # The key is the AMP name diff --git a/glances/plugins/glances_cloud.py b/glances/plugins/glances_cloud.py index 71eb8b34..72679e98 100644 --- a/glances/plugins/glances_cloud.py +++ b/glances/plugins/glances_cloud.py @@ -23,19 +23,22 @@ Supported Cloud API: - AWS EC2 (class ThreadAwsEc2Grabber, see bellow) """ -try: - import requests -except ImportError: - cloud_tag = False -else: - cloud_tag = True - import threading from glances.compat import iteritems, to_ascii from glances.plugins.glances_plugin import GlancesPlugin from glances.logger import logger +# Import plugin specific dependency +try: + import requests +except ImportError as e: + import_error_tag = True + # Display debu message if import KeyError + logger.warning("Missing Python Lib ({}), Cloud plugin is disable".format(e)) +else: + import_error_tag = False + class Plugin(GlancesPlugin): """Glances' cloud plugin. @@ -85,16 +88,17 @@ class Plugin(GlancesPlugin): self.reset() # Requests lib is needed to get stats from the Cloud API - if not cloud_tag: + if import_error_tag: return self.stats # Update the stats if self.input_method == 'local': - self.stats = self.aws_ec2.stats + # Example: # self.stats = {'ami-id': 'ami-id', # 'instance-id': 'instance-id', # 'instance-type': 'instance-type', # 'region': 'placement/availability-zone'} + self.stats = self.aws_ec2.stats return self.stats @@ -149,8 +153,7 @@ class ThreadAwsEc2Grabber(threading.Thread): Infinite loop, should be stopped by calling the stop() method """ - if not cloud_tag: - logger.debug("cloud plugin - Requests lib is not installed") + if import_error_tag: self.stop() return False diff --git a/glances/plugins/glances_docker.py b/glances/plugins/glances_docker.py index b05dc646..ffbb3113 100644 --- a/glances/plugins/glances_docker.py +++ b/glances/plugins/glances_docker.py @@ -33,10 +33,11 @@ from glances.plugins.glances_plugin import GlancesPlugin try: import docker except ImportError as e: - logger.debug("Docker library not found (%s). Glances cannot grab Docker info." % e) - docker_tag = False + import_error_tag = True + # Display debu message if import KeyError + logger.warning("Missing Python Lib ({}), Docker plugin is disable".format(e)) else: - docker_tag = True + import_error_tag = False class Plugin(GlancesPlugin): @@ -92,9 +93,7 @@ class Plugin(GlancesPlugin): def connect(self): """Connect to the Docker server.""" - global docker_tag - - if not docker_tag: + if import_error_tag: return None return docker.from_env() @@ -124,7 +123,7 @@ class Plugin(GlancesPlugin): self.reset() # The Docker-py lib is mandatory - if not docker_tag: + if import_error_tag: return self.stats if self.input_method == 'local': diff --git a/glances/plugins/glances_fs.py b/glances/plugins/glances_fs.py index df572c10..0ac7dca7 100644 --- a/glances/plugins/glances_fs.py +++ b/glances/plugins/glances_fs.py @@ -26,7 +26,6 @@ from glances.plugins.glances_plugin import GlancesPlugin import psutil - # SNMP OID # The snmpd.conf needs to be edited. # Add the following to enable it on all disk diff --git a/glances/plugins/glances_gpu.py b/glances/plugins/glances_gpu.py index 33518f28..800f4abf 100644 --- a/glances/plugins/glances_gpu.py +++ b/glances/plugins/glances_gpu.py @@ -26,11 +26,11 @@ from glances.plugins.glances_plugin import GlancesPlugin try: import pynvml except Exception as e: - logger.error("Could not import pynvml. NVIDIA stats will not be collected.") - logger.debug("pynvml error: {}".format(e)) - gpu_nvidia_tag = False + import_error_tag = True + # Display debu message if import KeyError + logger.warning("Missing Python Lib ({}), Nvidia GPU plugin is disable".format(e)) else: - gpu_nvidia_tag = True + import_error_tag = False class Plugin(GlancesPlugin): @@ -58,7 +58,7 @@ class Plugin(GlancesPlugin): def init_nvidia(self): """Init the NVIDIA API.""" - if not gpu_nvidia_tag: + if import_error_tag: self.nvml_ready = False try: diff --git a/glances/plugins/glances_ip.py b/glances/plugins/glances_ip.py index bf05661a..b487f92f 100644 --- a/glances/plugins/glances_ip.py +++ b/glances/plugins/glances_ip.py @@ -23,22 +23,18 @@ import threading from json import loads from glances.compat import iterkeys, urlopen, queue -from glances.globals import BSD from glances.logger import logger from glances.timer import Timer from glances.plugins.glances_plugin import GlancesPlugin -# XXX *BSDs: Segmentation fault (core dumped) -# -- https://bitbucket.org/al45tair/netifaces/issues/15 -# Also used in the ports_list script -if not BSD: - try: - import netifaces - netifaces_tag = True - except ImportError: - netifaces_tag = False +# Import plugin specific dependency +try: + import netifaces +except ImportError as e: + import_error_tag = True + logger.warning("Missing Python Lib ({}), IP plugin is disable".format(e)) else: - netifaces_tag = False + import_error_tag = False # List of online services to retreive public IP address # List of tuple (url, json, key) @@ -85,7 +81,7 @@ class Plugin(GlancesPlugin): # Reset stats self.reset() - if self.input_method == 'local' and netifaces_tag: + if self.input_method == 'local' and not import_error_tag: # Update stats using the netifaces lib try: default_gw = netifaces.gateways()['default'][netifaces.AF_INET] diff --git a/glances/plugins/glances_ports.py b/glances/plugins/glances_ports.py index 2fb7341e..de00f159 100644 --- a/glances/plugins/glances_ports.py +++ b/glances/plugins/glances_ports.py @@ -26,12 +26,6 @@ import socket import time import numbers -try: - import requests - requests_tag = True -except ImportError: - requests_tag = False - from glances.globals import WINDOWS, MACOS, BSD from glances.ports_list import GlancesPortsList from glances.web_list import GlancesWebList @@ -40,6 +34,13 @@ from glances.compat import bool_type from glances.logger import logger from glances.plugins.glances_plugin import GlancesPlugin +try: + import requests + requests_tag = True +except ImportError as e: + requests_tag = False + logger.warning("Missing Python Lib ({}), Ports plugin is limited to port scanning".format(e)) + class Plugin(GlancesPlugin): """Glances ports scanner plugin.""" diff --git a/glances/plugins/glances_quicklook.py b/glances/plugins/glances_quicklook.py index 46b346e6..3eb0d337 100644 --- a/glances/plugins/glances_quicklook.py +++ b/glances/plugins/glances_quicklook.py @@ -20,18 +20,18 @@ """Quicklook plugin.""" from glances.cpu_percent import cpu_percent +from glances.logger import logger from glances.outputs.glances_bars import Bar from glances.plugins.glances_plugin import GlancesPlugin import psutil -cpuinfo_tag = False +# Import plugin specific dependency try: from cpuinfo import cpuinfo -except ImportError: - # Correct issue #754 - # Waiting for a correction on the upstream Cpuinfo lib - pass +except ImportError as e: + cpuinfo_tag = False + logger.warning("Missing Python Lib ({}), Quicklook plugin will not display CPU info".format(e)) else: cpuinfo_tag = True diff --git a/glances/plugins/glances_raid.py b/glances/plugins/glances_raid.py index c0069b19..092bcd9a 100644 --- a/glances/plugins/glances_raid.py +++ b/glances/plugins/glances_raid.py @@ -23,11 +23,14 @@ from glances.compat import iterkeys from glances.logger import logger from glances.plugins.glances_plugin import GlancesPlugin -# pymdstat only available on GNU/Linux OS +# Import plugin specific dependency try: from pymdstat import MdStat -except ImportError: - logger.debug("pymdstat library not found. Glances cannot grab RAID info.") +except ImportError as e: + import_error_tag = True + logger.warning("Missing Python Lib ({}), Raid plugin is disable".format(e)) +else: + import_error_tag = False class Plugin(GlancesPlugin): @@ -57,6 +60,9 @@ class Plugin(GlancesPlugin): # Reset stats self.reset() + if import_error_tag: + return self.stats + if self.input_method == 'local': # Update stats using the PyMDstat lib (https://github.com/nicolargo/pymdstat) try: diff --git a/glances/plugins/glances_wifi.py b/glances/plugins/glances_wifi.py index 76cc240c..2d0238b0 100644 --- a/glances/plugins/glances_wifi.py +++ b/glances/plugins/glances_wifi.py @@ -31,11 +31,11 @@ import psutil try: from wifi.scan import Cell from wifi.exceptions import InterfaceError -except ImportError: - logger.debug("Wifi library not found. Glances cannot grab Wifi info.") - wifi_tag = False +except ImportError as e: + import_error_tag = True + logger.warning("Missing Python Lib ({}), Wifi plugin is disable".format(e)) else: - wifi_tag = True + import_error_tag = False class Plugin(GlancesPlugin): @@ -81,7 +81,7 @@ class Plugin(GlancesPlugin): self.reset() # Exist if we can not grab the stats - if not wifi_tag: + if import_error_tag: return self.stats if self.input_method == 'local': @@ -167,7 +167,7 @@ class Plugin(GlancesPlugin): ret = [] # Only process if stats exist and display plugin enable... - if not self.stats or not wifi_tag or self.is_disable(): + if not self.stats or import_error_tag or self.is_disable(): return ret # Max size for the interface name