summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
Diffstat (limited to 'src')
-rwxr-xr-xsrc/conf_mode/service_monitoring_telegraf.py (renamed from src/conf_mode/service_monitoring.py)53
-rwxr-xr-xsrc/etc/telegraf/custom_scripts/show_interfaces_input_filter.py47
-rwxr-xr-xsrc/etc/telegraf/custom_scripts/vyos_services_input_filter.py61
3 files changed, 140 insertions, 21 deletions
diff --git a/src/conf_mode/service_monitoring.py b/src/conf_mode/service_monitoring_telegraf.py
index e14ad78fc..a1e7a7286 100755
--- a/src/conf_mode/service_monitoring.py
+++ b/src/conf_mode/service_monitoring_telegraf.py
@@ -18,19 +18,23 @@ 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.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()
-cache_dir = '/run/telegraf/.cache'
-config_telegraf = '/run/telegraf/vyos-telegraf.conf'
+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'
@@ -59,18 +63,20 @@ def get_config(config=None):
conf = config
else:
conf = Config()
- base = ['service', 'monitoring']
+ 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)
- if monitoring:
- # Set default value for bucket if not configured
- if 'bucket' not in monitoring:
- monitoring['bucket'] = cmd('cat /etc/hostname')
+ # 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)
- # Get nftables chains
- monitoring['nft_chains'] = get_nft_filter_chains()
- monitoring['custom_scripts_dir'] = custom_scripts_dir
+ monitoring['nft_chains'] = get_nft_filter_chains()
+ monitoring['custom_scripts_dir'] = custom_scripts_dir
return monitoring
@@ -79,8 +85,10 @@ def verify(monitoring):
if not monitoring:
return None
- if 'login' not in monitoring['authentication'] or 'token' not in monitoring['authentication']:
- raise ConfigError(f'Authentication "login and token" are mandatory!')
+ 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!')
@@ -89,22 +97,23 @@ def verify(monitoring):
def generate(monitoring):
if not monitoring:
- # Delete config file
- if os.path.isfile(config_telegraf):
- os.unlink(config_telegraf)
- if os.path.isfile(systemd_override):
- os.unlink(systemd_override)
- if os.path.isfile(syslog_telegraf):
- os.unlink(syslog_telegraf)
+ # 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.mkdir(cache_dir)
+ os.makedirs(cache_dir)
- chown(cache_dir, 'telegraf', 'vyattacfg')
+ chown(cache_dir, 'telegraf', 'telegraf')
# Create systemd override dir
if not os.path.exists(systemd_telegraf_override_dir):
@@ -120,6 +129,8 @@ def generate(monitoring):
render(systemd_override, 'monitoring/override.conf.tmpl', monitoring, permission=0o640)
render(syslog_telegraf, 'monitoring/syslog_telegraf.tmpl', monitoring)
+ chown(base_dir, 'telegraf', 'telegraf')
+
return None
def apply(monitoring):
diff --git a/src/etc/telegraf/custom_scripts/show_interfaces_input_filter.py b/src/etc/telegraf/custom_scripts/show_interfaces_input_filter.py
new file mode 100755
index 000000000..0f5e366cd
--- /dev/null
+++ b/src/etc/telegraf/custom_scripts/show_interfaces_input_filter.py
@@ -0,0 +1,47 @@
+#!/usr/bin/env python3
+
+import subprocess
+import time
+
+def status_to_int(status):
+ switcher={
+ 'u':'0',
+ 'D':'1',
+ 'A':'2'
+ }
+ return switcher.get(status,"")
+
+def description_check(line):
+ desc=" ".join(line[3:])
+ if desc == "":
+ return "empty"
+ else:
+ return desc
+
+def gen_ip_list(index,interfaces):
+ line=interfaces[index].split()
+ ip_list=line[1]
+ if index < len(interfaces):
+ index += 1
+ while len(interfaces[index].split())==1:
+ ip = interfaces[index].split()
+ ip_list = ip_list + " " + ip[0]
+ index += 1
+ if index == len(interfaces):
+ break
+ return ip_list
+
+interfaces = subprocess.check_output("/usr/libexec/vyos/op_mode/show_interfaces.py --action=show-brief", shell=True).decode('utf-8').splitlines()
+del interfaces[:3]
+lines_count=len(interfaces)
+index=0
+while index<lines_count:
+ line=interfaces[index].split()
+ if len(line)>1:
+ print(f'show_interfaces,interface={line[0]} '
+ f'ip_addresses="{gen_ip_list(index,interfaces)}",'
+ f'state={status_to_int(line[2][0])}i,'
+ f'link={status_to_int(line[2][2])}i,'
+ f'description="{description_check(line)}" '
+ f'{str(int(time.time()))}000000000')
+ index += 1
diff --git a/src/etc/telegraf/custom_scripts/vyos_services_input_filter.py b/src/etc/telegraf/custom_scripts/vyos_services_input_filter.py
new file mode 100755
index 000000000..df4eed131
--- /dev/null
+++ b/src/etc/telegraf/custom_scripts/vyos_services_input_filter.py
@@ -0,0 +1,61 @@
+#!/usr/bin/env python3
+#
+# Copyright (C) 2021 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 time
+from vyos.configquery import ConfigTreeQuery
+from vyos.util import is_systemd_service_running, process_named_running
+
+# Availible services and prouceses
+# 1 - service
+# 2 - process
+services = {
+ "protocols bgp" : "bgpd",
+ "protocols ospf" : "ospfd",
+ "protocols ospfv3" : "ospf6d",
+ "protocols rip" : "ripd",
+ "protocols ripng" : "ripngd",
+ "protocols isis" : "isisd",
+ "service pppoe" : "accel-ppp@pppoe.service",
+ "vpn l2tp remote-access" : "accel-ppp@l2tp.service",
+ "vpn pptp remote-access" : "accel-ppp@pptp.service",
+ "vpn sstp" : "accel-ppp@sstp.service",
+ "vpn ipsec" : "charon"
+}
+
+# Configured services
+conf_services = {
+ 'zebra' : 0,
+ 'staticd' : 0,
+}
+# Get configured service and create list to check if process running
+config = ConfigTreeQuery()
+for service in services:
+ if config.exists(service):
+ conf_services[services[service]] = 0
+
+for conf_service in conf_services:
+ status = 0
+ if ".service" in conf_service:
+ # Check systemd service
+ if is_systemd_service_running(conf_service):
+ status = 1
+ else:
+ # Check process
+ if process_named_running(conf_service):
+ status = 1
+ print(f'vyos_services,service="{conf_service}" '
+ f'status={str(status)}i {str(int(time.time()))}000000000')