-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathuptime_kuma_monitor.py
49 lines (40 loc) · 1.73 KB
/
uptime_kuma_monitor.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
"""Contains the UptimeKuma class"""
from prometheus_client.parser import text_string_to_metric_families as pc
import requests
class UptimeKumaMonitor:
"""An object containing a dictionary representation of data returned by
UptimeKuma `/metrics` endpoint
Attributes:
uptime_kuma_url (str): Full https url to a uptime_kuma instance
user (str): Username of the Uptime Kuma user with access to the /metrics endpoint
password (str): Password for Uptime Kuma user
verify_ssl (bool): Allow bypassing ssl verification, but verify by default
"""
def __init__(self, uptime_kuma_url, user, password, verify_ssl=True):
self.data = dict()
self.metrics_url = (
f"{uptime_kuma_url}/metrics"
)
self.user = user
self.password = password
self.verify_ssl = verify_ssl
self.update()
def update(self):
try:
response = requests.get(
self.metrics_url, auth=(self.user, self.password), verify=self.verify_ssl
)
self.metrics = response.content
for family in pc(str(self.metrics, "UTF-8")):
for sample in family.samples:
if sample[0].startswith("monitor"): self.data [sample[1]["monitor_name"]] = {}
for family in pc(str(self.metrics, "UTF-8")):
for sample in family.samples:
if sample[0].startswith("monitor"): self.data [sample[1]["monitor_name"]][sample[0]] = sample[2]
except Exception as error:
raise UptimeKumaError(
f"Could not fetch Uptime Kuma metrics: {error}"
)
class UptimeKumaError(Exception):
"""Failed to fetch Uptime Kuma metrics."""
pass