Silently ignore permission errors

Those usually occur on special filesystems which we don't care about
anyway.
This commit is contained in:
Peter J. Holzer 2021-09-05 00:56:21 +02:00
parent 6c67de3bc9
commit 2f4096c5af
1 changed files with 28 additions and 24 deletions

52
df2db
View File

@ -12,34 +12,38 @@ csr = db.cursor()
with open("/proc/mounts") as mounts: with open("/proc/mounts") as mounts:
for mount in mounts: for mount in mounts:
(dev, mountpoint, fstype, rest) = mount.split(' ', 3) (dev, mountpoint, fstype, rest) = mount.split(' ', 3)
s = os.statvfs(mountpoint) try:
if s.f_blocks == 0: s = os.statvfs(mountpoint)
continue # not a real file system if s.f_blocks == 0:
continue # not a real file system
s_total = s.f_blocks * 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_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_used = (s.f_blocks - s.f_bfree) * s.f_bsize
f_total = s.f_files f_total = s.f_files
f_usable = s.f_files - s.f_ffree + s.f_favail f_usable = s.f_files - s.f_ffree + s.f_favail
f_used = s.f_files - s.f_ffree f_used = s.f_files - s.f_ffree
csr.execute( csr.execute(
""" """
insert into df( insert into df(
hostname, filesystem, 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, s_total, s_usable, s_used,
f_total, f_usable, f_used f_total, f_usable, f_used))
) except PermissionError:
values ( # silently ignore some errors
%s, %s, pass
%s, %s, %s,
%s, %s, %s
)
""",
(hostname, mountpoint,
s_total, s_usable, s_used,
f_total, f_usable, f_used))
db.commit() db.commit()