From c316843c0e91634b9e89f6281964351ccda073bd Mon Sep 17 00:00:00 2001 From: Aaron Griffith Date: Sat, 22 Aug 2020 20:45:21 -0400 Subject: [PATCH] node, fqdn, and custom choices for host tag (Issue #16) --- octoprint_influxdb/__init__.py | 50 +++++++++++++++++-- .../templates/influxdb_settings.jinja2 | 22 ++++++++ 2 files changed, 68 insertions(+), 4 deletions(-) diff --git a/octoprint_influxdb/__init__.py b/octoprint_influxdb/__init__.py index 2e990ac..df1bc91 100644 --- a/octoprint_influxdb/__init__.py +++ b/octoprint_influxdb/__init__.py @@ -2,6 +2,7 @@ from __future__ import absolute_import import platform +import socket import datetime import sys @@ -19,6 +20,11 @@ if sys.version_info < (3, 0): ALLOWED_TYPES = (unicode,) + ALLOWED_TYPES +# host methods +HOST_NODE = "node" +HOST_FQDN = "fqdn" +HOST_CUSTOM = "custom" + def __plugin_load__(): global __plugin_implementation__ __plugin_implementation__ = InfluxDBPlugin() @@ -41,9 +47,31 @@ def __init__(self): self.influx_db = None self.influx_last_reconnect = None self.influx_kwargs = None - self.influx_common_tags = { - 'host': platform.node(), - } + + @property + def influx_common_tags(self): + return dict( + host=self.influx_host_from_method( + self._settings.get(['hostmethod'])), + ) + + def influx_host_from_method(self, method): + if method == HOST_NODE: + return platform.node() + elif method == HOST_FQDN: + try: + return socket.getaddrinfo( + socket.gethostname(), + 0, 0, 0, 0, + socket.AI_CANONNAME, + )[0][3] + except Exception: + return socket.fqdn() + elif method == HOST_CUSTOM: + return self._settings.get(['hostcustom']) + else: + # reasonable fallback + return platform.node() def influx_flash_exception(self, message): self._logger.exception(message) @@ -288,6 +316,8 @@ def get_settings_defaults(self): verify_ssl=True, database='octoprint', prefix='', + hostmethod=HOST_NODE, + hostcustom='octoprint', username=None, password=None, @@ -308,8 +338,9 @@ def on_settings_migrate(self, target, current): raise RuntimeError("could not migrate InfluxDB settings") def on_settings_save(self, data): - octoprint.plugin.SettingsPlugin.on_settings_save(self, data) + r = octoprint.plugin.SettingsPlugin.on_settings_save(self, data) self.influx_reconnect(True) + return r ##~~ StartupPlugin mixin @@ -323,6 +354,17 @@ def get_template_configs(self): dict(type="settings", custom_bindings=False), ] + def get_template_vars(self): + return dict( + host_node=HOST_NODE, + host_node_s=self.influx_host_from_method(HOST_NODE), + + host_fqdn=HOST_FQDN, + host_fqdn_s=self.influx_host_from_method(HOST_FQDN), + + host_custom=HOST_CUSTOM, + ) + ##~~ Softwareupdate hook def get_update_information(self): diff --git a/octoprint_influxdb/templates/influxdb_settings.jinja2 b/octoprint_influxdb/templates/influxdb_settings.jinja2 index ce48b44..5866040 100644 --- a/octoprint_influxdb/templates/influxdb_settings.jinja2 +++ b/octoprint_influxdb/templates/influxdb_settings.jinja2 @@ -42,6 +42,28 @@ {{ _('Measurement names will be prefixed by this string. Helpful when sharing a database.') }} + + +
+ + + + + {{ _('Measurements will be tagged with this value for %(host)s.', host='host') }} + +