Merge pull request #2029 from RazCrimson/update-public-ip-at-intervals

Update public ip at intervals
This commit is contained in:
Nicolas Hennion 2022-04-15 15:47:15 +02:00 committed by GitHub
commit 909f893352
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 37 additions and 13 deletions

View File

@ -11,6 +11,17 @@ Additionally, on GNU/Linux, it also shows the kernel version.
In client mode, the server connection status is also displayed.
It is possible to define time interval to be used for refreshing the
public IP address (default is 300 seconds) from the configuration
file under the ``[ip]`` section:
.. code-block:: ini
[ip]
public_refresh_interval=240
**NOTE:** Setting low values will result in frequent HTTP request to
the IP detection servers. Recommended range: 120-600 seconds
**Connected**:
.. image:: ../_static/connected.png

View File

@ -24,7 +24,7 @@ from json import loads
from glances.compat import iterkeys, urlopen, queue
from glances.logger import logger
from glances.timer import Timer
from glances.timer import Timer, getTimeSinceLastUpdate
from glances.plugins.glances_plugin import GlancesPlugin
# Import plugin specific dependency
@ -56,6 +56,8 @@ class Plugin(GlancesPlugin):
stats is a dict
"""
_default_public_refresh_interval = 300
def __init__(self, args=None, config=None):
"""Init the plugin."""
super(Plugin, self).__init__(args=args, config=config)
@ -63,9 +65,11 @@ class Plugin(GlancesPlugin):
# We want to display the stat in the curse interface
self.display_curse = True
# Get the public IP address once (not for each refresh)
if not import_error_tag:
self.public_address = PublicIpAddress().get()
# For public IP address
self.public_address = ""
self.public_address_refresh_interval = self.get_conf_value(
"public_refresh_interval", default=self._default_public_refresh_interval
)
@GlancesPlugin._check_decorator
@GlancesPlugin._log_result_decorator
@ -83,15 +87,24 @@ class Plugin(GlancesPlugin):
default_gw = netifaces.gateways()['default'][netifaces.AF_INET]
except (KeyError, AttributeError) as e:
logger.debug("Cannot grab the default gateway ({})".format(e))
else:
return {}
try:
stats['address'] = netifaces.ifaddresses(default_gw[1])[netifaces.AF_INET][0]['addr']
stats['mask'] = netifaces.ifaddresses(default_gw[1])[netifaces.AF_INET][0]['netmask']
stats['mask_cidr'] = self.ip_to_cidr(stats['mask'])
stats['gateway'] = netifaces.gateways()['default'][netifaces.AF_INET][0]
stats['public_address'] = self.public_address
address = netifaces.ifaddresses(default_gw[1])[netifaces.AF_INET][0]['addr']
mask = netifaces.ifaddresses(default_gw[1])[netifaces.AF_INET][0]['netmask']
time_since_update = getTimeSinceLastUpdate('public-ip')
if self.stats.get('address') != address or time_since_update > self.public_address_refresh_interval:
self.public_address = PublicIpAddress().get()
except (KeyError, AttributeError) as e:
logger.debug("Cannot grab IP information: {}".format(e))
else:
stats['address'] = address
stats['mask'] = mask
stats['mask_cidr'] = self.ip_to_cidr(stats['mask'])
stats['gateway'] = default_gw[0]
stats['public_address'] = self.public_address
elif self.input_method == 'snmp':
# Not implemented yet
pass
@ -138,7 +151,7 @@ class Plugin(GlancesPlugin):
# Add KeyError exception (see https://github.com/nicolargo/glances/issues/1469)
pass
else:
if self.stats['public_address'] is not None:
if self.stats['public_address']:
msg = ' Pub '
ret.append(self.curse_add_line(msg, 'TITLE'))
ret.append(self.curse_add_line(msg_pub))