Merge branch 'develop' into issue1121

This commit is contained in:
nicolargo 2024-11-08 12:35:41 +01:00
commit 0f641cb9eb
6 changed files with 71 additions and 41 deletions

View File

@ -579,7 +579,7 @@ disable=False
# Configuration for the --export graph option
# Set the path where the graph (.svg files) will be created
# Can be overwrite by the --graph-path command line option
path=/tmp
path=/tmp/glances
# It is possible to generate the graphs automatically by setting the
# generate_every to a non zero value corresponding to the seconds between
# two generation. Set it to 0 to disable graph auto generation.

View File

@ -562,7 +562,7 @@ disable=False
# Configuration for the --export graph option
# Set the path where the graph (.svg files) will be created
# Can be overwrite by the --graph-path command line option
path=/tmp
path=/tmp/glances
# It is possible to generate the graphs automatically by setting the
# generate_every to a non zero value corresponding to the seconds between
# two generation. Set it to 0 to disable graph auto generation.

View File

@ -33,7 +33,7 @@ class Export(GlancesExport):
self.export_enable = self.load_conf('graph', options=['path', 'generate_every', 'width', 'height', 'style'])
# Manage options (command line arguments overwrite configuration file)
self.path = args.export_graph_path or self.path
self.path = self.path or args.export_graph_path
self.generate_every = int(getattr(self, 'generate_every', 0) or 0)
self.width = int(getattr(self, 'width', 800) or 800)
self.height = int(getattr(self, 'height', 600) or 600)

View File

@ -190,7 +190,6 @@ class PluginModel(GlancesPluginModel):
return ret
# Header
logger.info(self.stats)
if self.stats['net_connections_enabled'] or self.stats['nf_conntrack_enabled']:
msg = '{}'.format('TCP CONNECTIONS')
ret.append(self.curse_add_line(msg, "TITLE"))

View File

@ -8,6 +8,9 @@
"""NVidia Extension unit for Glances' GPU plugin."""
import os
import sys
from glances.globals import nativestr
from glances.logger import logger
@ -17,7 +20,13 @@ try:
# Avoid importing pynvml if NVML_LIB is not installed
from ctypes import CDLL
CDLL(NVML_LIB)
if (sys.platform[:3] == "win"):
try:
CDLL(os.path.join(os.getenv("WINDIR", "C:/Windows"), "System32/nvml.dll"))
except OSError:
CDLL(os.path.join(os.getenv("ProgramFiles", "C:/Program Files"), "NVIDIA Corporation/NVSMI/nvml.dll"))
else:
CDLL(NVML_LIB)
import pynvml
except OSError:
nvidia_gpu_enable = False

View File

@ -85,6 +85,51 @@ class PluginModel(GlancesPluginModel):
"public_refresh_interval", default=self._default_public_refresh_interval
)
def get_private_ip(self, stats, stop=False):
# Get the default gateway thanks to the netifaces lib
try:
default_gw = netifaces.gateways()['default'][netifaces.AF_INET]
except (KeyError, AttributeError) as e:
logger.debug(f"Cannot grab default gateway IP address ({e})")
stop = True
else:
stats['gateway'] = default_gw[0]
return (stop, stats)
def get_first_ip(self, stats, stop=False):
try:
default_gw = netifaces.gateways()['default'][netifaces.AF_INET]
address = netifaces.ifaddresses(default_gw[1])[netifaces.AF_INET][0]['addr']
mask = netifaces.ifaddresses(default_gw[1])[netifaces.AF_INET][0]['netmask']
except (KeyError, AttributeError) as e:
logger.debug(f"Cannot grab private IP address ({e})")
stop = True
else:
stats['address'] = address
stats['mask'] = mask
stats['mask_cidr'] = self.ip_to_cidr(stats['mask'])
return (stop, stats)
def get_public_ip(self, stats, stop=True):
time_since_update = getTimeSinceLastUpdate('public-ip')
try:
if not self.public_disabled and (
self.public_address == "" or time_since_update > self.public_address_refresh_interval
):
self.public_info = PublicIpInfo(self.public_api, self.public_username, self.public_password).get()
self.public_address = self.public_info['ip']
except (KeyError, AttributeError, TypeError) as e:
logger.debug(f"Cannot grab public IP information ({e})")
else:
stats['public_address'] = (
self.public_address if not self.args.hide_public_info else self.__hide_ip(self.public_address)
)
stats['public_info_human'] = self.public_info_for_human(self.public_info)
return (stop, stats)
@GlancesPluginModel._check_decorator
@GlancesPluginModel._log_result_decorator
def update(self):
@ -96,42 +141,7 @@ class PluginModel(GlancesPluginModel):
stats = self.get_init_value()
if self.input_method == 'local' and not import_error_tag:
# Private IP address
# Get the default gateway thanks to the netifaces lib
try:
default_gw = netifaces.gateways()['default'][netifaces.AF_INET]
except (KeyError, AttributeError) as e:
logger.debug(f"Cannot grab default gateway IP address ({e})")
return self.get_init_value()
else:
stats['gateway'] = default_gw[0]
# If multiple IP addresses are available, only the one with the default gateway is returned
try:
address = netifaces.ifaddresses(default_gw[1])[netifaces.AF_INET][0]['addr']
mask = netifaces.ifaddresses(default_gw[1])[netifaces.AF_INET][0]['netmask']
except (KeyError, AttributeError) as e:
logger.debug(f"Cannot grab private IP address ({e})")
return self.get_init_value()
else:
stats['address'] = address
stats['mask'] = mask
stats['mask_cidr'] = self.ip_to_cidr(stats['mask'])
# Public IP address
time_since_update = getTimeSinceLastUpdate('public-ip')
try:
if not self.public_disabled and (
self.public_address == "" or time_since_update > self.public_address_refresh_interval
):
self.public_info = PublicIpInfo(self.public_api, self.public_username, self.public_password).get()
self.public_address = self.public_info['ip']
except (KeyError, AttributeError, TypeError) as e:
logger.debug(f"Cannot grab public IP information ({e})")
else:
stats['public_address'] = (
self.public_address if not self.args.hide_public_info else self.__hide_ip(self.public_address)
)
stats['public_info_human'] = self.public_info_for_human(self.public_info)
stats = self.get_stats_for_local_input(stats)
elif self.input_method == 'snmp':
# Not implemented yet
@ -142,6 +152,18 @@ class PluginModel(GlancesPluginModel):
return self.stats
def get_stats_for_local_input(self, stats):
# Private IP address
stop, stats = self.get_private_ip(stats)
# If multiple IP addresses are available, only the one with the default gateway is returned
if not stop:
stop, stats = self.get_first_ip(stats)
# Public IP address
if not stop:
stop, stats = self.get_public_ip(stats)
return stats
def __hide_ip(self, ip):
"""Hide last to digit of the given IP address"""
return '.'.join(ip.split('.')[0:2]) + '.*.*'