PhysicalDisk: adds health option and result fields

Ref: #2726
This commit is contained in:
giovannilentini authored and Carter Li committed 2026-11-17 15:19:38 +08:00
1 parent 63a82100f6
commit 7751a7fdd3
12 files changed
+163 -1

No files matched your search

+9 -1
View File
@@ -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"
},
+17
View File
@@ -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);
@@ -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);
@@ -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:
@@ -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);
@@ -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) {
@@ -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,
@@ -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,
@@ -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;
@@ -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);
+3
View File
@@ -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");
+110
View File
@@ -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,
};