Factor out send_report
This commit is contained in:
parent
803a56d6df
commit
4bf3508d26
|
@ -0,0 +1,39 @@
|
||||||
|
import hmac
|
||||||
|
import json
|
||||||
|
import os
|
||||||
|
import random
|
||||||
|
import socket
|
||||||
|
import time
|
||||||
|
|
||||||
|
import requests
|
||||||
|
|
||||||
|
def send_report(report):
|
||||||
|
node = socket.gethostbyaddr(socket.gethostname())[0]
|
||||||
|
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:
|
||||||
|
for r in report:
|
||||||
|
timestamp = time.time()
|
||||||
|
msg = (node + " " + str(timestamp)).encode("UTF-8")
|
||||||
|
digest = hmac.new(client_config["key"].encode("UTF-8"), msg, "SHA256").hexdigest()
|
||||||
|
r["auth"] = {
|
||||||
|
"node": node,
|
||||||
|
"timestamp": timestamp,
|
||||||
|
"hmac": digest,
|
||||||
|
}
|
||||||
|
r = requests.post(baseurl + "report", json=report)
|
||||||
|
print(r)
|
||||||
|
if r.status_code == 200:
|
||||||
|
return True
|
||||||
|
elif r.status_code == 409:
|
||||||
|
time.sleep(0.5 + random.random())
|
||||||
|
continue
|
||||||
|
else:
|
||||||
|
return False
|
||||||
|
|
Loading…
Reference in New Issue