From 7751a7fdd3aa56b99bc0855213ef960967ceb46c Mon Sep 17 00:00:00 2001 From: giovannilentini Date: Mon, 16 Nov 2026 18:42:16 +0100 Subject: [PATCH] PhysicalDisk: adds health option and result fields Ref: #2726 --- doc/json_schema.json | 10 +- src/detection/physicaldisk/physicaldisk.h | 17 +++ .../physicaldisk/physicaldisk_apple.c | 3 + src/detection/physicaldisk/physicaldisk_bsd.c | 3 + .../physicaldisk/physicaldisk_haiku.c | 3 + .../physicaldisk/physicaldisk_linux.c | 3 + .../physicaldisk/physicaldisk_nbsd.c | 3 + .../physicaldisk/physicaldisk_obsd.c | 3 + .../physicaldisk/physicaldisk_sunos.c | 3 + .../physicaldisk/physicaldisk_windows.c | 3 + src/modules/physicaldisk/option.h | 3 + src/modules/physicaldisk/physicaldisk.c | 110 ++++++++++++++++++ 12 files changed, 163 insertions(+), 1 deletion(-) diff --git a/doc/json_schema.json b/doc/json_schema.json index 1470870b4..92368db67 100644 --- a/doc/json_schema.json +++ b/doc/json_schema.json @@ -453,7 +453,7 @@ "type": "string" }, "physicaldiskFormat": { - "description": "Output format for the `PhysicalDisk` module. See Wiki for formatting syntax\n 1. {size}: Device size (formatted)\n 2. {name}: Device name *\n 3. {interconnect}: Device interconnect type\n 4. {dev-path}: Device raw file path *\n 5. {serial}: Serial number\n 6. {physical-type}: Device kind (SSD or HDD)\n 7. {removable-type}: Device kind (Removable or Fixed)\n 8. {readonly-type}: Device kind (Read-only or Read-write)\n 9. {revision}: Product revision\n 10. {temperature}: Device temperature (formatted)", + "description": "Output format for the `PhysicalDisk` module. See Wiki for formatting syntax\n 1. {size}: Device size (formatted)\n 2. {name}: Device name *\n 3. {interconnect}: Device interconnect type\n 4. {dev-path}: Device raw file path *\n 5. {serial}: Serial number\n 6. {physical-type}: Device kind (SSD or HDD)\n 7. {removable-type}: Device kind (Removable or Fixed)\n 8. {readonly-type}: Device kind (Read-only or Read-write)\n 9. {revision}: Product revision\n 10. {temperature}: Device temperature (formatted)\n 11. {health}: Device health, 100 - percentage used (formatted)\n 12. {percent-used}: Percentage of rated endurance used\n 13. {available-spare}: Remaining spare capacity\n 14. {critical-warning}: Critical warnings", "type": "string" }, "physicalmemoryFormat": { @@ -3967,6 +3967,14 @@ "temp": { "$ref": "#/$defs/temperature" }, + "health": { + "description": "Whether to detect and display drive health (NVMe wear / SMART status), if supported", + "type": "boolean", + "default": false + }, + "percent": { + "$ref": "#/$defs/percent" + }, "key": { "$ref": "#/$defs/key" }, diff --git a/src/detection/physicaldisk/physicaldisk.h b/src/detection/physicaldisk/physicaldisk.h index 3481063be..7520c9712 100644 --- a/src/detection/physicaldisk/physicaldisk.h +++ b/src/detection/physicaldisk/physicaldisk.h @@ -2,6 +2,20 @@ #include "modules/physicaldisk/option.h" #define FF_PHYSICALDISK_TEMP_UNSET (-DBL_MAX) +#define FF_PHYSICALDISK_PERCENT_UNSET UINT8_MAX + +// Bits 0-5 match NVMe Critical Warning +typedef enum FFPhysicalDiskWarning: uint8_t { + FF_PHYSICALDISK_WARNING_NONE = 0, + FF_PHYSICALDISK_WARNING_SPARE = 1 << 0, + FF_PHYSICALDISK_WARNING_TEMPERATURE = 1 << 1, + FF_PHYSICALDISK_WARNING_RELIABILITY = 1 << 2, + FF_PHYSICALDISK_WARNING_READONLY = 1 << 3, + FF_PHYSICALDISK_WARNING_BACKUP = 1 << 4, + FF_PHYSICALDISK_WARNING_PMR_READONLY = 1 << 5, + FF_PHYSICALDISK_WARNING_UNSET = UINT8_MAX, +} FFPhysicalDiskWarning; +static_assert(sizeof(FFPhysicalDiskWarning) == sizeof(uint8_t), ""); typedef struct FFPhysicalDiskResult { FFstrbuf name; @@ -12,6 +26,9 @@ typedef struct FFPhysicalDiskResult { FFPhysicalDiskType type; uint64_t size; double temperature; + uint8_t percentUsed; + uint8_t availableSpare; + FFPhysicalDiskWarning criticalWarning; } FFPhysicalDiskResult; const char* ffDetectPhysicalDisk(FFlist* result, FFPhysicalDiskOptions* options); diff --git a/src/detection/physicaldisk/physicaldisk_apple.c b/src/detection/physicaldisk/physicaldisk_apple.c index 789ab624d..e56bbb4f1 100644 --- a/src/detection/physicaldisk/physicaldisk_apple.c +++ b/src/detection/physicaldisk/physicaldisk_apple.c @@ -144,6 +144,9 @@ const char* ffDetectPhysicalDisk(FFlist* result, FFPhysicalDiskOptions* options) device->type = type; device->size = size; device->temperature = FF_PHYSICALDISK_TEMP_UNSET; + device->percentUsed = FF_PHYSICALDISK_PERCENT_UNSET; + device->availableSpare = FF_PHYSICALDISK_PERCENT_UNSET; + device->criticalWarning = FF_PHYSICALDISK_WARNING_UNSET; if (entryMedia != IO_OBJECT_NULL) { FF_CFTYPE_AUTO_RELEASE CFBooleanRef removable = IORegistryEntryCreateCFProperty(entryMedia, CFSTR(kIOMediaRemovableKey), nullptr, kNilOptions); diff --git a/src/detection/physicaldisk/physicaldisk_bsd.c b/src/detection/physicaldisk/physicaldisk_bsd.c index 5d59fd9d9..93bdad0df 100644 --- a/src/detection/physicaldisk/physicaldisk_bsd.c +++ b/src/detection/physicaldisk/physicaldisk_bsd.c @@ -90,6 +90,9 @@ const char* ffDetectPhysicalDisk(FFlist* result, FFPhysicalDiskOptions* options) ffStrbufInitMove(&device->name, &name); device->size = size; device->temperature = FF_PHYSICALDISK_TEMP_UNSET; + device->percentUsed = FF_PHYSICALDISK_PERCENT_UNSET; + device->availableSpare = FF_PHYSICALDISK_PERCENT_UNSET; + device->criticalWarning = FF_PHYSICALDISK_WARNING_UNSET; switch (snapIter->device_type & DEVSTAT_TYPE_IF_MASK) { case DEVSTAT_TYPE_IF_SCSI: diff --git a/src/detection/physicaldisk/physicaldisk_haiku.c b/src/detection/physicaldisk/physicaldisk_haiku.c index ab5599658..8048786b6 100644 --- a/src/detection/physicaldisk/physicaldisk_haiku.c +++ b/src/detection/physicaldisk/physicaldisk_haiku.c @@ -71,6 +71,9 @@ static const char* searchRawDeviceFile(FFstrbuf* path, const char* diskType, FFl ffStrbufInit(&device->revision); ffStrbufInitS(&device->interconnect, diskType); device->temperature = FF_PHYSICALDISK_TEMP_UNSET; + device->percentUsed = FF_PHYSICALDISK_PERCENT_UNSET; + device->availableSpare = FF_PHYSICALDISK_PERCENT_UNSET; + device->criticalWarning = FF_PHYSICALDISK_WARNING_UNSET; device->type = type | (geometry.read_only ? FF_PHYSICALDISK_TYPE_READONLY : FF_PHYSICALDISK_TYPE_READWRITE) | (geometry.removable ? FF_PHYSICALDISK_TYPE_REMOVABLE : FF_PHYSICALDISK_TYPE_FIXED); diff --git a/src/detection/physicaldisk/physicaldisk_linux.c b/src/detection/physicaldisk/physicaldisk_linux.c index 7a9fd5f79..8a0624c99 100644 --- a/src/detection/physicaldisk/physicaldisk_linux.c +++ b/src/detection/physicaldisk/physicaldisk_linux.c @@ -107,6 +107,9 @@ static void parsePhysicalDisk(int dfd, const char* devName, FFPhysicalDiskOption device->type = type; device->size = size; device->temperature = FF_PHYSICALDISK_TEMP_UNSET; + device->percentUsed = FF_PHYSICALDISK_PERCENT_UNSET; + device->availableSpare = FF_PHYSICALDISK_PERCENT_UNSET; + device->criticalWarning = FF_PHYSICALDISK_WARNING_UNSET; bool isVirtio = false; if (devfd > 0) { diff --git a/src/detection/physicaldisk/physicaldisk_nbsd.c b/src/detection/physicaldisk/physicaldisk_nbsd.c index bf9bd3adb..23381b613 100644 --- a/src/detection/physicaldisk/physicaldisk_nbsd.c +++ b/src/detection/physicaldisk/physicaldisk_nbsd.c @@ -220,6 +220,9 @@ const char* ffDetectPhysicalDisk(FFlist* result, FFPhysicalDiskOptions* options) device->type = type; device->size = size; device->temperature = FF_PHYSICALDISK_TEMP_UNSET; + device->percentUsed = FF_PHYSICALDISK_PERCENT_UNSET; + device->availableSpare = FF_PHYSICALDISK_PERCENT_UNSET; + device->criticalWarning = FF_PHYSICALDISK_WARNING_UNSET; FF_DEBUG("Added disk entry: name='%s', devPath='%s', interconnect='%s', size=%llu", device->name.chars, device->devPath.chars, diff --git a/src/detection/physicaldisk/physicaldisk_obsd.c b/src/detection/physicaldisk/physicaldisk_obsd.c index 7b8680675..152df688b 100644 --- a/src/detection/physicaldisk/physicaldisk_obsd.c +++ b/src/detection/physicaldisk/physicaldisk_obsd.c @@ -187,6 +187,9 @@ const char* ffDetectPhysicalDisk(FFlist* result, FFPhysicalDiskOptions* options) device->type = type; device->size = size; device->temperature = FF_PHYSICALDISK_TEMP_UNSET; + device->percentUsed = FF_PHYSICALDISK_PERCENT_UNSET; + device->availableSpare = FF_PHYSICALDISK_PERCENT_UNSET; + device->criticalWarning = FF_PHYSICALDISK_WARNING_UNSET; FF_DEBUG("Added disk entry: name='%s', devPath='%s', interconnect='%s', size=%llu", device->name.chars, device->devPath.chars, diff --git a/src/detection/physicaldisk/physicaldisk_sunos.c b/src/detection/physicaldisk/physicaldisk_sunos.c index 9ac2a30ef..10b14b7f4 100644 --- a/src/detection/physicaldisk/physicaldisk_sunos.c +++ b/src/detection/physicaldisk/physicaldisk_sunos.c @@ -51,6 +51,9 @@ static int walkDevTree(di_node_t node, di_minor_t minor, struct FFWalkTreeBundle ffStrbufInit(&device->revision); ffStrbufInit(&device->interconnect); device->temperature = FF_PHYSICALDISK_TEMP_UNSET; + device->percentUsed = FF_PHYSICALDISK_PERCENT_UNSET; + device->availableSpare = FF_PHYSICALDISK_PERCENT_UNSET; + device->criticalWarning = FF_PHYSICALDISK_WARNING_UNSET; device->type = type; device->size = size; diff --git a/src/detection/physicaldisk/physicaldisk_windows.c b/src/detection/physicaldisk/physicaldisk_windows.c index 3d6dccc2f..923d0ac6b 100644 --- a/src/detection/physicaldisk/physicaldisk_windows.c +++ b/src/detection/physicaldisk/physicaldisk_windows.c @@ -214,6 +214,9 @@ static const char* detectPhysicalDisk(const char* physicalType, const wchar_t* s device->type = type; device->size = size; device->temperature = FF_PHYSICALDISK_TEMP_UNSET; + device->percentUsed = FF_PHYSICALDISK_PERCENT_UNSET; + device->availableSpare = FF_PHYSICALDISK_PERCENT_UNSET; + device->criticalWarning = FF_PHYSICALDISK_WARNING_UNSET; ffStrbufSetWS(&device->devPath, szDevice); diff --git a/src/modules/physicaldisk/option.h b/src/modules/physicaldisk/option.h index 0b3c81855..f084dfdd6 100644 --- a/src/modules/physicaldisk/option.h +++ b/src/modules/physicaldisk/option.h @@ -1,6 +1,7 @@ #pragma once #include "common/option.h" +#include "common/percent.h" typedef enum FFPhysicalDiskType: uint8_t { FF_PHYSICALDISK_TYPE_NONE = 0, @@ -27,6 +28,8 @@ typedef struct FFPhysicalDiskOptions { FFPhysicalDiskType hideType; bool temp; FFColorRangeConfig tempConfig; + bool health; + FFPercentageModuleConfig percent; } FFPhysicalDiskOptions; static_assert(sizeof(FFPhysicalDiskOptions) <= FF_OPTION_MAX_SIZE, "FFPhysicalDiskOptions size exceeds maximum allowed size"); diff --git a/src/modules/physicaldisk/physicaldisk.c b/src/modules/physicaldisk/physicaldisk.c index bf8c5c389..8a951cc1e 100644 --- a/src/modules/physicaldisk/physicaldisk.c +++ b/src/modules/physicaldisk/physicaldisk.c @@ -1,6 +1,7 @@ #include "common/printing.h" #include "common/jsonconfig.h" #include "common/temps.h" +#include "common/percent.h" #include "common/size.h" #include "detection/physicaldisk/physicaldisk.h" #include "modules/physicaldisk/physicaldisk.h" @@ -9,6 +10,19 @@ static int sortDevices(const FFPhysicalDiskResult* left, const FFPhysicalDiskRes return ffStrbufComp(&left->name, &right->name); } +static const char* const warningNames[] = { + "spare below threshold", + "temperature", + "reliability degraded", + "read-only", + "backup failed", + "pmr read-only", +}; + +static inline double getHealth(const FFPhysicalDiskResult* dev) { + return dev->percentUsed >= 100 ? 0 : 100 - dev->percentUsed; +} + static void formatKey(const FFPhysicalDiskOptions* options, FFPhysicalDiskResult* dev, uint32_t index, FFstrbuf* key) { if (options->moduleArgs.key.length == 0) { ffStrbufSetF(key, "%s (%s)", FF_MODULE_GET_DISPLAY_NAME(PhysicalDisk), dev->name.length ? dev->name.chars : dev->devPath.chars); @@ -35,6 +49,8 @@ bool ffPrintPhysicalDisk(FFPhysicalDiskOptions* options) { ffListSort(&result, sizeof(FFPhysicalDiskResult), (const void*) sortDevices); + FFPercentageTypeFlags percentType = options->percent.type == 0 ? instance.config.display.percentType : options->percent.type; + uint32_t index = 0; FF_STRBUF_AUTO_DESTROY key = ffStrbufCreate(); FF_STRBUF_AUTO_DESTROY buffer = ffStrbufCreate(); @@ -90,10 +106,55 @@ bool ffPrintPhysicalDisk(FFPhysicalDiskOptions* options) { ffTempsAppendNum(dev->temperature, &buffer, options->tempConfig, &options->moduleArgs); } + + if (dev->percentUsed != FF_PHYSICALDISK_PERCENT_UNSET && (percentType & (FF_PERCENTAGE_TYPE_NUM_BIT | FF_PERCENTAGE_TYPE_BAR_BIT))) { + ffStrbufAppendS(&buffer, " - Health "); + bool hasBar = percentType & FF_PERCENTAGE_TYPE_BAR_BIT; + if (hasBar) { + ffPercentAppendBar(&buffer, getHealth(dev), options->percent, &options->moduleArgs); + } + if (percentType & FF_PERCENTAGE_TYPE_NUM_BIT) { + if (hasBar) { + ffStrbufAppendC(&buffer, ' '); + } + ffPercentAppendNum(&buffer, getHealth(dev), options->percent, hasBar, &options->moduleArgs); + } + } + + if (dev->criticalWarning != FF_PHYSICALDISK_WARNING_UNSET) { + const char* separator = " - Warning: "; + for (uint32_t i = 0; i < ARRAY_SIZE(warningNames); ++i) { + if (dev->criticalWarning & (1 << i)) { + ffStrbufAppendS(&buffer, separator); + ffStrbufAppendS(&buffer, warningNames[i]); + separator = ", "; + } + } + } ffStrbufPutTo(&buffer, stdout); } else { FF_STRBUF_AUTO_DESTROY tempStr = ffStrbufCreate(); ffTempsAppendNum(dev->temperature, &tempStr, options->tempConfig, &options->moduleArgs); + + FF_STRBUF_AUTO_DESTROY healthStr = ffStrbufCreate(); + FF_STRBUF_AUTO_DESTROY percentUsedStr = ffStrbufCreate(); + if (dev->percentUsed != FF_PHYSICALDISK_PERCENT_UNSET) { + ffPercentAppendNum(&healthStr, getHealth(dev), options->percent, false, &options->moduleArgs); + ffStrbufAppendF(&percentUsedStr, "%u%%", (unsigned) dev->percentUsed); + } + FF_STRBUF_AUTO_DESTROY spareStr = ffStrbufCreate(); + if (dev->availableSpare != FF_PHYSICALDISK_PERCENT_UNSET) { + ffStrbufAppendF(&spareStr, "%u%%", (unsigned) dev->availableSpare); + } + FF_LIST_AUTO_DESTROY warnings = ffListCreate(); + if (dev->criticalWarning != FF_PHYSICALDISK_WARNING_UNSET) { + for (uint32_t i = 0; i < ARRAY_SIZE(warningNames); ++i) { + if (dev->criticalWarning & (1 << i)) { + ffStrbufInitStatic(FF_LIST_ADD(FFstrbuf, warnings), warningNames[i]); + } + } + } + if (dev->type & FF_PHYSICALDISK_TYPE_READWRITE) { readOnlyType = "Read-write"; } @@ -108,6 +169,10 @@ bool ffPrintPhysicalDisk(FFPhysicalDiskOptions* options) { FF_ARG(readOnlyType, "readonly-type"), FF_ARG(dev->revision, "revision"), FF_ARG(tempStr, "temperature"), + FF_ARG(healthStr, "health"), + FF_ARG(percentUsedStr, "percent-used"), + FF_ARG(spareStr, "available-spare"), + FF_ARG(warnings, "critical-warning"), })); } ++index; @@ -167,6 +232,19 @@ void ffParsePhysicalDiskJsonObject(FFPhysicalDiskOptions* options, yyjson_val* m continue; } + if (unsafe_yyjson_equals_str(key, "health")) { + if (!yyjson_is_bool(val)) { + ffPrintError(FF_MODULE_GET_DISPLAY_NAME(PhysicalDisk), 0, &options->moduleArgs, FF_PRINT_TYPE_DEFAULT, "health must be a boolean"); + } else { + options->health = unsafe_yyjson_is_true(val); + } + continue; + } + + if (ffPercentParseJsonObject(key, val, &options->percent)) { + continue; + } + ffPrintError(FF_MODULE_GET_DISPLAY_NAME(PhysicalDisk), 0, &options->moduleArgs, FF_PRINT_TYPE_DEFAULT, "Unknown JSON key %s", unsafe_yyjson_get_str(key)); } } @@ -179,6 +257,9 @@ void ffGeneratePhysicalDiskJsonConfig(FFPhysicalDiskOptions* options, yyjson_mut ffTempsGenerateJsonConfig(doc, module, options->temp, options->tempConfig); + yyjson_mut_obj_add_bool(doc, module, "health", options->health); + ffPercentGenerateJsonConfig(doc, module, options->percent); + yyjson_mut_obj_add_bool(doc, module, "hideVirtual", !!(options->hideType & FF_PHYSICALDISK_TYPE_VIRTUAL)); yyjson_mut_obj_add_bool(doc, module, "hideUnused", !!(options->hideType & FF_PHYSICALDISK_TYPE_UNUSED)); } @@ -237,6 +318,29 @@ bool ffGeneratePhysicalDiskJsonResult(FFPhysicalDiskOptions* options, yyjson_mut } else { yyjson_mut_obj_add_null(doc, obj, "temperature"); } + + if (dev->percentUsed != FF_PHYSICALDISK_PERCENT_UNSET) { + yyjson_mut_obj_add_uint(doc, obj, "percentUsed", dev->percentUsed); + } else { + yyjson_mut_obj_add_null(doc, obj, "percentUsed"); + } + + if (dev->availableSpare != FF_PHYSICALDISK_PERCENT_UNSET) { + yyjson_mut_obj_add_uint(doc, obj, "availableSpare", dev->availableSpare); + } else { + yyjson_mut_obj_add_null(doc, obj, "availableSpare"); + } + + if (dev->criticalWarning != FF_PHYSICALDISK_WARNING_UNSET) { + yyjson_mut_val* warnings = yyjson_mut_obj_add_arr(doc, obj, "criticalWarning"); + for (uint32_t i = 0; i < ARRAY_SIZE(warningNames); ++i) { + if (dev->criticalWarning & (1 << i)) { + yyjson_mut_arr_add_str(doc, warnings, warningNames[i]); + } + } + } else { + yyjson_mut_obj_add_null(doc, obj, "criticalWarning"); + } } FF_LIST_FOR_EACH (FFPhysicalDiskResult, dev, result) { @@ -256,6 +360,8 @@ void ffInitPhysicalDiskOptions(FFPhysicalDiskOptions* options) { ffStrbufInit(&options->namePrefix); options->temp = false; options->tempConfig = (FFColorRangeConfig) { 50, 70 }; + options->health = false; + options->percent = (FFPercentageModuleConfig) { 50, 20, 0 }; options->hideType = FF_PHYSICALDISK_TYPE_UNUSED; } @@ -307,6 +413,10 @@ FFModuleBaseInfo ffPhysicalDiskModuleInfo = { { "Device kind (Read-only or Read-write)", "readonly-type" }, { "Product revision", "revision" }, { "Device temperature (formatted)", "temperature" }, + { "Device health, 100 - percentage used (formatted)", "health" }, + { "Percentage of rated endurance used", "percent-used" }, + { "Remaining spare capacity", "available-spare" }, + { "Critical warnings (none if healthy)", "critical-warning" }, })), .defaultOrder = 69, };