Skip to content

Commit

Permalink
T6949: adds blackbox exporter
Browse files Browse the repository at this point in the history
  • Loading branch information
nvollmar committed Dec 24, 2024
1 parent b05cbfa commit 29f0d00
Show file tree
Hide file tree
Showing 4 changed files with 99 additions and 0 deletions.
25 changes: 25 additions & 0 deletions data/templates/prometheus/blackbox_exporter.service.j2
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
{% set vrf_command = 'ip vrf exec ' ~ vrf ~ ' runuser -u node_exporter -- ' if vrf is vyos_defined else '' %}
[Unit]
Description=Blackbox Exporter
Documentation=https://github.com/prometheus/blackbox_exporter
After=network.target

[Service]
{% if vrf is not vyos_defined %}
User=node_exporter
{% endif %}
ExecStart={{ vrf_command }}/usr/sbin/blackbox_exporter \
{% if listen_address is vyos_defined %}
{% for address in listen_address %}
--web.listen-address={{ address }}:{{ port }} \
{% endfor %}
{% else %}
--web.listen-address=:{{ port }} \
{% endif %}
{% if config_file is vyos_defined %}
--config.file={{ config_file }}
{% else %}
--config.file=/etc/blackbox_exporter/config.yml
{% endif %}
[Install]
WantedBy=multi-user.target
25 changes: 25 additions & 0 deletions interface-definitions/service_monitoring_prometheus.xml.in
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,31 @@
#include <include/interface/vrf.xml.i>
</children>
</node>
<node name="blackbox-exporter">
<properties>
<help>Prometheus exporter for probing endpoints</help>
</properties>
<children>
#include <include/listen-address.xml.i>
#include <include/port-number.xml.i>
<leafNode name="port">
<defaultValue>9115</defaultValue>
</leafNode>
#include <include/interface/vrf.xml.i>
<leafNode name="config-file">
<properties>
<help>Configuration to be used for blackbox exporter instead of default config</help>
<valueHelp>
<format>filename</format>
<description>Configuration filename, must be under /config/prometheus_exporter</description>
</valueHelp>
<constraint>
<validator name="file-path" argument="--file --parent-dir /config/prometheus_exporter --strict"/>
</constraint>
</properties>
</leafNode>
</children>
</node>
</children>
</node>
</children>
Expand Down
14 changes: 14 additions & 0 deletions smoketest/scripts/cli/test_service_monitoring_prometheus.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,12 +22,14 @@

NODE_EXPORTER_PROCESS_NAME = 'node_exporter'
FRR_EXPORTER_PROCESS_NAME = 'frr_exporter'
BLACKBOX_EXPORTER_PROCESS_NAME = 'blackbox_exporter'

base_path = ['service', 'monitoring', 'prometheus']
listen_if = 'dum3421'
listen_ip = '192.0.2.1'
node_exporter_service_file = '/etc/systemd/system/node_exporter.service'
frr_exporter_service_file = '/etc/systemd/system/frr_exporter.service'
blackbox_exporter_service_file = '/etc/systemd/system/blackbox_exporter.service'


class TestMonitoringPrometheus(VyOSUnitTestSHIM.TestCase):
Expand Down Expand Up @@ -75,6 +77,18 @@ def test_02_frr_exporter(self):
# Check for running process
self.assertTrue(process_named_running(FRR_EXPORTER_PROCESS_NAME))

def test_03_blackbox_exporter(self):
self.cli_set(base_path + ['blackbox-exporter', 'listen-address', listen_ip])

# commit changes
self.cli_commit()

file_content = read_file(blackbox_exporter_service_file)
self.assertIn(f'{listen_ip}:9115', file_content)

# Check for running process
self.assertTrue(process_named_running(BLACKBOX_EXPORTER_PROCESS_NAME))


if __name__ == '__main__':
unittest.main(verbosity=2)
35 changes: 35 additions & 0 deletions src/conf_mode/service_monitoring_prometheus.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,9 @@
frr_exporter_service_file = '/etc/systemd/system/frr_exporter.service'
frr_exporter_systemd_service = 'frr_exporter.service'

blackbox_exporter_service_file = '/etc/systemd/system/blackbox_exporter.service'
blackbox_exporter_systemd_service = 'blackbox_exporter.service'


def get_config(config=None):
if config:
Expand All @@ -57,6 +60,12 @@ def get_config(config=None):
if tmp:
monitoring.update({'frr_exporter_restart_required': {}})

tmp = False
for node in ['vrf', 'config-file']:
tmp = tmp or is_node_changed(conf, base + ['blackbox-exporter', node])
if tmp:
monitoring.update({'blackbox_exporter_restart_required': {}})

return monitoring


Expand All @@ -70,6 +79,9 @@ def verify(monitoring):
if 'frr_exporter' in monitoring:
verify_vrf(monitoring['frr_exporter'])

if 'blackbox_exporter' in monitoring:
verify_vrf(monitoring['blackbox_exporter'])

return None


Expand All @@ -84,6 +96,11 @@ def generate(monitoring):
if os.path.isfile(frr_exporter_service_file):
os.unlink(frr_exporter_service_file)

if not monitoring or 'blackbox_exporter' not in monitoring:
# Delete systemd files
if os.path.isfile(blackbox_exporter_service_file):
os.unlink(blackbox_exporter_service_file)

if not monitoring:
return None

Expand All @@ -103,6 +120,14 @@ def generate(monitoring):
monitoring['frr_exporter'],
)

if 'blackbox_exporter' in monitoring:
# Render blackbox_exporter service_file
render(
blackbox_exporter_service_file,
'prometheus/blackbox_exporter.service.j2',
monitoring['blackbox_exporter'],
)

return None


Expand All @@ -113,6 +138,8 @@ def apply(monitoring):
call(f'systemctl stop {node_exporter_systemd_service}')
if not monitoring or 'frr_exporter' not in monitoring:
call(f'systemctl stop {frr_exporter_systemd_service}')
if not monitoring or 'blackbox_exporter' not in monitoring:
call(f'systemctl stop {blackbox_exporter_systemd_service}')

if not monitoring:
return
Expand All @@ -133,6 +160,14 @@ def apply(monitoring):

call(f'systemctl {systemd_action} {frr_exporter_systemd_service}')

if 'blackbox_exporter' in monitoring:
# we need to restart the service if e.g. the VRF name changed
systemd_action = 'reload-or-restart'
if 'blackbox_exporter_restart_required' in monitoring:
systemd_action = 'restart'

call(f'systemctl {systemd_action} {blackbox_exporter_systemd_service}')


if __name__ == '__main__':
try:
Expand Down

0 comments on commit 29f0d00

Please sign in to comment.