From c5527fa4e696100a20bd9c1f354af3b8f3c5df51 Mon Sep 17 00:00:00 2001 From: Carter Li Date: Sun, 13 Oct 2024 17:18:53 +0800 Subject: [PATCH] Zpool: don't report invalid fragmentation --- src/detection/zpool/zpool.h | 2 +- src/detection/zpool/zpool_linux.c | 3 ++- src/modules/zpool/zpool.c | 9 ++++----- 3 files changed, 7 insertions(+), 7 deletions(-) diff --git a/src/detection/zpool/zpool.h b/src/detection/zpool/zpool.h index 8cd53da36..aa8bee03e 100644 --- a/src/detection/zpool/zpool.h +++ b/src/detection/zpool/zpool.h @@ -9,7 +9,7 @@ typedef struct FFZpoolResult uint64_t used; uint64_t total; uint64_t version; - uint64_t fragmentation; + double fragmentation; } FFZpoolResult; const char* ffDetectZpool(FFlist* result /* list of FFZpoolResult */); diff --git a/src/detection/zpool/zpool_linux.c b/src/detection/zpool/zpool_linux.c index 59890990b..6c28def6d 100644 --- a/src/detection/zpool/zpool_linux.c +++ b/src/detection/zpool/zpool_linux.c @@ -73,7 +73,8 @@ static int enumZpoolCallback(zpool_handle_t* zpool, void* param) item->version = data->ffzpool_get_prop_int(zpool, ZPOOL_PROP_VERSION, &source); item->total = data->ffzpool_get_prop_int(zpool, ZPOOL_PROP_SIZE, &source); item->used = item->total - data->ffzpool_get_prop_int(zpool, ZPOOL_PROP_FREE, &source); - item->fragmentation = data->ffzpool_get_prop_int(zpool, ZPOOL_PROP_FRAGMENTATION, &source); + uint64_t fragmentation = data->ffzpool_get_prop_int(zpool, ZPOOL_PROP_FRAGMENTATION, &source); + item->fragmentation = fragmentation == UINT64_MAX ? 0.0/0.0 : (double) fragmentation; return 0; } diff --git a/src/modules/zpool/zpool.c b/src/modules/zpool/zpool.c index 7288a03dd..719d411bf 100644 --- a/src/modules/zpool/zpool.c +++ b/src/modules/zpool/zpool.c @@ -34,7 +34,6 @@ static void printZpool(FFZpoolOptions* options, FFZpoolResult* result, uint8_t i ffParseSize(result->total, &totalPretty); double bytesPercentage = result->total > 0 ? (double) result->used / (double) result->total * 100.0 : 0; - double fragPercentage = (double) result->fragmentation; if(options->moduleArgs.outputFormat.length == 0) { @@ -44,7 +43,7 @@ static void printZpool(FFZpoolOptions* options, FFZpoolResult* result, uint8_t i ffStrbufSetF(&buffer, "%s / %s (", usedPretty.chars, totalPretty.chars); ffPercentAppendNum(&buffer, bytesPercentage, options->percent, false, &options->moduleArgs); ffStrbufAppendS(&buffer, ", "); - ffPercentAppendNum(&buffer, fragPercentage, options->percent, false, &options->moduleArgs); + ffPercentAppendNum(&buffer, result->fragmentation, options->percent, false, &options->moduleArgs); ffStrbufAppendF(&buffer, " frag) - %s", result->state.chars); ffStrbufPutTo(&buffer, stdout); } @@ -56,9 +55,9 @@ static void printZpool(FFZpoolOptions* options, FFZpoolResult* result, uint8_t i ffPercentAppendBar(&bytesPercentageBar, bytesPercentage, options->percent, &options->moduleArgs); FF_STRBUF_AUTO_DESTROY fragPercentageNum = ffStrbufCreate(); - ffPercentAppendNum(&fragPercentageNum, fragPercentage, options->percent, false, &options->moduleArgs); + ffPercentAppendNum(&fragPercentageNum, result->fragmentation, options->percent, false, &options->moduleArgs); FF_STRBUF_AUTO_DESTROY fragPercentageBar = ffStrbufCreate(); - ffPercentAppendBar(&fragPercentageBar, fragPercentage, options->percent, &options->moduleArgs); + ffPercentAppendBar(&fragPercentageBar, result->fragmentation, options->percent, &options->moduleArgs); FF_PRINT_FORMAT_CHECKED(buffer.chars, 0, &options->moduleArgs, FF_PRINT_TYPE_NO_CUSTOM_KEY, FF_ZPOOL_NUM_FORMAT_ARGS, ((FFformatarg[]) { FF_FORMAT_ARG(result->name, "name"), @@ -168,7 +167,7 @@ void ffGenerateZpoolJsonResult(FF_MAYBE_UNUSED FFZpoolOptions* options, yyjson_m yyjson_mut_obj_add_uint(doc, obj, "used", zpool->used); yyjson_mut_obj_add_uint(doc, obj, "total", zpool->total); yyjson_mut_obj_add_uint(doc, obj, "version", zpool->version); - yyjson_mut_obj_add_uint(doc, obj, "fragmentation", zpool->fragmentation); + yyjson_mut_obj_add_real(doc, obj, "fragmentation", zpool->fragmentation); } FF_LIST_FOR_EACH(FFZpoolResult, zpool, results)