diff --git a/clients/report_df b/clients/report_df new file mode 100755 index 0000000..05209f4 --- /dev/null +++ b/clients/report_df @@ -0,0 +1,100 @@ +#!/usr/bin/python3 + +import hmac +import json +import os +import pprint +import random +import socket +import subprocess +import time + +import requests + + +# Use a whitelist since there are a lot of uninteresting file system +# types on modern linux systems +interesting_fstypes = {"ext4", "tmpfs", "vfat"} + +# It's a bit weird that this works. +hostname = socket.gethostbyaddr(socket.gethostname())[0] + + +now = time.time() + +report = [] +with open("/proc/mounts") as mfh: + for ln in mfh: + (dev, mountpoint, fstype, options, _, _) = ln.split() + if fstype not in interesting_fstypes: + continue + st = os.statvfs(mountpoint) + + # We record the capacity usable by unprivileged processes, not the + # total size of the filesystem. This is more relevant and also what df + # uses as the "100%" mark. + blocks_reserved = st.f_bfree - st.f_bavail + blocks_usable = st.f_blocks - blocks_reserved + blocks_used = st.f_blocks - st.f_bfree + + bytes_usable = blocks_usable * st.f_bsize + bytes_used = blocks_used * st.f_bsize + + report.append( + { + "description": { + "hostname": hostname, + "mountpoint": mountpoint, + "fstype": fstype, + "measure": "bytes_usable", + "unit": "bytes", + }, + "data": [ + [now, bytes_usable] + ] + } + ) + report.append( + { + "description": { + "hostname": hostname, + "mountpoint": mountpoint, + "fstype": fstype, + "measure": "bytes_used", + "unit": "bytes", + }, + "data": [ + [now, bytes_used] + ] + } + ) + +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)