fix parsing and set dev version
This commit is contained in:
+2
-2
@@ -8,7 +8,7 @@
|
|||||||
#ifndef REHDD_H_
|
#ifndef REHDD_H_
|
||||||
#define REHDD_H_
|
#define REHDD_H_
|
||||||
|
|
||||||
#define REHDD_VERSION "V1.3.1"
|
#define REHDD_VERSION "V1.4.0-dev"
|
||||||
|
|
||||||
// Drive handling Settings
|
// Drive handling Settings
|
||||||
#define WORSE_HOURS 19200 // mark drive if at this limit or beyond
|
#define WORSE_HOURS 19200 // mark drive if at this limit or beyond
|
||||||
@@ -20,7 +20,7 @@
|
|||||||
|
|
||||||
// Logger Settings
|
// Logger Settings
|
||||||
#define LOG_PATH "./reHDD.log"
|
#define LOG_PATH "./reHDD.log"
|
||||||
#define DESCRIPTION "reHDD - Copyright Hendrik Schutter 2025"
|
#define DESCRIPTION "reHDD - Copyright Hendrik Schutter 2026"
|
||||||
#define DEVICE_ID "generic"
|
#define DEVICE_ID "generic"
|
||||||
#define SOFTWARE_VERSION REHDD_VERSION
|
#define SOFTWARE_VERSION REHDD_VERSION
|
||||||
#define HARDWARE_VERSION "generic"
|
#define HARDWARE_VERSION "generic"
|
||||||
|
|||||||
+67
-61
@@ -19,31 +19,31 @@ struct SMARTParseContext
|
|||||||
string modelName;
|
string modelName;
|
||||||
string serial;
|
string serial;
|
||||||
uint64_t capacity;
|
uint64_t capacity;
|
||||||
|
|
||||||
// Power and temperature (top-level JSON fields)
|
// Power and temperature (top-level JSON fields)
|
||||||
uint32_t errorCount;
|
uint32_t errorCount;
|
||||||
uint32_t powerOnHours;
|
uint32_t powerOnHours;
|
||||||
uint32_t powerCycles;
|
uint32_t powerCycles;
|
||||||
uint32_t temperature;
|
uint32_t temperature;
|
||||||
|
|
||||||
// Critical sector counts (from ata_smart_attributes table)
|
// Critical sector counts (from ata_smart_attributes table)
|
||||||
uint32_t reallocatedSectors; // ID 5
|
uint32_t reallocatedSectors; // ID 5
|
||||||
uint32_t pendingSectors; // ID 197
|
uint32_t pendingSectors; // ID 197
|
||||||
uint32_t uncorrectableSectors; // ID 198
|
uint32_t uncorrectableSectors; // ID 198
|
||||||
|
|
||||||
// Parser state machine
|
// Parser state machine
|
||||||
enum State
|
enum State
|
||||||
{
|
{
|
||||||
SEARCHING, // Looking for next field
|
SEARCHING, // Looking for next field
|
||||||
IN_ATTRIBUTE_5, // Inside ID 5 object
|
IN_ATTRIBUTE_5, // Inside ID 5 object
|
||||||
IN_ATTRIBUTE_197, // Inside ID 197 object
|
IN_ATTRIBUTE_197, // Inside ID 197 object
|
||||||
IN_ATTRIBUTE_198, // Inside ID 198 object
|
IN_ATTRIBUTE_198, // Inside ID 198 object
|
||||||
IN_RAW_SECTION // Inside "raw": { } of current attribute
|
IN_RAW_SECTION // Inside "raw": { } of current attribute
|
||||||
};
|
};
|
||||||
|
|
||||||
State state;
|
State state;
|
||||||
int currentAttributeId; // Which attribute are we parsing? (5, 197, 198)
|
int currentAttributeId; // Which attribute are we parsing? (5, 197, 198)
|
||||||
|
|
||||||
SMARTParseContext()
|
SMARTParseContext()
|
||||||
: capacity(0),
|
: capacity(0),
|
||||||
errorCount(0),
|
errorCount(0),
|
||||||
@@ -69,15 +69,15 @@ static string extractStringValue(const string &line)
|
|||||||
size_t colonPos = line.find(": ");
|
size_t colonPos = line.find(": ");
|
||||||
if (colonPos == string::npos)
|
if (colonPos == string::npos)
|
||||||
return "";
|
return "";
|
||||||
|
|
||||||
size_t firstQuote = line.find('"', colonPos + 2);
|
size_t firstQuote = line.find('"', colonPos + 2);
|
||||||
if (firstQuote == string::npos)
|
if (firstQuote == string::npos)
|
||||||
return "";
|
return "";
|
||||||
|
|
||||||
size_t secondQuote = line.find('"', firstQuote + 1);
|
size_t secondQuote = line.find('"', firstQuote + 1);
|
||||||
if (secondQuote == string::npos)
|
if (secondQuote == string::npos)
|
||||||
return "";
|
return "";
|
||||||
|
|
||||||
return line.substr(firstQuote + 1, secondQuote - firstQuote - 1);
|
return line.substr(firstQuote + 1, secondQuote - firstQuote - 1);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -91,18 +91,19 @@ static uint64_t extractIntegerValue(const string &line)
|
|||||||
size_t colonPos = line.find(": ");
|
size_t colonPos = line.find(": ");
|
||||||
if (colonPos == string::npos)
|
if (colonPos == string::npos)
|
||||||
return 0;
|
return 0;
|
||||||
|
|
||||||
string valueStr = line.substr(colonPos + 2);
|
string valueStr = line.substr(colonPos + 2);
|
||||||
|
|
||||||
// Remove whitespace, commas, braces
|
// Remove whitespace, commas, braces
|
||||||
valueStr.erase(remove_if(valueStr.begin(), valueStr.end(),
|
valueStr.erase(remove_if(valueStr.begin(), valueStr.end(),
|
||||||
[](char c) { return c == ' ' || c == ',' || c == '}' || c == '\n'; }),
|
[](char c)
|
||||||
|
{ return c == ' ' || c == ',' || c == '}' || c == '\n'; }),
|
||||||
valueStr.end());
|
valueStr.end());
|
||||||
|
|
||||||
// Verify it's a valid number
|
// Verify it's a valid number
|
||||||
if (valueStr.empty() || valueStr.find_first_not_of("0123456789") != string::npos)
|
if (valueStr.empty() || valueStr.find_first_not_of("0123456789") != string::npos)
|
||||||
return 0;
|
return 0;
|
||||||
|
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
return stoull(valueStr);
|
return stoull(valueStr);
|
||||||
@@ -128,54 +129,54 @@ static void processLine(const string &line, SMARTParseContext &ctx)
|
|||||||
{
|
{
|
||||||
trimmed = trimmed.substr(firstNonSpace);
|
trimmed = trimmed.substr(firstNonSpace);
|
||||||
}
|
}
|
||||||
|
|
||||||
// Parse top-level device information
|
// Parse top-level device information
|
||||||
if (trimmed.find("\"model_family\":") == 0)
|
if (trimmed.find("\"model_family\":") == 0)
|
||||||
{
|
{
|
||||||
ctx.modelFamily = extractStringValue(line);
|
ctx.modelFamily = extractStringValue(line);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (trimmed.find("\"model_name\":") == 0)
|
if (trimmed.find("\"model_name\":") == 0)
|
||||||
{
|
{
|
||||||
ctx.modelName = extractStringValue(line);
|
ctx.modelName = extractStringValue(line);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (trimmed.find("\"serial_number\":") == 0)
|
if (trimmed.find("\"serial_number\":") == 0)
|
||||||
{
|
{
|
||||||
ctx.serial = extractStringValue(line);
|
ctx.serial = extractStringValue(line);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Parse capacity from user_capacity.bytes
|
// Parse capacity from user_capacity.bytes
|
||||||
if (trimmed.find("\"bytes\":") == 0)
|
if (trimmed.find("\"bytes\":") == 0)
|
||||||
{
|
{
|
||||||
ctx.capacity = extractIntegerValue(line);
|
ctx.capacity = extractIntegerValue(line);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Parse error count from self_test log
|
// Parse error count from self_test log
|
||||||
if (trimmed.find("\"error_count_total\":") == 0)
|
if (trimmed.find("\"error_count_total\":") == 0)
|
||||||
{
|
{
|
||||||
ctx.errorCount = extractIntegerValue(line);
|
ctx.errorCount = extractIntegerValue(line);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Parse power-on hours
|
// Parse power-on hours
|
||||||
if (trimmed.find("\"hours\":") == 0)
|
if (trimmed.find("\"hours\":") == 0)
|
||||||
{
|
{
|
||||||
ctx.powerOnHours = extractIntegerValue(line);
|
ctx.powerOnHours = extractIntegerValue(line);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Parse power cycle count
|
// Parse power cycle count
|
||||||
if (trimmed.find("\"power_cycle_count\":") == 0)
|
if (trimmed.find("\"power_cycle_count\":") == 0)
|
||||||
{
|
{
|
||||||
ctx.powerCycles = extractIntegerValue(line);
|
ctx.powerCycles = extractIntegerValue(line);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Parse temperature
|
// Parse temperature
|
||||||
if (trimmed.find("\"current\":") == 0 && ctx.temperature == 0)
|
if (trimmed.find("\"current\":") == 0 && ctx.temperature == 0)
|
||||||
{
|
{
|
||||||
@@ -183,7 +184,7 @@ static void processLine(const string &line, SMARTParseContext &ctx)
|
|||||||
ctx.temperature = extractIntegerValue(line);
|
ctx.temperature = extractIntegerValue(line);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
// State machine for SMART attributes parsing
|
// State machine for SMART attributes parsing
|
||||||
switch (ctx.state)
|
switch (ctx.state)
|
||||||
{
|
{
|
||||||
@@ -205,7 +206,7 @@ static void processLine(const string &line, SMARTParseContext &ctx)
|
|||||||
ctx.currentAttributeId = 198;
|
ctx.currentAttributeId = 198;
|
||||||
}
|
}
|
||||||
break;
|
break;
|
||||||
|
|
||||||
case SMARTParseContext::IN_ATTRIBUTE_5:
|
case SMARTParseContext::IN_ATTRIBUTE_5:
|
||||||
case SMARTParseContext::IN_ATTRIBUTE_197:
|
case SMARTParseContext::IN_ATTRIBUTE_197:
|
||||||
case SMARTParseContext::IN_ATTRIBUTE_198:
|
case SMARTParseContext::IN_ATTRIBUTE_198:
|
||||||
@@ -214,20 +215,21 @@ static void processLine(const string &line, SMARTParseContext &ctx)
|
|||||||
{
|
{
|
||||||
ctx.state = SMARTParseContext::IN_RAW_SECTION;
|
ctx.state = SMARTParseContext::IN_RAW_SECTION;
|
||||||
}
|
}
|
||||||
// Look for end of attribute object
|
// Look for end of attribute object (more indented closing brace = end of attribute)
|
||||||
else if (trimmed.find("},") == 0 || trimmed.find("}") == 0)
|
// " }," or " }" at attribute level (6 spaces)
|
||||||
|
else if (line.find(" },") == 0 || line.find(" }") == 0)
|
||||||
{
|
{
|
||||||
ctx.state = SMARTParseContext::SEARCHING;
|
ctx.state = SMARTParseContext::SEARCHING;
|
||||||
ctx.currentAttributeId = 0;
|
ctx.currentAttributeId = 0;
|
||||||
}
|
}
|
||||||
break;
|
break;
|
||||||
|
|
||||||
case SMARTParseContext::IN_RAW_SECTION:
|
case SMARTParseContext::IN_RAW_SECTION:
|
||||||
// Look for "value": number inside raw section
|
// Look for "value": number inside raw section
|
||||||
if (trimmed.find("\"value\":") == 0)
|
if (trimmed.find("\"value\":") == 0)
|
||||||
{
|
{
|
||||||
uint64_t value = extractIntegerValue(line);
|
uint64_t value = extractIntegerValue(line);
|
||||||
|
|
||||||
// Store value in appropriate field based on current attribute
|
// Store value in appropriate field based on current attribute
|
||||||
if (ctx.currentAttributeId == 5)
|
if (ctx.currentAttributeId == 5)
|
||||||
{
|
{
|
||||||
@@ -241,11 +243,16 @@ static void processLine(const string &line, SMARTParseContext &ctx)
|
|||||||
{
|
{
|
||||||
ctx.uncorrectableSectors = static_cast<uint32_t>(value);
|
ctx.uncorrectableSectors = static_cast<uint32_t>(value);
|
||||||
}
|
}
|
||||||
|
|
||||||
// Exit raw section after finding value
|
// Stay in raw section - closing brace will exit
|
||||||
ctx.state = (ctx.currentAttributeId == 5) ? SMARTParseContext::IN_ATTRIBUTE_5 :
|
}
|
||||||
(ctx.currentAttributeId == 197) ? SMARTParseContext::IN_ATTRIBUTE_197 :
|
// Look for end of raw object (less indented = back to attribute level)
|
||||||
SMARTParseContext::IN_ATTRIBUTE_198;
|
// " }" at raw level (8 spaces)
|
||||||
|
else if (line.find(" }") == 0)
|
||||||
|
{
|
||||||
|
// Return to attribute state (raw section closed)
|
||||||
|
ctx.state = (ctx.currentAttributeId == 5) ? SMARTParseContext::IN_ATTRIBUTE_5 : (ctx.currentAttributeId == 197) ? SMARTParseContext::IN_ATTRIBUTE_197
|
||||||
|
: SMARTParseContext::IN_ATTRIBUTE_198;
|
||||||
}
|
}
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
@@ -260,17 +267,17 @@ void SMART::readSMARTData(Drive *drive)
|
|||||||
{
|
{
|
||||||
SMARTParseContext ctx;
|
SMARTParseContext ctx;
|
||||||
uint8_t exitStatus = 255U;
|
uint8_t exitStatus = 255U;
|
||||||
|
|
||||||
// Command order optimized for USB adapters
|
// Command order optimized for USB adapters
|
||||||
// Standard commands first, then device-specific variants
|
// Standard commands first, then device-specific variants
|
||||||
string sSmartctlCommands[] = {
|
string sSmartctlCommands[] = {
|
||||||
" --json -a ", // Try standard first
|
" --json -a ", // Try standard first
|
||||||
" --json -d sat -a ", // SAT (SCSI/ATA Translation) - most USB adapters
|
" --json -d sat -a ", // SAT (SCSI/ATA Translation) - most USB adapters
|
||||||
" --json -d usbjmicron -a ", // USB JMicron
|
" --json -d usbjmicron -a ", // USB JMicron
|
||||||
" --json -d usbprolific -a ", // USB Prolific
|
" --json -d usbprolific -a ", // USB Prolific
|
||||||
" --json -d usbsunplus -a " // USB Sunplus
|
" --json -d usbsunplus -a " // USB Sunplus
|
||||||
};
|
};
|
||||||
|
|
||||||
for (const string &sSmartctlCommand : sSmartctlCommands)
|
for (const string &sSmartctlCommand : sSmartctlCommands)
|
||||||
{
|
{
|
||||||
// Build command with timeout
|
// Build command with timeout
|
||||||
@@ -278,9 +285,9 @@ void SMART::readSMARTData(Drive *drive)
|
|||||||
sCMD.append(sSmartctlCommand);
|
sCMD.append(sSmartctlCommand);
|
||||||
sCMD.append(drive->getPath());
|
sCMD.append(drive->getPath());
|
||||||
// Note: stderr NOT suppressed for debugging
|
// Note: stderr NOT suppressed for debugging
|
||||||
|
|
||||||
Logger::logThis()->info("SMART: Executing: " + sCMD);
|
Logger::logThis()->info("SMART: Executing: " + sCMD);
|
||||||
|
|
||||||
// Execute smartctl with timeout protection
|
// Execute smartctl with timeout protection
|
||||||
FILE *outputfileSmart = popen(sCMD.c_str(), "r");
|
FILE *outputfileSmart = popen(sCMD.c_str(), "r");
|
||||||
if (outputfileSmart == nullptr)
|
if (outputfileSmart == nullptr)
|
||||||
@@ -288,42 +295,42 @@ void SMART::readSMARTData(Drive *drive)
|
|||||||
Logger::logThis()->error("SMART: Failed to execute smartctl");
|
Logger::logThis()->error("SMART: Failed to execute smartctl");
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Reset context for new attempt
|
// Reset context for new attempt
|
||||||
ctx = SMARTParseContext();
|
ctx = SMARTParseContext();
|
||||||
|
|
||||||
// Parse output line by line
|
// Parse output line by line
|
||||||
char *cLine = nullptr;
|
char *cLine = nullptr;
|
||||||
size_t len = 0;
|
size_t len = 0;
|
||||||
int lineCount = 0;
|
int lineCount = 0;
|
||||||
|
|
||||||
while (getline(&cLine, &len, outputfileSmart) != -1)
|
while (getline(&cLine, &len, outputfileSmart) != -1)
|
||||||
{
|
{
|
||||||
string sLine(cLine);
|
string sLine(cLine);
|
||||||
lineCount++;
|
lineCount++;
|
||||||
|
|
||||||
// Parse exit status
|
// Parse exit status
|
||||||
if (sLine.find("\"exit_status\":") != string::npos)
|
if (sLine.find("\"exit_status\":") != string::npos)
|
||||||
{
|
{
|
||||||
exitStatus = static_cast<uint8_t>(extractIntegerValue(sLine));
|
exitStatus = static_cast<uint8_t>(extractIntegerValue(sLine));
|
||||||
}
|
}
|
||||||
|
|
||||||
// Process this line
|
// Process this line
|
||||||
processLine(sLine, ctx);
|
processLine(sLine, ctx);
|
||||||
}
|
}
|
||||||
|
|
||||||
free(cLine);
|
free(cLine);
|
||||||
int pcloseStatus = pclose(outputfileSmart);
|
int pcloseStatus = pclose(outputfileSmart);
|
||||||
|
|
||||||
Logger::logThis()->info("SMART: Parsed " + to_string(lineCount) + " lines, exit status: " + to_string(exitStatus));
|
Logger::logThis()->info("SMART: Parsed " + to_string(lineCount) + " lines, exit status: " + to_string(exitStatus));
|
||||||
|
|
||||||
// Check if timeout killed the process
|
// Check if timeout killed the process
|
||||||
if (WIFSIGNALED(pcloseStatus) && WTERMSIG(pcloseStatus) == SIGTERM)
|
if (WIFSIGNALED(pcloseStatus) && WTERMSIG(pcloseStatus) == SIGTERM)
|
||||||
{
|
{
|
||||||
Logger::logThis()->warning("SMART: Command timed out (5s) - skipping to next variant");
|
Logger::logThis()->warning("SMART: Command timed out (5s) - skipping to next variant");
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
|
||||||
// IGNORE exit status - instead check if we got valid data!
|
// IGNORE exit status - instead check if we got valid data!
|
||||||
// Exit status 64 means "error log contains errors" but SMART data is still valid
|
// Exit status 64 means "error log contains errors" but SMART data is still valid
|
||||||
// Exit status 4 means "some prefail attributes concerning" but data is valid
|
// Exit status 4 means "some prefail attributes concerning" but data is valid
|
||||||
@@ -339,12 +346,12 @@ void SMART::readSMARTData(Drive *drive)
|
|||||||
Logger::logThis()->info("SMART: Reallocated Sectors: " + to_string(ctx.reallocatedSectors));
|
Logger::logThis()->info("SMART: Reallocated Sectors: " + to_string(ctx.reallocatedSectors));
|
||||||
Logger::logThis()->info("SMART: Pending Sectors: " + to_string(ctx.pendingSectors));
|
Logger::logThis()->info("SMART: Pending Sectors: " + to_string(ctx.pendingSectors));
|
||||||
Logger::logThis()->info("SMART: Uncorrectable Sectors: " + to_string(ctx.uncorrectableSectors));
|
Logger::logThis()->info("SMART: Uncorrectable Sectors: " + to_string(ctx.uncorrectableSectors));
|
||||||
|
|
||||||
if (exitStatus != 0)
|
if (exitStatus != 0)
|
||||||
{
|
{
|
||||||
Logger::logThis()->info("SMART: Note - exit status " + to_string(exitStatus) + " indicates warnings/errors in SMART log");
|
Logger::logThis()->info("SMART: Note - exit status " + to_string(exitStatus) + " indicates warnings/errors in SMART log");
|
||||||
}
|
}
|
||||||
|
|
||||||
break; // Success - we got data!
|
break; // Success - we got data!
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
@@ -352,18 +359,18 @@ void SMART::readSMARTData(Drive *drive)
|
|||||||
Logger::logThis()->warning("SMART: No valid data parsed (exit status: " + to_string(exitStatus) + ")");
|
Logger::logThis()->warning("SMART: No valid data parsed (exit status: " + to_string(exitStatus) + ")");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Check if we got ANY data
|
// Check if we got ANY data
|
||||||
if (ctx.modelName.empty() && ctx.serial.empty())
|
if (ctx.modelName.empty() && ctx.serial.empty())
|
||||||
{
|
{
|
||||||
Logger::logThis()->warning("SMART: No SMART data available for this drive - may not support SMART or need root privileges");
|
Logger::logThis()->warning("SMART: No SMART data available for this drive - may not support SMART or need root privileges");
|
||||||
|
|
||||||
// Try basic device info without SMART (use hdparm or similar as fallback)
|
// Try basic device info without SMART (use hdparm or similar as fallback)
|
||||||
// For now, just log that SMART is not available
|
// For now, just log that SMART is not available
|
||||||
ctx.modelName = "SMART not available";
|
ctx.modelName = "SMART not available";
|
||||||
ctx.serial = "N/A";
|
ctx.serial = "N/A";
|
||||||
}
|
}
|
||||||
|
|
||||||
// Write parsed data to drive
|
// Write parsed data to drive
|
||||||
drive->setDriveSMARTData(
|
drive->setDriveSMARTData(
|
||||||
ctx.modelFamily,
|
ctx.modelFamily,
|
||||||
@@ -376,6 +383,5 @@ void SMART::readSMARTData(Drive *drive)
|
|||||||
ctx.temperature,
|
ctx.temperature,
|
||||||
ctx.reallocatedSectors,
|
ctx.reallocatedSectors,
|
||||||
ctx.pendingSectors,
|
ctx.pendingSectors,
|
||||||
ctx.uncorrectableSectors
|
ctx.uncorrectableSectors);
|
||||||
);
|
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user