added: search drive, filter out ingored drives and prtint drive

This commit is contained in:
2020-05-01 18:31:02 +02:00
parent 20cfc44b51
commit 3547369a49
20 changed files with 361 additions and 218 deletions

174
src/app.cpp Normal file
View File

@ -0,0 +1,174 @@
/**
* @file app.cpp
* @brief app logic
* @author hendrik schutter
* @date 01.05.2020
*/
#include "app.h"
/**
* \brief app constructor
* \param void
* \return instance of App
*/
App::App(void) {
cout << "created app" << endl;
}
/**
* \brief app logic
* \param void
* \return void
*/
void App::app_logic(void)
{
cout << "app logic" << endl;
searchDrives(&listDrives); //search for new drives and store them in list
filterIgnoredDrives(&listDrives); //filter out ignored drives
printDrives(&listDrives); //print currently attached drives
}
/**
* \brief search attached drives on /dev/sd*
* \param pointer of list <struct drive> *listDrives
* \return void
*/
void App::searchDrives(list <struct drive> *listDrives)
{
cout << "search drives ..." << endl;
char * cLine = NULL;
size_t len = 0;
FILE* outputfileHwinfo = popen("./hwinfo --short --disk", "r");
if (outputfileHwinfo == NULL)
{
exit(EXIT_FAILURE);
}
while ((getline(&cLine, &len, outputfileHwinfo)) != -1)
{
if (string(cLine).find("/dev/sd") != string::npos)
{
struct drive tmpDrive;
tmpDrive.u32ErrorCount = 0U;
tmpDrive.u32PowerOnHours = 0U;
tmpDrive.u32SpinUpCount = 0U;
tmpDrive.sPath = string(cLine).substr (2,8);
listDrives->push_back(tmpDrive);
}
}
fclose(outputfileHwinfo);
}
/**
* \brief filter out drives that are listed in "ignoreDrives.conf"
* \param pointer of list <struct drive> *listDrives
* \return void
*/
void App::filterIgnoredDrives(list <struct drive> *listDrives)
{
string sDelimiter = ":";
string sIgnoredDrivePath;
string sIgnoredDriveUUID;
vector<tuple<string, string>> vtlIgnoredDevices; //store drives from ingnore file
ifstream input( "ignoreDrives.conf" ); //read ingnore file
for(string sLine; getline( input, sLine );)
{
if (string(sLine).find("/dev/sd") != string::npos)
{
size_t pos = 0;
string token;
while ((pos = sLine.find(sDelimiter)) != string::npos)
{
token = sLine.substr(0, pos);
sIgnoredDrivePath = token;
sLine.erase(0, pos + sDelimiter.length());
sIgnoredDriveUUID = sLine;
} //end while
//cout << "Path: " << sIgnoredDrivePath << std::endl;
//cout << "UUID: " << sIgnoredDriveUUID << std::endl;
vtlIgnoredDevices.emplace_back(sIgnoredDrivePath, sIgnoredDriveUUID); //add found path and uuid from ingnore file to vector
}
}
//loop through found entries in ingnore file
for(auto row : vtlIgnoredDevices)
{
//cout << get<0>(row) << " is " << get<1>(row) << endl;
list <struct drive>::iterator it;
// loop through found drives
for (it = listDrives->begin(); it != listDrives->end(); ++it)
{
string sUUID;
if (!get<0>(row).compare(it->sPath)) //find same drive based on path
{
// cout << "Same drive path found" << endl;
char * cLine = NULL;
size_t len = 0;
string sCMD = "./blkid ";
sCMD.append(it->sPath);
// cout << "cmd: " << sCMD << endl;
FILE* outputfileBlkid = popen(sCMD.c_str(), "r"); //get UUID from drive
if (outputfileBlkid == NULL)
{
exit(EXIT_FAILURE);
}
while ((getline(&cLine, &len, outputfileBlkid)) != -1) //parse UUID from blkid
{
if (string(cLine).find("PTUUID") != string::npos)
{
string sBlkidOut = string(cLine);
sBlkidOut.erase(0, 18);
sBlkidOut.erase(36, sBlkidOut.length() - 36);
sUUID = sBlkidOut;
//cout << "blkid uuid:" << sUUID << endl;
}
}
fclose(outputfileBlkid);
//cout << "blkid uuid:" << sUUID << endl;
if (get<1>(row).compare(sUUID)) //compare uuid from ignore file and uuid from drive
{
cout << "[ERROR] different uuid found than in ignore file:" << it->sPath << endl;
exit(EXIT_FAILURE); // exit to prevent accidentally shred a system drive
}
else
{
// same uuid found than in ignore file --> ignore this drive
it = listDrives->erase(it);
}
}
}
}
}
/**
* \brief print drives with all information
* \param pointer of list <struct drive> *listDrives
* \return void
*/
void App::printDrives(list <struct drive> *listDrives)
{
cout << "------------DRIVES---------------" << endl;
list <struct drive>::iterator it;
for (it = listDrives->begin(); it != listDrives->end(); ++it)
{
cout << "Path: " << it->sPath << endl;
cout << "Model: " << it->sModel << endl;
cout << "Manufacturer: " << it->sManufacturer << endl;
cout << "Capacity: " << it->sCapacity << endl;
cout << "Serial: " << it->sSerial << endl;
cout << "PowerOnHours: " << it->u32PowerOnHours << endl;
cout << "SpinUpCount: " << it->u32SpinUpCount << endl;
cout << "ErrorCount: " << it->u32ErrorCount << endl;
cout << endl;
}
cout << "---------------------------------" << endl;
}

