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

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

3
.gitignore vendored
View File

@ -32,3 +32,6 @@
*.out
*.app
# Astyle
*.orig

View File

@ -0,0 +1 @@
<mxfile host="app.diagrams.net" modified="2020-04-30T20:05:53.432Z" agent="5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/81.0.4044.92 Safari/537.36" etag="5yI57G0tj_AETib5-SCg" version="13.0.4" type="device"><diagram id="C5RBs43oDa-KdzZeNtuy" name="Page-1">7VttU+o4FP41zOx+kGnTF+CjgKh3vczdxV31050Ioc1aGkyDiL9+kzalL6mXqlOCrDMONqdpSM85zzlPTkLLGiyezylc+t/JDAUtYMyeW9awBYBpA9ASf8Zsk0jcrpUIPIpnslMmmOAXJIWGlK7wDEWFjoyQgOFlUTglYYimrCCDlJJ1sducBMVvXUIPKYLJFAaq9AbPmJ9Iu6CTyS8Q9vz0m023l9xZwLSzfJPIhzOyzomss5Y1oISw5GrxPECBUF6ql5vLzU1w9eCef/szeoR/9/+4Hv9zkgw2essj21egKGTvHtpe/bx9PJsPfl6ML+l4fHUz8V5O5LtGbJPqC824+mSTUOYTj4QwOMukfUpW4QyJUQ3eyvpcEbLkQpML/0WMbaQvwBUjXOSzRSDv8regm1vxfNtJm3dyuLgxfC60NrKVzFVMsGTaHXqR/SKyolP0C2VIOzNIPcR+0c/aGp+jBpEF4pPkz1EUQIafipOD0n29bb/MRPxCWukNFpOTfILBSn7TD0o8ChcRnzdTzRkEHGnCbGsfMzRZwlgFaw72olHksIgy9PwO9arqkKNYHelj6wx9piHfws8hzzUa0phpqlr5/zq5VdPJTaParPvxckvx8hDxK+AGfM79e8qvPBZry4UL4cnhfST+XQyHYjoL/vFj0LJGVU+UfIH6ZHG/inYjZBv/hZnmJGQjuMCBsNsFCp4Qw1Mob0inMLn++jDAXsgbU24+RMUwjJKHb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C7Fm67ORXQfsi+y+C/Z2if51zX0AGbzCrOsRauNE3DQPnVmXy8L6mbWr1g/GCKslQq4IVtSR8tMdoS48hcGpvLHAs1mCdRThF3gfDyUgKB2Qj+v0W85QjMXhHcnzUQ2q30lVm6aodD2XU3+q6bz2QWPaV73+GzxO3dtOiR701J+RVB3jfIfueTP73XESoLJfb1tn/wE=</diagram></mxfile>

Binary file not shown.

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.

8
vcCodium.code-workspace Normal file
View File

@ -0,0 +1,8 @@
{
"folders": [
{
"path": "."
}
],
"settings": {}
}