-
Notifications
You must be signed in to change notification settings - Fork 0
/
prom_ping.py
executable file
·82 lines (66 loc) · 2.51 KB
/
prom_ping.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
#!/usr/bin/env python3
import os
import sys
import time
import subprocess
import re
import threading
from flask import Flask, jsonify, request
from prometheus_client import make_wsgi_app, Counter, Histogram, Gauge
from werkzeug.middleware.dispatcher import DispatcherMiddleware
loc = os.environ.get("LOCATION")
if loc is not None:
print("LOCATION:", loc)
else:
print("LOCATION env var is not set. Create .env and add 'export LOCATION=xyz'")
sys.exit(1)
system_info = os.uname()
computer_name = system_info.nodename
app = Flask(__name__)
app.wsgi_app = DispatcherMiddleware(app.wsgi_app, {
'/metrics': make_wsgi_app()
})
labels = [loc]
PING_GAUGE = Gauge('ping_gauge', 'Ping', ['name', 'location', 'destination'])
PING_HIST = Histogram('ping_histogram', 'Ping', ['name', 'location', 'destination'])
@app.route('/')
def index():
return redirect('/metrics')
def ping_and_get_time(destinationHost):
# Execute the ping command
command = ['ping', '-c', '1', destinationHost]
try:
output = subprocess.check_output(command, universal_newlines=True)
# Use regex to find the time value in the output
match = re.search(r'time=(\d+\.?\d*) ms', output)
if match:
# Return the time in milliseconds
return float(match.group(1))
else:
print(f"Time not found in ping output: {output}")
return 0
except subprocess.CalledProcessError as e:
print(f"Failed to execute ping: {str(e)}")
return 0
def ping_every_5_seconds():
while True:
### First measure ping to 8.8.8.8
destinationHost = '8.8.8.8'
ping_time = ping_and_get_time(destinationHost)
if ping_time != -1:
PING_HIST.labels(computer_name, loc, destinationHost).observe(ping_time)
PING_GAUGE.labels(computer_name, loc, destinationHost).set(ping_time)
### Then measure ping to sz.inetg.bg
destinationHost = 'sz.inetg.bg'
ping_time = ping_and_get_time(destinationHost)
if ping_time != -1:
PING_HIST.labels(computer_name, loc, destinationHost).observe(ping_time)
PING_GAUGE.labels(computer_name, loc, destinationHost).set(ping_time)
time.sleep(5)
if __name__ == '__main__':
# Create a thread that runs the ping_every_5_seconds() function
thread = threading.Thread(target=ping_every_5_seconds)
# Daemon threads automatically shut down when the main program exits
thread.daemon = True
thread.start()
app.run(host='0.0.0.0', port=5000)