fix parsing and set dev version

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