135 lines
3.6 KiB
C++
135 lines
3.6 KiB
C++
/**
|
|
* @file smart.cpp
|
|
* @brief read S.M.A.R.T values
|
|
* @author hendrik schutter
|
|
* @date 01.05.2020
|
|
*/
|
|
|
|
#include "smart.h"
|
|
|
|
string SMART::modelFamily;
|
|
string SMART::modelName;
|
|
string SMART::serial;
|
|
string SMART::capacity;
|
|
uint32_t SMART::errorCount = 0U;
|
|
uint32_t SMART::powerOnHours = 0U;
|
|
uint32_t SMART::spinUpCount = 0U;
|
|
|
|
void SMART::readSMARTData(Drive drive)
|
|
{
|
|
size_t len = 0; //lenght of found line
|
|
char* cLine = NULL; //found line
|
|
|
|
string sCMD = ("./smartctl --json -a ");
|
|
sCMD.append(drive.getPath());
|
|
const char* cpComand = sCMD.c_str();
|
|
|
|
FILE* outputfileSmart = popen(cpComand, "r");
|
|
|
|
while ((getline(&cLine, &len, outputfileSmart)) != -1) {
|
|
string sLine = string(cLine);
|
|
|
|
SMART::parseModelFamily(sLine);
|
|
SMART::parseModelName(sLine);
|
|
SMART::parseSerial(sLine);
|
|
SMART::parseCapacity(sLine);
|
|
SMART::parseErrorCount(sLine);
|
|
SMART::parsePowerOnHours(sLine);
|
|
SMART::parseSpinUpCount(sLine);
|
|
}
|
|
fclose(outputfileSmart);
|
|
//drive.setDriveSMARTData(modelFamily, modelName, serial, capacity, errorCount, powerOnHours, spinUpCount);
|
|
}
|
|
|
|
void SMART::parseModelFamily(string sLine)
|
|
{
|
|
string search("\"model_family\": ");
|
|
size_t found = sLine.find(search);
|
|
if (found!=string::npos) {
|
|
// cout << sLine;
|
|
sLine.erase(0, sLine.find(": ") + 3);
|
|
sLine.erase(sLine.length()-3, 3);
|
|
// modelFamily = sLine;
|
|
cout << "ModelFamily |" << sLine << "|" << endl;
|
|
}
|
|
}
|
|
|
|
void SMART::parseModelName(string sLine)
|
|
{
|
|
string search("\"model_name\": ");
|
|
size_t found = sLine.find(search);
|
|
if (found!=string::npos) {
|
|
// cout << sLine;
|
|
sLine.erase(0, sLine.find(": ") + 3);
|
|
sLine.erase(sLine.length()-3, 3);
|
|
// modelName = sLine;
|
|
cout << "ModelName |" << sLine << "|" << endl;
|
|
}
|
|
}
|
|
|
|
void SMART::parseSerial(string sLine)
|
|
{
|
|
string search("\"serial_number\": ");
|
|
size_t found = sLine.find(search);
|
|
if (found!=string::npos) {
|
|
// cout << sLine;
|
|
sLine.erase(0, sLine.find(": ") + 3);
|
|
sLine.erase(sLine.length()-3, 3);
|
|
// serial = sLine;
|
|
cout << "Serial |" << sLine << "|" << endl;
|
|
}
|
|
}
|
|
|
|
void SMART::parseCapacity(string sLine)
|
|
{
|
|
string search("\"bytes\": ");
|
|
size_t found = sLine.find(search);
|
|
if (found!=string::npos) {
|
|
// cout << sLine;
|
|
sLine.erase(0, sLine.find(": ") + 2);
|
|
sLine.erase(sLine.length()-1, 1);
|
|
// capacity = sLine;
|
|
cout << "Capacity |" << sLine << "|" << endl;
|
|
}
|
|
}
|
|
|
|
void SMART::parseErrorCount(string sLine)
|
|
{
|
|
string search("\"error_count_total\": ");
|
|
size_t found = sLine.find(search);
|
|
if (found!=string::npos) {
|
|
//cout << sLine;
|
|
sLine.erase(0, sLine.find(": ")+2);
|
|
sLine.erase(sLine.length()-2, 2);
|
|
errorCount = stoi(sLine);
|
|
cout << "ErrorCount |" << sLine << "|" << endl;
|
|
}
|
|
}
|
|
|
|
void SMART::parsePowerOnHours(string sLine)
|
|
{
|
|
string search("\"hours\": ");
|
|
size_t found = sLine.find(search);
|
|
if (found!=string::npos) {
|
|
// cout << sLine;
|
|
sLine.erase(0, sLine.find(": ") + 2);
|
|
sLine.erase(sLine.length()-1, 1);
|
|
// powerOnHours = stoi(sLine);
|
|
cout << "PowerOnHours |" << sLine << "|" << endl;
|
|
}
|
|
}
|
|
|
|
void SMART::parseSpinUpCount(string sLine)
|
|
{
|
|
string search("\"power_cycle_count\": ");
|
|
size_t found = sLine.find(search);
|
|
if (found!=string::npos) {
|
|
// cout << sLine;
|
|
sLine.erase(0, sLine.find(": ") + 2);
|
|
sLine.erase(sLine.length()-2, 2);
|
|
//spinUpCount = stoi(sLine);
|
|
cout << "SpinUpCount |" << sLine << "|" << endl;
|
|
}
|
|
}
|
|
|