https://t.me/RX1948
Server : Apache
System : Linux iad1-shared-b8-43 6.6.49-grsec-jammy+ #10 SMP Thu Sep 12 23:23:08 UTC 2024 x86_64
User : dh_edsupp ( 6597262)
PHP Version : 8.2.26
Disable Function : NONE
Directory :  /opt/prometheus-monitoring-scripts/lib/python3.10/site-packages/custom_exporters/disk/

Upload File :
current_dir [ Writeable ] document_root [ Writeable ]

 

Current File : //opt/prometheus-monitoring-scripts/lib/python3.10/site-packages/custom_exporters/disk/podman.py
from ..base import BaseExporter
from ..utils import nsenter_batch_command, get_podman_data
import os
import re
import shutil
import subprocess

class CustomExporter(BaseExporter):
    def metric_config(self):
        return {
            "container_disk_avail_bytes": {"type": "Gauge", "labels": ["filesystem", "machine"]},
            "container_disk_used_bytes": {"type": "Gauge", "labels": ["filesystem", "machine"]},
            "container_disk_avail_inodes": {"type": "Gauge", "labels": ["filesystem", "machine"]},
            "container_disk_used_inodes": {"type": "Gauge", "labels": ["filesystem", "machine"]},
            "container_proc_mount_broken": {"type": "Gauge", "labels": ["machine"]}
        }

    def generate(self):
        disk_avail_bytes = self.metrics.get('container_disk_avail_bytes')
        disk_used_bytes = self.metrics.get('container_disk_used_bytes')
        disk_avail_inodes = self.metrics.get('container_disk_avail_inodes')
        disk_used_inodes = self.metrics.get('container_disk_used_inodes')
        proc_mount_broken =  self.metrics.get('container_proc_mount_broken')
        
        pod_data = get_podman_data()
        pod_names = [d['Names'][0] for d in pod_data if 'Names' in d and len(d['Names'])]
        xfs_data = self._get_xfs_quota()
        for pod in pod_names:
            root_path = "/podman/container_data/%s/root" % pod
            if not os.path.isdir(root_path):
                continue
            target = os.readlink(root_path)
            if not os.path.isdir(target):
                continue
            disk_usage_info = shutil.disk_usage(target)
            disk_used_bytes.labels('rootfs', pod).set(disk_usage_info.used)
            disk_avail_bytes.labels('rootfs', pod).set(disk_usage_info.free)

            # TODO CHECK for accuracy
            disk_stats = os.statvfs(target)
            disk_avail_inodes.labels('rootfs', pod).set(disk_stats.f_ffree)
            disk_used_inodes.labels('rootfs', pod).set(disk_stats.f_files - disk_stats.f_ffree)

            if pod in xfs_data:
                pod_xfs_data = xfs_data[pod]
                disk_used_bytes.labels('home', pod).set(pod_xfs_data['bytes_used'] * 1000)
                disk_avail_bytes.labels('home', pod).set((pod_xfs_data['bytes_total'] - pod_xfs_data['bytes_used']) * 1000)
                disk_avail_inodes.labels('home', pod).set((pod_xfs_data['inodes_total'] - pod_xfs_data['inodes_used']))
                disk_used_inodes.labels('home', pod).set(pod_xfs_data['inodes_used'])

        container_pid_map = {d['Names'][0]: d['Pid'] for d in pod_data if 'Names' in d and len(d['Names'])}
        meminfo_data = nsenter_batch_command(
            pool_size=50,
            container_pid_map=container_pid_map,
            command=["wc", "-l", "/proc/meminfo"]
        )
        for pod, result in meminfo_data.items():
            if result.returncode == 0:
                if re.search('^\s*[0-9]',result.stdout):
                    proc_mount_broken.labels(pod).set(0)
                else:
                    proc_mount_broken.labels(pod).set(1)

    def _get_xfs_quota(self):
        script = "xfs_quota -x -c 'report -p -bi' /podman"
        result = subprocess.run(["bash", "-c", script], capture_output=True, text=True)
        data = {}

        for line in result.stdout.strip().splitlines():
            xfs_fields = line.split()
            machine = xfs_fields[0]
            if machine.startswith('vps') or machine.startswith('sql'):
                data[machine] = {
                    'bytes_used': float(xfs_fields[1]),
                    'bytes_total': float(xfs_fields[3]),
                    'inodes_used': float(xfs_fields[6]),
                    'inodes_total': float(xfs_fields[8])
                    }
        return data

https://t.me/RX1948 - 2025