kiwi-simple-metrics/kiwi_simple_metrics/main.py

51 lines
1 KiB
Python
Raw Normal View History

2023-08-31 22:10:40 +00:00
#!/usr/bin/python3
2023-08-31 22:55:31 +00:00
import asyncio
2023-08-31 23:49:02 +00:00
import urllib.parse
import requests
2023-08-31 22:55:31 +00:00
2023-08-31 11:17:19 +00:00
from . import metrics
2023-08-31 22:55:31 +00:00
from .settings import SETTINGS
2023-09-01 00:19:04 +00:00
def handle_report() -> None:
# create single report from metrics
report = metrics.Report.summary(
metrics.cpu(),
metrics.memory(),
metrics.disk(),
metrics.external(),
2023-09-01 00:19:04 +00:00
)
# maybe print this to stdout
if SETTINGS.log.enabled:
print(report)
# maybe push this to a webhook
if (url := SETTINGS.webhook.get_url(failed=report.failed)) is not None:
requests.get(
url=str(url).format(
urllib.parse.quote_plus(report.result)
),
verify=not SETTINGS.webhook.insecure,
2023-08-31 23:09:50 +00:00
)
2023-08-31 22:55:31 +00:00
2023-09-01 00:19:04 +00:00
async def run_metrics() -> None:
loop = asyncio.get_running_loop()
2023-08-31 23:49:02 +00:00
2023-09-01 00:19:04 +00:00
while True:
await asyncio.gather(
asyncio.sleep(SETTINGS.interval),
loop.run_in_executor(None, handle_report),
)
2023-08-30 22:01:31 +00:00
def main() -> None:
2023-09-01 00:19:04 +00:00
asyncio.run(run_metrics())
2023-08-30 23:49:07 +00:00
if __name__ == "__main__":
main()