2020-04-25 19:39:16 +00:00
|
|
|
import json
|
|
|
|
import os
|
2020-06-12 11:25:32 +00:00
|
|
|
import subprocess
|
2020-04-25 19:39:16 +00:00
|
|
|
|
2020-06-12 11:25:32 +00:00
|
|
|
from prometheus_client.core import REGISTRY
|
2020-04-25 19:39:16 +00:00
|
|
|
from prometheus_client.exposition import start_http_server
|
|
|
|
|
|
|
|
|
2020-11-06 14:44:47 +00:00
|
|
|
def path_stats(path):
|
|
|
|
fs_stat = os.statvfs(path)
|
|
|
|
return {
|
|
|
|
"fs_size": fs_stat.f_frsize * fs_stat.f_blocks,
|
|
|
|
"fs_free": fs_stat.f_frsize * fs_stat.f_bfree,
|
|
|
|
"fs_files": fs_stat.f_files,
|
|
|
|
"fs_files_free": fs_stat.f_ffree,
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2021-01-15 23:15:31 +00:00
|
|
|
def device_stats(dev):
|
|
|
|
output = subprocess.run(
|
|
|
|
f"blockdev --getsize64 {dev}", shell=True, check=True, capture_output=True
|
|
|
|
).stdout.decode()
|
|
|
|
dev_size = int(output)
|
|
|
|
return {"dev_size": dev_size}
|
2020-04-25 19:39:16 +00:00
|
|
|
|
2020-06-12 11:25:32 +00:00
|
|
|
|
|
|
|
def dev_to_mountpoint(dev_name):
|
|
|
|
try:
|
|
|
|
output = subprocess.run(
|
|
|
|
f"findmnt --json --first-only {dev_name}",
|
|
|
|
shell=True,
|
|
|
|
check=True,
|
|
|
|
capture_output=True,
|
|
|
|
).stdout.decode()
|
|
|
|
data = json.loads(output)
|
|
|
|
return data["filesystems"][0]["target"]
|
|
|
|
except subprocess.CalledProcessError:
|
|
|
|
return None
|
|
|
|
|
|
|
|
|
2020-11-06 14:44:47 +00:00
|
|
|
def mountpoint_to_dev(mountpoint):
|
|
|
|
res = subprocess.run(
|
2021-03-01 10:10:06 +00:00
|
|
|
f"findmnt --json --first-only --nofsroot --mountpoint {mountpoint}",
|
2020-11-06 14:44:47 +00:00
|
|
|
shell=True,
|
|
|
|
capture_output=True,
|
|
|
|
)
|
|
|
|
if res.returncode != 0:
|
|
|
|
return None
|
|
|
|
data = json.loads(res.stdout.decode().strip())
|
|
|
|
return data["filesystems"][0]["source"]
|
|
|
|
|
|
|
|
|
2021-01-15 23:15:31 +00:00
|
|
|
class VolumeStatsCollector(object):
|
|
|
|
def collect(self):
|
|
|
|
return []
|
|
|
|
|
|
|
|
|
2020-06-12 11:25:32 +00:00
|
|
|
def expose_metrics():
|
|
|
|
REGISTRY.register(VolumeStatsCollector())
|
2020-04-25 19:39:16 +00:00
|
|
|
start_http_server(9100)
|