Page Menu
Home
VyOS Platform
Search
Configure Global Search
Log In
Files
F119062346
No One
Temporary
Actions
View File
Edit File
Delete File
View Transforms
Subscribe
Flag For Later
Award Token
Size
18 KB
Referenced Files
None
Subscribers
None
View Options
diff --git a/data/templates/monitoring/telegraf.j2 b/data/templates/monitoring/telegraf.j2
index cf33eec4e..336a1d2c4 100644
--- a/data/templates/monitoring/telegraf.j2
+++ b/data/templates/monitoring/telegraf.j2
@@ -1,60 +1,78 @@
# Generated by /usr/libexec/vyos/conf_mode/service_monitoring_telegraf.py
[agent]
interval = "15s"
round_interval = true
metric_batch_size = 1000
metric_buffer_limit = 10000
collection_jitter = "5s"
flush_interval = "15s"
flush_jitter = "0s"
precision = ""
debug = false
quiet = false
logfile = ""
hostname = ""
omit_hostname = false
+{% if influxdb_configured is vyos_defined %}
[[outputs.influxdb_v2]]
urls = ["{{ url }}:{{ port }}"]
insecure_skip_verify = true
token = "$INFLUX_TOKEN"
organization = "{{ authentication.organization }}"
bucket = "{{ bucket }}"
+{% endif %}
+{% if prometheus_client is vyos_defined %}
+[[outputs.prometheus_client]]
+ ## Address to listen on
+ listen = "{{ prometheus_client.listen_address if prometheus_client.listen_address is vyos_defined else '' }}:{{ prometheus_client.port }}"
+ metric_version = {{ prometheus_client.metric_version }}
+{% if prometheus_client.authentication.username is vyos_defined and prometheus_client.authentication.password is vyos_defined %}
+ ## Use HTTP Basic Authentication
+ basic_username = "{{ prometheus_client.authentication.username }}"
+ basic_password = "{{ prometheus_client.authentication.password }}"
+{% endif %}
+{% if prometheus_client.allow_from is vyos_defined %}
+ ip_range = {{ prometheus_client.allow_from }}
+{% endif %}
+{% endif %}
[[inputs.cpu]]
percpu = true
totalcpu = true
collect_cpu_time = false
report_active = false
[[inputs.disk]]
ignore_fs = ["devtmpfs", "devfs"]
[[inputs.diskio]]
[[inputs.mem]]
[[inputs.net]]
[[inputs.system]]
[[inputs.netstat]]
[[inputs.processes]]
[[inputs.kernel]]
[[inputs.interrupts]]
[[inputs.linux_sysctl_fs]]
[[inputs.systemd_units]]
[[inputs.conntrack]]
files = ["ip_conntrack_count","ip_conntrack_max","nf_conntrack_count","nf_conntrack_max"]
dirs = ["/proc/sys/net/ipv4/netfilter","/proc/sys/net/netfilter"]
[[inputs.ethtool]]
interface_include = {{ interfaces_ethernet }}
[[inputs.ntpq]]
dns_lookup = true
[[inputs.internal]]
[[inputs.nstat]]
[[inputs.syslog]]
server = "unixgram:///run/telegraf/telegraf_syslog.sock"
best_effort = true
syslog_standard = "RFC3164"
+{% if influxdb_configured is vyos_defined %}
[[inputs.exec]]
commands = [
"{{ custom_scripts_dir }}/show_firewall_input_filter.py",
"{{ custom_scripts_dir }}/show_interfaces_input_filter.py",
"{{ custom_scripts_dir }}/vyos_services_input_filter.py"
]
timeout = "10s"
data_format = "influx"
+{% endif %}
diff --git a/interface-definitions/service_monitoring_telegraf.xml.in b/interface-definitions/service_monitoring_telegraf.xml.in
index 01bc16fc9..021174701 100644
--- a/interface-definitions/service_monitoring_telegraf.xml.in
+++ b/interface-definitions/service_monitoring_telegraf.xml.in
@@ -1,111 +1,194 @@
<?xml version="1.0"?>
<interfaceDefinition>
<node name="service">
<children>
<node name="monitoring">
<properties>
<help>Monitoring services</help>
<priority>1280</priority>
</properties>
<children>
<node name="telegraf" owner="${vyos_conf_scripts_dir}/service_monitoring_telegraf.py">
<properties>
<help>Telegraf monitoring</help>
</properties>
<children>
<node name="authentication">
<properties>
<help>Authentication parameters</help>
</properties>
<children>
<leafNode name="organization">
<properties>
<help>Authentication organization for InfluxDB v2 [REQUIRED]</help>
<constraint>
<regex>[a-zA-Z][1-9a-zA-Z@_\-.]{2,50}</regex>
</constraint>
<constraintErrorMessage>Organization name must be alphanumeric and can contain hyphens, underscores and at symbol.</constraintErrorMessage>
</properties>
</leafNode>
<leafNode name="token">
<properties>
<help>Authentication token for InfluxDB v2 [REQUIRED]</help>
<valueHelp>
<format>txt</format>
<description>Authentication token</description>
</valueHelp>
<constraint>
<regex>[a-zA-Z0-9-_]{86}==</regex>
</constraint>
<constraintErrorMessage>Token must be 88 characters long and must contain only [a-zA-Z0-9-_] and '==' characters.</constraintErrorMessage>
</properties>
</leafNode>
</children>
</node>
<leafNode name="bucket">
<properties>
<help>Remote bucket</help>
</properties>
<defaultValue>main</defaultValue>
</leafNode>
<leafNode name="source">
<properties>
<help>Source parameters for monitoring</help>
<completionHelp>
<list>all hardware-utilization logs network system telegraf</list>
</completionHelp>
<valueHelp>
<format>all</format>
<description>All parameters</description>
</valueHelp>
<valueHelp>
<format>hardware-utilization</format>
<description>Hardware-utilization parameters (CPU, disk, memory)</description>
</valueHelp>
<valueHelp>
<format>logs</format>
<description>Logs parameters</description>
</valueHelp>
<valueHelp>
<format>network</format>
<description>Network parameters (net, netstat, nftables)</description>
</valueHelp>
<valueHelp>
<format>system</format>
<description>System parameters (system, processes, interrupts)</description>
</valueHelp>
<valueHelp>
<format>telegraf</format>
<description>Telegraf internal statistics</description>
</valueHelp>
<constraint>
<regex>(all|hardware-utilization|logs|network|system|telegraf)</regex>
</constraint>
<multi/>
</properties>
<defaultValue>all</defaultValue>
</leafNode>
+ <node name="prometheus-client">
+ <properties>
+ <help>Output plugin Prometheus client</help>
+ </properties>
+ <children>
+ <node name="authentication">
+ <properties>
+ <help>HTTP basic authentication parameters</help>
+ </properties>
+ <children>
+ <leafNode name="username">
+ <properties>
+ <help>Authentication username</help>
+ </properties>
+ </leafNode>
+ <leafNode name="password">
+ <properties>
+ <help>Authentication password</help>
+ <valueHelp>
+ <format>txt</format>
+ <description>Authentication password</description>
+ </valueHelp>
+ </properties>
+ </leafNode>
+ </children>
+ </node>
+ <leafNode name="allow-from">
+ <properties>
+ <help>Networks allowed to query this server</help>
+ <valueHelp>
+ <format>ipv4net</format>
+ <description>IP address and prefix length</description>
+ </valueHelp>
+ <valueHelp>
+ <format>ipv6net</format>
+ <description>IPv6 address and prefix length</description>
+ </valueHelp>
+ <multi/>
+ <constraint>
+ <validator name="ip-prefix"/>
+ </constraint>
+ </properties>
+ </leafNode>
+ <leafNode name="listen-address">
+ <properties>
+ <help>Local IP addresses to listen on</help>
+ <completionHelp>
+ <script>${vyos_completion_dir}/list_local_ips.sh --both</script>
+ </completionHelp>
+ <valueHelp>
+ <format>ipv4</format>
+ <description>IPv4 address to listen for incoming connections</description>
+ </valueHelp>
+ <valueHelp>
+ <format>ipv6</format>
+ <description>IPv6 address to listen for incoming connections</description>
+ </valueHelp>
+ <constraint>
+ <validator name="ipv4-address"/>
+ <validator name="ipv6-address"/>
+ <validator name="ipv6-link-local"/>
+ </constraint>
+ </properties>
+ </leafNode>
+ <leafNode name="metric-version">
+ <properties>
+ <help>Metric version control mapping from Telegraf to Prometheus format</help>
+ <valueHelp>
+ <format>u32:1-2</format>
+ <description>Metric version (default: 2)</description>
+ </valueHelp>
+ <constraint>
+ <validator name="numeric" argument="--range 1-2"/>
+ </constraint>
+ </properties>
+ <defaultValue>2</defaultValue>
+ </leafNode>
+ #include <include/port-number.xml.i>
+ <leafNode name="port">
+ <defaultValue>9273</defaultValue>
+ </leafNode>
+ </children>
+ </node>
<leafNode name="url">
<properties>
<help>Remote URL [REQUIRED]</help>
<valueHelp>
<format>url</format>
<description>Remote URL to InfluxDB v2</description>
</valueHelp>
<constraint>
<regex>(http:\/\/www\.|https:\/\/www\.|http:\/\/|https:\/\/)?[a-z0-9]+([\-\.]{1}[a-z0-9]+)*\.[a-z]{2,5}?(\/.*)?</regex>
</constraint>
<constraintErrorMessage>Incorrect URL format.</constraintErrorMessage>
</properties>
</leafNode>
#include <include/port-number.xml.i>
<leafNode name="port">
<defaultValue>8086</defaultValue>
</leafNode>
</children>
</node>
</children>
</node>
</children>
</node>
</interfaceDefinition>
diff --git a/src/conf_mode/service_monitoring_telegraf.py b/src/conf_mode/service_monitoring_telegraf.py
index 94bc02f10..2398b0174 100755
--- a/src/conf_mode/service_monitoring_telegraf.py
+++ b/src/conf_mode/service_monitoring_telegraf.py
@@ -1,175 +1,185 @@
#!/usr/bin/env python3
#
# Copyright (C) 2021-2022 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
# published by the Free Software Foundation.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
import os
import json
from sys import exit
from shutil import rmtree
from vyos.config import Config
from vyos.configdict import dict_merge
from vyos.ifconfig import Section
from vyos.template import render
from vyos.util import call
from vyos.util import chown
from vyos.util import cmd
from vyos.xml import defaults
from vyos import ConfigError
from vyos import airbag
airbag.enable()
base_dir = '/run/telegraf'
cache_dir = f'/etc/telegraf/.cache'
config_telegraf = f'{base_dir}/vyos-telegraf.conf'
custom_scripts_dir = '/etc/telegraf/custom_scripts'
syslog_telegraf = '/etc/rsyslog.d/50-telegraf.conf'
systemd_telegraf_service = '/etc/systemd/system/vyos-telegraf.service'
systemd_telegraf_override_dir = '/etc/systemd/system/vyos-telegraf.service.d'
systemd_override = f'{systemd_telegraf_override_dir}/10-override.conf'
def get_interfaces(type='', vlan=True):
"""
Get interfaces
get_interfaces()
['dum0', 'eth0', 'eth1', 'eth1.5', 'lo', 'tun0']
get_interfaces("dummy")
['dum0']
"""
interfaces = []
ifaces = Section.interfaces(type)
for iface in ifaces:
if vlan == False and '.' in iface:
continue
interfaces.append(iface)
return interfaces
def get_nft_filter_chains():
"""
Get nft chains for table filter
"""
nft = cmd('nft --json list table ip filter')
nft = json.loads(nft)
chain_list = []
for output in nft['nftables']:
if 'chain' in output:
chain = output['chain']['name']
chain_list.append(chain)
return chain_list
def get_config(config=None):
if config:
conf = config
else:
conf = Config()
base = ['service', 'monitoring', 'telegraf']
if not conf.exists(base):
return None
monitoring = conf.get_config_dict(base, key_mangling=('-', '_'), get_first_key=True,
no_tag_node_value_mangle=True)
# We have gathered the dict representation of the CLI, but there are default
# options which we need to update into the dictionary retrived.
default_values = defaults(base)
monitoring = dict_merge(default_values, monitoring)
monitoring['custom_scripts_dir'] = custom_scripts_dir
monitoring['interfaces_ethernet'] = get_interfaces('ethernet', vlan=False)
monitoring['nft_chains'] = get_nft_filter_chains()
+ if 'authentication' in monitoring or \
+ 'url' in monitoring:
+ monitoring['influxdb_configured'] = True
+
+ # Ignore default XML values if config doesn't exists
+ # Delete key from dict
+ if not conf.exists(base + ['prometheus-client']):
+ del monitoring['prometheus_client']
+
return monitoring
def verify(monitoring):
# bail out early - looks like removal from running config
if not monitoring:
return None
- if 'authentication' not in monitoring or \
- 'organization' not in monitoring['authentication'] or \
- 'token' not in monitoring['authentication']:
- raise ConfigError(f'Authentication "organization and token" are mandatory!')
+ if 'influxdb_configured' in monitoring:
+ if 'authentication' not in monitoring or \
+ 'organization' not in monitoring['authentication'] or \
+ 'token' not in monitoring['authentication']:
+ raise ConfigError(f'Authentication "organization and token" are mandatory!')
- if 'url' not in monitoring:
- raise ConfigError(f'Monitoring "url" is mandatory!')
+ if 'url' not in monitoring:
+ raise ConfigError(f'Monitoring "url" is mandatory!')
return None
def generate(monitoring):
if not monitoring:
# Delete config and systemd files
config_files = [config_telegraf, systemd_telegraf_service, systemd_override, syslog_telegraf]
for file in config_files:
if os.path.isfile(file):
os.unlink(file)
# Delete old directories
if os.path.isdir(cache_dir):
rmtree(cache_dir, ignore_errors=True)
return None
# Create telegraf cache dir
if not os.path.exists(cache_dir):
os.makedirs(cache_dir)
chown(cache_dir, 'telegraf', 'telegraf')
# Create systemd override dir
if not os.path.exists(systemd_telegraf_override_dir):
os.mkdir(systemd_telegraf_override_dir)
# Create custome scripts dir
if not os.path.exists(custom_scripts_dir):
os.mkdir(custom_scripts_dir)
# Render telegraf configuration and systemd override
render(config_telegraf, 'monitoring/telegraf.j2', monitoring)
render(systemd_telegraf_service, 'monitoring/systemd_vyos_telegraf_service.j2', monitoring)
render(systemd_override, 'monitoring/override.conf.j2', monitoring, permission=0o640)
render(syslog_telegraf, 'monitoring/syslog_telegraf.j2', monitoring)
chown(base_dir, 'telegraf', 'telegraf')
return None
def apply(monitoring):
# Reload systemd manager configuration
call('systemctl daemon-reload')
if monitoring:
call('systemctl restart vyos-telegraf.service')
else:
call('systemctl stop vyos-telegraf.service')
# Telegraf include custom rsyslog config changes
call('systemctl restart rsyslog')
if __name__ == '__main__':
try:
c = get_config()
verify(c)
generate(c)
apply(c)
except ConfigError as e:
print(e)
exit(1)
File Metadata
Details
Attached
Mime Type
text/x-diff
Expires
Fri, Nov 6, 6:39 AM (2 h, 28 m)
Storage Engine
blob
Storage Format
Raw Data
Storage Handle
4652810
Default Alt Text
(18 KB)
Attached To
Mode
rVYOSONEX vyos-1x
Attached
Detach File
Event Timeline
Log In to Comment