Compare commits

..

11 Commits

Author SHA1 Message Date
illustris
bd955e8067 Bump version to 1.3.3 2025-05-11 05:28:45 +05:30
illustris
b1e9b1e0b5 Add host binding option 2025-05-11 05:27:37 +05:30
illustris
4ac1ba1f24 bump inputs 2025-05-11 05:22:46 +05:30
illustris
b57db23a35 Bump version to 1.3.2 2025-04-14 08:33:25 +05:30
illustris
ebcc08cc8f Use zpool command to measure ZFS pool sizes accurately
Replace statvfs with zpool list command for ZFS storage pools to get accurate
size and free space metrics. This resolves the 'mountpoint' key error for ZFS
pools and provides more accurate capacity information.
2025-04-14 08:30:44 +05:30
illustris
1d06e1c180 convert bool labels in storage info to strings 2025-04-14 08:19:05 +05:30
illustris
4cc5a1f207 Bump version to 1.3.1 2025-04-14 08:07:13 +05:30
illustris
8207792bf7 Fix storage config parsing for keys without values
Set keys without values (like 'sparse') to True when parsing storage configuration.
2025-04-14 08:06:10 +05:30
illustris
066753ebc7 Bump version to 1.3.0 2025-03-08 15:08:56 +05:30
illustris
46bd7d67d2 Add pool information to VM metrics
- Parse /etc/pve/user.cfg to extract pool membership for VMs
- Add pool-related labels to pve_kvm info metrics:
  - pool: Full hierarchical pool name
  - pool_levels: Number of pool hierarchy levels
  - pool1/pool2/pool3: Individual pool hierarchy levels
- Cache pool data based on file modification time to avoid repeated reads
2025-03-08 15:07:02 +05:30
illustris
7923d425a5 Create LICENSE 2024-09-09 01:06:49 +05:30
6 changed files with 184 additions and 23 deletions

21
LICENSE Normal file
View File

@@ -0,0 +1,21 @@
MIT License
Copyright (c) 2024 Harikrishnan R
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.

12
flake.lock generated
View File

