-
Notifications
You must be signed in to change notification settings - Fork 0
/
prom.py
executable file
·133 lines (102 loc) · 4.53 KB
/
prom.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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
#!/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
from Phidget22.Phidget import *
from Phidget22.Devices.VoltageRatioInput import *
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)
phidgets_serial = os.environ.get("PHIDGETS_SERIAL")
if phidgets_serial is not None:
print("PHIDGETS_SERIAL:", phidgets_serial)
else:
print("PHIDGETS_SERIAL env var is not set. Create .env and add 'export PHIDGETS_SERIAL=123'")
sys.exit(1)
ping_hosts = [host.strip() for host in os.getenv('PING_HOSTS', '').split(',')]
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]
TEM_GAUGE = Gauge('temperature_gauge', 'Temperature', ['name', 'symbol', 'location'])
TEM_HIST = Histogram('temperature_histogram', 'Temperature', ['name', 'symbol', 'location'])
HUM_GAUGE = Gauge('humidity_gauge', 'Humidity', ['name', 'symbol', 'location'])
HUM_HIST = Histogram('humidity_histogram', 'Humidity', ['name', 'symbol', 'location'])
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 onTempChange(self, sensorValue, sensorUnit):
temperature = sensorValue
symbol = sensorUnit.symbol
# sensorUnit.symbol for temperature sensor is "°C"
# we are converting the temperature - so we rewrite symbol to "°F"
temperature = (temperature * 9/5) + 32
symbol = "°F"
TEM_HIST.labels(computer_name, symbol, loc).observe(temperature)
TEM_GAUGE.labels(computer_name, symbol, loc).set(temperature)
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 -1
except subprocess.CalledProcessError as e:
print(f"Failed to execute ping: {str(e)}")
return -1
def ping_every_5_seconds():
while True:
for destinationHost in ping_hosts:
### print(f"Working on Ping Host {destinationHost}")
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)
def onHumidityChange(self, sensorValue, sensorUnit):
HUM_HIST.labels(computer_name, sensorUnit.symbol, loc).observe(sensorValue)
HUM_GAUGE.labels(computer_name, sensorUnit.symbol, loc).set(sensorValue)
if __name__ == '__main__':
voltageRatioInput0 = VoltageRatioInput()
voltageRatioInput1 = VoltageRatioInput()
voltageRatioInput0.setIsHubPortDevice(True)
voltageRatioInput0.setHubPort(0)
voltageRatioInput0.setDeviceSerialNumber(int(phidgets_serial))
voltageRatioInput1.setIsHubPortDevice(True)
voltageRatioInput1.setHubPort(1)
voltageRatioInput1.setDeviceSerialNumber(int(phidgets_serial))
voltageRatioInput0.setOnSensorChangeHandler(onTempChange)
voltageRatioInput1.setOnSensorChangeHandler(onHumidityChange)
voltageRatioInput0.openWaitForAttachment(5000)
voltageRatioInput1.openWaitForAttachment(5000)
# Sensor Types: SENSOR_TYPE_1125_HUMIDITY, SENSOR_TYPE_1125_TEMPERATURE
voltageRatioInput0.setSensorType(VoltageRatioSensorType.SENSOR_TYPE_1125_TEMPERATURE)
voltageRatioInput1.setSensorType(VoltageRatioSensorType.SENSOR_TYPE_1125_HUMIDITY)
# 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)
voltageRatioInput0.close()
voltageRatioInput1.close()