commit fbfb9fad98bd95a7c6b9240aa20c3d4a2fcaa82b Author: Peter J. Holzer Date: Sun Sep 29 00:43:45 2019 +0200 Write filesystem stats to default postgresql database diff --git a/df2db b/df2db new file mode 100755 index 0000000..41c95d1 --- /dev/null +++ b/df2db @@ -0,0 +1,47 @@ +#!/usr/bin/python3 + +import os +import socket + +import psycopg2 + +hostname = socket.getfqdn() +db = psycopg2.connect('') +csr = db.cursor() + +with open("/proc/mounts") as mounts: + for mount in mounts: + (dev, mountpoint, fstype, rest) = mount.split(' ', 3) + s = os.statvfs(mountpoint) + if s.f_blocks == 0: + continue # not a real file system + print(dev, mountpoint, fstype, s) + + s_total = s.f_blocks * s.f_bsize + s_usable = (s.f_blocks - s.f_bfree + s.f_bavail) * s.f_bsize + s_used = (s.f_blocks - s.f_bfree) * s.f_bsize + + f_total = s.f_files + f_usable = s.f_files - s.f_ffree + s.f_favail + f_used = s.f_files - s.f_ffree + + csr.execute( + """ + insert into df( + hostname, filesystem, + s_total, s_usable, s_used, + f_total, f_usable, f_used + ) + values ( + %s, %s, + %s, %s, %s, + %s, %s, %s + ) + """, + (hostname, mountpoint, + s_total, s_usable, s_used, + f_total, f_usable, f_used)) + +db.commit() + +