Skip to content

Commit

Permalink
node, fqdn, and custom choices for host tag (Issue #16)
Browse files Browse the repository at this point in the history
  • Loading branch information
agrif committed Aug 23, 2020
1 parent 591ac23 commit c316843
Show file tree
Hide file tree
Showing 2 changed files with 68 additions and 4 deletions.
50 changes: 46 additions & 4 deletions octoprint_influxdb/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
from __future__ import absolute_import

import platform
import socket
import datetime
import sys

Expand All @@ -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()
Expand All @@ -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)
Expand Down Expand Up @@ -288,6 +316,8 @@ def get_settings_defaults(self):
verify_ssl=True,
database='octoprint',
prefix='',
hostmethod=HOST_NODE,
hostcustom='octoprint',
username=None,
password=None,

Expand All @@ -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

Expand All @@ -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):
Expand Down
22 changes: 22 additions & 0 deletions octoprint_influxdb/templates/influxdb_settings.jinja2
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,28 @@
{{ _('Measurement names will be prefixed by this string. Helpful when sharing a database.') }}
</span>
</div>

<label class="control-label">{{ _('%(host)s Tag', host='<tt>host</tt>') }}</label>
<div class="controls">
<label class="radio">
<input type="radio" class="input-medium" id="influxdb.host_node" name="influxdb.hostmethod" value="{{ plugin_influxdb_host_node }}" data-bind="checked: settings.plugins.influxdb.hostmethod">
<tt>{{ plugin_influxdb_host_node_s }}</tt>
({{ _('node name') }})
</label>
<label class="radio">
<input type="radio" class="input-medium" id="influxdb.host_fqdn" name="influxdb.hostmethod" value="{{ plugin_influxdb_host_fqdn }}" data-bind="checked: settings.plugins.influxdb.hostmethod">
<tt>{{ plugin_influxdb_host_fqdn_s }}</tt>
({{ _('fully-qualified domain name') }})
</label>
<label class="radio">
<input type="radio" class="input-medium" id="influxdb.host_custom" name="influxdb.hostmethod" value="{{ plugin_influxdb_host_custom }}" data-bind="checked: settings.plugins.influxdb.hostmethod">
{{ _('Custom:') }}
<input type="text" class="input-medium" placeholder="(empty)" data-bind="value: settings.plugins.influxdb.hostcustom, enable: settings.plugins.influxdb.hostmethod() == '{{ plugin_influxdb_host_custom }}'">
</label>
<span class="help-block">
{{ _('Measurements will be tagged with this value for %(host)s.', host='<tt>host</tt>') }}
</span>
</div>
</div>

<div class="control-group" data-bind="visible: settings.plugins.influxdb.authenticate">
Expand Down

0 comments on commit c316843

Please sign in to comment.