From fb31becf1a465a77f43dcd9c4a5f98a0c2582cbe Mon Sep 17 00:00:00 2001 From: localhorst Date: Sat, 1 Jun 2024 14:14:58 +0200 Subject: [PATCH 1/5] get status from smartctl correct --- include/reHDD.h | 4 +- include/smart.h | 27 +++---- src/reHDD.cpp | 4 +- src/smart.cpp | 204 ++++++++++++++++++++++++++++++------------------ src/tui.cpp | 10 +++ 5 files changed, 150 insertions(+), 99 deletions(-) diff --git a/include/reHDD.h b/include/reHDD.h index aa3085d..eb5b670 100644 --- a/include/reHDD.h +++ b/include/reHDD.h @@ -8,7 +8,7 @@ #ifndef REHDD_H_ #define REHDD_H_ -#define REHDD_VERSION "V1.1.1" +#define REHDD_VERSION "V1.1.2" // Drive handling Settings #define WORSE_HOURS 19200 // mark drive if at this limit or beyond @@ -31,7 +31,7 @@ #endif // Logic -// #define DRYRUN //don´t touch the drives +#define DRYRUN //don´t touch the drives #define FROZEN_ALERT // show alert if drive is frozen #define ZERO_CHECK // check drive after shred if all bytes are zero, show alert if this fails diff --git a/include/smart.h b/include/smart.h index d19d231..735f92e 100644 --- a/include/smart.h +++ b/include/smart.h @@ -19,24 +19,15 @@ public: private: SMART(void); - static uint8_t parseExitStatus(string sLine); - static void parseModelFamily(string sLine); - static void parseModelName(string sLine); - static void parseSerial(string sLine); - static void parseCapacity(string sLine); - static void parseErrorCount(string sLine); - static void parsePowerOnHours(string sLine); - static void parsePowerCycle(string sLine); - static void parseTemperature(string sLine); - - static string modelFamily; - static string modelName; - static string serial; - static uint64_t capacity; - static uint32_t errorCount; - static uint32_t powerOnHours; - static uint32_t powerCycle; - static uint32_t temperature; + static bool parseExitStatus(string sLine, uint8_t &status); + static bool parseModelFamily(string sLine, string &modelFamily); + static bool parseModelName(string sLine, string &modelName); + static bool parseSerial(string sLine, string &serial); + static bool parseCapacity(string sLine, uint64_t &capacity); + static bool parseErrorCount(string sLine, uint32_t &errorCount); + static bool parsePowerOnHours(string sLine, uint32_t &powerOnHours); + static bool parsePowerCycles(string sLine, uint32_t &powerCycles); + static bool parseTemperature(string sLine, uint32_t &temperature); }; #endif // SMART_H_ \ No newline at end of file diff --git a/src/reHDD.cpp b/src/reHDD.cpp index 14587c1..bc4f5e3 100644 --- a/src/reHDD.cpp +++ b/src/reHDD.cpp @@ -560,7 +560,7 @@ void reHDD::handleEnter() { Logger::logThis()->info("Started shred/check for: " + getSelectedDrive()->getModelName() + "-" + getSelectedDrive()->getSerial()); getSelectedDrive()->state = Drive::TaskState::SHRED_ACTIVE; - // task for drive is running --> don´t show more task options + // task for drive is running --> don't show more task options Drive *pTmpDrive = getSelectedDrive(); thread(ThreadShred, pTmpDrive).detach(); } @@ -569,7 +569,7 @@ void reHDD::handleEnter() { Logger::logThis()->info("Started delete for: " + getSelectedDrive()->getModelName() + "-" + getSelectedDrive()->getSerial()); getSelectedDrive()->state = Drive::TaskState::DELETE_ACTIVE; - // task for drive is running --> don´t show more task options + // task for drive is running --> don't show more task options thread(ThreadDelete).detach(); } } diff --git a/src/smart.cpp b/src/smart.cpp index b26d034..6478caa 100644 --- a/src/smart.cpp +++ b/src/smart.cpp @@ -7,15 +7,6 @@ #include "../include/reHDD.h" -string SMART::modelFamily; -string SMART::modelName; -string SMART::serial; -uint64_t SMART::capacity = 0U; -uint32_t SMART::errorCount = 0U; -uint32_t SMART::powerOnHours = 0U; -uint32_t SMART::powerCycle = 0U; -uint32_t SMART::temperature = 0U; - /** * \brief get and set S.M.A.R.T. values in Drive * \param pointer of Drive instance @@ -23,14 +14,18 @@ uint32_t SMART::temperature = 0U; */ void SMART::readSMARTData(Drive *drive) { + string modelFamily; + string modelName; + string serial; + uint64_t capacity = 0U; + uint32_t errorCount = 0U; + uint32_t powerOnHours = 0U; + uint32_t powerCycles = 0U; + uint32_t temperature = 0U; + modelFamily.clear(); modelName.clear(); serial.clear(); - capacity = 0U; - errorCount = 0U; - powerOnHours = 0U; - powerCycle = 0U; - temperature = 0U; string sSmartctlCommands[] = {" --json -a ", " --json -d sntjmicron -a ", " --json -d sntasmedia -a ", " --json -d sntrealtek -a "}; @@ -41,24 +36,26 @@ void SMART::readSMARTData(Drive *drive) sCMD.append(drive->getPath()); const char *cpComand = sCMD.c_str(); + // Logger::logThis()->info(cpComand); + FILE *outputfileSmart = popen(cpComand, "r"); - size_t len = 0; // length of found line + size_t len = 0U; // length of found line char *cLine = NULL; // found line - uint8_t status = 255; + uint8_t status = 255U; while ((getline(&cLine, &len, outputfileSmart)) != -1) { string sLine = string(cLine); - status = SMART::parseExitStatus(sLine); - SMART::parseModelFamily(sLine); - SMART::parseModelName(sLine); - SMART::parseSerial(sLine); - SMART::parseCapacity(sLine); - SMART::parseErrorCount(sLine); - SMART::parsePowerOnHours(sLine); - SMART::parsePowerCycle(sLine); - SMART::parseTemperature(sLine); + SMART::parseExitStatus(sLine, status); + SMART::parseModelFamily(sLine, modelFamily); + SMART::parseModelName(sLine, modelName); + SMART::parseSerial(sLine, serial); + SMART::parseCapacity(sLine, capacity); + SMART::parseErrorCount(sLine, errorCount); + SMART::parsePowerOnHours(sLine, powerOnHours); + SMART::parsePowerCycles(sLine, powerCycles); + SMART::parseTemperature(sLine, temperature); } pclose(outputfileSmart); @@ -66,71 +63,89 @@ void SMART::readSMARTData(Drive *drive) if (status == 0U) { // Found S.M.A.R.T. data with this command + // Logger::logThis()->info("Found S.M.A.R.T. data with this command"); break; } } - drive->setDriveSMARTData(modelFamily, modelName, serial, capacity, errorCount, powerOnHours, powerCycle, temperature); // wirte data in drive + drive->setDriveSMARTData(modelFamily, modelName, serial, capacity, errorCount, powerOnHours, powerCycles, temperature); // write data in drive } /** - * \brief parse ExitStatus - * \param string output line of smartctl - * \return uint_8 exit status + * \brief parse ExitStatus + * \param string output line of smartctl + * \param uint8_t parsed status + * \return bool if parsing was possible */ -uint8_t SMART::parseExitStatus(string sLine) +bool SMART::parseExitStatus(string sLine, uint8_t &status) { - uint8_t exitStatus = -1; string search("\"exit_status\": "); size_t found = sLine.find(search); if (found != string::npos) { - sLine.erase(0, sLine.find(": ") + 1); - exitStatus = stol(sLine); + sLine.erase(0U, sLine.find(": ") + 1U); + status = stol(sLine); + return true; + } + else + { + return false; } - return exitStatus; } /** - * \brief parse ModelFamiliy - * \param string output line of smartctl - * \return void + * \brief parse ModelFamily + * \param string output line of smartctl + * \param string parsed model family + * \return bool if parsing was possible */ -void SMART::parseModelFamily(string sLine) +bool SMART::parseModelFamily(string sLine, string &modelFamily) { string search("\"model_family\": "); size_t found = sLine.find(search); if (found != string::npos) { - sLine.erase(0, sLine.find(": ") + 3); - sLine.erase(sLine.length() - 3, 3); + sLine.erase(0U, sLine.find(": ") + 3U); + sLine.erase(sLine.length() - 3U, 3U); modelFamily = sLine; + return true; + } + else + { + return false; } } /** - * \brief parse ModelName - * \param string output line of smartctl - * \return void + * \brief parse ModelName + * \param string output line of smartctl + * \param string parsed model name + * \return bool if parsing was possible */ -void SMART::parseModelName(string sLine) +bool SMART::parseModelName(string sLine, string &modelName) { string search("\"model_name\": "); size_t found = sLine.find(search); if (found != string::npos) { - sLine.erase(0, sLine.find(": ") + 3); - sLine.erase(sLine.length() - 3, 3); + sLine.erase(0U, sLine.find(": ") + 3U); + sLine.erase(sLine.length() - 3U, 3U); modelName = sLine; + return true; + } + else + { + return false; } } /** - * \brief parse Serial - * \param string output line of smartctl - * \return void + * \brief parse Serial + * \param string output line of smartctl + * \param string parsed serial + * \return bool if parsing was possible */ -void SMART::parseSerial(string sLine) +bool SMART::parseSerial(string sLine, string &serial) { string search("\"serial_number\": "); size_t found = sLine.find(search); @@ -139,15 +154,21 @@ void SMART::parseSerial(string sLine) sLine.erase(0, sLine.find(": ") + 3); sLine.erase(sLine.length() - 3, 3); serial = sLine; + return true; + } + else + { + return false; } } /** - * \brief parse Capacity - * \param string output line of smartctl - * \return void + * \brief parse Capacity + * \param string output line of smartctl + * \param string parsed capacity + * \return bool if parsing was possible */ -void SMART::parseCapacity(string sLine) +bool SMART::parseCapacity(string sLine, uint64_t &capacity) { string search("\"bytes\": "); size_t found = sLine.find(search); @@ -156,49 +177,67 @@ void SMART::parseCapacity(string sLine) sLine.erase(0, sLine.find(": ") + 2); sLine.erase(sLine.length() - 1, 1); capacity = stol(sLine); + return true; + } + else + { + return false; } } /** - * \brief parse ErrorCount - * \param string output line of smartctl - * \return void + * \brief parse ErrorCount + * \param string output line of smartctl + * \param uint32_t parsed error count + * \return bool if parsing was possible */ -void SMART::parseErrorCount(string sLine) +bool SMART::parseErrorCount(string sLine, uint32_t &errorCount) { string search("\"error_count_total\": "); size_t found = sLine.find(search); if (found != string::npos) { - sLine.erase(0, sLine.find(": ") + 2); - sLine.erase(sLine.length() - 2, 2); + sLine.erase(0U, sLine.find(": ") + 2U); + sLine.erase(sLine.length() - 2U, 2U); errorCount = stol(sLine); + return true; + } + else + { + return false; } } /** - * \brief parse PowerOnHours - * \param string output line of smartctl - * \return void + * \brief parse PowerOnHours + * \param string output line of smartctl\ + * \param uint32_t parsed power on hours + * \return bool if parsing was possible */ -void SMART::parsePowerOnHours(string sLine) +bool SMART::parsePowerOnHours(string sLine, uint32_t &powerOnHours) { string search("\"hours\": "); size_t found = sLine.find(search); if (found != string::npos) { - sLine.erase(0, sLine.find(": ") + 2); - sLine.erase(sLine.length() - 1, 1); + sLine.erase(0U, sLine.find(": ") + 2U); + sLine.erase(sLine.length() - 1U, 1U); powerOnHours = stol(sLine); + return true; + } + else + { + return false; } } /** - * \brief parse PowerCycle - * \param string output line of smartctl - * \return void + * \brief parse PowerCycle + * \param string output line of smartctl + * \param uint32_t parsed power cycles + * \return bool if parsing was possible */ -void SMART::parsePowerCycle(string sLine) +bool SMART::parsePowerCycles(string sLine, uint32_t &powerCycles) { string search("\"power_cycle_count\": "); size_t found = sLine.find(search); @@ -206,23 +245,29 @@ void SMART::parsePowerCycle(string sLine) { sLine.erase(0, sLine.find(": ") + 2); sLine.erase(sLine.length() - 2, 2); - powerCycle = stol(sLine); + powerCycles = stol(sLine); + return true; + } + else + { + return false; } } /** - * \brief parse temperature - * \param string output line of smartctl - * \return void + * \brief parse temperature + * \param string output line of smartctl + * \param uint32_t parsed temperature + * \return bool if parsing was possible */ -void SMART::parseTemperature(string sLine) +bool SMART::parseTemperature(string sLine, uint32_t &temperature) { string search("\"current\": "); size_t found = sLine.find(search); if (found != string::npos) { - sLine.erase(0, sLine.find(": ") + 2); - sLine.erase(sLine.length() - 1, 2); + sLine.erase(0U, sLine.find(": ") + 2U); + sLine.erase(sLine.length() - 1U, 2U); if (sLine == "{") { temperature = 0U; // this drive doesn't support temperature @@ -231,5 +276,10 @@ void SMART::parseTemperature(string sLine) { temperature = stol(sLine); } + return true; + } + else + { + return false; } } diff --git a/src/tui.cpp b/src/tui.cpp index 3ed1c32..b14036e 100644 --- a/src/tui.cpp +++ b/src/tui.cpp @@ -43,11 +43,21 @@ void TUI::initTUI() init_pair(COLOR_AREA_ENTRY_EVEN, COLOR_BLACK, COLOR_WHITE); init_pair(COLOR_AREA_ENTRY_ODD, COLOR_BLUE, COLOR_WHITE); + +#ifdef DRYRUN + init_pair(COLOR_AREA_ENTRY_SELECTED, COLOR_WHITE, COLOR_GREEN); +#elif init_pair(COLOR_AREA_ENTRY_SELECTED, COLOR_WHITE, COLOR_RED); +#endif + init_pair(COLOR_AREA_OVERVIEW, COLOR_BLACK, COLOR_WHITE); init_pair(COLOR_AREA_DETAIL, COLOR_BLACK, COLOR_WHITE); +#ifdef DRYRUN + mvprintw(0, 2, "reHDD - HDD refurbishing tool - GPL 3.0 DRYRUN is active! Don't use in production!"); +#elif mvprintw(0, 2, "reHDD - HDD refurbishing tool - GPL 3.0 "); +#endif Logger::logThis()->info("UI successfully initialized"); } From 76c728c24134a553f78ad5a2b7538322dc564a0f Mon Sep 17 00:00:00 2001 From: localhorst Date: Sat, 1 Jun 2024 14:23:53 +0200 Subject: [PATCH 2/5] fix dryrun --- include/reHDD.h | 2 +- src/tui.cpp | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/include/reHDD.h b/include/reHDD.h index eb5b670..7bd1e90 100644 --- a/include/reHDD.h +++ b/include/reHDD.h @@ -31,7 +31,7 @@ #endif // Logic -#define DRYRUN //don´t touch the drives +//#define DRYRUN //don´t touch the drives #define FROZEN_ALERT // show alert if drive is frozen #define ZERO_CHECK // check drive after shred if all bytes are zero, show alert if this fails diff --git a/src/tui.cpp b/src/tui.cpp index b14036e..8941c0f 100644 --- a/src/tui.cpp +++ b/src/tui.cpp @@ -46,7 +46,7 @@ void TUI::initTUI() #ifdef DRYRUN init_pair(COLOR_AREA_ENTRY_SELECTED, COLOR_WHITE, COLOR_GREEN); -#elif +#else init_pair(COLOR_AREA_ENTRY_SELECTED, COLOR_WHITE, COLOR_RED); #endif @@ -55,7 +55,7 @@ void TUI::initTUI() #ifdef DRYRUN mvprintw(0, 2, "reHDD - HDD refurbishing tool - GPL 3.0 DRYRUN is active! Don't use in production!"); -#elif +#else mvprintw(0, 2, "reHDD - HDD refurbishing tool - GPL 3.0 "); #endif Logger::logThis()->info("UI successfully initialized"); From fe11419e3722934f124af3ec08cf897d970b2d6f Mon Sep 17 00:00:00 2001 From: localhorst Date: Sat, 1 Jun 2024 14:37:03 +0200 Subject: [PATCH 3/5] add usb sata bridge --- src/shred.cpp | 3 +-- src/smart.cpp | 2 +- 2 files changed, 2 insertions(+), 3 deletions(-) diff --git a/src/shred.cpp b/src/shred.cpp index 158e19b..3cb9c48 100644 --- a/src/shred.cpp +++ b/src/shred.cpp @@ -90,8 +90,7 @@ int Shred::shredDrive(Drive *drive, int *ipSignalFd) tfng_prng_seedkey(ucKey); this->ulDriveByteSize = getDriveSizeInBytes(driveFileDiscr); - drive->sShredSpeed.chronoShredTimestamp = std::chrono::system_clock::now(); - ; // set inital timestamp for speed metric + drive->sShredSpeed.chronoShredTimestamp = std::chrono::system_clock::now(); // set inital timestamp for speed metric drive->sShredSpeed.ulSpeedMetricBytesWritten = 0U; // uses to calculate speed metric #ifdef LOG_LEVEL_HIGH diff --git a/src/smart.cpp b/src/smart.cpp index 6478caa..339d94f 100644 --- a/src/smart.cpp +++ b/src/smart.cpp @@ -27,7 +27,7 @@ void SMART::readSMARTData(Drive *drive) modelName.clear(); serial.clear(); - string sSmartctlCommands[] = {" --json -a ", " --json -d sntjmicron -a ", " --json -d sntasmedia -a ", " --json -d sntrealtek -a "}; + string sSmartctlCommands[] = {" --json -a ", " --json -d sntjmicron -a ", " --json -d sntasmedia -a ", " --json -d sntrealtek -a ", " --json -d sat -a "}; for (string sSmartctlCommand : sSmartctlCommands) { From 84a2da8bc2dba4743757f1a8580ebbcda2de4391 Mon Sep 17 00:00:00 2001 From: localhorst Date: Sat, 1 Jun 2024 14:52:50 +0200 Subject: [PATCH 4/5] reuse working smartclt cmd --- include/drive.h | 5 ++++ src/drive.cpp | 9 +++++++ src/smart.cpp | 66 +++++++++++++++++++++++++++++++++++-------------- 3 files changed, 61 insertions(+), 19 deletions(-) diff --git a/include/drive.h b/include/drive.h index f1e28ab..4432ce7 100644 --- a/include/drive.h +++ b/include/drive.h @@ -45,6 +45,7 @@ private: double d32TaskPercentage = 0U; // in percent for Shred (1 to 100) time_t u32TimestampTaskStart = 0U; // unix timestamp for duration of an action time_t u32TaskDuration = 0U; // time needed to complete the task + string desiredSmartctlCommand; //command used to gather S.M.A.R.T. values from drive struct { @@ -77,6 +78,7 @@ public: uint32_t getPowerOnHours(void); // in hours uint32_t getPowerCycles(void); uint32_t getTemperature(void); // in Fahrenheit, just kidding: degree Celsius + void checkFrozenDrive(void); void setDriveSMARTData(string modelFamily, @@ -100,6 +102,9 @@ public: void setActionStartTimestamp(); time_t getActionStartTimestamp(); + void setSmartcltCommand(string cmd); + string getSmartcltCommand(void); + void calculateTaskDuration(); time_t getTaskDuration(); }; diff --git a/src/drive.cpp b/src/drive.cpp index 1ca3e61..43cd793 100644 --- a/src/drive.cpp +++ b/src/drive.cpp @@ -161,6 +161,15 @@ time_t Drive::getActionStartTimestamp() return this->u32TimestampTaskStart; } +void Drive::setSmartcltCommand(string cmd) +{ + this->desiredSmartctlCommand = cmd; +} +string Drive::getSmartcltCommand(void) +{ + return this->desiredSmartctlCommand; +} + void Drive::calculateTaskDuration() { time_t u32localtime; diff --git a/src/smart.cpp b/src/smart.cpp index 339d94f..d739333 100644 --- a/src/smart.cpp +++ b/src/smart.cpp @@ -27,27 +27,62 @@ void SMART::readSMARTData(Drive *drive) modelName.clear(); serial.clear(); - string sSmartctlCommands[] = {" --json -a ", " --json -d sntjmicron -a ", " --json -d sntasmedia -a ", " --json -d sntrealtek -a ", " --json -d sat -a "}; - - for (string sSmartctlCommand : sSmartctlCommands) + if (drive->getSmartcltCommand().empty()) { - string sCMD = ("smartctl"); - sCMD.append(sSmartctlCommand); - sCMD.append(drive->getPath()); - const char *cpComand = sCMD.c_str(); + // No working Smartctl command is known --> try all + string sSmartctlCommands[] = {" --json -a ", " --json -d sntjmicron -a ", " --json -d sntasmedia -a ", " --json -d sntrealtek -a ", " --json -d sat -a "}; - // Logger::logThis()->info(cpComand); + for (string sSmartctlCommand : sSmartctlCommands) + { + string sCMD = ("smartctl"); + sCMD.append(sSmartctlCommand); + sCMD.append(drive->getPath()); + const char *cpComand = sCMD.c_str(); - FILE *outputfileSmart = popen(cpComand, "r"); - size_t len = 0U; // length of found line + // Logger::logThis()->info(cpComand); + + FILE *outputfileSmart = popen(cpComand, "r"); + size_t len = 0U; // length of found line + char *cLine = NULL; // found line + uint8_t status = 255U; + + while ((getline(&cLine, &len, outputfileSmart)) != -1) + { + string sLine = string(cLine); + + SMART::parseExitStatus(sLine, status); + SMART::parseModelFamily(sLine, modelFamily); + SMART::parseModelName(sLine, modelName); + SMART::parseSerial(sLine, serial); + SMART::parseCapacity(sLine, capacity); + SMART::parseErrorCount(sLine, errorCount); + SMART::parsePowerOnHours(sLine, powerOnHours); + SMART::parsePowerCycles(sLine, powerCycles); + SMART::parseTemperature(sLine, temperature); + } + + pclose(outputfileSmart); + + if (status == 0U) + { + // Found S.M.A.R.T. data with this command + // Logger::logThis()->info("Found S.M.A.R.T. data with this command"); + drive->setSmartcltCommand(sCMD); + break; + } + } + } + else + { + // A working Smartctl command is known + FILE *outputfileSmart = popen(drive->getSmartcltCommand().c_str(), "r"); + size_t len = 0U; // length of found line char *cLine = NULL; // found line - uint8_t status = 255U; while ((getline(&cLine, &len, outputfileSmart)) != -1) { string sLine = string(cLine); - SMART::parseExitStatus(sLine, status); SMART::parseModelFamily(sLine, modelFamily); SMART::parseModelName(sLine, modelName); SMART::parseSerial(sLine, serial); @@ -59,13 +94,6 @@ void SMART::readSMARTData(Drive *drive) } pclose(outputfileSmart); - - if (status == 0U) - { - // Found S.M.A.R.T. data with this command - // Logger::logThis()->info("Found S.M.A.R.T. data with this command"); - break; - } } drive->setDriveSMARTData(modelFamily, modelName, serial, capacity, errorCount, powerOnHours, powerCycles, temperature); // write data in drive From 93c52f9a69b71acd7ac76f6e04c39954b1a61147 Mon Sep 17 00:00:00 2001 From: localhorst Date: Sat, 1 Jun 2024 15:04:06 +0200 Subject: [PATCH 5/5] Revert "reuse working smartclt cmd" This reverts commit 84a2da8bc2dba4743757f1a8580ebbcda2de4391. --- include/drive.h | 5 ---- src/drive.cpp | 9 ------- src/smart.cpp | 66 ++++++++++++++----------------------------------- 3 files changed, 19 insertions(+), 61 deletions(-) diff --git a/include/drive.h b/include/drive.h index 4432ce7..f1e28ab 100644 --- a/include/drive.h +++ b/include/drive.h @@ -45,7 +45,6 @@ private: double d32TaskPercentage = 0U; // in percent for Shred (1 to 100) time_t u32TimestampTaskStart = 0U; // unix timestamp for duration of an action time_t u32TaskDuration = 0U; // time needed to complete the task - string desiredSmartctlCommand; //command used to gather S.M.A.R.T. values from drive struct { @@ -78,7 +77,6 @@ public: uint32_t getPowerOnHours(void); // in hours uint32_t getPowerCycles(void); uint32_t getTemperature(void); // in Fahrenheit, just kidding: degree Celsius - void checkFrozenDrive(void); void setDriveSMARTData(string modelFamily, @@ -102,9 +100,6 @@ public: void setActionStartTimestamp(); time_t getActionStartTimestamp(); - void setSmartcltCommand(string cmd); - string getSmartcltCommand(void); - void calculateTaskDuration(); time_t getTaskDuration(); }; diff --git a/src/drive.cpp b/src/drive.cpp index 43cd793..1ca3e61 100644 --- a/src/drive.cpp +++ b/src/drive.cpp @@ -161,15 +161,6 @@ time_t Drive::getActionStartTimestamp() return this->u32TimestampTaskStart; } -void Drive::setSmartcltCommand(string cmd) -{ - this->desiredSmartctlCommand = cmd; -} -string Drive::getSmartcltCommand(void) -{ - return this->desiredSmartctlCommand; -} - void Drive::calculateTaskDuration() { time_t u32localtime; diff --git a/src/smart.cpp b/src/smart.cpp index d739333..339d94f 100644 --- a/src/smart.cpp +++ b/src/smart.cpp @@ -27,62 +27,27 @@ void SMART::readSMARTData(Drive *drive) modelName.clear(); serial.clear(); - if (drive->getSmartcltCommand().empty()) + string sSmartctlCommands[] = {" --json -a ", " --json -d sntjmicron -a ", " --json -d sntasmedia -a ", " --json -d sntrealtek -a ", " --json -d sat -a "}; + + for (string sSmartctlCommand : sSmartctlCommands) { - // No working Smartctl command is known --> try all - string sSmartctlCommands[] = {" --json -a ", " --json -d sntjmicron -a ", " --json -d sntasmedia -a ", " --json -d sntrealtek -a ", " --json -d sat -a "}; + string sCMD = ("smartctl"); + sCMD.append(sSmartctlCommand); + sCMD.append(drive->getPath()); + const char *cpComand = sCMD.c_str(); - for (string sSmartctlCommand : sSmartctlCommands) - { - string sCMD = ("smartctl"); - sCMD.append(sSmartctlCommand); - sCMD.append(drive->getPath()); - const char *cpComand = sCMD.c_str(); + // Logger::logThis()->info(cpComand); - // Logger::logThis()->info(cpComand); - - FILE *outputfileSmart = popen(cpComand, "r"); - size_t len = 0U; // length of found line - char *cLine = NULL; // found line - uint8_t status = 255U; - - while ((getline(&cLine, &len, outputfileSmart)) != -1) - { - string sLine = string(cLine); - - SMART::parseExitStatus(sLine, status); - SMART::parseModelFamily(sLine, modelFamily); - SMART::parseModelName(sLine, modelName); - SMART::parseSerial(sLine, serial); - SMART::parseCapacity(sLine, capacity); - SMART::parseErrorCount(sLine, errorCount); - SMART::parsePowerOnHours(sLine, powerOnHours); - SMART::parsePowerCycles(sLine, powerCycles); - SMART::parseTemperature(sLine, temperature); - } - - pclose(outputfileSmart); - - if (status == 0U) - { - // Found S.M.A.R.T. data with this command - // Logger::logThis()->info("Found S.M.A.R.T. data with this command"); - drive->setSmartcltCommand(sCMD); - break; - } - } - } - else - { - // A working Smartctl command is known - FILE *outputfileSmart = popen(drive->getSmartcltCommand().c_str(), "r"); - size_t len = 0U; // length of found line + FILE *outputfileSmart = popen(cpComand, "r"); + size_t len = 0U; // length of found line char *cLine = NULL; // found line + uint8_t status = 255U; while ((getline(&cLine, &len, outputfileSmart)) != -1) { string sLine = string(cLine); + SMART::parseExitStatus(sLine, status); SMART::parseModelFamily(sLine, modelFamily); SMART::parseModelName(sLine, modelName); SMART::parseSerial(sLine, serial); @@ -94,6 +59,13 @@ void SMART::readSMARTData(Drive *drive) } pclose(outputfileSmart); + + if (status == 0U) + { + // Found S.M.A.R.T. data with this command + // Logger::logThis()->info("Found S.M.A.R.T. data with this command"); + break; + } } drive->setDriveSMARTData(modelFamily, modelName, serial, capacity, errorCount, powerOnHours, powerCycles, temperature); // write data in drive