python-esmonitor/pymonitor/monitors/diskspace.py

95 lines
2.9 KiB
Python
Raw Normal View History

2018-10-04 18:50:34 -07:00
from pymonitor import Metric
2015-12-05 17:34:20 -08:00
from os import statvfs
import logging
2015-12-05 17:34:20 -08:00
2017-05-15 17:29:20 -07:00
2017-10-03 21:18:44 -07:00
def diskspace(filesystems=[], discover=True, omit=[]):
"""
Emit disk space usage statistics for the passed filesystems.
:param filesystems: list of mountpoints to gather stats for
:param discover: automatically find non-temporary filesystems to gather statistics for. Duplicates from the
filesystems param will be ignored.
2017-10-03 21:18:44 -07:00
:param omit: list of paths that, if prefix a discovered mountpoint, to not report on
"""
2018-10-04 18:50:34 -07:00
filesystems = [f.rstrip("/") if f != "/" else f for f in filesystems]
if discover:
with open("/proc/mounts") as f:
for line in f.readlines():
device, mountpoint, fstype, options, _, _ = line.split(" ")
# filter out some mountpoints we probably don't care about space on
if any([mountpoint.startswith(prefix) for prefix in ["/sys", "/proc", "/dev", "/run"]]):
continue
filesystems.append(mountpoint)
for fs in set(filesystems):
2017-10-03 21:18:44 -07:00
if any([fs.startswith(i) for i in omit or []]):
continue
try:
stats = statvfs(fs)
except FileNotFoundError:
2018-10-04 18:50:34 -07:00
logging.warning("filesystem not found: %s", repr(fs))
continue
2017-05-15 17:29:20 -07:00
2015-12-05 17:34:20 -08:00
info = {
"diskfree": stats.f_bsize * stats.f_bavail,
2017-05-15 17:29:20 -07:00
"diskused": (stats.f_blocks - stats.f_bavail) * stats.f_bsize,
2016-01-28 22:31:57 -08:00
"disksize": stats.f_bsize * stats.f_blocks,
"inodesmax": stats.f_files,
"inodesfree": stats.f_favail,
"inodesused": stats.f_files - stats.f_favail
2015-12-05 17:34:20 -08:00
}
2017-05-15 17:29:20 -07:00
2018-10-04 18:50:34 -07:00
info["diskpctused"] = round(info["diskused"] / info["disksize"] if info["disksize"] > 0 else 0.0, 5)
info["diskpctfree"] = round(info["diskfree"] / info["disksize"] if info["disksize"] > 0 else 0.0, 5)
2017-05-15 17:29:20 -07:00
2018-10-04 18:50:34 -07:00
info["inodesused_pct"] = round(info["inodesused"] / info["inodesmax"] if info["inodesmax"] > 0 else 0.0, 5)
info["inodesfree_pct"] = round(info["inodesfree"] / info["inodesmax"] if info["inodesmax"] > 0 else 0.0, 5)
2017-05-15 17:29:20 -07:00
2018-10-04 18:50:34 -07:00
yield Metric(info, {"fs": fs})
2015-12-05 17:34:20 -08:00
2017-05-15 17:29:20 -07:00
2015-12-05 17:34:20 -08:00
mapping = {
2018-10-04 18:50:34 -07:00
"diskfree": {
"type": "long"
},
"diskused": {
"type": "long"
},
"disksize": {
"type": "long"
},
"diskpctused": {
"type": "double"
},
"diskpctfree": {
"type": "double"
},
"fs": {
"type": "text"
},
"fs_raw": {
"type": "keyword"
},
"inodesmax": {
"type": "long"
},
"inodesfree": {
"type": "long"
},
"inodesused": {
"type": "long"
},
"inodesused_pct": {
"type": "double"
},
"inodesfree_pct": {
"type": "double"
2015-12-05 17:34:20 -08:00
}
}
2017-05-15 17:29:20 -07:00
2015-12-05 17:34:20 -08:00
if __name__ == '__main__':
from pprint import pprint
2017-10-03 21:18:44 -07:00
for item in diskspace(filesystems=[], discover=True, omit=None):
pprint(item)