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/xfs.py
from ..base import BaseExporter
import subprocess
import pwd

class CustomExporter(BaseExporter):
    def metric_config(self):
        return {
            "disk_used_bytes": {"type": "Gauge", "labels": ["user"]},
            "disk_quota_bytes": {"type": "Gauge", "labels": ["user"]},
        }

    def generate(self):
        disk_quota_bytes = self.metrics.get('disk_quota_bytes')
        disk_used_bytes = self.metrics.get('disk_used_bytes')
        user_dict = {u.pw_uid: u for u in pwd.getpwall()}
        for user_id, usage in self._get_xfs_user_quota(mount=self.args[0]).items():
            if int(user_id) in user_dict:
                user = user_dict[int(user_id)]
                disk_used_bytes.labels(user.pw_name).set(usage['bytes_used'])
                disk_quota_bytes.labels(user.pw_name).set(usage['bytes_total'])

    def _get_xfs_user_quota(self, mount):
        script = "xfs_quota -x -c 'report -N -bnui' %s" % mount
        result = subprocess.run(["bash", "-c", script], capture_output=True, text=True)
        data = {}

        for line in result.stdout.strip().splitlines():
            xfs_fields = line.split()
            key = xfs_fields[0].lstrip("#")
            if float(key) >= 10000:
                try:
                    data[key] = self._parse_xfs_fields(xfs_fields)
                except Exception:
                    # this line is not parseable
                    continue
        return data

    def _parse_xfs_fields(self, xfs_fields):
        return {
            'bytes_used': float(xfs_fields[1]) * 1000,
            'bytes_total': float(xfs_fields[3]) * 1000,
            'inodes_used': float(xfs_fields[6]),
            'inodes_total': float(xfs_fields[8])
        }

https://t.me/RX1948 - 2025