Added check for ismount() and empty directories.

Previously the free space of the underlying filesystem would be reported if the path provided was a directory but not a valid mountpoint. This adds a check to first confirm whether a directory is a mountpoint using os.path.ismount(), and if not, then runs an os.listdir() to count the files; empty directories are considered not mounted.

This functionality allows for usage on setups with NFS and will not report free space of underlying filesystem in cases with local mountpoints as path.
This commit is contained in:
Douglas Eddie 2015-11-24 22:51:01 +00:00 committed by enkore
parent 1c4f941304
commit 731749f0e3

View File

@ -37,17 +37,25 @@ class Disk(IntervalModule):
round_size = 2 round_size = 2
mounted_only = False mounted_only = False
def not_mounted(self):
if self.mounted_only:
self.output = {}
else:
self.output = {} if not self.format_not_mounted else {
"full_text": self.format_not_mounted,
"color": self.color_not_mounted,
}
def run(self): def run(self):
if os.path.isdir(self.path) and not os.path.ismount(self.path):
if len(os.listdir(self.path)) == 0:
self.not_mounted()
return
try: try:
stat = os.statvfs(self.path) stat = os.statvfs(self.path)
except Exception: except Exception:
if self.mounted_only: self.not_mounted()
self.output = {}
else:
self.output = {} if not self.format_not_mounted else {
"full_text": self.format_not_mounted,
"color": self.color_not_mounted,
}
return return
available = (stat.f_bsize * stat.f_bavail) / self.divisor available = (stat.f_bsize * stat.f_bavail) / self.divisor