ltsdb/clients/record_loadavg

56 lines
1.5 KiB
Plaintext
Raw Permalink Normal View History

2022-09-02 18:42:29 +02:00
#!/usr/bin/python3
import hmac
import json
import os
import pprint
2022-09-04 13:20:41 +02:00
import random
2022-09-02 18:42:29 +02:00
import socket
import time
import requests
hostname = socket.gethostbyaddr(socket.gethostname())[0]
def report_loadavg():
now = time.time()
load = os.getloadavg()
with open("config.json") as fh:
client_config = json.load(fh)
2022-09-04 12:47:20 +02:00
baseurl = client_config["server"]
2022-09-02 18:42:29 +02:00
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, "SHA256").hexdigest()
2022-09-02 18:42:29 +02:00
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)
2022-09-04 13:20:41 +02:00
if r.status_code != 409:
return
time.sleep(0.5 + random.random())
2022-09-02 18:42:29 +02:00
report_loadavg()