Skip to content

Commit 0c7fd05

Browse files
Refactor prometheus.py, README.md, and dashboard_network.html
1 parent 6cba830 commit 0c7fd05

11 files changed

Lines changed: 236 additions & 265 deletions

File tree

src/docs/README.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,3 +23,7 @@
2323
## Security Analysis
2424

2525
![screenshot_007](/src/static/images/screenshot_007.png)
26+
27+
## Central Dashboard
28+
29+
![screenshot_008](/src/static/images/tracking.png)

src/routes/api.py

Lines changed: 43 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,15 @@
88
from flask import request, jsonify
99
import gc
1010
from datetime import datetime, timezone
11+
12+
from src.routes.helper.common_helper import admin_required
13+
1114
api_bp = blueprints.Blueprint("api", __name__)
1215

13-
PROMETHEUS_URL = 'http://localhost:9090/api/v1/query_range' # Change if using a different URL or port
16+
PROMETHEUS_URL = 'http://localhost:9090' # Change if using a different URL or port
17+
QUERY_API_URL = f'{PROMETHEUS_URL}/api/v1/query'
18+
TARGETS_API_URL = f'{PROMETHEUS_URL}/api/v1/targets'
19+
1420
PROMETHEUS_METRICS = {
1521
'cpu': 'cpu_usage_percentage', # Adjusting to match the defined gauge
1622
'memory': 'memory_usage_percentage',
@@ -283,7 +289,7 @@ def graph_data_api_v3():
283289
}
284290

285291
# Send the query to Prometheus
286-
response = requests.get(PROMETHEUS_URL, params=params)
292+
response = requests.get(QUERY_API_URL, params=params)
287293

288294
# Check if the request was successful
289295
if response.status_code == 200:
@@ -368,7 +374,7 @@ def graph_data_api_v3_():
368374
}
369375

370376
# Send the query to Prometheus
371-
response = requests.get(PROMETHEUS_URL, params=params)
377+
response = requests.get(QUERY_API_URL, params=params)
372378

373379
# Check if the request was successful
374380
if response.status_code == 200:
@@ -423,6 +429,40 @@ def graph_data_api_v3_():
423429
# Handle and log the error for debugging purposes
424430
return jsonify({'error': 'An error occurred while fetching the graph data', 'details': str(e)}), 500
425431

432+
433+
@app.route('/api/v1/targets', methods=['GET'])
434+
@admin_required
435+
def get_prometheus_targets():
436+
try:
437+
# Query Prometheus API to get the targets
438+
response = requests.get(TARGETS_API_URL)
439+
440+
# Check if the request was successful
441+
if response.status_code == 200:
442+
targets_data = response.json().get('data', {})
443+
active_targets = targets_data.get('activeTargets', [])
444+
dropped_targets = targets_data.get('droppedTargets', [])
445+
446+
# Return the active and dropped targets as JSON
447+
return jsonify({
448+
'active_targets': active_targets,
449+
'dropped_targets': dropped_targets
450+
}), 200
451+
else:
452+
# Handle non-200 responses from Prometheus
453+
return jsonify({
454+
'error': 'Failed to fetch targets from Prometheus',
455+
'details': response.text
456+
}), response.status_code
457+
458+
except Exception as e:
459+
# Handle exceptions
460+
return jsonify({
461+
'error': 'An error occurred while fetching Prometheus targets',
462+
'details': str(e)
463+
}), 500
464+
465+
426466
@app.route('/api/v1/refresh-interval', methods=['GET', 'POST'])
427467
@login_required
428468
def manage_refresh_interval():

src/routes/dashboard_network.py

Lines changed: 4 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -1,63 +1,11 @@
1-
from flask import render_template, blueprints, flash, redirect, url_for, request
1+
from flask import render_template, blueprints
22

3-
from src.config import app, db
4-
from src.models import DashboardNetworkSettings
3+
from src.config import app
54
from src.routes.helper.common_helper import admin_required
65

76
network_bp = blueprints.Blueprint('network', __name__)
87

9-
@app.route('/network', methods=['GET'])
8+
@app.route('/dashboard_network', methods=['GET'])
109
@admin_required
1110
def dashboard_network():
12-
groups = DashboardNetworkSettings.query.all() # Fetch all dashboard groups
13-
return render_template('network/dashboard_network.html', groups=groups)
14-
15-
@app.route('/add_server', methods=['GET', 'POST'])
16-
@admin_required
17-
def add_server():
18-
if request.method == 'POST':
19-
name = request.form.get('name')
20-
description = request.form.get('description')
21-
ip_address = request.form.get('ip_address')
22-
port = request.form.get('port')
23-
link = request.form.get('link')
24-
25-
# Check if the server name already exists
26-
existing_server = DashboardNetworkSettings.query.filter_by(name=name).first()
27-
if existing_server:
28-
flash('Server name already exists. Please choose a different name.', 'danger')
29-
return redirect(url_for('add_server'))
30-
31-
# Create a new server entry
32-
new_server = DashboardNetworkSettings(name=name, description=description, ip_address=ip_address, port=port, link=link)
33-
db.session.add(new_server)
34-
db.session.commit()
35-
36-
flash('Server added successfully!', 'success')
37-
return redirect(url_for('dashboard_network'))
38-
39-
return render_template('network/add_server.html')
40-
41-
@app.route('/edit_server/<int:server_id>', methods=['GET', 'POST'])
42-
@admin_required
43-
def edit_server(server_id):
44-
server = DashboardNetworkSettings.query.get_or_404(server_id)
45-
if request.method == 'POST':
46-
server.name = request.form['name']
47-
server.description = request.form['description']
48-
server.ip_address = request.form['ip_address']
49-
server.port = request.form['port']
50-
server.link = request.form['link']
51-
db.session.commit()
52-
flash('Server updated successfully!', 'success')
53-
return redirect(url_for('dashboard_network'))
54-
return render_template('network/edit_server.html', server=server)
55-
56-
@app.route('/delete_server/<int:server_id>', methods=['POST'])
57-
@admin_required
58-
def delete_server(server_id):
59-
server = DashboardNetworkSettings.query.get_or_404(server_id)
60-
db.session.delete(server)
61-
db.session.commit()
62-
flash('Server deleted successfully!', 'success')
63-
return redirect(url_for('dashboard_network'))
11+
return render_template('network/dashboard_network.html')

