This repository has been archived by the owner on Aug 27, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
reporter.py
69 lines (57 loc) · 2.25 KB
/
reporter.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
import inspect
import os
import sys
import math
import threading
from http.server import HTTPServer
import socket
import time
import obd
from prometheus.collectors import Gauge
from prometheus.registry import Registry
from prometheus.exporter import PrometheusMetricHandler
print("Connecting to obd...")
connection = obd.OBD("/dev/ttyUSB0", 115200)
print("Connected...")
PORT_NUMBER = 8081
def gather_data(registry):
"""Gathers the metrics"""
host = socket.gethostname()
tags = {'host': host}
# Create our collectors
engine_rpm = Gauge("obd_engine_rpm", "OBD Engine RPM", tags)
engine_load = Gauge("obd_engine_load", "OBD Engine Load", tags)
vehicle_speed = Gauge("obd_vehicle_speed", "OBD Vehicle Speed", tags)
coolant_temp = Gauge("obd_coolant_temperature", "OBD Coolant temp", tags)
throttle_pos = Gauge("obd_throttle_position", "OBD throttle pos", tags)
timing_advance = Gauge("obd_timing_advance", "OBD timing advance", tags)
# register the metric collectors
metrics = [engine_rpm, engine_load, vehicle_speed, coolant_temp, throttle_pos, timing_advance]
commands = ["RPM", "ENGINE_LOAD", "SPEED", "COOLANT_TEMP", "THROTTLE_POS", "TIMING_ADVANCE"]
for metric in metrics:
registry.register(metric)
# Start gathering metrics every half second
while True:
time.sleep(0.5)
for idx, metric in enumerate(metrics):
command = commands[idx]
response = connection.query(obd.commands[command])
if response != None and not response.is_null():
value = response.value
metric.set({}, value.magnitude)
if __name__ == "__main__":
# Create the registry
registry = Registry()
# Create the thread that gathers the data while we serve it
thread = threading.Thread(target=gather_data, args=(registry, ))
thread.start()
# Set a server to export (expose to prometheus) the data (in a thread)
try:
# We make this to set the registry in the handler
def handler(*args, **kwargs):
PrometheusMetricHandler(registry, *args, **kwargs)
server = HTTPServer(('', PORT_NUMBER), handler)
server.serve_forever()
except KeyboardInterrupt:
server.socket.close()
thread.join()