Expose volume metrics through gRPC calls rather than metrics endpoint
This commit is contained in:
parent
c58dd14bf7
commit
b4faf9d7cb
83
metrics.py
83
metrics.py
@ -4,10 +4,6 @@ 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
|
||||||
from prometheus_client.metrics_core import GaugeMetricFamily
|
|
||||||
|
|
||||||
import rawfile_util
|
|
||||||
from rawfile_util import attached_loops
|
|
||||||
|
|
||||||
|
|
||||||
def path_stats(path):
|
def path_stats(path):
|
||||||
@ -20,74 +16,12 @@ def path_stats(path):
|
|||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
def volume_stats(volume_id: str) -> dict:
|
def device_stats(dev):
|
||||||
img_file = rawfile_util.img_file(volume_id)
|
output = subprocess.run(
|
||||||
dev_stat = img_file.stat()
|
f"blockdev --getsize64 {dev}", shell=True, check=True, capture_output=True
|
||||||
stats = {
|
).stdout.decode()
|
||||||
"dev_size": dev_stat.st_size,
|
dev_size = int(output)
|
||||||
"dev_free": dev_stat.st_size - dev_stat.st_blocks * 512,
|
return {"dev_size": dev_size}
|
||||||
}
|
|
||||||
mountpoint = volume_to_mountpoint(img_file)
|
|
||||||
if mountpoint is not None:
|
|
||||||
stats.update(path_stats(mountpoint))
|
|
||||||
return stats
|
|
||||||
|
|
||||||
|
|
||||||
class VolumeStatsCollector(object):
|
|
||||||
def collect(self):
|
|
||||||
VOLUME_ID = "volume_id"
|
|
||||||
fs_size = GaugeMetricFamily(
|
|
||||||
"rawfile_filesystem_size_bytes",
|
|
||||||
"Filesystem size in bytes.",
|
|
||||||
labels=[VOLUME_ID],
|
|
||||||
)
|
|
||||||
fs_free = GaugeMetricFamily(
|
|
||||||
"rawfile_filesystem_avail_bytes",
|
|
||||||
"Filesystem free space in bytes",
|
|
||||||
labels=[VOLUME_ID],
|
|
||||||
)
|
|
||||||
fs_files = GaugeMetricFamily(
|
|
||||||
"rawfile_filesystem_files",
|
|
||||||
"Filesystem total file nodes.",
|
|
||||||
labels=[VOLUME_ID],
|
|
||||||
)
|
|
||||||
fs_files_free = GaugeMetricFamily(
|
|
||||||
"rawfile_filesystem_files_free",
|
|
||||||
"Filesystem total free file nodes",
|
|
||||||
labels=[VOLUME_ID],
|
|
||||||
)
|
|
||||||
dev_size = GaugeMetricFamily(
|
|
||||||
"rawfile_device_size_bytes", "Device size in bytes.", labels=[VOLUME_ID]
|
|
||||||
)
|
|
||||||
dev_free = GaugeMetricFamily(
|
|
||||||
"rawfile_device_free_bytes",
|
|
||||||
"Device free space in bytes.",
|
|
||||||
labels=[VOLUME_ID],
|
|
||||||
)
|
|
||||||
|
|
||||||
for volume_id in rawfile_util.list_all_volumes():
|
|
||||||
labels = [volume_id]
|
|
||||||
key_to_gauge = {
|
|
||||||
"dev_size": dev_size,
|
|
||||||
"dev_free": dev_free,
|
|
||||||
"fs_size": fs_size,
|
|
||||||
"fs_free": fs_free,
|
|
||||||
"fs_files": fs_files,
|
|
||||||
"fs_files_free": fs_files_free,
|
|
||||||
}
|
|
||||||
stats = volume_stats(volume_id)
|
|
||||||
for key in stats.keys():
|
|
||||||
key_to_gauge[key].add_metric(labels, stats[key])
|
|
||||||
|
|
||||||
return [fs_size, fs_free, fs_files, fs_files_free, dev_size, dev_free]
|
|
||||||
|
|
||||||
|
|
||||||
def volume_to_mountpoint(img_file):
|
|
||||||
for dev in attached_loops(img_file):
|
|
||||||
mountpoint = dev_to_mountpoint(dev)
|
|
||||||
if mountpoint is not None:
|
|
||||||
return mountpoint
|
|
||||||
return None
|
|
||||||
|
|
||||||
|
|
||||||
def dev_to_mountpoint(dev_name):
|
def dev_to_mountpoint(dev_name):
|
||||||
@ -116,6 +50,11 @@ def mountpoint_to_dev(mountpoint):
|
|||||||
return data["filesystems"][0]["source"]
|
return data["filesystems"][0]["source"]
|
||||||
|
|
||||||
|
|
||||||
|
class VolumeStatsCollector(object):
|
||||||
|
def collect(self):
|
||||||
|
return []
|
||||||
|
|
||||||
|
|
||||||
def expose_metrics():
|
def expose_metrics():
|
||||||
REGISTRY.register(VolumeStatsCollector())
|
REGISTRY.register(VolumeStatsCollector())
|
||||||
start_http_server(9100)
|
start_http_server(9100)
|
||||||
|
@ -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 volume_stats
|
from metrics 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
|
||||||
@ -105,8 +105,9 @@ class RawFileNodeServicer(csi_pb2_grpc.NodeServicer):
|
|||||||
|
|
||||||
# @log_grpc_request
|
# @log_grpc_request
|
||||||
def NodeGetVolumeStats(self, request, context):
|
def NodeGetVolumeStats(self, request, context):
|
||||||
volume_id = request.volume_id
|
volume_path = request.volume_path
|
||||||
stats = volume_stats(volume_id) # FIXME
|
dev = mountpoint_to_dev(volume_path)
|
||||||
|
stats = device_stats(dev=dev)
|
||||||
return csi_pb2.NodeGetVolumeStatsResponse(
|
return csi_pb2.NodeGetVolumeStatsResponse(
|
||||||
usage=[
|
usage=[
|
||||||
csi_pb2.VolumeUsage(
|
csi_pb2.VolumeUsage(
|
||||||
|
Loading…
Reference in New Issue
Block a user