From 75922917e5d1248178427de2e4b8a55ffd667589 Mon Sep 17 00:00:00 2001 From: Imanol-Mikel Barba Sabariego Date: Wed, 6 Nov 2024 18:16:22 +0100 Subject: [PATCH] Fix incorrect attr matching --- check_smart/check_smart.cpp | 64 +++++++++++++++++++++++++++++++++++++--------------------------- check_smart/check_smart.h | 13 +++++++++++-- 2 files changed, 48 insertions(+), 29 deletions(-) diff --git a/check_smart/check_smart.cpp b/check_smart/check_smart.cpp index f511a50..aa5599b 100644 --- a/check_smart/check_smart.cpp +++ b/check_smart/check_smart.cpp @@ -42,6 +42,15 @@ int getSmartAttrID(string line) { return id; } +std::string getSmartAttrName(std::string line) { + size_t first = line.find_first_not_of(' ', line.find_first_of(' ',line.find_first_not_of(' '))); + size_t last = line.find_first_of(' ', first); + if(first == std::string::npos || last == std::string::npos) { + return ""; + } + return line.substr(first,last-first); +} + int checkDriveType(const char* disk) { string output = ""; string RRLine = ""; @@ -70,20 +79,21 @@ int checkDriveType(const char* disk) { return HDD; } -map prepareAttrMap(int driveType) { - map map; - map[REALLOC_SEC_COUNT_ID] = reallocated; - map[REP_UNCORRECT_ID] = rep_uncorrect; +map prepareAttrMap(int driveType) { + map map; + map[REALLOC_SEC_COUNT_NAME] = &reallocated; + map[REP_UNCORRECT_NAME] = &rep_uncorrect; + map[UNCORRECT_COUNT_NAME] = &rep_uncorrect; switch(driveType) { case HDD: - map[CURRENT_PENDING_SEC_ID] = pending; - map[OFFLINE_UNCORRECT_ID] = off_uncorrect; + map[CURRENT_PENDING_SEC_NAME] = &pending; + map[OFFLINE_UNCORRECT_NAME] = &off_uncorrect; break; case SSD: - map[WEAR_COUNT_ID] = wear; - map[MEDIA_WEAROUT_ID] = wearout; - map[RUNTIME_BAD_BLOCKS_ID] = badblocks; + map[WEAR_COUNT_NAME] = &wear; + map[MEDIA_WEAROUT_NAME] = &wearout; + map[RUNTIME_BAD_BLOCKS_NAME] = &badblocks; break; } return map; @@ -99,30 +109,30 @@ int evalStatus(const char* disk, int driveType, string *status) { exit(UNKN); } - map attrMap = prepareAttrMap(driveType); + map attrMap = prepareAttrMap(driveType); stringstream sstream; sstream.str(output); while(getline(sstream,line)) { - for(map::iterator it = attrMap.begin(); it != attrMap.end(); ++it) { - int id = it->first; - int attrID = getSmartAttrID(line); - if(attrID == -1) { + for(map::iterator it = attrMap.begin(); it != attrMap.end(); ++it) { + std::string name = it->first; + std::string attrName = getSmartAttrName(line); + if(attrName == "") { continue; } - if(getSmartAttrID(line) == id) { - attrMap[id].value = getSmartAttrValue(line, attrMap[id].col); + if(attrName == name) { + attrMap[name]->value = getSmartAttrValue(line, attrMap[name]->col); } } } int ret = OK; *status = string(disk); - for(map::iterator it = attrMap.begin(); it != attrMap.end(); ++it) { - int id = it->first; - SMARTAttr attr = it->second; + for(map::iterator it = attrMap.begin(); it != attrMap.end(); ++it) { + std::string name = it->first; + SMARTAttr* attr = it->second; - if(attr.value == -1) { - if(attr.optional) { + if(attr->value == -1) { + if(attr->optional) { continue; } *status = string(disk) + " status UNKNOWN"; @@ -131,18 +141,18 @@ int evalStatus(const char* disk, int driveType, string *status) { int veredict = 0; - if(attr.lower_than) { - if(attr.value < attr.threshold_warn) { + if(attr->lower_than) { + if(attr->value < attr->threshold_warn) { veredict = WARN; } - if(attr.threshold_crit != -1 && attr.value < attr.threshold_crit) { + if(attr->threshold_crit != -1 && attr->value < attr->threshold_crit) { veredict = CRIT; } } else { - if(attr.value > attr.threshold_warn) { + if(attr->value > attr->threshold_warn) { veredict = WARN; } - if(attr.threshold_crit != -1 && attr.value > attr.threshold_crit) { + if(attr->threshold_crit != -1 && attr->value > attr->threshold_crit) { veredict = CRIT; } } @@ -159,7 +169,7 @@ int evalStatus(const char* disk, int driveType, string *status) { ret = CRIT; break; } - *status += " " + attr.name + ":" + to_string(attr.value); + *status += " " + attr->name + ":" + to_string(attr->value); } return ret; } diff --git a/check_smart/check_smart.h b/check_smart/check_smart.h index 78a4320..92c6a00 100755 --- a/check_smart/check_smart.h +++ b/check_smart/check_smart.h @@ -35,6 +35,15 @@ #define RUNTIME_BAD_BLOCKS_ID 183 #define REP_UNCORRECT_ID 187 +#define REALLOC_SEC_COUNT_NAME "Reallocated_Sector_Ct" +#define CURRENT_PENDING_SEC_NAME "Current_Pending_Sector" +#define OFFLINE_UNCORRECT_NAME "Offline_Uncorrectable" +#define WEAR_COUNT_NAME "Wear_Leveling_Count" +#define MEDIA_WEAROUT_NAME "Media_Wearout_Indicator" +#define RUNTIME_BAD_BLOCKS_NAME "Runtime_Bad_Block" +#define REP_UNCORRECT_NAME "Reported_Uncorrect" +#define UNCORRECT_COUNT_NAME "Uncorrectable_Error_Cnt" + #define HDD 0 #define SSD 1 @@ -115,7 +124,7 @@ SMARTAttr badblocks = { .value = -1, .threshold_warn = 0, .threshold_crit = 0, - .optional = false, + .optional = true, .col = 9, .lower_than = false, }; @@ -131,7 +140,7 @@ SMARTAttr rep_uncorrect = { .lower_than = false, }; -map prepareAttrMap(int driveType); +map prepareAttrMap(int driveType); int getSmartAttrValue(string line); int getSmartAttrID(string line); int run_smartctl_cmd(const string command, const char* disk, string* output); -- libgit2 0.22.2