From 2f4096c5aff486641276601e3b08430b9f4934ec Mon Sep 17 00:00:00 2001 From: "Peter J. Holzer" Date: Sun, 5 Sep 2021 00:56:21 +0200 Subject: [PATCH] Silently ignore permission errors Those usually occur on special filesystems which we don't care about anyway. --- df2db | 52 ++++++++++++++++++++++++++++------------------------ 1 file changed, 28 insertions(+), 24 deletions(-) diff --git a/df2db b/df2db index 739843c..31f6683 100755 --- a/df2db +++ b/df2db @@ -12,34 +12,38 @@ 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 + try: + s = os.statvfs(mountpoint) + if s.f_blocks == 0: + continue # not a real file system - 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 + 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 + 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, + 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 - ) - values ( - %s, %s, - %s, %s, %s, - %s, %s, %s - ) - """, - (hostname, mountpoint, - s_total, s_usable, s_used, - f_total, f_usable, f_used)) + f_total, f_usable, f_used)) + except PermissionError: + # silently ignore some errors + pass db.commit()