Refactor: Extract utility functions out of metrics module

This commit is contained in:
Mehran Kholdi 2021-07-02 20:30:07 +04:30
parent c651f69e9c
commit 2b6a0a33b8
4 changed files with 49 additions and 50 deletions

View File

@ -18,7 +18,7 @@ from declarative import (
be_formatted, be_formatted,
be_fs_expanded, be_fs_expanded,
) )
from metrics import path_stats, mountpoint_to_dev from fs_util import path_stats, mountpoint_to_dev
from util import log_grpc_request from util import log_grpc_request

47
fs_util.py Normal file
View File

@ -0,0 +1,47 @@
import json
import os
import subprocess
def path_stats(path):
fs_stat = os.statvfs(path)
return {
"fs_size": fs_stat.f_frsize * fs_stat.f_blocks,
"fs_avail": fs_stat.f_frsize * fs_stat.f_bavail,
"fs_files": fs_stat.f_files,
"fs_files_avail": fs_stat.f_favail,
}
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}
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
def mountpoint_to_dev(mountpoint):
res = subprocess.run(
f"findmnt --json --first-only --nofsroot --mountpoint {mountpoint}",
shell=True,
capture_output=True,
)
if res.returncode != 0:
return None
data = json.loads(res.stdout.decode().strip())
return data["filesystems"][0]["source"]

View File

@ -1,55 +1,7 @@
import json
import os
import subprocess
from prometheus_client.core import REGISTRY from prometheus_client.core import REGISTRY
from prometheus_client.exposition import start_http_server from prometheus_client.exposition import start_http_server
def path_stats(path):
fs_stat = os.statvfs(path)
return {
"fs_size": fs_stat.f_frsize * fs_stat.f_blocks,
"fs_avail": fs_stat.f_frsize * fs_stat.f_bavail,
"fs_files": fs_stat.f_files,
"fs_files_avail": fs_stat.f_favail,
}
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}
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
def mountpoint_to_dev(mountpoint):
res = subprocess.run(
f"findmnt --json --first-only --nofsroot --mountpoint {mountpoint}",
shell=True,
capture_output=True,
)
if res.returncode != 0:
return None
data = json.loads(res.stdout.decode().strip())
return data["filesystems"][0]["source"]
class VolumeStatsCollector(object): class VolumeStatsCollector(object):
def collect(self): def collect(self):
return [] return []

View File

@ -7,7 +7,7 @@ import rawfile_util
from consts import PROVISIONER_VERSION, PROVISIONER_NAME from consts import PROVISIONER_VERSION, PROVISIONER_NAME
from csi import csi_pb2, csi_pb2_grpc from csi import csi_pb2, csi_pb2_grpc
from declarative import be_symlink, be_absent from declarative import be_symlink, be_absent
from metrics import device_stats, mountpoint_to_dev from fs_util import device_stats, mountpoint_to_dev
from orchestrator.k8s import volume_to_node, run_on_node from orchestrator.k8s import volume_to_node, run_on_node
from rawfile_util import attach_loop, detach_loops from rawfile_util import attach_loop, detach_loops
from remote import init_rawfile, scrub, expand_rawfile from remote import init_rawfile, scrub, expand_rawfile