-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add Prometheus monitoring (#120)
Co-authored-by: nhnn <[email protected]> Co-authored-by: Inex Code <[email protected]> Reviewed-on: https://git.selfprivacy.org/SelfPrivacy/selfprivacy-rest-api/pulls/120 Co-authored-by: dettlaff <[email protected]> Co-committed-by: dettlaff <[email protected]>
- Loading branch information
1 parent
1259c08
commit 4cd90d0
Showing
15 changed files
with
1,416 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,120 @@ | ||
import strawberry | ||
from typing import Optional | ||
from datetime import datetime | ||
from selfprivacy_api.models.services import ServiceStatus | ||
from selfprivacy_api.services.prometheus import Prometheus | ||
from selfprivacy_api.utils.monitoring import ( | ||
MonitoringQueries, | ||
MonitoringQueryError, | ||
MonitoringValuesResult, | ||
MonitoringMetricsResult, | ||
) | ||
|
||
|
||
@strawberry.type | ||
class CpuMonitoring: | ||
start: Optional[datetime] | ||
end: Optional[datetime] | ||
step: int | ||
|
||
@strawberry.field | ||
def overall_usage(self) -> MonitoringValuesResult: | ||
if Prometheus().get_status() != ServiceStatus.ACTIVE: | ||
return MonitoringQueryError(error="Prometheus is not running") | ||
|
||
return MonitoringQueries.cpu_usage_overall(self.start, self.end, self.step) | ||
|
||
|
||
@strawberry.type | ||
class MemoryMonitoring: | ||
start: Optional[datetime] | ||
end: Optional[datetime] | ||
step: int | ||
|
||
@strawberry.field | ||
def overall_usage(self) -> MonitoringValuesResult: | ||
if Prometheus().get_status() != ServiceStatus.ACTIVE: | ||
return MonitoringQueryError(error="Prometheus is not running") | ||
|
||
return MonitoringQueries.memory_usage_overall(self.start, self.end, self.step) | ||
|
||
@strawberry.field | ||
def average_usage_by_service(self) -> MonitoringMetricsResult: | ||
if Prometheus().get_status() != ServiceStatus.ACTIVE: | ||
return MonitoringQueryError(error="Prometheus is not running") | ||
|
||
return MonitoringQueries.memory_usage_average_by_slice(self.start, self.end) | ||
|
||
@strawberry.field | ||
def max_usage_by_service(self) -> MonitoringMetricsResult: | ||
if Prometheus().get_status() != ServiceStatus.ACTIVE: | ||
return MonitoringQueryError(error="Prometheus is not running") | ||
|
||
return MonitoringQueries.memory_usage_max_by_slice(self.start, self.end) | ||
|
||
|
||
@strawberry.type | ||
class DiskMonitoring: | ||
start: Optional[datetime] | ||
end: Optional[datetime] | ||
step: int | ||
|
||
@strawberry.field | ||
def overall_usage(self) -> MonitoringMetricsResult: | ||
if Prometheus().get_status() != ServiceStatus.ACTIVE: | ||
return MonitoringQueryError(error="Prometheus is not running") | ||
|
||
return MonitoringQueries.disk_usage_overall(self.start, self.end, self.step) | ||
|
||
|
||
@strawberry.type | ||
class NetworkMonitoring: | ||
start: Optional[datetime] | ||
end: Optional[datetime] | ||
step: int | ||
|
||
@strawberry.field | ||
def overall_usage(self) -> MonitoringMetricsResult: | ||
if Prometheus().get_status() != ServiceStatus.ACTIVE: | ||
return MonitoringQueryError(error="Prometheus is not running") | ||
|
||
return MonitoringQueries.network_usage_overall(self.start, self.end, self.step) | ||
|
||
|
||
@strawberry.type | ||
class Monitoring: | ||
@strawberry.field | ||
def cpu_usage( | ||
self, | ||
start: Optional[datetime] = None, | ||
end: Optional[datetime] = None, | ||
step: int = 60, | ||
) -> CpuMonitoring: | ||
return CpuMonitoring(start=start, end=end, step=step) | ||
|
||
@strawberry.field | ||
def memory_usage( | ||
self, | ||
start: Optional[datetime] = None, | ||
end: Optional[datetime] = None, | ||
step: int = 60, | ||
) -> MemoryMonitoring: | ||
return MemoryMonitoring(start=start, end=end, step=step) | ||
|
||
@strawberry.field | ||
def disk_usage( | ||
self, | ||
start: Optional[datetime] = None, | ||
end: Optional[datetime] = None, | ||
step: int = 60, | ||
) -> DiskMonitoring: | ||
return DiskMonitoring(start=start, end=end, step=step) | ||
|
||
@strawberry.field | ||
def network_usage( | ||
self, | ||
start: Optional[datetime] = None, | ||
end: Optional[datetime] = None, | ||
step: int = 60, | ||
) -> NetworkMonitoring: | ||
return NetworkMonitoring(start=start, end=end, step=step) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
from selfprivacy_api.migrations.migration import Migration | ||
|
||
from selfprivacy_api.services.flake_service_manager import FlakeServiceManager | ||
from selfprivacy_api.utils import ReadUserData, WriteUserData | ||
from selfprivacy_api.utils.block_devices import BlockDevices | ||
|
||
|
||
class AddMonitoring(Migration): | ||
"""Adds monitoring service if it is not present.""" | ||
|
||
def get_migration_name(self) -> str: | ||
return "add_monitoring" | ||
|
||
def get_migration_description(self) -> str: | ||
return "Adds the Monitoring if it is not present." | ||
|
||
def is_migration_needed(self) -> bool: | ||
with FlakeServiceManager() as manager: | ||
if "monitoring" not in manager.services: | ||
return True | ||
with ReadUserData() as data: | ||
if "monitoring" not in data["modules"]: | ||
return True | ||
return False | ||
|
||
def migrate(self) -> None: | ||
with FlakeServiceManager() as manager: | ||
if "monitoring" not in manager.services: | ||
manager.services["monitoring"] = ( | ||
"git+https://git.selfprivacy.org/SelfPrivacy/selfprivacy-nixos-config.git?ref=flakes&dir=sp-modules/monitoring" | ||
) | ||
with WriteUserData() as data: | ||
if "monitoring" not in data["modules"]: | ||
data["modules"]["monitoring"] = { | ||
"enable": True, | ||
"location": BlockDevices().get_root_block_device().name, | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,86 @@ | ||
"""Class representing Nextcloud service.""" | ||
|
||
import base64 | ||
import subprocess | ||
from typing import Optional, List | ||
|
||
from selfprivacy_api.services.owned_path import OwnedPath | ||
from selfprivacy_api.utils.systemd import get_service_status | ||
from selfprivacy_api.services.service import Service, ServiceStatus | ||
|
||
from selfprivacy_api.services.prometheus.icon import PROMETHEUS_ICON | ||
|
||
|
||
class Prometheus(Service): | ||
"""Class representing Prometheus service.""" | ||
|
||
@staticmethod | ||
def get_id() -> str: | ||
return "monitoring" | ||
|
||
@staticmethod | ||
def get_display_name() -> str: | ||
return "Prometheus" | ||
|
||
@staticmethod | ||
def get_description() -> str: | ||
return "Prometheus is used for resource monitoring and alerts." | ||
|
||
@staticmethod | ||
def get_svg_icon() -> str: | ||
return base64.b64encode(PROMETHEUS_ICON.encode("utf-8")).decode("utf-8") | ||
|
||
@staticmethod | ||
def get_url() -> Optional[str]: | ||
"""Return service url.""" | ||
return None | ||
|
||
@staticmethod | ||
def get_subdomain() -> Optional[str]: | ||
return None | ||
|
||
@staticmethod | ||
def is_movable() -> bool: | ||
return False | ||
|
||
@staticmethod | ||
def is_required() -> bool: | ||
return True | ||
|
||
@staticmethod | ||
def can_be_backed_up() -> bool: | ||
return False | ||
|
||
@staticmethod | ||
def get_backup_description() -> str: | ||
return "Backups are not available for Prometheus." | ||
|
||
@staticmethod | ||
def get_status() -> ServiceStatus: | ||
return get_service_status("prometheus.service") | ||
|
||
@staticmethod | ||
def stop(): | ||
subprocess.run(["systemctl", "stop", "prometheus.service"]) | ||
|
||
@staticmethod | ||
def start(): | ||
subprocess.run(["systemctl", "start", "prometheus.service"]) | ||
|
||
@staticmethod | ||
def restart(): | ||
subprocess.run(["systemctl", "restart", "prometheus.service"]) | ||
|
||
@staticmethod | ||
def get_logs(): | ||
return "" | ||
|
||
@staticmethod | ||
def get_owned_folders() -> List[OwnedPath]: | ||
return [ | ||
OwnedPath( | ||
path="/var/lib/prometheus", | ||
owner="prometheus", | ||
group="prometheus", | ||
), | ||
] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
PROMETHEUS_ICON = """ | ||
<svg width="128" height="128" viewBox="0 0 128 128" fill="none" xmlns="http://www.w3.org/2000/svg"> | ||
<path d="M64.125 0.51C99.229 0.517 128.045 29.133 128 63.951C127.955 99.293 99.258 127.515 63.392 127.49C28.325 127.466 -0.0249987 98.818 1.26289e-06 63.434C0.0230013 28.834 28.898 0.503 64.125 0.51ZM44.72 22.793C45.523 26.753 44.745 30.448 43.553 34.082C42.73 36.597 41.591 39.022 40.911 41.574C39.789 45.777 38.52 50.004 38.052 54.3C37.381 60.481 39.81 65.925 43.966 71.34L24.86 67.318C24.893 67.92 24.86 68.148 24.925 68.342C26.736 73.662 29.923 78.144 33.495 82.372C33.872 82.818 34.732 83.046 35.372 83.046C54.422 83.084 73.473 83.08 92.524 83.055C93.114 83.055 93.905 82.945 94.265 82.565C98.349 78.271 101.47 73.38 103.425 67.223L83.197 71.185C84.533 68.567 86.052 66.269 86.93 63.742C89.924 55.099 88.682 46.744 84.385 38.862C80.936 32.538 77.754 26.242 79.475 18.619C75.833 22.219 74.432 26.798 73.543 31.517C72.671 36.167 72.154 40.881 71.478 45.6C71.38 45.457 71.258 45.35 71.236 45.227C71.1507 44.7338 71.0919 44.2365 71.06 43.737C70.647 36.011 69.14 28.567 65.954 21.457C64.081 17.275 62.013 12.995 63.946 8.001C62.639 8.694 61.456 9.378 60.608 10.357C58.081 13.277 57.035 16.785 56.766 20.626C56.535 23.908 56.22 27.205 55.61 30.432C54.97 33.824 53.96 37.146 51.678 40.263C50.76 33.607 50.658 27.019 44.722 22.793H44.72ZM93.842 88.88H34.088V99.26H93.842V88.88ZM45.938 104.626C45.889 113.268 54.691 119.707 65.571 119.24C74.591 118.851 82.57 111.756 81.886 104.626H45.938Z" fill="black"/> | ||
</svg> | ||
""" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.