diff --git a/core/metrics.py b/core/metrics.py index aa35272..3a34ef7 100644 --- a/core/metrics.py +++ b/core/metrics.py @@ -115,33 +115,18 @@ VOLUME_DEFINITIONS = [ { "mountpoint": "/", "device": "/dev/sda1", - "container_paths": ["/host_os/home", "/hostfs/host_mnt/home", "/home", "/host_os_disk", "/hostfs", "/"] + "container_paths": ["/host_os/home", "/home"] }, { "mountpoint": "/projects", "device": "/dev/sda2", "container_paths": ["/host_os/projects", "/projects"] - }, - { - "mountpoint": "/boot", - "device": "/dev/sda4", - "container_paths": ["/host_os/boot", "/boot"] - }, - { - "mountpoint": "/boot/efi", - "device": "/dev/sda3", - "container_paths": ["/host_os/boot_efi", "/boot/efi"] - }, - { - "mountpoint": "/run/media/mamad/WIN11_25H2_", - "device": "/dev/sdc1", - "container_paths": ["/host_os/media/mamad/WIN11_25H2_", "/run/media/mamad/WIN11_25H2_"] } ] def update_disk_metrics(): - """Update Prometheus gauges with real host OS filesystem disk usage for each mounted volume.""" + """Update Prometheus gauges with real host OS filesystem disk usage for verified mounted volumes.""" try: recorded_mounts = set() for v in VOLUME_DEFINITIONS: @@ -161,14 +146,34 @@ def update_disk_metrics(): except Exception as err: logger.debug(f"Error measuring disk usage for {path}: {err}") - # If none of the specific mounts matched, fallback to root + # Dynamically check any mounted external media drives under /host_os/media + media_root = "/host_os/media" + if os.path.exists(media_root) and os.path.isdir(media_root): + try: + for user in os.listdir(media_root): + user_dir = os.path.join(media_root, user) + if os.path.isdir(user_dir): + for drive in os.listdir(user_dir): + drive_path = os.path.join(user_dir, drive) + if os.path.isdir(drive_path): + total, used, free = shutil.disk_usage(drive_path) + mnt_label = f"/run/media/{user}/{drive}" + DISK_TOTAL_BYTES.labels(mountpoint=mnt_label, device="external").set(total) + DISK_USED_BYTES.labels(mountpoint=mnt_label, device="external").set(used) + DISK_FREE_BYTES.labels(mountpoint=mnt_label, device="external").set(free) + free_pct = (free / total * 100.0) if total > 0 else 0.0 + DISK_FREE_PERCENT.labels(mountpoint=mnt_label, device="external").set(free_pct) + except Exception as e: + logger.debug(f"Error scanning external media mounts: {e}") + + # If neither specific mount matched, fallback to root if not recorded_mounts: total, used, free = shutil.disk_usage("/") - DISK_TOTAL_BYTES.labels(mountpoint="/", device="default").set(total) - DISK_USED_BYTES.labels(mountpoint="/", device="default").set(used) - DISK_FREE_BYTES.labels(mountpoint="/", device="default").set(free) + DISK_TOTAL_BYTES.labels(mountpoint="/", device="/dev/sda1").set(total) + DISK_USED_BYTES.labels(mountpoint="/", device="/dev/sda1").set(used) + DISK_FREE_BYTES.labels(mountpoint="/", device="/dev/sda1").set(free) free_pct = (free / total * 100.0) if total > 0 else 0.0 - DISK_FREE_PERCENT.labels(mountpoint="/", device="default").set(free_pct) + DISK_FREE_PERCENT.labels(mountpoint="/", device="/dev/sda1").set(free_pct) except Exception as e: logger.debug(f"Error updating host disk metrics: {e}") diff --git a/monitoring/grafana/dashboards/copykar.json b/monitoring/grafana/dashboards/copykar.json index bd6b715..e20c2a5 100644 --- a/monitoring/grafana/dashboards/copykar.json +++ b/monitoring/grafana/dashboards/copykar.json @@ -1099,155 +1099,18 @@ }, { "id": 18, - "type": "bargauge", - "title": "💽 فضای خالی هر درایو و Mount Point (Free Space)", - "description": "نمایش حجم و فضای خالی تفکیکی هر یک از درایوها و پارتیشن‌های مونت‌شده سیستم‌عامل به صورت خوانا (Human-Readable).", - "datasource": { - "type": "prometheus", - "uid": "copykar-prometheus" - }, - "gridPos": { - "h": 8, - "w": 12, - "x": 0, - "y": 51 - }, - "targets": [ - { - "datasource": { - "type": "prometheus", - "uid": "copykar-prometheus" - }, - "refId": "A", - "expr": "copykar_disk_free_bytes", - "legendFormat": "{{mountpoint}} ({{device}})", - "instant": true - } - ], - "options": { - "displayMode": "gradient", - "orientation": "horizontal", - "showUnfilled": true, - "reduceOptions": { - "values": false, - "calcs": [ - "lastNotNull" - ], - "fields": "" - } - }, - "fieldConfig": { - "defaults": { - "unit": "bytes", - "color": { - "mode": "palette-classic" - }, - "thresholds": { - "mode": "absolute", - "steps": [ - { - "color": "red", - "value": null - }, - { - "color": "orange", - "value": 1073741824 - }, - { - "color": "green", - "value": 5368709120 - } - ] - } - }, - "overrides": [] - } - }, - { - "id": 19, - "type": "gauge", - "title": "📈 درصد فضای آزاد هر پارتیشن (Free % Gauges)", - "description": "درصد فضای خالی هر پارتیشن مونت‌شده سیستم‌عامل.", - "datasource": { - "type": "prometheus", - "uid": "copykar-prometheus" - }, - "gridPos": { - "h": 8, - "w": 12, - "x": 12, - "y": 51 - }, - "targets": [ - { - "datasource": { - "type": "prometheus", - "uid": "copykar-prometheus" - }, - "refId": "A", - "expr": "copykar_disk_free_percent", - "legendFormat": "{{mountpoint}}", - "instant": true - } - ], - "options": { - "reduceOptions": { - "values": false, - "calcs": [ - "lastNotNull" - ], - "fields": "" - }, - "showThresholdLabels": false, - "showThresholdMarkers": true - }, - "fieldConfig": { - "defaults": { - "unit": "percent", - "min": 0, - "max": 100, - "color": { - "mode": "thresholds" - }, - "thresholds": { - "mode": "absolute", - "steps": [ - { - "color": "red", - "value": null - }, - { - "color": "orange", - "value": 15 - }, - { - "color": "yellow", - "value": 30 - }, - { - "color": "green", - "value": 50 - } - ] - } - }, - "overrides": [] - } - }, - { - "id": 20, "type": "timeseries", - "title": "💽 روند زمانی فضای آزاد هر درایو (Free Space History)", - "description": "روند زمانی فضای آزاد هر یک از درایوهای مونت‌شده سیستم‌عامل.", + "title": "💽 روند زمانی فضای آزاد درایوهای سیستم‌عامل (Host OS Free Storage)", + "description": "روند زمانی فضای آزاد واقعی هر یک از درایوهای مونت‌شده سیستم‌عامل سرور به صورت خوانا (Human-Readable).", "datasource": { "type": "prometheus", "uid": "copykar-prometheus" }, "gridPos": { - "h": 8, + "h": 9, "w": 24, "x": 0, - "y": 59 + "y": 51 }, "targets": [ { @@ -1256,7 +1119,7 @@ "uid": "copykar-prometheus" }, "refId": "A", - "expr": "copykar_disk_free_bytes", + "expr": "copykar_disk_free_bytes{mountpoint=~\".+\"}", "legendFormat": "{{mountpoint}} ({{device}})" } ], @@ -1314,6 +1177,6 @@ "timezone": "browser", "title": "داشبورد مدیریت کپی‌کار (Copykar Dashboard)", "uid": "copykar-executive-dashboard", - "version": 7, + "version": 8, "weekStart": "" } \ No newline at end of file