Skip to content

Commit

Permalink
T6477: Add telegraf loki output plugin
Browse files Browse the repository at this point in the history
Add Loki plugin to telegraf

set service monitoring telegraf loki url xxx
  • Loading branch information
sever-sever committed Jun 27, 2024
1 parent 4f89e4b commit ea50e3b
Show file tree
Hide file tree
Showing 4 changed files with 86 additions and 4 deletions.
15 changes: 15 additions & 0 deletions data/templates/telegraf/telegraf.j2
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,21 @@
bucket = "{{ influxdb.bucket }}"
### End InfluxDB2 ###
{% endif %}
{% if loki is vyos_defined %}
### Loki ###
[[outputs.loki]]
## The domain of Loki
domain = "{{ loki.url }}:{{ loki.port }}"
{% if loki.authentication.username is vyos_defined and loki.authentication.password is vyos_defined %}
## Basic Authentication
username = "{{ loki.authentication.username }}"
password = "{{ loki.authentication.password }}"
{% endif %}
{% if loki.metric_name_label is vyos_defined %}
metric_name_label = "{{ loki.metric_name_label }}"
{% endif %}
### End Loki ###
{% endif %}
{% if prometheus_client is vyos_defined %}
### Prometheus ###
[[outputs.prometheus_client]]
Expand Down
33 changes: 33 additions & 0 deletions interface-definitions/service_monitoring_telegraf.xml.in
Original file line number Diff line number Diff line change
Expand Up @@ -148,6 +148,39 @@
#include <include/url-http-https.xml.i>
</children>
</node>
<node name="loki">
<properties>
<help>Output plugin Loki</help>
</properties>
<children>
<node name="authentication">
<properties>
<help>HTTP basic authentication parameters</help>
</properties>
<children>
#include <include/generic-username.xml.i>
#include <include/generic-password.xml.i>
</children>
</node>
<leafNode name="metric-name-label">
<properties>
<help>Metric name label</help>
<valueHelp>
<format>txt</format>
<description>Label to use for the metric name</description>
</valueHelp>
<constraint>
#include <include/constraint/alpha-numeric-hyphen-underscore-dot.xml.i>
</constraint>
</properties>
</leafNode>
#include <include/port-number.xml.i>
<leafNode name="port">
<defaultValue>3100</defaultValue>
</leafNode>
#include <include/url-http-https.xml.i>
</children>
</node>
<leafNode name="source">
<properties>
<help>Source parameters for monitoring</help>
Expand Down
18 changes: 18 additions & 0 deletions smoketest/scripts/cli/test_service_monitoring_telegraf.py
Original file line number Diff line number Diff line change
Expand Up @@ -63,5 +63,23 @@ def test_01_basic_config(self):
for input in inputs:
self.assertIn(input, config)

def test_02_loki(self):
label = 'r123'
loki_url = 'http://localhost'
port = '3100'

self.cli_set(base_path + ['loki', 'url', loki_url])
self.cli_set(base_path + ['loki', 'port', port])
self.cli_set(base_path + ['loki', 'metric-name-label', label])

# commit changes
self.cli_commit()

config = read_file(TELEGRAF_CONF)
self.assertIn(f'[[outputs.loki]]', config)
self.assertIn(f'domain = "{loki_url}:{port}"', config)
self.assertIn(f'metric_name_label = "{label}"', config)


if __name__ == '__main__':
unittest.main(verbosity=2)
24 changes: 20 additions & 4 deletions src/conf_mode/service_monitoring_telegraf.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#!/usr/bin/env python3
#
# Copyright (C) 2021-2023 VyOS maintainers and contributors
# Copyright (C) 2021-2024 VyOS maintainers and contributors
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License version 2 or later as
Expand Down Expand Up @@ -113,6 +113,9 @@ def get_config(config=None):
if not conf.exists(base + ['azure-data-explorer']):
del monitoring['azure_data_explorer']

if not conf.exists(base + ['loki']):
del monitoring['loki']

return monitoring

def verify(monitoring):
Expand All @@ -130,7 +133,7 @@ def verify(monitoring):
raise ConfigError(f'influxdb authentication "organization and token" are mandatory!')

if 'url' not in monitoring['influxdb']:
raise ConfigError(f'Monitoring influxdb "url" is mandatory!')
raise ConfigError(f'Monitoring influxdb URL is mandatory!')

# Verify azure-data-explorer
if 'azure_data_explorer' in monitoring:
Expand All @@ -144,7 +147,7 @@ def verify(monitoring):
raise ConfigError(f'Monitoring "database" is mandatory!')

if 'url' not in monitoring['azure_data_explorer']:
raise ConfigError(f'Monitoring "url" is mandatory!')
raise ConfigError(f'Monitoring URL is mandatory!')

if monitoring['azure_data_explorer']['group_metrics'] == 'SingleTable' and \
'table' not in monitoring['azure_data_explorer']:
Expand All @@ -157,7 +160,20 @@ def verify(monitoring):
raise ConfigError(f'Authentication "organization and token" are mandatory!')

if 'url' not in monitoring['splunk']:
raise ConfigError(f'Monitoring splunk "url" is mandatory!')
raise ConfigError(f'Monitoring splunk URL is mandatory!')

# Verify Loki
if 'loki' in monitoring:
if 'url' not in monitoring['loki']:
raise ConfigError(f'Monitoring loki URL is mandatory!')
if 'authentication' in monitoring['loki']:
if (
'username' not in monitoring['loki']['authentication']
or 'password' not in monitoring['loki']['authentication']
):
raise ConfigError(
f'Authentication "username" and "password" are mandatory!'
)

return None

Expand Down

0 comments on commit ea50e3b

Please sign in to comment.