Skip to content

Commit c80881d

Browse files
chore: premetheus container will update after changing yml file
1 parent 7641ab6 commit c80881d

5 files changed

Lines changed: 44 additions & 12 deletions

File tree

src/routes/helper/prometheus_helper.py

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
11
import os
22
import yaml
3+
import subprocess
34
from src.utils import ROOT_DIR
45

56
prometheus_yml_path = os.path.join(ROOT_DIR, 'prometheus_config/prometheus.yml')
7+
update_prometheus_path = os.path.join(ROOT_DIR, 'src/scripts/update_prometheus.sh')
68

79
def is_valid_file(file_path: str) -> bool:
810
"""Checks if a file is valid and has key-value pairs separated by a colon."""
@@ -38,3 +40,22 @@ def show_targets():
3840
'scrape_interval': scrape_interval
3941
})
4042
return targets_info
43+
44+
45+
def update_prometheus_container():
46+
"""Update the Prometheus container."""
47+
# Define the path to your shell script
48+
try:
49+
# Use subprocess.run to execute the shell script
50+
result = subprocess.run(['bash', update_prometheus_path], check=True, text=True, capture_output=True)
51+
52+
# Print the output of the script
53+
print("Output:")
54+
print(result.stdout)
55+
56+
# Print any errors (if any)
57+
if result.stderr:
58+
print("Errors:")
59+
print(result.stderr)
60+
except subprocess.CalledProcessError as e:
61+
print(f"An error occurred: {e}")

src/routes/prometheus.py

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,14 @@
66
from src.config import app, db
77
from src.models import ExternalMonitornig
88
from src.utils import ROOT_DIR
9-
from src.routes.helper.prometheus_helper import load_yaml, save_yaml, is_valid_file, show_targets, prometheus_yml_path
9+
from src.routes.helper.prometheus_helper import (
10+
load_yaml,
11+
save_yaml,
12+
is_valid_file,
13+
show_targets,
14+
prometheus_yml_path,
15+
update_prometheus_container)
16+
1017

1118
# Define the Prometheus Blueprint
1219
prometheus_bp = Blueprint('prometheus', __name__)
@@ -57,6 +64,7 @@ def targets():
5764
targets_info = show_targets()
5865
return render_template('other/targets.html', targets_info=targets_info)
5966

67+
6068
@app.route('/targets/add_target', methods=['POST'])
6169
def add_target():
6270
job_name = request.form.get('job_name')
@@ -75,7 +83,8 @@ def add_target():
7583
'scrape_interval': scrape_interval # Set the specific interval
7684
}
7785
config['scrape_configs'].append(new_job)
78-
86+
87+
update_prometheus_container()
7988
save_yaml(config, prometheus_yml_path)
8089
flash('Target added successfully!', 'success')
8190
return redirect(url_for('targets'))
@@ -92,7 +101,8 @@ def remove_target():
92101
if target_to_remove in targets:
93102
targets.remove(target_to_remove)
94103
flash(f'Target {target_to_remove} removed successfully!', 'success')
95-
104+
105+
update_prometheus_container()
96106
# Check if this was the last target, then remove the job
97107
if not targets: # If the list is now empty
98108
config['scrape_configs'].remove(scrape_config)
@@ -116,6 +126,7 @@ def change_interval():
116126
if scrape_config['job_name'] == job_name:
117127
scrape_config['scrape_interval'] = new_interval
118128
flash('Scrape interval updated successfully!', 'success')
129+
update_prometheus_container()
119130
break
120131

121132
save_yaml(config, prometheus_yml_path)

src/scripts/update_prometheus.sh

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,12 @@
11
#!/bin/bash
22

33
# Configuration
4+
CURRENT_SCRIPT_DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" &> /dev/null && pwd )"
5+
CURRENT_DIR="$CURRENT_SCRIPT_DIR"
6+
ROOT_DIR="$(dirname "$(dirname "$CURRENT_DIR")")"
7+
PROMETHEUS_CONFIG_DIR="$ROOT_DIR/prometheus_config"
48
CONTAINER_NAME="prometheus"
5-
PROMETHEUS_CONFIG="$(pwd)/prometheus.yml"
9+
PROMETHEUS_CONFIG="$PROMETHEUS_CONFIG_DIR/prometheus.yml"
610
PROMETHEUS_IMAGE="prom/prometheus" # Add your image name if needed
711
NETWORK_NAME="flask-prometheus-net" # Specify your network name
812
PROMETHEUS_PORT="9090" # Specify your port

src/static/css/targets.css

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,12 @@
1-
* {
2-
box-sizing: border-box;
3-
margin: 0;
4-
padding: 0;
5-
}
61

7-
body {
2+
3+
/* body {
84
font-family: 'Arial', sans-serif;
95
background-color: #f4f4f9;
106
color: #333;
117
line-height: 1.6;
128
padding: 20px;
13-
}
9+
} */
1410

1511
h1, h2 {
1612
color: #4a4a4a;

src/templates/dashboard/developer.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
{% extends 'base/base.html' %}
2-
{% block title %}Server Dashboard{% endblock %}
2+
{% block title %}{{ system_info['system_username'] }}@{{ system_info['nodename'] }} - SystemGuard{% endblock %}
33
{% block extra_head %}
44
<link rel="stylesheet" href="{{ url_for('static', filename='css/refresher.css') }}">
55
{% endblock %}

0 commit comments

Comments
 (0)