34
src/app.h Normal file
View File

@ -0,0 +1,34 @@
/**
* @file app.h
* @brief app logic
* @author hendrik schutter
* @date 01.05.2020
*/
#ifndef APP_H_
#define APP_H_
#include "refurbishingHddTool.h"
#include "drive.h"
class App {
protected:
public:
App(void);
void app_logic();
private:
list <struct drive> listDrives; //stores all drive data
void searchDrives(list <struct drive> *listDrives);
void printDrives(list <struct drive> *listDrives);
void filterIgnoredDrives(list <struct drive> *listDrives);
};
#endif // APP_H_

BIN
src/blkid Executable file

Binary file not shown.

13
src/drive.cpp Normal file
View File

@ -0,0 +1,13 @@
/**
* @file drive.cpp
* @brief
* @author hendrik schutter
* @date
*/
#include "drive.h"
Drive::Drive(void) {
cout << "created drive" << endl;
}

40
src/drive.h Normal file
View File

@ -0,0 +1,40 @@
/**
* @file drive.h
* @brief represent
* @author hendrik schutter
* @date 16.12.2017
*/
#ifndef DRIVE_H_
#define DRIVE_H_
#include "refurbishingHddTool.h"
struct drive
{
string sPath;
string sManufacturer;
string sModel;
string sSerial;
string sCapacity;
uint32_t u32ErrorCount;
uint32_t u32PowerOnHours;
uint32_t u32SpinUpCount;
};
class Drive {
protected:
public:
Drive(void);
};
#endif // DRIVE_H_

View File

BIN
src/hwinfo Executable file

Binary file not shown.

3
src/ignoreDrives.conf Normal file
View File

@ -0,0 +1,3 @@
/dev/sda:508ef27d-5039-4e8b-9e2c-22d7528b7149
/dev/sdb:07f4ad14-c4b6-46e7-9cdf-3cfa9841d53d
/dev/sdc:4673974d-1af2-44fd-996b-a2d8e4c43d9a

25
src/main.cpp Normal file
View File

@ -0,0 +1,25 @@
/**
* @file main.cpp
* @brief app entry point
* @author hendrik schutter
* @date 01.05.2020
*/
#include "refurbishingHddTool.h"
#include "app.h"
/**
* \brief app entry point
* \param void
* \return Status-Code
*/
int main(void)
{
cout << "refurbishingHddTool" << endl;
App* app = new App();
app->app_logic();
return EXIT_SUCCESS;
}

View File

