From 831a8920411d842d71445afe9727417610efba20 Mon Sep 17 00:00:00 2001 From: localhorst Date: Tue, 28 Apr 2026 21:20:30 +0200 Subject: [PATCH] show HDD warnings based on sectors --- include/drive.h | 13 ++++- include/smart.h | 3 ++ include/tui.h | 2 +- src/drive.cpp | 23 ++++++++- src/reHDD.cpp | 2 +- src/smart.cpp | 129 +++++++++++++++++++++++++++++++++++++++++++++++- src/tui.cpp | 28 +++++++++-- 7 files changed, 191 insertions(+), 9 deletions(-) diff --git a/include/drive.h b/include/drive.h index bafe7e5..3220a3a 100644 --- a/include/drive.h +++ b/include/drive.h @@ -72,7 +72,10 @@ private: uint32_t u32ErrorCount = 0U; uint32_t u32PowerOnHours = 0U; // in hours uint32_t u32PowerCycles = 0U; - uint32_t u32Temperature = 0U; // in Fahrenheit, just kidding: degree Celsius + uint32_t u32Temperature = 0U; // in Fahrenheit, just kidding: degree Celsius + uint32_t u32ReallocatedSectors = 0U; // ID 0x05 - Reallocated Sectors Count + uint32_t u32PendingSectors = 0U; // ID 0xC5 - Current Pending Sector Count + uint32_t u32UncorrectableSectors = 0U; // ID 0xC6 - Offline Uncorrectable Sector Count } sSmartData; private: @@ -106,6 +109,9 @@ public: uint32_t getPowerOnHours(void); // in hours uint32_t getPowerCycles(void); uint32_t getTemperature(void); // in Fahrenheit, just kidding: degree Celsius + uint32_t getReallocatedSectors(void); + uint32_t getPendingSectors(void); + uint32_t getUncorrectableSectors(void); void checkFrozenDrive(void); void setDriveSMARTData(std::string modelFamily, @@ -115,7 +121,10 @@ public: uint32_t errorCount, uint32_t powerOnHours, uint32_t powerCycles, - uint32_t temperature); + uint32_t temperature, + uint32_t reallocatedSectors, + uint32_t pendingSectors, + uint32_t uncorrectableSectors); std::string sCapacityToText(); std::string sErrorCountToText(); diff --git a/include/smart.h b/include/smart.h index 2d4f4fd..7c7e340 100644 --- a/include/smart.h +++ b/include/smart.h @@ -28,6 +28,9 @@ private: static bool parsePowerOnHours(std::string sLine, uint32_t &powerOnHours); static bool parsePowerCycles(std::string sLine, uint32_t &powerCycles); static bool parseTemperature(std::string sLine, uint32_t &temperature); + static bool parseReallocatedSectors(std::string sLine, uint32_t &reallocatedSectors); + static bool parsePendingSectors(std::string sLine, uint32_t &pendingSectors); + static bool parseUncorrectableSectors(std::string sLine, uint32_t &uncorrectableSectors); }; #endif // SMART_H_ \ No newline at end of file diff --git a/include/tui.h b/include/tui.h index 3826650..d4a201b 100644 --- a/include/tui.h +++ b/include/tui.h @@ -76,7 +76,7 @@ private: static WINDOW *createMenuView(int iXSize, int iYSize, int iXStart, int iYStart, struct MenuState menustate); static WINDOW *createDialog(int iXSize, int iYSize, int iXStart, int iYStart, std::string selectedTask, std::string optionA, std::string optionB); static WINDOW *createFrozenWarning(int iXSize, int iYSize, int iXStart, int iYStart, std::string sPath, std::string sModelFamily, std::string sModelName, std::string sSerial, std::string sProgress); - static WINDOW *createSmartWarning(int iXSize, int iYSize, int iXStart, int iYStart, std::string sPath, uint32_t u32PowerOnHours, uint32_t u32PowerCycles, uint32_t u32ErrorCount, uint32_t u32Temperature); + static WINDOW *createSmartWarning(int iXSize, int iYSize, int iXStart, int iYStart, std::string sPath, uint32_t u32PowerOnHours, uint32_t u32PowerCycles, uint32_t u32ErrorCount, uint32_t u32Temperature, uint32_t u32ReallocatedSectors, uint32_t u32PendingSectors, uint32_t u32UncorrectableSectors); static WINDOW *createZeroChecksumWarning(int iXSize, int iYSize, int iXStart, int iYStart, std::string sPath, std::string sModelFamily, std::string sModelName, std::string sSerial, uint32_t u32Checksum); void displaySelectedDrive(Drive &drive, int stdscrX, int stdscrY); diff --git a/src/drive.cpp b/src/drive.cpp index c1683ce..58ffb76 100644 --- a/src/drive.cpp +++ b/src/drive.cpp @@ -140,6 +140,21 @@ uint32_t Drive::getTemperature(void) return sSmartData.u32Temperature; } +uint32_t Drive::getReallocatedSectors(void) +{ + return sSmartData.u32ReallocatedSectors; +} + +uint32_t Drive::getPendingSectors(void) +{ + return sSmartData.u32PendingSectors; +} + +uint32_t Drive::getUncorrectableSectors(void) +{ + return sSmartData.u32UncorrectableSectors; +} + string Drive::sCapacityToText() { char acBuffer[16]; @@ -226,7 +241,10 @@ void Drive::setDriveSMARTData(string modelFamily, uint32_t errorCount, uint32_t powerOnHours, uint32_t powerCycle, - uint32_t temperature) + uint32_t temperature, + uint32_t reallocatedSectors, + uint32_t pendingSectors, + uint32_t uncorrectableSectors) { this->sSmartData.sModelFamily = modelFamily; this->sSmartData.sModelName = modelName; @@ -236,6 +254,9 @@ void Drive::setDriveSMARTData(string modelFamily, this->sSmartData.u32PowerOnHours = powerOnHours; this->sSmartData.u32PowerCycles = powerCycle; this->sSmartData.u32Temperature = temperature; + this->sSmartData.u32ReallocatedSectors = reallocatedSectors; + this->sSmartData.u32PendingSectors = pendingSectors; + this->sSmartData.u32UncorrectableSectors = uncorrectableSectors; } void Drive::setTimestamp() diff --git a/src/reHDD.cpp b/src/reHDD.cpp index e7b0a98..37707af 100644 --- a/src/reHDD.cpp +++ b/src/reHDD.cpp @@ -332,7 +332,7 @@ void reHDD::filterNewDrives(list *plistOldDrives, list *plistNewDr { itOld->bIsOffline = false; // drive is still attached // copy new smart data to existing drive - itOld->setDriveSMARTData(itNew->getModelFamily(), itNew->getModelName(), itNew->getSerial(), itNew->getCapacity(), itNew->getErrorCount(), itNew->getPowerOnHours(), itNew->getPowerCycles(), itNew->getTemperature()); + itOld->setDriveSMARTData(itNew->getModelFamily(), itNew->getModelName(), itNew->getSerial(), itNew->getCapacity(), itNew->getErrorCount(), itNew->getPowerOnHours(), itNew->getPowerCycles(), itNew->getTemperature(), itNew->getReallocatedSectors(), itNew->getPendingSectors(), itNew->getUncorrectableSectors()); #ifdef LOG_LEVEL_HIGH Logger::logThis()->info("Delete new drive, because already attached: " + itNew->getModelName()); #endif diff --git a/src/smart.cpp b/src/smart.cpp index 2a10855..eb0c127 100644 --- a/src/smart.cpp +++ b/src/smart.cpp @@ -23,6 +23,9 @@ void SMART::readSMARTData(Drive *drive) uint32_t powerOnHours = 0U; uint32_t powerCycles = 0U; uint32_t temperature = 0U; + uint32_t reallocatedSectors = 0U; + uint32_t pendingSectors = 0U; + uint32_t uncorrectableSectors = 0U; modelFamily.clear(); modelName.clear(); @@ -57,6 +60,9 @@ void SMART::readSMARTData(Drive *drive) SMART::parsePowerOnHours(sLine, powerOnHours); SMART::parsePowerCycles(sLine, powerCycles); SMART::parseTemperature(sLine, temperature); + SMART::parseReallocatedSectors(sLine, reallocatedSectors); + SMART::parsePendingSectors(sLine, pendingSectors); + SMART::parseUncorrectableSectors(sLine, uncorrectableSectors); } free(cLine); @@ -70,7 +76,7 @@ void SMART::readSMARTData(Drive *drive) } } - drive->setDriveSMARTData(modelFamily, modelName, serial, capacity, errorCount, powerOnHours, powerCycles, temperature); // write data in drive + drive->setDriveSMARTData(modelFamily, modelName, serial, capacity, errorCount, powerOnHours, powerCycles, temperature, reallocatedSectors, pendingSectors, uncorrectableSectors); // write data in drive } /** @@ -309,3 +315,124 @@ bool SMART::parseTemperature(string sLine, uint32_t &temperature) return false; } } + +/** + * \brief parse Reallocated Sectors Count (SMART ID 0x05) + * \param string output line of smartctl + * \param uint32_t parsed reallocated sectors count + * \return bool if parsing was possible + */ +bool SMART::parseReallocatedSectors(string sLine, uint32_t &reallocatedSectors) +{ + string search("\"id\": 5,"); + size_t found = sLine.find(search); + if (found != string::npos) + { + // Found attribute ID 5 (Reallocated_Sector_Ct) + // Now we need to find the raw value in the next lines + // smartctl JSON format: "raw": { "value": , ... } + return true; // Mark that we found the attribute + } + + // Look for the raw value if we're in the right attribute + search = "\"value\":"; + found = sLine.find(search); + if (found != string::npos && sLine.find("\"raw\":") != string::npos) + { + // Extract value after "value": + sLine.erase(0U, sLine.find("\"value\":") + 8U); + // Remove trailing characters + size_t comma = sLine.find(","); + if (comma != string::npos) + { + sLine = sLine.substr(0, comma); + } + // Remove whitespace + sLine.erase(remove(sLine.begin(), sLine.end(), ' '), sLine.end()); + + if (!sLine.empty() && sLine.find_first_not_of("0123456789") == string::npos) + { + reallocatedSectors = stoul(sLine); + return true; + } + } + + return false; +} + +/** + * \brief parse Current Pending Sector Count (SMART ID 0xC5) + * \param string output line of smartctl + * \param uint32_t parsed pending sectors count + * \return bool if parsing was possible + */ +bool SMART::parsePendingSectors(string sLine, uint32_t &pendingSectors) +{ + string search("\"id\": 197,"); // 0xC5 = 197 decimal + size_t found = sLine.find(search); + if (found != string::npos) + { + return true; // Mark that we found the attribute + } + + // Look for the raw value + search = "\"value\":"; + found = sLine.find(search); + if (found != string::npos && sLine.find("\"raw\":") != string::npos) + { + sLine.erase(0U, sLine.find("\"value\":") + 8U); + size_t comma = sLine.find(","); + if (comma != string::npos) + { + sLine = sLine.substr(0, comma); + } + sLine.erase(remove(sLine.begin(), sLine.end(), ' '), sLine.end()); + + if (!sLine.empty() && sLine.find_first_not_of("0123456789") == string::npos) + { + pendingSectors = stoul(sLine); + return true; + } + } + + return false; +} + +/** + * \brief parse Offline Uncorrectable Sectors (SMART ID 0xC6) + * \param string output line of smartctl + * \param uint32_t parsed uncorrectable sectors count + * \return bool if parsing was possible + */ +bool SMART::parseUncorrectableSectors(string sLine, uint32_t &uncorrectableSectors) +{ + string search("\"id\": 198,"); // 0xC6 = 198 decimal + size_t found = sLine.find(search); + if (found != string::npos) + { + return true; // Mark that we found the attribute + } + + // Look for the raw value + search = "\"value\":"; + found = sLine.find(search); + if (found != string::npos && sLine.find("\"raw\":") != string::npos) + { + sLine.erase(0U, sLine.find("\"value\":") + 8U); + size_t comma = sLine.find(","); + if (comma != string::npos) + { + sLine = sLine.substr(0, comma); + } + sLine.erase(remove(sLine.begin(), sLine.end(), ' '), sLine.end()); + + if (!sLine.empty() && sLine.find_first_not_of("0123456789") == string::npos) + { + uncorrectableSectors = stoul(sLine); + return true; + } + } + + return false; +} + diff --git a/src/tui.cpp b/src/tui.cpp index 49e01b8..a21bd8c 100644 --- a/src/tui.cpp +++ b/src/tui.cpp @@ -110,10 +110,10 @@ void TUI::updateTUI(list *plistDrives, uint8_t u8SelectedEntry) bSelectedEntry = true; // mark this drive in entries list displaySelectedDrive(*it, u16StdscrX, u16StdscrY); - if ((it->getPowerOnHours() >= WORSE_HOURS) || (it->getPowerCycles() >= WORSE_POWERUP) || (it->getErrorCount() > 0) || (it->getTemperature() >= WORSE_TEMPERATURE)) + if ((it->getPowerOnHours() >= WORSE_HOURS) || (it->getPowerCycles() >= WORSE_POWERUP) || (it->getErrorCount() > 0) || (it->getTemperature() >= WORSE_TEMPERATURE) || (it->getReallocatedSectors() > 0) || (it->getPendingSectors() > 0) || (it->getUncorrectableSectors() > 0)) { // smart values are bad --> show warning - smartWarning = createSmartWarning(50, 10, ((u16StdscrX) - (int)(u16StdscrX / 2) + 35), (int)(u16StdscrY / 2) - 5, it->getPath(), it->getPowerOnHours(), it->getPowerCycles(), it->getErrorCount(), it->getTemperature()); + smartWarning = createSmartWarning(50, 14, ((u16StdscrX) - (int)(u16StdscrX / 2) + 35), (int)(u16StdscrY / 2) - 7, it->getPath(), it->getPowerOnHours(), it->getPowerCycles(), it->getErrorCount(), it->getTemperature(), it->getReallocatedSectors(), it->getPendingSectors(), it->getUncorrectableSectors()); wrefresh(smartWarning); } } @@ -721,7 +721,7 @@ void TUI::displaySelectedDrive(Drive &drive, int stdscrX, int stdscrY) } } -WINDOW *TUI::createSmartWarning(int iXSize, int iYSize, int iXStart, int iYStart, string sPath, uint32_t u32PowerOnHours, uint32_t u32PowerCycles, uint32_t u32ErrorCount, uint32_t u32Temperature) +WINDOW *TUI::createSmartWarning(int iXSize, int iYSize, int iXStart, int iYStart, string sPath, uint32_t u32PowerOnHours, uint32_t u32PowerCycles, uint32_t u32ErrorCount, uint32_t u32Temperature, uint32_t u32ReallocatedSectors, uint32_t u32PendingSectors, uint32_t u32UncorrectableSectors) { WINDOW *newWindow; newWindow = newwin(iYSize, iXSize, iYStart, iXStart); @@ -763,6 +763,28 @@ WINDOW *TUI::createSmartWarning(int iXSize, int iYSize, int iXStart, int iYStart { string sLineTmp = "Drive too hot: " + to_string(u32Temperature) + " C"; mvwaddstr(newWindow, u16Line++, (iXSize / 2) - (sLine01.size() / 2), sLineTmp.c_str()); + u16Line++; } + + if (u32ReallocatedSectors > 0) + { + string sLineTmp = "CRITICAL: Reallocated sectors detected: " + to_string(u32ReallocatedSectors); + mvwaddstr(newWindow, u16Line++, (iXSize / 2) - (sLine01.size() / 2), sLineTmp.c_str()); + u16Line++; + } + + if (u32PendingSectors > 0) + { + string sLineTmp = "CRITICAL: Pending sectors detected: " + to_string(u32PendingSectors); + mvwaddstr(newWindow, u16Line++, (iXSize / 2) - (sLine01.size() / 2), sLineTmp.c_str()); + u16Line++; + } + + if (u32UncorrectableSectors > 0) + { + string sLineTmp = "CRITICAL: Uncorrectable sectors: " + to_string(u32UncorrectableSectors); + mvwaddstr(newWindow, u16Line++, (iXSize / 2) - (sLine01.size() / 2), sLineTmp.c_str()); + } + return newWindow; } -- 2.54.0