chore: Review fixes

This commit is contained in:
Inex Code 2024-07-25 16:48:34 +03:00
parent 6f18eefc25
commit e0380d149b
3 changed files with 55 additions and 31 deletions

View file

@ -33,5 +33,5 @@ class AddMonitoring(Migration):
if "monitoring" not in data["modules"]: if "monitoring" not in data["modules"]:
data["modules"]["monitoring"] = { data["modules"]["monitoring"] = {
"enable": True, "enable": True,
"location": BlockDevices().get_root_block_device().name "location": BlockDevices().get_root_block_device().name,
} }

View file

@ -12,7 +12,7 @@ from selfprivacy_api.services.prometheus.icon import PROMETHEUS_ICON
class Prometheus(Service): class Prometheus(Service):
"""Class representing Pleroma service.""" """Class representing Prometheus service."""
@staticmethod @staticmethod
def get_id() -> str: def get_id() -> str:
@ -24,7 +24,7 @@ class Prometheus(Service):
@staticmethod @staticmethod
def get_description() -> str: def get_description() -> str:
return "Prometheus is a free software application used for event monitoring and alerting." return "Prometheus is used for resource monitoring and alerts."
@staticmethod @staticmethod
def get_svg_icon() -> str: def get_svg_icon() -> str:
@ -49,7 +49,7 @@ class Prometheus(Service):
@staticmethod @staticmethod
def get_backup_description() -> str: def get_backup_description() -> str:
return "For Prometheus backups are not available." return "Backups are not available for Prometheus."
@staticmethod @staticmethod
def get_status() -> ServiceStatus: def get_status() -> ServiceStatus:

View file

@ -44,81 +44,105 @@ class PrometheusQueries:
@staticmethod @staticmethod
def cpu_usage( def cpu_usage(
start: Optional[int] = None, start: Optional[datetime] = None,
end: Optional[int] = None, end: Optional[datetime] = None,
step: int = 60, # seconds step: int = 60, # seconds
) -> PrometheusQueryResult: ) -> PrometheusQueryResult:
""" """
Get CPU information. Get CPU information.
Args: Args:
start (int, optional): Unix timestamp indicating the start time. start (datetime, optional): The start time.
Defaults to 20 minutes ago if not provided. Defaults to 20 minutes ago if not provided.
end (int, optional): Unix timestamp indicating the end time. end (datetime, optional): The end time.
Defaults to current time if not provided. Defaults to current time if not provided.
step (int): Interval in seconds for querying disk usage data. step (int): Interval in seconds for querying disk usage data.
""" """
if not start: if start is None:
start = int((datetime.now() - timedelta(minutes=20)).timestamp()) start = datetime.now() - timedelta(minutes=20)
if not end: if end is None:
end = int(datetime.now().timestamp()) end = datetime.now()
start_timestamp = int(start.timestamp())
end_timestamp = int(end.timestamp())
query = '100 - (avg by (instance) (rate(node_cpu_seconds_total{mode="idle"}[5m])) * 100)' query = '100 - (avg by (instance) (rate(node_cpu_seconds_total{mode="idle"}[5m])) * 100)'
return PrometheusQueries._send_query(query, start, end, step) return PrometheusQueries._send_query(
query,
start_timestamp,
end_timestamp,
step,
)
@staticmethod @staticmethod
def memory_usage( def memory_usage(
start: Optional[int] = None, start: Optional[datetime] = None,
end: Optional[int] = None, end: Optional[datetime] = None,
step: int = 60, # seconds step: int = 60, # seconds
) -> PrometheusQueryResult: ) -> PrometheusQueryResult:
""" """
Get memory usage. Get memory usage.
Args: Args:
start (int, optional): Unix timestamp indicating the start time. start (datetime, optional): The start time.
Defaults to 20 minutes ago if not provided. Defaults to 20 minutes ago if not provided.
end (int, optional): Unix timestamp indicating the end time. end (datetime, optional): The end time.
Defaults to current time if not provided. Defaults to current time if not provided.
step (int): Interval in seconds for querying memory usage data. step (int): Interval in seconds for querying memory usage data.
""" """
if not start: if start is None:
start = int((datetime.now() - timedelta(minutes=20)).timestamp()) start = datetime.now() - timedelta(minutes=20)
if not end: if end is None:
end = int(datetime.now().timestamp()) end = datetime.now()
start_timestamp = int(start.timestamp())
end_timestamp = int(end.timestamp())
query = "100 - (100 * (node_memory_MemAvailable_bytes / node_memory_MemTotal_bytes))" query = "100 - (100 * (node_memory_MemAvailable_bytes / node_memory_MemTotal_bytes))"
return PrometheusQueries._send_query(query, start, end, step) return PrometheusQueries._send_query(
query,
start_timestamp,
end_timestamp,
step,
)
@staticmethod @staticmethod
def disk_usage( def disk_usage(
start: Optional[int] = None, start: Optional[datetime] = None,
end: Optional[int] = None, end: Optional[datetime] = None,
step: int = 60, # seconds step: int = 60, # seconds
) -> PrometheusQueryResult: ) -> PrometheusQueryResult:
""" """
Get disk usage information. Get disk usage information.
Args: Args:
start (int, optional): Unix timestamp indicating the start time. start (datetime, optional): The start time.
Defaults to 20 minutes ago if not provided. Defaults to 20 minutes ago if not provided.
end (int, optional): Unix timestamp indicating the end time. end (datetime, optional): The end time.
Defaults to current time if not provided. Defaults to current time if not provided.
step (int): Interval in seconds for querying disk usage data. step (int): Interval in seconds for querying disk usage data.
""" """
if not start: if start is None:
start = int((datetime.now() - timedelta(minutes=20)).timestamp()) start = datetime.now() - timedelta(minutes=20)
if not end: if end is None:
end = int(datetime.now().timestamp()) end = datetime.now()
start_timestamp = int(start.timestamp())
end_timestamp = int(end.timestamp())
query = """100 - (100 * sum by (device) (node_filesystem_avail_bytes{fstype!="rootfs"}) / sum by (device) (node_filesystem_size_bytes{fstype!="rootfs"}))""" query = """100 - (100 * sum by (device) (node_filesystem_avail_bytes{fstype!="rootfs"}) / sum by (device) (node_filesystem_size_bytes{fstype!="rootfs"}))"""
return PrometheusQueries._send_query(query, start, end, step) return PrometheusQueries._send_query(
query,
start_timestamp,
end_timestamp,
step,
)