Compare commits
6 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| 716ab5614f | |||
| e017aeca0b | |||
| 42a1567b32 | |||
| 95f5037529 | |||
| 55481b86fd | |||
| 50e88c8e84 |
+2
-2
@@ -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"
|
||||
|
||||
+46
-2
@@ -16,9 +16,28 @@
|
||||
#include <fcntl.h>
|
||||
#include <unistd.h>
|
||||
#include <string.h>
|
||||
#include <chrono>
|
||||
|
||||
#define CHUNK_SIZE 1024 * 1024 * 32 // amount of bytes that are overwritten at once --> 32MB
|
||||
#define TFNG_DATA_SIZE CHUNK_SIZE // amount of bytes used by tfng
|
||||
// Adaptive chunk size optimization - uncomment to enable
|
||||
#define ADAPTIVE_CHUNK_SIZE
|
||||
|
||||
// Chunk size configuration
|
||||
#define CHUNK_SIZE_START 1024 * 1024 * 32 // Starting chunk size: 32MB
|
||||
#define CHUNK_SIZE_MIN 1024 * 1024 * 4 // Minimum chunk size: 4MB
|
||||
#define CHUNK_SIZE_MAX 1024 * 1024 * 128 // Maximum chunk size: 128MB
|
||||
#define CHUNK_SIZE_STEP_UP 1024 * 1024 * 2 // Increase step: 2MB
|
||||
#define CHUNK_SIZE_STEP_DOWN 1024 * 1024 * 4 // Decrease step: 4MB
|
||||
#define CHUNK_MEASURE_INTERVAL 64 // Measure performance every 64 chunks
|
||||
|
||||
#ifdef ADAPTIVE_CHUNK_SIZE
|
||||
// Use max buffer size when adaptive mode is enabled
|
||||
#define CHUNK_SIZE CHUNK_SIZE_MAX
|
||||
#define TFNG_DATA_SIZE CHUNK_SIZE_MAX
|
||||
#else
|
||||
// Use fixed chunk size when adaptive mode is disabled
|
||||
#define CHUNK_SIZE CHUNK_SIZE_START
|
||||
#define TFNG_DATA_SIZE CHUNK_SIZE
|
||||
#endif
|
||||
|
||||
// #define DEMO_DRIVE_SIZE 1024*1024*256L // 256MB
|
||||
// #define DEMO_DRIVE_SIZE 1024*1024*1024L // 1GB
|
||||
@@ -38,13 +57,38 @@ public:
|
||||
private:
|
||||
fileDescriptor randomSrcFileDiscr;
|
||||
fileDescriptor driveFileDiscr;
|
||||
|
||||
#ifdef ADAPTIVE_CHUNK_SIZE
|
||||
unsigned char* caTfngData; // Dynamic buffer allocation for adaptive mode
|
||||
unsigned char* caReadBuffer; // Dynamic buffer allocation for adaptive mode
|
||||
#else
|
||||
unsigned char caTfngData[TFNG_DATA_SIZE];
|
||||
unsigned char caReadBuffer[CHUNK_SIZE];
|
||||
#endif
|
||||
|
||||
unsigned long ulDriveByteSize;
|
||||
unsigned long ulDriveByteOverallCount = 0; // all bytes shredded in all iterations + checking -> used for progress calculation
|
||||
double d32Percent = 0.0;
|
||||
double d32TmpPercent = 0.0;
|
||||
|
||||
#ifdef ADAPTIVE_CHUNK_SIZE
|
||||
// Adaptive chunk size optimization members
|
||||
size_t currentChunkSize;
|
||||
size_t bestChunkSize;
|
||||
unsigned int chunkCounter;
|
||||
std::chrono::high_resolution_clock::time_point measurementStartTime;
|
||||
double bestThroughputMBps;
|
||||
double lastThroughputMBps;
|
||||
unsigned long bytesWrittenInMeasurement;
|
||||
bool throughputIncreasing;
|
||||
|
||||
// Adaptive methods
|
||||
void startMeasurement();
|
||||
void evaluateThroughput(Drive* drive);
|
||||
void adjustChunkSize(Drive* drive);
|
||||
size_t getCurrentChunkSize() const;
|
||||
#endif
|
||||
|
||||
inline double calcProgress();
|
||||
int iRewindDrive(fileDescriptor file);
|
||||
long getDriveSizeInBytes(fileDescriptor file);
|
||||
|
||||
+16
-14
@@ -10,27 +10,29 @@
|
||||
|
||||
#include "reHDD.h"
|
||||
|
||||
/**
|
||||
* @brief SMART data reader for drives
|
||||
*
|
||||
* Parses smartctl JSON output to extract:
|
||||
* - Device information (model, serial, capacity)
|
||||
* - Power statistics (hours, cycles)
|
||||
* - Temperature
|
||||
* - Critical sector counts (reallocated, pending, uncorrectable)
|
||||
*
|
||||
* Uses deterministic state machine parser for reliable multi-line JSON parsing.
|
||||
*/
|
||||
class SMART
|
||||
{
|
||||
protected:
|
||||
public:
|
||||
/**
|
||||
* @brief Read S.M.A.R.T. data from drive and populate Drive object
|
||||
* @param drive Pointer to Drive instance to populate with SMART data
|
||||
*/
|
||||
static void readSMARTData(Drive *drive);
|
||||
|
||||
private:
|
||||
SMART(void);
|
||||
|
||||
static bool parseExitStatus(std::string sLine, uint8_t &status);
|
||||
static bool parseModelFamily(std::string sLine, std::string &modelFamily);
|
||||
static bool parseModelName(std::string sLine, std::string &modelName);
|
||||
static bool parseSerial(std::string sLine, std::string &serial);
|
||||
static bool parseCapacity(std::string sLine, uint64_t &capacity);
|
||||
static bool parseErrorCount(std::string sLine, uint32_t &errorCount);
|
||||
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);
|
||||
SMART(void); // Utility class - no instances
|
||||
};
|
||||
|
||||
#endif // SMART_H_
|
||||
+228
-27
@@ -21,11 +21,168 @@ const static char *randomsrc = (char *)"/dev/urandom";
|
||||
|
||||
Shred::Shred()
|
||||
{
|
||||
#ifdef ADAPTIVE_CHUNK_SIZE
|
||||
// Allocate aligned buffers for maximum chunk size
|
||||
if (posix_memalign((void **)&caTfngData, 4096, CHUNK_SIZE_MAX) != 0)
|
||||
{
|
||||
Logger::logThis()->error("Failed to allocate aligned buffer for tfng data");
|
||||
caTfngData = nullptr;
|
||||
}
|
||||
|
||||
if (posix_memalign((void **)&caReadBuffer, 4096, CHUNK_SIZE_MAX) != 0)
|
||||
{
|
||||
Logger::logThis()->error("Failed to allocate aligned buffer for read buffer");
|
||||
caReadBuffer = nullptr;
|
||||
}
|
||||
|
||||
// Initialize adaptive tracking variables
|
||||
currentChunkSize = CHUNK_SIZE_START;
|
||||
bestChunkSize = CHUNK_SIZE_START;
|
||||
chunkCounter = 0;
|
||||
bestThroughputMBps = 0.0;
|
||||
lastThroughputMBps = 0.0;
|
||||
bytesWrittenInMeasurement = 0;
|
||||
throughputIncreasing = true;
|
||||
|
||||
Logger::logThis()->info("Adaptive chunk size optimization ENABLED - Starting with " +
|
||||
to_string(currentChunkSize / (1024 * 1024)) + " MB chunks");
|
||||
#endif
|
||||
}
|
||||
|
||||
Shred::~Shred()
|
||||
{
|
||||
#ifdef ADAPTIVE_CHUNK_SIZE
|
||||
if (caTfngData != nullptr)
|
||||
{
|
||||
free(caTfngData);
|
||||
caTfngData = nullptr;
|
||||
}
|
||||
if (caReadBuffer != nullptr)
|
||||
{
|
||||
free(caReadBuffer);
|
||||
caReadBuffer = nullptr;
|
||||
}
|
||||
#endif
|
||||
}
|
||||
|
||||
#ifdef ADAPTIVE_CHUNK_SIZE
|
||||
/**
|
||||
* \brief Start performance measurement interval
|
||||
* \return void
|
||||
*/
|
||||
void Shred::startMeasurement()
|
||||
{
|
||||
measurementStartTime = std::chrono::high_resolution_clock::now();
|
||||
bytesWrittenInMeasurement = 0;
|
||||
chunkCounter = 0;
|
||||
}
|
||||
|
||||
/**
|
||||
* \brief shred drive with shred
|
||||
* \param pointer of Drive instance
|
||||
* \param file descriptor for signaling
|
||||
* \return 0 on success, -1 on error
|
||||
*/
|
||||
void Shred::evaluateThroughput(Drive *drive)
|
||||
{
|
||||
auto measurementEndTime = std::chrono::high_resolution_clock::now();
|
||||
std::chrono::duration<double> elapsed = measurementEndTime - measurementStartTime;
|
||||
double elapsedSeconds = elapsed.count();
|
||||
|
||||
if (elapsedSeconds > 0.0)
|
||||
{
|
||||
double throughputMBps = (bytesWrittenInMeasurement / (1024.0 * 1024.0)) / elapsedSeconds;
|
||||
lastThroughputMBps = throughputMBps;
|
||||
|
||||
Logger::logThis()->info("Throughput measurement - ChunkSize: " +
|
||||
to_string(currentChunkSize / (1024 * 1024)) + " MB, " +
|
||||
"Throughput: " + to_string((int)throughputMBps) + " MB/s, " +
|
||||
"Best: " + to_string((int)bestThroughputMBps) + " MB/s" +
|
||||
" - Drive: " + drive->getSerial());
|
||||
|
||||
// Check if this is better than our best
|
||||
if (throughputMBps > bestThroughputMBps)
|
||||
{
|
||||
bestThroughputMBps = throughputMBps;
|
||||
bestChunkSize = currentChunkSize;
|
||||
throughputIncreasing = true;
|
||||
|
||||
Logger::logThis()->info("NEW BEST throughput: " + to_string((int)bestThroughputMBps) +
|
||||
" MB/s with " + to_string(currentChunkSize / (1024 * 1024)) +
|
||||
" MB chunks - Drive: " + drive->getSerial());
|
||||
}
|
||||
else
|
||||
{
|
||||
throughputIncreasing = false;
|
||||
}
|
||||
}
|
||||
|
||||
// Adjust chunk size for next measurement interval
|
||||
adjustChunkSize(drive);
|
||||
|
||||
// Start new measurement
|
||||
startMeasurement();
|
||||
}
|
||||
|
||||
/**
|
||||
* \brief Adjust chunk size based on throughput trend
|
||||
* \param pointer to Drive instance
|
||||
* \return void
|
||||
*/
|
||||
void Shred::adjustChunkSize(Drive *drive)
|
||||
{
|
||||
size_t oldChunkSize = currentChunkSize;
|
||||
|
||||
if (throughputIncreasing)
|
||||
{
|
||||
// Throughput is improving - increase chunk size
|
||||
currentChunkSize += CHUNK_SIZE_STEP_UP;
|
||||
|
||||
// Clamp to maximum
|
||||
if (currentChunkSize > CHUNK_SIZE_MAX)
|
||||
{
|
||||
currentChunkSize = CHUNK_SIZE_MAX;
|
||||
Logger::logThis()->info("Reached maximum chunk size: " +
|
||||
to_string(currentChunkSize / (1024 * 1024)) + " MB" +
|
||||
" - Drive: " + drive->getSerial());
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
// Throughput decreased - decrease chunk size to find sweet spot
|
||||
if (currentChunkSize > CHUNK_SIZE_STEP_DOWN)
|
||||
{
|
||||
currentChunkSize -= CHUNK_SIZE_STEP_DOWN;
|
||||
}
|
||||
|
||||
// Clamp to minimum
|
||||
if (currentChunkSize < CHUNK_SIZE_MIN)
|
||||
{
|
||||
currentChunkSize = CHUNK_SIZE_MIN;
|
||||
Logger::logThis()->info("Reached minimum chunk size: " +
|
||||
to_string(currentChunkSize / (1024 * 1024)) + " MB" +
|
||||
" - Drive: " + drive->getSerial());
|
||||
}
|
||||
}
|
||||
|
||||
if (oldChunkSize != currentChunkSize)
|
||||
{
|
||||
Logger::logThis()->info("Adjusted chunk size: " +
|
||||
to_string(oldChunkSize / (1024 * 1024)) + " MB -> " +
|
||||
to_string(currentChunkSize / (1024 * 1024)) + " MB" +
|
||||
" - Drive: " + drive->getSerial());
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* \brief Get current chunk size for adaptive mode
|
||||
* \return current chunk size in bytes
|
||||
*/
|
||||
size_t Shred::getCurrentChunkSize() const
|
||||
{
|
||||
return currentChunkSize;
|
||||
}
|
||||
#endif
|
||||
|
||||
/**
|
||||
* \brief shred drive with shred
|
||||
@@ -76,6 +233,15 @@ int Shred::shredDrive(Drive *drive, int *ipSignalFd)
|
||||
const char *cpDrivePath = sDrivePath.c_str();
|
||||
unsigned char ucKey[TFNG_KEY_SIZE];
|
||||
|
||||
#ifdef ADAPTIVE_CHUNK_SIZE
|
||||
// Validate buffers were allocated
|
||||
if (caTfngData == nullptr || caReadBuffer == nullptr)
|
||||
{
|
||||
Logger::logThis()->error("Shred-Task: Aligned buffers not allocated! - Drive: " + drive->getSerial());
|
||||
return -1;
|
||||
}
|
||||
#endif
|
||||
|
||||
// Open random source
|
||||
Logger::logThis()->info("Shred-Task: Opening random source: " + string(randomsrc) + " - Drive: " + drive->getSerial());
|
||||
randomSrcFileDiscr = open(randomsrc, O_RDONLY | O_LARGEFILE);
|
||||
@@ -182,14 +348,18 @@ int Shred::shredDrive(Drive *drive, int *ipSignalFd)
|
||||
}
|
||||
|
||||
Drive::ShredSpeed shredSpeed = drive->sShredSpeed.load();
|
||||
shredSpeed.chronoShredTimestamp = std::chrono::system_clock::now(); // set inital timestamp for speed metric
|
||||
shredSpeed.ulSpeedMetricBytesWritten = 0U; // uses to calculate speed metric
|
||||
shredSpeed.chronoShredTimestamp = std::chrono::system_clock::now();
|
||||
shredSpeed.ulSpeedMetricBytesWritten = 0U;
|
||||
drive->sShredSpeed.store(shredSpeed);
|
||||
|
||||
#ifdef LOG_LEVEL_HIGH
|
||||
Logger::logThis()->info("Shred-Task: Bytes-Size of Drive: " + to_string(this->ulDriveByteSize) + " - Drive: " + drive->getSerial());
|
||||
#endif
|
||||
|
||||
#ifdef ADAPTIVE_CHUNK_SIZE
|
||||
// Start first measurement interval
|
||||
startMeasurement();
|
||||
#endif
|
||||
// Main shredding loop
|
||||
for (unsigned int uiShredIterationCounter = 0U; uiShredIterationCounter < SHRED_ITERATIONS; uiShredIterationCounter++)
|
||||
{
|
||||
@@ -200,44 +370,39 @@ int Shred::shredDrive(Drive *drive, int *ipSignalFd)
|
||||
if (uiShredIterationCounter == (SHRED_ITERATIONS - 1))
|
||||
{
|
||||
// last shred iteration --> overwrite (just the write chunk) bytes with zeros instead with random data
|
||||
#ifdef ADAPTIVE_CHUNK_SIZE
|
||||
memset(caTfngData, 0U, CHUNK_SIZE_MAX);
|
||||
#else
|
||||
memset(caTfngData, 0U, CHUNK_SIZE);
|
||||
#endif
|
||||
}
|
||||
|
||||
while (ulDriveByteCounter < ulDriveByteSize)
|
||||
{
|
||||
// Check if task was aborted
|
||||
if (drive->state.load() != Drive::TaskState::SHRED_ACTIVE)
|
||||
{
|
||||
Logger::logThis()->info("Shred-Task: Aborted by user at " + to_string(d32Percent) +
|
||||
"% in iteration " + to_string(uiShredIterationCounter + 1) +
|
||||
" - Drive: " + drive->getSerial());
|
||||
drive->setTaskPercentage(0);
|
||||
d32Percent = 0.00;
|
||||
d32TmpPercent = 0.00;
|
||||
cleanup();
|
||||
|
||||
// CRITICAL: Mark as NOT shredded on abort
|
||||
drive->state = Drive::TaskState::NONE;
|
||||
drive->bWasShredded = false;
|
||||
drive->bWasChecked = false;
|
||||
return -1;
|
||||
}
|
||||
#ifdef ADAPTIVE_CHUNK_SIZE
|
||||
size_t activeChunkSize = getCurrentChunkSize();
|
||||
#else
|
||||
size_t activeChunkSize = CHUNK_SIZE;
|
||||
#endif
|
||||
|
||||
int iBytesToShred = 0;
|
||||
|
||||
if (uiShredIterationCounter != (SHRED_ITERATIONS - 1))
|
||||
{
|
||||
// Generate random data for this chunk
|
||||
#ifdef ADAPTIVE_CHUNK_SIZE
|
||||
tfng_prng_genrandom(caTfngData, activeChunkSize);
|
||||
#else
|
||||
tfng_prng_genrandom(caTfngData, TFNG_DATA_SIZE);
|
||||
#endif
|
||||
}
|
||||
|
||||
if ((ulDriveByteSize - ulDriveByteCounter) < CHUNK_SIZE)
|
||||
if ((ulDriveByteSize - ulDriveByteCounter) < activeChunkSize)
|
||||
{
|
||||
iBytesToShred = (ulDriveByteSize - ulDriveByteCounter);
|
||||
}
|
||||
else
|
||||
{
|
||||
iBytesToShred = CHUNK_SIZE;
|
||||
iBytesToShred = activeChunkSize;
|
||||
}
|
||||
|
||||
int iByteShredded = write(driveFileDiscr, caTfngData, iBytesToShred);
|
||||
@@ -267,7 +432,20 @@ int Shred::shredDrive(Drive *drive, int *ipSignalFd)
|
||||
|
||||
ulDriveByteCounter += iByteShredded;
|
||||
ulDriveByteOverallCount += iByteShredded;
|
||||
|
||||
#ifdef ADAPTIVE_CHUNK_SIZE
|
||||
bytesWrittenInMeasurement += iByteShredded;
|
||||
chunkCounter++;
|
||||
|
||||
// Evaluate throughput after measurement interval
|
||||
if (chunkCounter >= CHUNK_MEASURE_INTERVAL)
|
||||
{
|
||||
evaluateThroughput(drive);
|
||||
}
|
||||
#endif
|
||||
|
||||
d32Percent = this->calcProgress();
|
||||
|
||||
#ifdef LOG_LEVEL_HIGH
|
||||
Logger::logThis()->info("Shred-Task: ByteCount: " + to_string(ulDriveByteCounter) +
|
||||
" - iteration: " + to_string((uiShredIterationCounter + 1)) +
|
||||
@@ -277,12 +455,23 @@ int Shred::shredDrive(Drive *drive, int *ipSignalFd)
|
||||
|
||||
if ((d32Percent - d32TmpPercent) >= 0.01)
|
||||
{
|
||||
// set shred percantage
|
||||
// set shred percentage
|
||||
drive->setTaskPercentage(d32TmpPercent);
|
||||
d32TmpPercent = d32Percent;
|
||||
// signal process in shredding
|
||||
write(*ipSignalFd, "A", 1);
|
||||
}
|
||||
|
||||
if (drive->state != Drive::TaskState::SHRED_ACTIVE)
|
||||
{
|
||||
drive->setTaskPercentage(0);
|
||||
d32Percent = 0.00;
|
||||
d32TmpPercent = 0.00;
|
||||
ulDriveByteCounter = 0U;
|
||||
Logger::logThis()->info("Aborted shred for: " + drive->getModelName() + "-" + drive->getSerial());
|
||||
cleanup();
|
||||
return -1;
|
||||
}
|
||||
}
|
||||
|
||||
Logger::logThis()->info("Shred-Task: Iteration " + to_string(uiShredIterationCounter + 1) + "/" +
|
||||
@@ -304,12 +493,19 @@ int Shred::shredDrive(Drive *drive, int *ipSignalFd)
|
||||
}
|
||||
}
|
||||
|
||||
#ifdef ADAPTIVE_CHUNK_SIZE
|
||||
Logger::logThis()->info("Shred completed - Optimal chunk size: " +
|
||||
to_string(bestChunkSize / (1024 * 1024)) + " MB, " +
|
||||
"Best throughput: " + to_string((int)bestThroughputMBps) + " MB/s" +
|
||||
" - Drive: " + drive->getSerial());
|
||||
#endif
|
||||
|
||||
// All shred iterations completed successfully
|
||||
tfng_prng_seedkey(NULL);
|
||||
|
||||
// ONLY mark as shredded if ALL iterations completed AND fsync succeeded
|
||||
drive->bWasShredded = true;
|
||||
Logger::logThis()->info("Shred-Task finished successfully - Drive: " + drive->getModelName() + "-" + drive->getSerial() + " @" + address.str());
|
||||
Logger::logThis()->info("Shred-Task finished - Drive: " + drive->getModelName() + "-" + drive->getSerial() + " @" + address.str());
|
||||
|
||||
#ifdef ZERO_CHECK
|
||||
drive->state = Drive::TaskState::CHECK_ACTIVE;
|
||||
@@ -449,6 +645,12 @@ unsigned int Shred::uiCalcChecksum(fileDescriptor file, Drive *drive, int *ipSig
|
||||
|
||||
Logger::logThis()->info("Check-Task: Starting checksum verification - Drive: " + drive->getSerial());
|
||||
|
||||
#ifdef ADAPTIVE_CHUNK_SIZE
|
||||
size_t checkChunkSize = CHUNK_SIZE_MAX;
|
||||
#else
|
||||
size_t checkChunkSize = CHUNK_SIZE;
|
||||
#endif
|
||||
|
||||
while (ulDriveByteCounter < ulDriveByteSize)
|
||||
{
|
||||
// Check if task was aborted
|
||||
@@ -459,14 +661,13 @@ unsigned int Shred::uiCalcChecksum(fileDescriptor file, Drive *drive, int *ipSig
|
||||
}
|
||||
|
||||
int iBytesToCheck = 0;
|
||||
|
||||
if ((ulDriveByteSize - ulDriveByteCounter) < CHUNK_SIZE)
|
||||
if ((ulDriveByteSize - ulDriveByteCounter) < checkChunkSize)
|
||||
{
|
||||
iBytesToCheck = (ulDriveByteSize - ulDriveByteCounter);
|
||||
}
|
||||
else
|
||||
{
|
||||
iBytesToCheck = CHUNK_SIZE;
|
||||
iBytesToCheck = checkChunkSize;
|
||||
}
|
||||
int iReadBytes = read(file, caReadBuffer, iBytesToCheck);
|
||||
|
||||
|
||||
+342
-393
@@ -6,8 +6,258 @@
|
||||
*/
|
||||
|
||||
#include "../include/reHDD.h"
|
||||
#include <sys/wait.h> // For WIFSIGNALED, WTERMSIG
|
||||
using namespace std;
|
||||
|
||||
/**
|
||||
* \brief Parse context for SMART attribute values
|
||||
*/
|
||||
struct SMARTParseContext
|
||||
{
|
||||
// Device information (top-level JSON fields)
|
||||
string modelFamily;
|
||||
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
|
||||
};
|
||||
|
||||
State state;
|
||||
int currentAttributeId; // Which attribute are we parsing? (5, 197, 198)
|
||||
|
||||
SMARTParseContext()
|
||||
: capacity(0),
|
||||
errorCount(0),
|
||||
powerOnHours(0),
|
||||
powerCycles(0),
|
||||
temperature(0),
|
||||
reallocatedSectors(0),
|
||||
pendingSectors(0),
|
||||
uncorrectableSectors(0),
|
||||
state(SEARCHING),
|
||||
currentAttributeId(0)
|
||||
{
|
||||
}
|
||||
};
|
||||
|
||||
/**
|
||||
* \brief Extract JSON string value
|
||||
* \param line containing "key": "value"
|
||||
* \return extracted string value
|
||||
*/
|
||||
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);
|
||||
}
|
||||
|
||||
/**
|
||||
* \brief Extract JSON integer value
|
||||
* \param line containing "key": number
|
||||
* \return extracted integer value
|
||||
*/
|
||||
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'; }),
|
||||
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);
|
||||
}
|
||||
catch (...)
|
||||
{
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* \brief Process a single line of JSON output
|
||||
* \param line from smartctl JSON output
|
||||
* \param context parsing context with state
|
||||
* \return void
|
||||
*/
|
||||
static void processLine(const string &line, SMARTParseContext &ctx)
|
||||
{
|
||||
// Trim whitespace for consistent parsing
|
||||
string trimmed = line;
|
||||
size_t firstNonSpace = trimmed.find_first_not_of(" \t\r\n");
|
||||
if (firstNonSpace != string::npos)
|
||||
{
|
||||
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)
|
||||
{
|
||||
// Only parse first occurrence (temperature section, not other "current" fields)
|
||||
ctx.temperature = extractIntegerValue(line);
|
||||
return;
|
||||
}
|
||||
|
||||
// State machine for SMART attributes parsing
|
||||
switch (ctx.state)
|
||||
{
|
||||
case SMARTParseContext::SEARCHING:
|
||||
// Look for critical attribute IDs
|
||||
if (trimmed.find("\"id\": 5,") == 0)
|
||||
{
|
||||
ctx.state = SMARTParseContext::IN_ATTRIBUTE_5;
|
||||
ctx.currentAttributeId = 5;
|
||||
}
|
||||
else if (trimmed.find("\"id\": 197,") == 0)
|
||||
{
|
||||
ctx.state = SMARTParseContext::IN_ATTRIBUTE_197;
|
||||
ctx.currentAttributeId = 197;
|
||||
}
|
||||
else if (trimmed.find("\"id\": 198,") == 0)
|
||||
{
|
||||
ctx.state = SMARTParseContext::IN_ATTRIBUTE_198;
|
||||
ctx.currentAttributeId = 198;
|
||||
}
|
||||
break;
|
||||
|
||||
case SMARTParseContext::IN_ATTRIBUTE_5:
|
||||
case SMARTParseContext::IN_ATTRIBUTE_197:
|
||||
case SMARTParseContext::IN_ATTRIBUTE_198:
|
||||
// Look for "raw": { start
|
||||
if (trimmed.find("\"raw\":") == 0)
|
||||
{
|
||||
ctx.state = SMARTParseContext::IN_RAW_SECTION;
|
||||
}
|
||||
// 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)
|
||||
{
|
||||
ctx.reallocatedSectors = static_cast<uint32_t>(value);
|
||||
}
|
||||
else if (ctx.currentAttributeId == 197)
|
||||
{
|
||||
ctx.pendingSectors = static_cast<uint32_t>(value);
|
||||
}
|
||||
else if (ctx.currentAttributeId == 198)
|
||||
{
|
||||
ctx.uncorrectableSectors = static_cast<uint32_t>(value);
|
||||
}
|
||||
|
||||
// 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;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* \brief get and set S.M.A.R.T. values in Drive
|
||||
* \param pointer of Drive instance
|
||||
@@ -15,424 +265,123 @@ using namespace std;
|
||||
*/
|
||||
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;
|
||||
uint32_t reallocatedSectors = 0U;
|
||||
uint32_t pendingSectors = 0U;
|
||||
uint32_t uncorrectableSectors = 0U;
|
||||
SMARTParseContext ctx;
|
||||
uint8_t exitStatus = 255U;
|
||||
|
||||
modelFamily.clear();
|
||||
modelName.clear();
|
||||
serial.clear();
|
||||
// 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 -d usbprolific -a ", // USB Prolific
|
||||
" --json -d usbsunplus -a " // USB Sunplus
|
||||
};
|
||||
|
||||
string sSmartctlCommands[] = {" --json -a ", " --json -d sntjmicron -a ", " --json -d sntasmedia -a ", " --json -d sntrealtek -a ", " --json -d sat -a "};
|
||||
|
||||
for (string sSmartctlCommand : sSmartctlCommands)
|
||||
for (const string &sSmartctlCommand : sSmartctlCommands)
|
||||
{
|
||||
string sCMD = ("smartctl");
|
||||
// Build command with timeout
|
||||
string sCMD = "timeout 5 smartctl"; // 5 second timeout prevents hanging
|
||||
sCMD.append(sSmartctlCommand);
|
||||
sCMD.append(drive->getPath());
|
||||
const char *cpComand = sCMD.c_str();
|
||||
// Note: stderr NOT suppressed for debugging
|
||||
|
||||
// Logger::logThis()->info(cpComand);
|
||||
Logger::logThis()->info("SMART: Executing: " + sCMD);
|
||||
|
||||
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)
|
||||
// Execute smartctl with timeout protection
|
||||
FILE *outputfileSmart = popen(sCMD.c_str(), "r");
|
||||
if (outputfileSmart == nullptr)
|
||||
{
|
||||
string sLine = string(cLine);
|
||||
Logger::logThis()->error("SMART: Failed to execute smartctl");
|
||||
continue;
|
||||
}
|
||||
|
||||
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);
|
||||
SMART::parseReallocatedSectors(sLine, reallocatedSectors);
|
||||
SMART::parsePendingSectors(sLine, pendingSectors);
|
||||
SMART::parseUncorrectableSectors(sLine, uncorrectableSectors);
|
||||
// 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);
|
||||
pclose(outputfileSmart);
|
||||
int pcloseStatus = pclose(outputfileSmart);
|
||||
|
||||
if (status == 0U)
|
||||
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)
|
||||
{
|
||||
// Found S.M.A.R.T. data with this command
|
||||
// Logger::logThis()->info("Found S.M.A.R.T. data with this command");
|
||||
break;
|
||||
}
|
||||
Logger::logThis()->warning("SMART: Command timed out (5s) - skipping to next variant");
|
||||
continue;
|
||||
}
|
||||
|
||||
drive->setDriveSMARTData(modelFamily, modelName, serial, capacity, errorCount, powerOnHours, powerCycles, temperature, reallocatedSectors, pendingSectors, uncorrectableSectors); // write data in drive
|
||||
// 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
|
||||
// What matters: Did we parse model name and serial?
|
||||
if (!ctx.modelName.empty() && !ctx.serial.empty())
|
||||
{
|
||||
Logger::logThis()->info("SMART: Successfully parsed data");
|
||||
Logger::logThis()->info("SMART: Model: " + ctx.modelName);
|
||||
Logger::logThis()->info("SMART: Serial: " + ctx.serial);
|
||||
Logger::logThis()->info("SMART: Capacity: " + to_string(ctx.capacity) + " bytes");
|
||||
Logger::logThis()->info("SMART: Power-On Hours: " + to_string(ctx.powerOnHours));
|
||||
Logger::logThis()->info("SMART: Temperature: " + to_string(ctx.temperature) + " C");
|
||||
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");
|
||||
}
|
||||
|
||||
/**
|
||||
* \brief parse ExitStatus
|
||||
* \param string output line of smartctl
|
||||
* \param uint8_t parsed status
|
||||
* \return bool if parsing was possible
|
||||
*/
|
||||
bool SMART::parseExitStatus(string sLine, uint8_t &status)
|
||||
{
|
||||
string search("\"exit_status\": ");
|
||||
size_t found = sLine.find(search);
|
||||
if (found != string::npos)
|
||||
{
|
||||
sLine.erase(0U, sLine.find(": ") + 1U);
|
||||
status = stol(sLine);
|
||||
return true;
|
||||
break; // Success - we got data!
|
||||
}
|
||||
else
|
||||
{
|
||||
return false;
|
||||
Logger::logThis()->warning("SMART: No valid data parsed (exit status: " + to_string(exitStatus) + ")");
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* \brief parse ModelFamily
|
||||
* \param string output line of smartctl
|
||||
* \param string parsed model family
|
||||
* \return bool if parsing was possible
|
||||
*/
|
||||
bool SMART::parseModelFamily(string sLine, string &modelFamily)
|
||||
// Check if we got ANY data
|
||||
if (ctx.modelName.empty() && ctx.serial.empty())
|
||||
{
|
||||
string search("\"model_family\": ");
|
||||
size_t found = sLine.find(search);
|
||||
if (found != string::npos)
|
||||
{
|
||||
sLine.erase(0U, sLine.find(": ") + 3U);
|
||||
if (sLine.length() >= 3U)
|
||||
{
|
||||
sLine.erase(sLine.length() - 3U, 3U);
|
||||
}
|
||||
modelFamily = sLine;
|
||||
return true;
|
||||
}
|
||||
else
|
||||
{
|
||||
return false;
|
||||
}
|
||||
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";
|
||||
}
|
||||
|
||||
/**
|
||||
* \brief parse ModelName
|
||||
* \param string output line of smartctl
|
||||
* \param string parsed model name
|
||||
* \return bool if parsing was possible
|
||||
*/
|
||||
bool SMART::parseModelName(string sLine, string &modelName)
|
||||
{
|
||||
string search("\"model_name\": ");
|
||||
size_t found = sLine.find(search);
|
||||
if (found != string::npos)
|
||||
{
|
||||
sLine.erase(0U, sLine.find(": ") + 3U);
|
||||
if (sLine.length() >= 3U)
|
||||
{
|
||||
sLine.erase(sLine.length() - 3U, 3U);
|
||||
// Write parsed data to drive
|
||||
drive->setDriveSMARTData(
|
||||
ctx.modelFamily,
|
||||
ctx.modelName,
|
||||
ctx.serial,
|
||||
ctx.capacity,
|
||||
ctx.errorCount,
|
||||
ctx.powerOnHours,
|
||||
ctx.powerCycles,
|
||||
ctx.temperature,
|
||||
ctx.reallocatedSectors,
|
||||
ctx.pendingSectors,
|
||||
ctx.uncorrectableSectors);
|
||||
}
|
||||
modelName = sLine;
|
||||
return true;
|
||||
}
|
||||
else
|
||||
{
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* \brief parse Serial
|
||||
* \param string output line of smartctl
|
||||
* \param string parsed serial
|
||||
* \return bool if parsing was possible
|
||||
*/
|
||||
bool SMART::parseSerial(string sLine, string &serial)
|
||||
{
|
||||
string search("\"serial_number\": ");
|
||||
size_t found = sLine.find(search);
|
||||
if (found != string::npos)
|
||||
{
|
||||
sLine.erase(0, sLine.find(": ") + 3);
|
||||
if (sLine.length() >= 3U)
|
||||
{
|
||||
sLine.erase(sLine.length() - 3U, 3U);
|
||||
}
|
||||
serial = sLine;
|
||||
return true;
|
||||
}
|
||||
else
|
||||
{
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* \brief parse Capacity
|
||||
* \param string output line of smartctl
|
||||
* \param string parsed capacity
|
||||
* \return bool if parsing was possible
|
||||
*/
|
||||
bool SMART::parseCapacity(string sLine, uint64_t &capacity)
|
||||
{
|
||||
string search("\"bytes\": ");
|
||||
size_t found = sLine.find(search);
|
||||
if (found != string::npos)
|
||||
{
|
||||
sLine.erase(0, sLine.find(": ") + 2);
|
||||
if (sLine.length() >= 1U)
|
||||
{
|
||||
sLine.erase(sLine.length() - 1U, 1U);
|
||||
}
|
||||
capacity = stol(sLine);
|
||||
return true;
|
||||
}
|
||||
else
|
||||
{
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* \brief parse ErrorCount
|
||||
* \param string output line of smartctl
|
||||
* \param uint32_t parsed error count
|
||||
* \return bool if parsing was possible
|
||||
*/
|
||||
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(0U, sLine.find(": ") + 2U);
|
||||
if (sLine.length() >= 2U)
|
||||
{
|
||||
sLine.erase(sLine.length() - 2U, 2U);
|
||||
}
|
||||
errorCount = stol(sLine);
|
||||
return true;
|
||||
}
|
||||
else
|
||||
{
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* \brief parse PowerOnHours
|
||||
* \param string output line of smartctl\
|
||||
* \param uint32_t parsed power on hours
|
||||
* \return bool if parsing was possible
|
||||
*/
|
||||
bool SMART::parsePowerOnHours(string sLine, uint32_t &powerOnHours)
|
||||
{
|
||||
string search("\"hours\": ");
|
||||
size_t found = sLine.find(search);
|
||||
if (found != string::npos)
|
||||
{
|
||||
sLine.erase(0U, sLine.find(": ") + 2U);
|
||||
if (sLine.length() >= 1U)
|
||||
{
|
||||
sLine.erase(sLine.length() - 1U, 1U);
|
||||
}
|
||||
powerOnHours = stol(sLine);
|
||||
return true;
|
||||
}
|
||||
else
|
||||
{
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* \brief parse PowerCycle
|
||||
* \param string output line of smartctl
|
||||
* \param uint32_t parsed power cycles
|
||||
* \return bool if parsing was possible
|
||||
*/
|
||||
bool SMART::parsePowerCycles(string sLine, uint32_t &powerCycles)
|
||||
{
|
||||
string search("\"power_cycle_count\": ");
|
||||
size_t found = sLine.find(search);
|
||||
if (found != string::npos)
|
||||
{
|
||||
sLine.erase(0, sLine.find(": ") + 2);
|
||||
if (sLine.length() >= 2U)
|
||||
{
|
||||
sLine.erase(sLine.length() - 2U, 2U);
|
||||
}
|
||||
powerCycles = stol(sLine);
|
||||
return true;
|
||||
}
|
||||
else
|
||||
{
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* \brief parse temperature
|
||||
* \param string output line of smartctl
|
||||
* \param uint32_t parsed temperature
|
||||
* \return bool if parsing was possible
|
||||
*/
|
||||
bool SMART::parseTemperature(string sLine, uint32_t &temperature)
|
||||
{
|
||||
string search("\"current\": ");
|
||||
size_t found = sLine.find(search);
|
||||
if (found != string::npos)
|
||||
{
|
||||
sLine.erase(0U, sLine.find(": ") + 2U);
|
||||
if (sLine.length() >= 1U)
|
||||
{
|
||||
sLine.erase(sLine.length() - 1U, 2U);
|
||||
}
|
||||
if (sLine == "{")
|
||||
{
|
||||
temperature = 0U; // this drive doesn't support temperature
|
||||
}
|
||||
else
|
||||
{
|
||||
temperature = stol(sLine);
|
||||
}
|
||||
return true;
|
||||
}
|
||||
else
|
||||
{
|
||||
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": <number>, ... }
|
||||
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;
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user