2022-11-26 19:16:00 +01:00
|
|
|
import hmac
|
|
|
|
import json
|
|
|
|
import os
|
|
|
|
import random
|
|
|
|
import socket
|
|
|
|
import time
|
|
|
|
|
|
|
|
import requests
|
|
|
|
|
2023-01-07 13:26:40 +01:00
|
|
|
node = socket.gethostbyaddr(socket.gethostname())[0]
|
|
|
|
|
2023-01-07 13:08:44 +01:00
|
|
|
def record_observations(observations):
|
2022-11-26 19:16:00 +01:00
|
|
|
for dir in (".", os.environ["HOME"] + "/.config/ltsdb", "/etc/ltsdb"):
|
|
|
|
try:
|
|
|
|
with open(dir + "/config.json") as fh:
|
|
|
|
client_config = json.load(fh)
|
|
|
|
baseurl = client_config["server"]
|
|
|
|
break
|
|
|
|
except FileNotFoundError:
|
|
|
|
pass
|
|
|
|
while True:
|
2023-01-07 13:08:44 +01:00
|
|
|
for obs in observations:
|
2022-11-26 19:16:00 +01:00
|
|
|
timestamp = time.time()
|
|
|
|
msg = (node + " " + str(timestamp)).encode("UTF-8")
|
|
|
|
digest = hmac.new(client_config["key"].encode("UTF-8"), msg, "SHA256").hexdigest()
|
2023-01-07 13:08:44 +01:00
|
|
|
obs["auth"] = {
|
2022-11-26 19:16:00 +01:00
|
|
|
"node": node,
|
|
|
|
"timestamp": timestamp,
|
|
|
|
"hmac": digest,
|
|
|
|
}
|
2023-01-07 13:08:44 +01:00
|
|
|
r = requests.post(baseurl + "record", json=observations)
|
2022-11-26 19:16:00 +01:00
|
|
|
print(r)
|
|
|
|
if r.status_code == 200:
|
|
|
|
return True
|
|
|
|
elif r.status_code == 409:
|
|
|
|
time.sleep(0.5 + random.random())
|
|
|
|
continue
|
|
|
|
else:
|
|
|
|
return False
|
|
|
|
|