From 847ba1f4101d707751e45b9cde7942b61c1d1a39 Mon Sep 17 00:00:00 2001 From: "Peter J. Holzer" Date: Fri, 2 Sep 2022 18:42:29 +0200 Subject: [PATCH] Report loadavg (demo client) --- clients/report_loadavg | 52 ++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 52 insertions(+) create mode 100755 clients/report_loadavg diff --git a/clients/report_loadavg b/clients/report_loadavg new file mode 100755 index 0000000..0dd946e --- /dev/null +++ b/clients/report_loadavg @@ -0,0 +1,52 @@ +#!/usr/bin/python3 + +import hmac +import json +import os +import pprint +import socket +import time + +import requests + + +hostname = socket.gethostbyaddr(socket.gethostname())[0] +baseurl = "http://127.0.0.1:5000/" + +def report_loadavg(): + now = time.time() + load = os.getloadavg() + with open("config.json") as fh: + client_config = json.load(fh) + + while True: + report = [] + for i, period in enumerate(("1m", "5m", "15m")): + node = hostname + timestamp = time.time() + msg = (node + " " + str(timestamp)).encode("UTF-8") + digest = hmac.new(client_config["key"].encode("UTF-8"), msg, "SHA-256").hexdigest() + report.append( + { + "description": { + "hostname": hostname, + "measure": "loadavg", + "unit": "processes", + "period": period, + }, + "data": [ + [ now, load[i] ] + ], + "auth": { + "node": node, + "timestamp": timestamp, + "hmac": digest, + } + }, + ) + pprint.pp(report) + r = requests.post(baseurl + "report", json=report) + print(r) + return + +report_loadavg()