#!/usr/bin/python3

import os
import socket
import time

from ltsdb_json import LTS

# 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]


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)
        now = time.time()

        # 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

        ts = LTS(
            {
                "hostname": hostname,
                "mountpoint": mountpoint,
                "fstype": fstype,
                "measure": "bytes_usable",
                "unit": "bytes",
            }
        )
        ts.add(now, bytes_usable)
        ts.save()
        ts = LTS(
            {
                "hostname": hostname,
                "mountpoint": mountpoint,
                "fstype": fstype,
                "measure": "bytes_used",
                "unit": "bytes",
            }
        )
        ts.add(now, bytes_used)
        ts.save()