Files
opt/collectd/usr/local/bin/du-data
2024-12-23 10:54:34 +01:00

83 lines
2.2 KiB
Python
Executable File

#!/usr/bin/python3
#
# Imports
#
import sys
import time
import subprocess
import argparse
#
# Methods
#
def get_disk_usage(path, human_readable):
"""disk usage in human readable format (e.g. '2,1GB')"""
arguments = "-sh" if human_readable else "-s"
command = "du %s %s" % (arguments, path)
status, output = subprocess.getstatusoutput(command)
if status != 0:
raise Exception(command)
disk_usage = output.split()[0]
if not human_readable:
# du reports in units of 1024 bytes, convert to plain number of bytes
disk_usage = int(disk_usage) * 1024
return disk_usage
#
# Directories to scan
#
hostname = "server"
interval = 10
directories = list()
directories.append(["bram", "/host/media/helios/Bram"])
directories.append(["rik", "/host/media/helios/Rik"])
directories.append(["books", "/host/media/neptune/Books"])
directories.append(["games", "/host/media/mercury/Games"])
directories.append(["misc", "/host/media/neptune/Miscellaneous"])
directories.append(["shows", "/host/media/neptune/Video/Shows"])
directories.append(["movies", "/host/media/neptune/Video/Movies"])
directories.append(["music", "/host/media/neptune/Music"])
directories.append(["photographs", "/host/media/helios/Photographs"])
directories.append(["pictures", "/host/media/helios/Pictures"])
directories.append(["software", "/host/media/mercury/Software"])
#
# Command line arguments
#
parser = argparse.ArgumentParser(description="Get BTRFS disk usage")
parser.add_argument("-s", action="store_true", help="print in human readable format")
args = parser.parse_args()
human_readable = args.s
#
# Main
#
if human_readable:
for (name, path) in directories:
disk_usage = get_disk_usage(path, human_readable)
print(("%s: %s" % (name, disk_usage)))
else:
# RRD mode
while True:
for (name, path) in directories:
disk_usage = get_disk_usage(path, human_readable)
timestamp = int(time.time())
size = float(disk_usage)
print(
(
"PUTVAL {}/exec-du_{}/gauge-size {}:{:.1f}".format(
hostname, name, timestamp, size
)
)
)
sys.stdout.flush()
time.sleep(interval)