From 2b54a3862ef339c51e7a3fed83c7d34768868561 Mon Sep 17 00:00:00 2001 From: "Peter J. Holzer" Date: Sun, 4 Sep 2022 21:04:03 +0200 Subject: [PATCH] Report top 100 directories --- clients/report_dus | 70 ++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 70 insertions(+) create mode 100755 clients/report_dus diff --git a/clients/report_dus b/clients/report_dus new file mode 100755 index 0000000..7674fb0 --- /dev/null +++ b/clients/report_dus @@ -0,0 +1,70 @@ +#!/usr/bin/python3 + +import argparse +import hmac +import json +import os +import pprint +import random +import socket +import subprocess +import time + +import requests + +ap = argparse.ArgumentParser() +ap.add_argument("directory") +args = ap.parse_args() + +# It's a bit weird that this works. +hostname = socket.gethostbyaddr(socket.gethostname())[0] + + +now = time.time() +p = subprocess.run(["/usr/bin/dus", args.directory], capture_output=True, text=True) +report = [] +for ln in p.stdout.split("\n")[:-1]: + (size, directory) = ln.split("\t") + + report.append( + { + "description": { + "hostname": hostname, + "directory": directory, + "measure": "diskusage", + "unit": "bytes", + }, + "data": [ + [now, int(size)*1024] + ] + } + ) +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: + node = hostname + 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, + } + #pprint.pp(report) + r = requests.post(baseurl + "report", json=report) + print(r) + if r.status_code == 200: + exit(0) + elif r.status_code == 409: + time.sleep(0.5 + random.random()) + continue + else: + exit(1)