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:
|
|
|
|
# === conc experiment ===
|
|
|
|
|
|
|
|
futures = concurrent.futures.wait(
|
|
|
|
executor.submit(metric) for metric in _metrics
|
|
|
|
).done
|
|
|
|
|
|
|
|
print(list(future.result() for future in futures))
|
|
|
|
|
|
|
|
# === end conc ===
|
|
|
|
|
2023-09-02 01:56:20 +00:00
|
|
|
reports = (metric() for metric in _metrics)
|
|
|
|
|
2023-09-01 00:19:04 +00:00
|
|
|
# create single report from metrics
|
2023-09-02 01:56:20 +00:00
|
|
|
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-01 00:19:04 +00:00
|
|
|
while True:
|
2023-09-02 02:14:40 +00:00
|
|
|
with concurrent.futures.ThreadPoolExecutor() as pool:
|
|
|
|
await asyncio.gather(
|
|
|
|
asyncio.sleep(SETTINGS.interval),
|
|
|
|
loop.run_in_executor(
|
|
|
|
None, run_metrics,
|
|
|
|
pool,
|
|
|
|
metrics.cpu,
|
|
|
|
metrics.memory,
|
|
|
|
metrics.disk,
|
|
|
|
metrics.external,
|
|
|
|
),
|
|
|
|
)
|
2023-08-30 22:01:31 +00:00
|
|
|
|
2023-08-30 21:08:16 +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
|
|
|
|
2023-08-30 21:08:16 +00:00
|
|
|
|
|
|
|
if __name__ == "__main__":
|
|
|
|
main()
|