Skip to content

Commit f02b6f4

Browse files
option added to restart prometheus
1 parent 568a5b1 commit f02b6f4

6 files changed

Lines changed: 52 additions & 3 deletions

File tree

src/routes/helper/common_helper.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,10 @@
44
from functools import wraps
55
from flask import flash, redirect, url_for, render_template, request, session
66
import subprocess
7+
import os
8+
from src.utils import ROOT_DIR
9+
10+
711

812
def get_email_addresses(user_level=None, receive_email_alerts=True, fetch_all_users=False):
913
""" Retrieve email addresses of users based on filters."""
@@ -113,3 +117,4 @@ def decorated_function(*args, **kwargs):
113117
return f(*args, **kwargs)
114118
return decorated_function
115119
return decorator
120+

src/routes/helper/prometheus_helper.py

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,25 @@ def show_targets():
4141
})
4242
return targets_info
4343

44+
def update_prometheus_config():
45+
"""
46+
update the first target whenever the function called
47+
as the network change the ip address will also change
48+
so 1st ip address in the list is the ip adddress of the machine
49+
with the job localhost
50+
"""
51+
print("Updating Prometheus config...")
52+
ipv4_address = subprocess.run(['hostname', '-I'], capture_output=True, text=True, check=True).stdout.split()[0]
53+
prometheus_config_path = os.path.join(ROOT_DIR, 'prometheus_config', 'prometheus.yml')
54+
config = load_yaml(prometheus_config_path)
55+
# fetch the localhost job
56+
localhost_job = next((job for job in config['scrape_configs'] if job['job_name'] == 'localhost'), None)
57+
if localhost_job:
58+
localhost_job['static_configs'][0]['targets'][0] = f'{ipv4_address}:5050'
59+
save_yaml(config, prometheus_config_path)
60+
return True
61+
62+
return False
4463

4564
def update_prometheus_container():
4665
"""Update the Prometheus container."""

src/routes/prometheus.py

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,9 @@
1212
is_valid_file,
1313
show_targets,
1414
prometheus_yml_path,
15-
update_prometheus_container)
15+
update_prometheus_container,
16+
update_prometheus_config)
17+
1618

1719

1820
# Define the Prometheus Blueprint
@@ -61,9 +63,17 @@ def delete_file_path(id):
6163

6264
@app.route('/targets')
6365
def targets():
66+
update_prometheus_config()
67+
# update_prometheus_container()
6468
targets_info = show_targets()
6569
return render_template('other/targets.html', targets_info=targets_info)
6670

71+
@app.route('/targets/restart_prometheus')
72+
def restart_prometheus():
73+
update_prometheus_config
74+
update_prometheus_container()
75+
flash('Prometheus container restarted successfully!', 'success')
76+
return redirect(url_for('dashboard_network'))
6777

6878
@app.route('/targets/add_target', methods=['POST'])
6979
def add_target():
@@ -72,6 +82,11 @@ def add_target():
7282
scrape_interval = request.form.get('scrape_interval', '15s') + 's' # New scrape interval
7383
config = load_yaml(prometheus_yml_path)
7484

85+
# new target should be <ip>:<port> check if it is in the correct format
86+
if ':' not in new_target:
87+
flash('Invalid target format. It should be in the format <ip>:<port>.', 'danger')
88+
return redirect(url_for('dashboard_network'))
89+
7590
for scrape_config in config['scrape_configs']:
7691
if scrape_config['job_name'] == job_name:
7792
scrape_config['static_configs'][0]['targets'].append(new_target)
@@ -127,7 +142,7 @@ def change_interval():
127142
scrape_config['scrape_interval'] = new_interval
128143
flash('Scrape interval updated successfully!', 'success')
129144
break
130-
145+
131146
save_yaml(config, prometheus_yml_path)
132147
update_prometheus_container()
133148
return redirect(url_for('targets'))

src/scripts/prometheus.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ mkdir -p "$PROMETHEUS_DATA_DIR"
4343
log "Generating prometheus.yml configuration file."
4444
cat > "$PROMETHEUS_CONFIG_FILE" <<EOL
4545
scrape_configs:
46-
- job_name: $JOB_NAME
46+
- job_name: localhost
4747
scrape_interval: $SCRAPING_INTERVAL
4848
static_configs:
4949
- targets:

src/templates/network/dashboard_network.html

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
{% block content %}
88
<div class="container">
99
<h1>SystemGuard Active Targets</h1>
10+
{% include 'ext/message.html' %}
1011
<table>
1112
<thead>
1213
<tr>
@@ -28,6 +29,14 @@ <h1>SystemGuard Active Targets</h1>
2829
</table>
2930
</div>
3031

32+
33+
<div class="text-center">
34+
<h2>Restart Prometheus Docker Service</h2>
35+
<form action="{{ url_for('restart_prometheus') }}">
36+
<input type="submit" value="Restart Prometheus">
37+
</form>
38+
</div>
39+
3140
<div class="text-center">
3241
<h2>Add New Target</h2>
3342
<form action="{{ url_for('add_target') }}" method="post">

src/templates/other/targets.html

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77

88
{% block content %}
99
<h1>SystemGuard Targets</h1>
10+
{% include 'ext/message.html' %}
1011
<table>
1112
<tr>
1213
<th>Job Name</th>

0 commit comments

Comments
 (0)