kiwi-simple-metrics/kiwi_simple_metrics/main.py

72 lines
1.6 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-09-02 02:14:40 +00:00
import concurrent.futures
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-02 02:14:40 +00:00
def run_metrics(
executor: concurrent.futures.Executor,
*_metrics: metrics.Metric,
) -> None:
2023-09-02 02:38:12 +00:00
# run metrics in executor
tasks = [
executor.submit(metric)
for metric in _metrics
]
# wait for finish
# pair up each result with its task index
results = (
(tasks.index(future), future.result())
for future in concurrent.futures.wait(tasks).done
)
# extract reports in task index order
reports = (
report
for _, report in sorted(results, key=lambda x: x[0])
)
# create summary report
report = metrics.Report.summary(*reports)
2023-09-01 00:19:04 +00:00
# maybe print this to stdout
if SETTINGS.log.enabled:
print(report)
2023-09-01 22:56:20 +00:00
# maybe push this to the configured webhook
report.push_webhook()
2023-08-31 23:09:50 +00:00
2023-08-31 22:55:31 +00:00
2023-09-02 02:14:40 +00:00
async def async_main_loop() -> None:
2023-09-01 00:19:04 +00:00
loop = asyncio.get_running_loop()
2023-08-31 23:49:02 +00:00
2023-09-02 14:33:38 +00:00
with concurrent.futures.ThreadPoolExecutor(
max_workers=SETTINGS.threads,
) as pool:
while True:
# start interval and metrics at the same time
2023-09-02 02:14:40 +00:00
await asyncio.gather(
asyncio.sleep(SETTINGS.interval),
loop.run_in_executor(
None, run_metrics,
pool,
2023-09-02 14:33:38 +00:00
# metrics are reported in this order
2023-09-02 02:14:40 +00:00
metrics.cpu,
metrics.memory,
metrics.disk,
metrics.external,
),
)
2023-08-30 22:01:31 +00:00
def main() -> None:
2023-09-02 02:14:40 +00:00
asyncio.run(async_main_loop())
2023-08-30 23:49:07 +00:00
if __name__ == "__main__":
main()