@@ -3,11 +3,11 @@
"debBundler": {
"flake": false,
"locked": {
"lastModified": 1725149456,
"narHash": "sha256-rRrSD7itoPm+VIT4bIzSupQ7jw+H4eOjxRiRA89Kxb4=",
"lastModified": 1746317543,
"narHash": "sha256-1Xph5g1Lazzkc9XuY1nOkG5Fn7+lmSdldAC91boDawY=",
"owner": "illustris",
"repo": "flake",
"rev": "257a6c986cb9a67c4d6d0e0363507cab7f958b63",
"rev": "e86bd104d76d22b2ba36fede405e7bff290ef489",
"type": "github"
},
"original": {
@@ -18,11 +18,11 @@
},
"nixpkgs": {
"locked": {
"lastModified": 1725103162,
"narHash": "sha256-Ym04C5+qovuQDYL/rKWSR+WESseQBbNAe5DsXNx5trY=",
"lastModified": 1746663147,
"narHash": "sha256-Ua0drDHawlzNqJnclTJGf87dBmaO/tn7iZ+TCkTRpRc=",
"owner": "nixos",
"repo": "nixpkgs",
"rev": "12228ff1752d7b7624a54e9c1af4b222b3c1073b",
"rev": "dda3dcd3fe03e991015e9a74b22d35950f264a54",
"type": "github"
},
"original": {

View File

@@ -14,7 +14,7 @@ rec {
packages.x86_64-linux = with nixpkgs.legacyPackages.x86_64-linux; rec {
pvemon = python3Packages.buildPythonApplication {
pname = "pvemon";
version = "1.2.0";
version = "1.3.3";
src = ./src;
propagatedBuildInputs = with python3Packages; [
pexpect

View File

@@ -24,9 +24,17 @@ import qmblock
import builtins
# Cache for pool data
pool_cache = {
'last_mtime': 0,
'vm_pool_map': {},
'pools': {}
}
DEFAULT_PORT = 9116
DEFAULT_INTERVAL = 10
DEFAULT_PREFIX = "pve"
DEFAULT_HOST = "0.0.0.0"
gauge_settings = [
('kvm_cpu', 'CPU time for VM', ['id', 'mode']),
@@ -135,6 +143,67 @@ def read_interface_stats(ifname):
pass
return stats
def get_pool_info():
"""
Read pool information from /etc/pve/user.cfg, caching based on file modification time.
Returns a tuple of (vm_to_pool_map, pool_info) where:
- vm_to_pool_map maps VM IDs to their pool names
- pool_info contains details about each pool (levels, etc.)
"""
pool_cfg_path = '/etc/pve/user.cfg'
try:
# Check modification time
current_mtime = os.path.getmtime(pool_cfg_path)
# If file hasn't changed, return cached data
if current_mtime <= pool_cache['last_mtime'] and pool_cache['vm_pool_map']:
return pool_cache['vm_pool_map'], pool_cache['pools']
# File has changed or first run, parse it
logging.debug(f"Reading pool configuration from {pool_cfg_path}")
vm_pool_map = {}
pools = {}
with open(pool_cfg_path, 'r') as f:
for line in f:
if line.startswith('pool:'):
parts = line.strip().split(':')
if len(parts) < 3:
continue
pool_name = parts[1]
vm_list = parts[3] if len(parts) > 3 else ''
# Store pool info
pool_parts = pool_name.split('/')
pool_level_count = len(pool_parts)
pools[pool_name] = {
'level_count': pool_level_count,
'level1': pool_parts[0] if pool_level_count > 0 else '',
'level2': pool_parts[1] if pool_level_count > 1 else '',
'level3': pool_parts[2] if pool_level_count > 2 else ''
}
# Map VMs to this pool
if vm_list:
for vm_id in vm_list.split(','):
if vm_id.strip():
vm_pool_map[vm_id.strip()] = pool_name
# Update cache
pool_cache['last_mtime'] = current_mtime
pool_cache['vm_pool_map'] = vm_pool_map
pool_cache['pools'] = pools
return vm_pool_map, pools
except (FileNotFoundError, PermissionError) as e:
logging.warning(f"Could not read pool configuration: {e}")
return {}, {}
def collect_kvm_metrics():
logging.debug("collect_kvm_metrics() called")
gauge_dict = {}
@@ -170,13 +239,34 @@ def collect_kvm_metrics():
except (psutil.NoSuchProcess, psutil.AccessDenied, psutil.ZombieProcess):
continue
# Try to find ID to pool mapping here
# Get VM to pool mapping
vm_pool_map, pools = get_pool_info()
for proc, cmdline, id in procs:
# Extract vm labels from cmdline
info_label_dict = {get_label_name(l): flag_to_label_value(cmdline,l) for l in label_flags}
info_label_dict['pid'] = str(proc.pid)
logging.debug(f"got PID: {proc.pid}")
# Add pool information if available
if id in vm_pool_map:
pool_name = vm_pool_map[id]
pool_info = pools[pool_name]
info_label_dict['pool'] = pool_name
info_label_dict['pool_levels'] = str(pool_info['level_count'])
info_label_dict['pool1'] = pool_info['level1']
info_label_dict['pool2'] = pool_info['level2']
info_label_dict['pool3'] = pool_info['level3']
logging.debug(f"VM {id} belongs to pool {pool_name}")
else:
# VM not in any pool
info_label_dict['pool'] = ''
info_label_dict['pool_levels'] = '0'
info_label_dict['pool1'] = ''
info_label_dict['pool2'] = ''
info_label_dict['pool3'] = ''
info_dict["kvm"].add_metric([], info_label_dict)
d = {
@@ -264,6 +354,7 @@ class PVECollector(object):
def main():
parser = argparse.ArgumentParser(description='PVE metrics exporter for Prometheus')
parser.add_argument('--port', type=int, default=DEFAULT_PORT, help='Port for the exporter to listen on')
parser.add_argument('--host', type=str, default=DEFAULT_HOST, help='Host address to bind the exporter to')
parser.add_argument('--interval', type=int, default=DEFAULT_INTERVAL, help='THIS OPTION DOES NOTHING')
parser.add_argument('--collect-running-vms', type=str, default='true', help='Enable or disable collecting running VMs metric (true/false)')
parser.add_argument('--collect-storage', type=str, default='true', help='Enable or disable collecting storage info (true/false)')
@@ -299,7 +390,7 @@ def main():
return
else:
REGISTRY.register(PVECollector())
start_http_server(cli_args.port)
start_http_server(cli_args.port, addr=cli_args.host)
while True:
time.sleep(100)

View File

@@ -65,9 +65,15 @@ def parse_storage_cfg(file_path='/etc/pve/storage.cfg'):
else:
# Parse key-value pairs within the current storage
if current_storage:
key, value = line.split(None, 1)
sanitized_key = sanitize_key(key.strip())
current_storage[sanitized_key] = value.strip()
parts = line.split(None, 1)
key = parts[0].strip()
sanitized_key = sanitize_key(key)
if len(parts) > 1:
# Regular key-value pair
current_storage[sanitized_key] = parts[1].strip()
else:
# Key with no value, set it to True
current_storage[sanitized_key] = True
# Append the last storage section to the list if any
if current_storage:
@@ -81,15 +87,53 @@ def parse_storage_cfg(file_path='/etc/pve/storage.cfg'):
def get_storage_size(storage):
try:
if storage["type"] in ["dir", "nfs", "cephfs", "zfspool"]:
if storage["type"] == "zfspool":
path = storage["mountpoint"]
else:
if "pool" not in storage:
logging.debug(f"ZFS pool {storage['name']} has no pool name configured")
return None
# Extract the pool name (could be in format like rpool/data)
pool_name = storage["pool"].split("/")[0]
# Use zpool command to get accurate size information
import subprocess
try:
result = subprocess.run(
["zpool", "list", pool_name, "-p"],
capture_output=True,
text=True,
check=True
)
# Parse the output
lines = result.stdout.strip().split("\n")
if len(lines) < 2:
logging.warn(f"Unexpected zpool list output format for {pool_name}")
return None
# Extract values from the second line (the data line)
values = lines[1].split()
if len(values) < 4:
logging.warn(f"Insufficient data in zpool list output for {pool_name}")
return None
# Values are: NAME SIZE ALLOC FREE ...
# We need the SIZE and FREE values (index 1 and 3)
total_size = int(values[1])
free_space = int(values[3])
return {
"total": total_size,
"free": free_space
}
except (subprocess.SubprocessError, ValueError, IndexError) as e:
logging.warn(f"Error running zpool list for {pool_name}: {e}")
return None
elif storage["type"] in ["dir", "nfs", "cephfs"]:
# For non-ZFS storage, use statvfs
path = storage["path"]
# Get filesystem statistics
stats = os.statvfs(path)
# Calculate total size and free space in bytes
# TODO: find an alternative way to calculate total_size for ZFS
total_size = stats.f_frsize * stats.f_blocks
free_space = stats.f_frsize * stats.f_bavail
return {
@@ -120,7 +164,12 @@ def collect_storage_metrics():
storage_pools = parse_storage_cfg()
for storage in storage_pools:
info_dict["node_storage"].add_metric([], storage)
# Convert any non-string values to strings for InfoMetricFamily
storage_info = {}
for key, value in storage.items():
storage_info[key] = str(value) if not isinstance(value, str) else value
info_dict["node_storage"].add_metric([], storage_info)
size = get_storage_size(storage)
if size != None:
gauge_dict["node_storage_size"].add_metric([storage["name"], storage["type"]], size["total"])

View File

@@ -2,7 +2,7 @@ from setuptools import setup, find_packages
setup(
name='pvemon',
version = "1.2.0",
version = "1.3.3",
packages=find_packages(),
entry_points={
'console_scripts': [