src/routes/prometheus.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,4 +65,3 @@ def delete_file_path(id):
6565
db.session.commit()
6666
flash('File path deleted successfully!', 'success')
6767
return redirect(url_for('external_monitoring'))
68-
Lines changed: 32 additions & 76 deletions
Original file line numberDiff line numberDiff line change
@@ -1,101 +1,57 @@
1+
2+
13
.container {
24
max-width: 1200px;
3-
margin: 0 auto;
4-
padding: 20px;
5-
text-align: center;
6-
color: #fff;
7-
}
8-
9-
.card-container {
10-
display: flex;
11-
flex-wrap: wrap;
12-
gap: 20px;
13-
justify-content: center;
5+
margin-top: 20px;
146
}
157

16-
.card {
17-
background-color: var(--color-username);
18-
border: 1px solid #ddd;
19-
border-radius: 8px;
20-
padding: 20px;
21-
width: calc(33.333% - 40px); /* Adjust based on gaps */
22-
box-shadow: 0 4px 8px rgba(0, 0, 0, 0.1);
23-
text-decoration: none;
8+
h1 {
9+
text-align: center;
2410
color: #333;
25-
transition: transform 0.2s;
26-
}
27-
28-
.card:hover {
29-
transform: translateX(-10px); /* Slight lift effect */
30-
box-shadow: 0 10px 20px #378fe7;
31-
transition: transform 0.3s ease, box-shadow 0.3s ease; /* Smooth transition */
3211
}
3312

34-
.card h2 {
35-
margin-top: 0;
36-
font-size: 1.5em;
37-
color: #fff;
38-
}
39-
40-
.card p {
41-
margin: 5px 0;
42-
color: #fff;
43-
}
44-
45-
.card a {
46-
color: #fff;
47-
text-decoration: none;
48-
}
49-
50-
.add-server-link {
13+
.target-table {
5114
margin-top: 20px;
5215
}
5316

54-
.add-server-link .btn {
55-
font-size: 1.2em;
56-
padding: 10px 20px;
57-
border-radius: 5px;
58-
background-color: #007bff;
59-
color: white;
60-
text-decoration: none;
61-
transition: background-color 0.3s;
17+
table {
18+
width: 100%;
19+
border-collapse: collapse;
20+
margin-bottom: 20px;
6221
}
6322

64-
.add-server-link .btn:hover {
65-
background-color: #0056b3;
23+
table th, table td {
24+
padding: 12px;
25+
border: 1px solid #ddd;
26+
text-align: left;
6627
}
6728

68-
/* Responsive layout */
69-
@media (max-width: 992px) {
70-
.card {
71-
width: calc(50% - 30px); /* Two cards per line on medium screens */
72-
}
29+
table th {
30+
background-color: #3f51b5;
31+
color: white;
7332
}
7433

75-
@media (max-width: 768px) {
76-
.card {
77-
width: calc(100% - 40px); /* One card per line on small screens */
78-
}
34+
table tr:nth-child(even) {
35+
background-color: #f9f9f9;
7936
}
8037

81-
.card-actions {
82-
margin-top: 10px;
38+
table td.health-up {
39+
color: green;
40+
font-weight: bold;
8341
}
8442

85-
.card-actions .btn {
86-
margin-right: 5px;
87-
padding: 5px 10px;
88-
font-size: 14px;
43+
table td.health-down {
44+
color: red;
45+
font-weight: bold;
8946
}
9047

91-
.card-actions .btn-warning {
92-
background-color: #ffc107;
93-
border-color: #ffc107;
94-
color: #fff;
48+
.error-message {
49+
color: red;
50+
font-style: italic;
9551
}
9652

97-
.card-actions .btn-danger {
98-
background-color: #dc3545;
99-
border-color: #dc3545;
100-
color: #fff;
53+
.footer {
54+
text-align: center;
55+
color: #666;
56+
font-size: 12px;
10157
}

src/static/css/targets.css

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
2+
3+
.container {
4+
max-width: 1200px;
5+
margin-top: 20px;
6+
}
7+
8+
h1 {
9+
text-align: center;
10+
color: #333;
11+
}
12+
13+
.target-table {
14+
margin-top: 20px;
15+
}
16+
17+
table {
18+
width: 100%;
19+
border-collapse: collapse;
20+
margin-bottom: 20px;
21+
}
22+
23+
table th, table td {
24+
padding: 12px;
25+
border: 1px solid #ddd;
26+
text-align: left;
27+
}
28+
29+
table th {
30+
background-color: #3f51b5;
31+
color: white;
32+
}
33+
34+
table tr:nth-child(even) {
35+
background-color: #f9f9f9;
36+
}
37+
38+
table td.health-up {
39+
color: green;
40+
font-weight: bold;
41+
}
42+
43+
table td.health-down {
44+
color: red;
45+
font-weight: bold;
46+
}
47+
48+
.error-message {
49+
color: red;
50+
font-style: italic;
51+
}
52+
53+
.footer {
54+
text-align: center;
55+
color: #666;
56+
font-size: 12px;
57+
}

src/static/images/tracking.png

224 KB
Loading

0 commit comments

Comments
 (0)