@ -1,110 +1,49 @@
#include <stdio.h>
#include <string.h>
#include <iostream>
#include <fstream>
using namespace std;
struct deviceData{
string model;
string manufacturer;
short rotationRate; //in rpm
short capacity; //in Gigabytes
};
string removeLastNewLine(string s);
deviceData getDeviceData(string path);
/*
*
* clear && g++ -Wall -o test mainTest.cpp
*
*/
int main(void)
{
cout << "Hello World!\n";
char * cLine = NULL;
string path;
size_t len = 0;
int loop = 0;
int devicesSize = 0;
string devices[5];
FILE* outputfileHwinfo = popen("hwinfo --short --disk", "r");
if (outputfileHwinfo == NULL) {
exit(EXIT_FAILURE);
}
while ((getline(&cLine, &len, outputfileHwinfo)) != -1) {
if(loop > 0) {
string line = string(cLine);
path = line.substr (2,8);
devices[devicesSize] = path;
devicesSize++;
}
loop++;
}
fclose(outputfileHwinfo);
cout << "Model: " << getDeviceData(devices[0]) << endl;
return 0;
}
deviceData getDeviceData(string path) {
struct deviceData hdd;
size_t len = 0; //lenght of found line
char * cLine = NULL; //found line
string comand = ("smartctl -a " + path);
string comand = ("./smartctl -a " + path);
const char *cComand = comand.c_str();
FILE* outputfileSmart = popen(cComand, "r");
while ((getline(&cLine, &len, outputfileSmart)) != -1) {
string line = string(cLine);
cout << line << "XYZ\n";
string search ("Device Model:");
size_t found = line.find(search);
if (found!=string::npos) {
int lenght = line.length(); //lenght of line
string str3 (":");
found = line.find(str3);
//cout << "Found1: " << found << endl;
cout << "Found1: " << found << endl;
for(int i = (found+1); i < lenght; i++) {
if(line[i] != ' ') {
// cout << i << endl;
cout << i << endl;
found = i;
break;
}
@ -117,17 +56,17 @@ deviceData getDeviceData(string path) {
model = removeLastNewLine(model);
// cout << model << endl;
cout << model << endl;
return model;
return hdd;
}
}
fclose(outputfileSmart);
return "error";
return hdd;
}
@ -139,3 +78,4 @@ string removeLastNewLine(string s) {
}
return s;
}
*/

View File

@ -1,133 +0,0 @@
#include <stdio.h>
#include <string.h>
#include <iostream>
#include <fstream>
using namespace std;
string removeLastNewLine(string s);
string getModel(string path);
/*
*
* g++ -Wall -o cctv_radar cctv_radar.cpp mcp3008Spi.cpp logger.cpp libftpclient.a -lcurl -lwiringPi -pthread
*
*/
int main(void)
{
cout << "Hello World!\n";
char * cLine = NULL;
string path;
size_t len = 0;
int loop = 0;
int devicesSize = 0;
string devices[5];
FILE* outputfileHwinfo = popen("hwinfo --short --disk", "r");
if (outputfileHwinfo == NULL) {
exit(EXIT_FAILURE);
}
while ((getline(&cLine, &len, outputfileHwinfo)) != -1) {
if(loop > 0) {
string line = string(cLine);
path = line.substr (2,8);
devices[devicesSize] = path;
devicesSize++;
}
loop++;
}
fclose(outputfileHwinfo);
cout << "Model: " << getModel(devices[0]) << endl;
return 0;
}
string getModel(string path){
size_t len = 0;
char * cLine = NULL;
string comand = ("smartctl -a " + path);
const char *cComand = comand.c_str();
FILE* outputfileSmart = popen(cComand, "r");
while ((getline(&cLine, &len, outputfileSmart)) != -1) {
string line = string(cLine);
//cout << line << endl;
string str2 ("Device Model:");
size_t found = line.find(str2);
if (found!=string::npos) {
// cout << "Device Model: at: " << found << '\n';
// cout << line << endl;
int size = line.length();
string str3 (":");
found = line.find(str3);
//cout << "Found1: " << found << endl;
for(int i = (found+1); i < size; i++) {
if(line[i] != ' ') {
// cout << i << endl;
found = i;
break;
}
}
string model = line.substr (found, size);
model = removeLastNewLine(model);
// cout << model << endl;
return model;
}
}
return "error";
}
string removeLastNewLine(string s) {
if (!s.empty() && s[s.length()-1] == '\n') {
s.erase(s.length()-1);
}
return s;
}

14
src/makefile Normal file
View File

@ -0,0 +1,14 @@
refurbishingHddTool: main.o app.o drive.o
g++ -Wall -o refurbishingHddTool main.o app.o drive.o
main.o: main.cpp
g++ -c main.cpp
app.o: app.cpp
g++ -c app.cpp
drive.o: drive.cpp
g++ -c drive.cpp
clean :
rm refurbishingHddTool main.o app.o drive.o

BIN
src/refurbishingHddTool Executable file

Binary file not shown.

View File

@ -0,0 +1,21 @@
/**
* @file refurbishingHddTool.h
* @brief represent
* @author hendrik schutter
* @date 01.05.2020
*/
#ifndef REFURBISHING_HDD_TOOL_H_
#define REFURBISHING_HDD_TOOL_H_
#include <list>
#include <iostream>
#include <string>
#include <fstream>
#include <tuple>
#include <vector>
using namespace std;
#endif // REFURBISHING_HDD_TOOL_H_

BIN
src/smartctl Executable file

Binary file not shown.

BIN
src/test

Binary file not shown.