Skip to content

Commit a0acfc5

Browse files
vineeshahevanugarte
authored andcommitted
refined code for required promurl
1 parent 315cf9e commit a0acfc5

File tree

3 files changed

+68
-26
lines changed

3 files changed

+68
-26
lines changed

status/docker-compose.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ services:
2323
ports:
2424
- "8000:8000"
2525
restart: 'on-failure'
26-
command: python3 scraper.py --json json.json --promurl "http://one.sce/prometheus/metrics"
26+
command: python3 scraper.py --json json.json --promurl "http://one.sce/prometheus"
2727

2828

2929
nginx:

status/scraper.py

Lines changed: 14 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -54,8 +54,8 @@ def check_status(query):
5454
return None
5555

5656
def polling_loop(interval, config):
57+
global metrics_data
5758
while True:
58-
global metrics_data
5959
metrics_data = []
6060
for hosts in config:
6161
service_name = hosts["job-id"]
@@ -83,20 +83,22 @@ def process_up_query(query, service_name):
8383
return
8484

8585
for metric in result:
86-
job_name = metric.get('metric',{}).get('job', "")#for later use in dataclass
87-
time_stamp = metric.get('value', [])[0]#for later use in dataclass
86+
instance = metric["metric"].get("instance", "unknown")
87+
job_name = metric["metric"].get("job", "unknown")#for later use in dataclass
8888
value = metric.get('value', [])[1]
8989
# last_active = datetime.now(pacific_tz).strftime("%Y-%m-%d %H:%M:%S %Z")
9090
status = "Healthy" if float(value) > 0 else "Unhealthy"
9191
if status == "Unhealthy":
9292
current = get_first_match_time(prom=prom, prom_query="up", match_value=0, hours=up_hours)
9393
metrics_data.append({
94-
"instance": service_name,
94+
"instance": instance,
95+
"job":job_name,
9596
"status": current
9697
})
9798
else:
9899
metrics_data.append({
99-
"instance": service_name,
100+
"instance": instance,
101+
"job": job_name,
100102
"status": "Healthy"
101103
})
102104
except Exception as e:
@@ -112,11 +114,13 @@ def process_time_query(query, service_name):
112114
try:
113115
result = prom.custom_query(query=query)
114116
if result and len(result) > 0:
115-
first_result = result[0]
116-
uptime_seconds = float(first_result["value"][1])
117-
up_hours = int(uptime_seconds/3600)
118-
if up_hours == 0:
119-
up_hours = 1
117+
for metric in result:
118+
instance = metric["metric"].get("instance", "unknown")
119+
job_name = metric["metric"].get("job", "unknown")
120+
uptime_seconds = float(metric["value"][1])
121+
up_hours = int(uptime_seconds / 3600)
122+
if up_hours == 0:
123+
up_hours = 1
120124
except Exception as e:
121125
print(f"Error processing time query '{query}': {e}")
122126

status/templates/health.html

Lines changed: 53 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -4,33 +4,71 @@
44
<title>Prometheus Metrics</title>
55
<style>
66
body {
7-
font-family: Arial, sans-serif;
8-
margin: 20px;
7+
font-family: 'Roboto', Arial, sans-serif;
8+
margin: 0;
9+
padding: 0;
10+
background-color: #f3f4f6;
11+
color: #333;
912
}
1013
h1 {
11-
color: #333;
14+
color: #222;
15+
font-size: 28px;
16+
text-align: center;
17+
margin: 20px 0;
18+
}
19+
p {
20+
text-align: center;
21+
font-size: 16px;
22+
color: #555;
23+
margin-bottom: 20px;
1224
}
1325
table {
1426
border-collapse: collapse;
15-
width: 100%;
16-
margin-top: 20px;
27+
width: 90%;
28+
margin: 0 auto;
29+
box-shadow: 0 6px 12px rgba(0, 0, 0, 0.1);
30+
background-color: #fff;
31+
border-radius: 8px;
32+
overflow: hidden;
1733
}
1834
th, td {
1935
border: 1px solid #ddd;
20-
padding: 8px;
21-
text-align: left;
36+
padding: 15px;
37+
text-align: center;
2238
}
2339
th {
24-
background-color: #f2f2f2;
40+
background-color: #007BFF;
41+
color: white;
42+
font-size: 18px;
43+
text-transform: uppercase;
44+
}
45+
td {
46+
font-size: 16px;
2547
}
2648
.healthy {
27-
color: green;
49+
color: #28a745;
50+
font-weight: bold;
51+
}
52+
.unhealthy {
53+
color: #dc3545;
54+
font-weight: bold;
2855
}
2956
.tie {
30-
color: blue;
57+
color: #17a2b8;
58+
font-weight: bold;
3159
}
32-
.unhealthy {
33-
color: red;
60+
tr:nth-child(even) {
61+
background-color: #f8f9fa;
62+
}
63+
tr:hover {
64+
background-color: #e9ecef;
65+
cursor: pointer;
66+
}
67+
footer {
68+
text-align: center;
69+
margin-top: 20px;
70+
font-size: 14px;
71+
color: #666;
3472
}
3573
</style>
3674
</head>
@@ -39,12 +77,12 @@ <h1>Prometheus Metrics</h1>
3977
<p>Last updated: {{ timestamp }}</p>
4078
<table>
4179
<tr>
42-
<th>Instance</th>
43-
<th>Value</th>
80+
<th>Job</th>
81+
<th>Status</th>
4482
</tr>
4583
{% for item in metrics %}
4684
<tr>
47-
<td>{{ item.instance }}</td>
85+
<td>{{ item.job }}</td>
4886
<td class="{% if item.status == 'Healthy' %}healthy{% else %}unhealthy{% endif %}">
4987
{{ item.status }}
5088
</td>

0 commit comments

Comments
 (0)