selfprivacy-rest-api/selfprivacy_api/utils/prometheus.py

127 lines
4 KiB
Python
Raw Normal View History

2024-06-15 18:17:08 +00:00
"""Prometheus monitoring queries."""
# pylint: disable=too-few-public-methods
2024-06-22 12:12:29 +00:00
import requests
2024-07-08 15:00:49 +00:00
import strawberry
from strawberry.scalars import JSON
2024-07-08 15:00:49 +00:00
from dataclasses import dataclass
from typing import Optional
2024-06-25 17:25:31 +00:00
from datetime import datetime, timedelta
2024-06-15 18:17:08 +00:00
2024-06-17 18:56:58 +00:00
PROMETHEUS_URL = "http://localhost:9001"
2024-06-15 18:17:08 +00:00
2024-07-08 15:00:49 +00:00
@strawberry.type
@dataclass
class PrometheusQueryResult:
result_type: str
result: JSON
2024-06-15 18:17:08 +00:00
class PrometheusQueries:
@staticmethod
2024-07-16 02:41:06 +00:00
def _send_query(query: str, start: int, end: int, step: int):
2024-06-15 18:17:08 +00:00
try:
2024-07-08 15:00:49 +00:00
response = requests.get(
2024-07-16 02:41:06 +00:00
f"{PROMETHEUS_URL}/api/v1/query",
2024-07-08 15:00:49 +00:00
params={
"query": query,
2024-07-16 02:41:06 +00:00
"start": start,
"end": end,
2024-07-08 15:00:49 +00:00
"step": step,
},
2024-06-15 18:17:08 +00:00
)
2024-07-24 17:04:03 +00:00
print(response)
2024-07-08 15:00:49 +00:00
if response.status_code != 200:
raise Exception("Prometheus returned unexpected HTTP status code")
json = response.json()
2024-07-24 17:04:03 +00:00
print(json)
2024-07-08 15:00:49 +00:00
return PrometheusQueryResult(
result_type=json["resultType"], result=json["result"]
2024-06-15 18:17:08 +00:00
)
2024-07-08 15:00:49 +00:00
except Exception as error:
raise Exception("Prometheus request failed! " + str(error))
2024-06-15 18:17:08 +00:00
@staticmethod
2024-06-21 16:33:37 +00:00
def cpu_usage(
2024-07-16 02:41:06 +00:00
start: Optional[int] = None,
end: Optional[int] = None,
2024-07-07 12:33:15 +00:00
step: int = 60, # seconds
2024-07-08 15:00:49 +00:00
) -> PrometheusQueryResult:
"""
Get CPU information.
2024-07-07 12:33:15 +00:00
Args:
2024-07-16 02:41:06 +00:00
start (int, optional): Unix timestamp indicating the start time.
2024-07-07 12:33:15 +00:00
Defaults to 20 minutes ago if not provided.
2024-07-16 02:41:06 +00:00
end (int, optional): Unix timestamp indicating the end time.
2024-07-07 12:33:15 +00:00
Defaults to current time if not provided.
step (int): Interval in seconds for querying disk usage data.
"""
if not start:
2024-07-16 02:41:06 +00:00
start = int((datetime.now() - timedelta(minutes=20)).timestamp())
2024-07-07 12:33:15 +00:00
if not end:
2024-07-16 02:41:06 +00:00
end = int(datetime.now().timestamp())
2024-06-21 16:33:37 +00:00
2024-06-15 18:17:08 +00:00
query = '100 - (avg by (instance) (rate(node_cpu_seconds_total{mode="idle"}[5m])) * 100)'
2024-07-08 15:00:49 +00:00
return PrometheusQueries._send_query(query, start, end, step)
2024-07-08 15:18:07 +00:00
@staticmethod
def memory_usage(
2024-07-16 02:41:06 +00:00
start: Optional[int] = None,
end: Optional[int] = None,
2024-07-08 15:18:07 +00:00
step: int = 60, # seconds
) -> PrometheusQueryResult:
"""
Get memory usage.
Args:
2024-07-16 02:41:06 +00:00
start (int, optional): Unix timestamp indicating the start time.
2024-07-08 15:18:07 +00:00
Defaults to 20 minutes ago if not provided.
2024-07-16 02:41:06 +00:00
end (int, optional): Unix timestamp indicating the end time.
2024-07-08 15:18:07 +00:00
Defaults to current time if not provided.
step (int): Interval in seconds for querying memory usage data.
"""
if not start:
2024-07-16 02:41:06 +00:00
start = int((datetime.now() - timedelta(minutes=20)).timestamp())
2024-07-08 15:18:07 +00:00
if not end:
2024-07-16 02:41:06 +00:00
end = int(datetime.now().timestamp())
2024-07-08 15:18:07 +00:00
query = "100 - (100 * (node_memory_MemAvailable_bytes / node_memory_MemTotal_bytes))"
return PrometheusQueries._send_query(query, start, end, step)
2024-06-15 18:17:08 +00:00
@staticmethod
2024-06-21 16:33:37 +00:00
def disk_usage(
2024-07-16 02:41:06 +00:00
start: Optional[int] = None,
end: Optional[int] = None,
2024-07-07 12:33:15 +00:00
step: int = 60, # seconds
2024-07-08 15:00:49 +00:00
) -> PrometheusQueryResult:
2024-07-07 12:33:15 +00:00
"""
Get disk usage information.
Args:
2024-07-16 02:41:06 +00:00
start (int, optional): Unix timestamp indicating the start time.
2024-07-07 12:33:15 +00:00
Defaults to 20 minutes ago if not provided.
2024-07-16 02:41:06 +00:00
end (int, optional): Unix timestamp indicating the end time.
2024-07-07 12:33:15 +00:00
Defaults to current time if not provided.
step (int): Interval in seconds for querying disk usage data.
"""
if not start:
2024-07-16 02:41:06 +00:00
start = int((datetime.now() - timedelta(minutes=20)).timestamp())
2024-07-07 12:33:15 +00:00
if not end:
2024-07-16 02:41:06 +00:00
end = int(datetime.now().timestamp())
2024-07-07 12:33:15 +00:00
2024-07-16 02:41:06 +00:00
query = """100 - (100 * sum by (device) (node_filesystem_avail_bytes{fstype!="rootfs"}) / sum by (device) (node_filesystem_size_bytes{fstype!="rootfs"}))"""
2024-06-16 19:01:25 +00:00
2024-07-08 15:00:49 +00:00
return PrometheusQueries._send_query(query, start, end, step)