-
Notifications
You must be signed in to change notification settings - Fork 0
/
ltm.cpp
389 lines (262 loc) · 12.7 KB
/
ltm.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
#include "ltm.h"
#include "ui_ltm.h"
#include <memory>
#include <iostream>
#include "cpustats.h"
#include <QThread>
#include <qwt_curve_fitter.h>
#include "cpuitemwidget.h"
#include <QVector>
#include "cpustatreader.h"
#include "processstatreader.h"
#include <qwt_scale_engine.h>
#include "ramitemwidget.h"
#include <QProcess>
#include <QDir>
#include <cmath>
#include <cstdlib>
LTM::LTM(QWidget *parent)
: QMainWindow(parent)
, ui(new Ui::LTM)
, updateprocess(nullptr)
{
ui->setupUi(this);
connect(ui->actionUpdate,&QAction::triggered,this, <M::selfUpdate);
connect(ui->actionInfo,&QAction::triggered,this, <M::printInfo);
//setting up plots
try {
setup_cpu_plots();
setup_network_plots();
setup_ram_plots();
setup_disk_plots();
for(double i=0.0; i<600; i++){
xAchsisBase600.push_back(i);
}
} catch (std::exception& e) {
qDebug() << "execption when setting up plots: " << e.what();
} catch (...){
qDebug() << "unknown execption occured while setting up plots";
}
try {
m_CPUStatReaderT.start();
} catch (std::exception& e) {
qDebug() << "execption when initialising CPUreader: " << e.what();
} catch (...){
qDebug() << "unknown execption occured when initialising CPUreader";
}
qDebug()<< "cpu stat reader started";
try {
m_RamStatReaderT.start();
} catch (std::exception& e) {
qDebug() << "execption when initialising RamStatReader: " << e.what();
} catch (...){
qDebug() << "unknown execption occured when initialising RamStatReader";
}
qDebug()<< "ram stat reader started";
try {
m_DiskStatReaderT.start();
} catch (std::exception& e) {
qDebug() << "execption when DiskStatReader: " << e.what();
} catch (...){
qDebug() << "unknown execption occured when initialising DiskStatReader";
}
qDebug()<< "disk stat reader started";
try {
m_NetworkStatReaderT.start();
} catch (std::exception& e) {
qDebug() << "execption when NetworkStatReader: " << e.what();
} catch (...){
qDebug() << "unknown execption occured when initialising NetworkStatReader";
}
qDebug()<< "network stat reader started";
try {
m_ProcessStatReaderT.start();
} catch (std::exception& e) {
qDebug() << "execption when ProcessStatReader: " << e.what();
} catch (...){
qDebug() << "unknown execption occured when initialising ProcessStatReader";
}
qDebug()<< "process stat reader started";
//assembling the left ItemWidget with currently present devices
qDebug()<< "assembling the left ItemWidget with currently present devices";
try {
qDebug()<< "creating CPU entry";
m_cpuItemWidgetPtr = new CPUItemWidget("CPU",ui->listWidget);
qDebug()<< "creating RAM entry";
m_RamItemWidgetPtr = new RamItemWidget("RAM",ui->listWidget);
qDebug()<< "creating DISK entries";
for(int i=0; i<m_DiskStatReaderT.getDeviceCount(); i++){
m_DiskItemWidgetPtrVec.push_back(new DiskItemWidget(m_DiskStatReaderT.getStatData(i).diskType,m_DiskStatReaderT.getStatData(i).diskName,ui->listWidget));
}
qDebug()<< "creating Network entries";
for(int i=0; i<m_NetworkStatReaderT.getDeviceCount(); i++){
m_EthItemWidgetPtrVec.push_back(new NetworkItemWidget(m_NetworkStatReaderT.getStatData(i).AdapterName,ui->listWidget));
}
ui->listWidget->item(0)->setSelected(true);
on_listWidget_itemClicked(ui->listWidget->item(0));
} catch (std::exception& e) {
qDebug() << "execption when assembling ItemWidget: " << e.what();
} catch (...){
qDebug() << "unknown execption occured when assembling itemWidget";
}
if(connect(&m_CPUStatReaderT,&CPUStatReader::data_ready,this,<M::plot_cpu_activity ,Qt::ConnectionType::QueuedConnection))
qDebug()<< "cpu stat reader connected";
if(connect(&m_RamStatReaderT,&RamStatReader::data_ready,this,<M::plot_ram_activity ,Qt::ConnectionType::QueuedConnection))
qDebug()<< "ram stat reader connected";
if(connect(&m_DiskStatReaderT,&DiskStatReader::data_ready,this,<M::plot_disk_activity ,Qt::ConnectionType::QueuedConnection))
qDebug()<< "disk stat reader connected";
if(connect(&m_NetworkStatReaderT,&NetworkStatReader::data_ready,this,<M::plot_network_activity ,Qt::ConnectionType::QueuedConnection))
qDebug()<< "network stat reader connected";
if(connect(&m_ProcessStatReaderT,&ProcessStatReader::data_ready,this,<M::handle_process_data ,Qt::ConnectionType::QueuedConnection))
qDebug()<< "process stat reader connected";
}
LTM::~LTM()
{
delete ui;
}
void LTM::deselectAllCategories()
{
}
void LTM::on_listWidget_itemClicked(QListWidgetItem *item){
auto perfItemWidetPtr = dynamic_cast<PerfItemWidget*>(item);
switch(perfItemWidetPtr->getPlotType()){
case PlotType::CPU : ui->stackedWidget->setCurrentIndex(0); break;
case PlotType::RAM : ui->stackedWidget->setCurrentIndex(1); break;
case PlotType::DISK : ui->stackedWidget->setCurrentIndex(2); break;
case PlotType::Ethernet : ui->stackedWidget->setCurrentIndex(3); break;
case PlotType::WIFI : ui->stackedWidget->setCurrentIndex(4); break;
case PlotType::GPU : ui->stackedWidget->setCurrentIndex(5); break;
}
}
void LTM::selfUpdate()
{
ui->stackedWidget->setCurrentIndex(4);
ui->tabWidget->setCurrentIndex(0);
updateprocess = new QProcess(this);
//ui->updateText->show();
qDebug() << "App path : " << qApp->applicationDirPath();
//QString binDirectory = QFileInfo(qApp->applicationDirPath()).dir().absolutePath();
if(! QFile(qApp->applicationDirPath()+"/appimageupdatetool-x86_64.AppImage").exists()) ui->updateText->appendPlainText("ERROR: FAILED appimageupdate not packaged");
// else if(! QFile("/usr/appimageupdatetool-x86_64.AppImage").exists()) ui->updateText->appendPlainText("ERROR: FAILED appimageupdate tool not isntalled");
std::cout << "$APPIMAGE:" << qgetenv("APPIMAGE").toStdString() << std::endl;
updateprocess->setReadChannelMode(QProcess::ProcessChannelMode::MergedChannels);
// updateprocess->start("./usr/AppImageUpdate.AppImage");
ui->updateText->appendPlainText("starting updater...");
qApp->processEvents();
updateprocess->start(qApp->applicationDirPath()+"/appimageupdatetool-x86_64.AppImage",QStringList() <<qgetenv("APPIMAGE"));
//updateprocess->start("/usr/appimageupdatetool-x86_64.AppImage",QStringList() <<qgetenv("APPIMAGE"));
updateprocess->waitForStarted(1000);
while(updateprocess->state() == QProcess::ProcessState::Running){
if( updateprocess->waitForReadyRead(2000)){
QString appImageUpdatePath = updateprocess->readAllStandardOutput();
appImageUpdatePath.replace("\r","");
ui->updateText->appendPlainText(appImageUpdatePath);
qApp->processEvents();
}
}
updateprocess->waitForFinished(500);
QString appImageUpdatePath = updateprocess->readAllStandardOutput();
// QString appImageErr= updateprocess->readAllStandardError();
qDebug()<< appImageUpdatePath;// << appImageErr;
// ui->okButton->show();
// while(1)
//auto state= updateprocess->state();
}
void LTM::printInfo()
{
ui->updateText->clear();
ui->stackedWidget->setCurrentIndex(4);
ui->tabWidget->setCurrentIndex(0);
ui->updateText->appendPlainText("Authored by Juergen Kratochwill 2020");
ui->updateText->appendPlainText("Published under GPLv3");
ui->updateText->appendPlainText("All Linux and QT related Licesing info can be accesed under: ");
QDir license(qApp->applicationDirPath());
license.cdUp();
license.cd("share");
license.cd("doc");
ui->updateText->appendPlainText(license.path());
ui->updateText->appendPlainText(" ");
ui->updateText->appendPlainText("Additonal Attributions:");
ui->updateText->appendPlainText("QDarkStyleSheet Project by Colin Duquesnoy: https://github.com/ColinDuquesnoy/QDarkStyleSheet");
ui->updateText->appendPlainText("Icons from: https://www.flaticon.com/Freepik");
ui->updateText->appendPlainText("Net I/O pre process nethgos/libnethog from https://github.com/raboof/nethogs/ ");
}
void LTM::on_butUpdateOK_clicked()
{
on_listWidget_itemClicked(ui->listWidget->currentItem());
}
void LTM::handle_process_data()
{
try{
auto& dataVec = m_ProcessStatReaderT.m_DataVec;
QItemSelectionModel *select = ui->tableWProcess->selectionModel();
//int curSelectedRow = select->selectedRows().first().row();
int currentSelectedPID = 0;
if(select->selectedRows().size()>0){
int curSelectedRow= select->selectedRows().first().row();
currentSelectedPID = ui->tableWProcess->item(curSelectedRow,0)->data(Qt::DisplayRole).toInt();
}
else {
currentSelectedPID =-1;
}
std::lock_guard<std::mutex> lck(m_ProcessStatReaderT.m_DataVecMutex);
ui->tableWProcess->setRowCount(dataVec.size());
//must be a better way than rewriting the table...
ui->tableWProcess->setSortingEnabled(false);
//ui->tableWProcess->clear();
constexpr int colums = 7;
for(int pr=0; pr<dataVec.size();pr++){
if(ui->tableWProcess->item(pr,0)==nullptr) {//does not exist yet -> create whole line
for(int k=0; k<colums; k++){
QTableWidgetItem *newItem = new QTableWidgetItem();
ui->tableWProcess->setItem(pr, k, newItem);
ui->tableWProcess->item(pr,k)->setTextAlignment(Qt::AlignVCenter | Qt::AlignRight);
}
}
ui->tableWProcess->item(pr,0)->setData(Qt::DisplayRole,dataVec[pr].pid);
ui->tableWProcess->setColumnWidth(0,60);
ui->tableWProcess->item(pr,1)->setData(Qt::DisplayRole,dataVec[pr].user);
ui->tableWProcess->setColumnWidth(1,80);
ui->tableWProcess->item(pr,2)->setData(Qt::DisplayRole,dataVec[pr].currentCPUPct );
// ui->tableWProcess->item(pr,2)->setTextAlignment(Qt::AlignVCenter | Qt::AlignRight);
ui->tableWProcess->setColumnWidth(2,70);
// ui->tableWProcess->item(pr,3)->setTextAlignment(Qt::AlignVCenter | Qt::AlignRight);
ui->tableWProcess->item(pr,3)->setData(Qt::DisplayRole,dataVec[pr].currentMemKiB/1024);
ui->tableWProcess->setColumnWidth(3,100);
ui->tableWProcess->item(pr,4)->setData(Qt::DisplayRole,(dataVec[pr].currentReadKiBsDiskData+dataVec[pr].currentWriteKiBsDiskData)/1024);
ui->tableWProcess->setColumnWidth(4,100);
ui->tableWProcess->item(pr,5)->setData(Qt::DisplayRole,(dataVec[pr].currentReadKiBsNetData+dataVec[pr].currentWriteKiBsNetData)/1024);
ui->tableWProcess->setColumnWidth(5,100);
ui->tableWProcess->item(pr,6)->setData(Qt::DisplayRole,dataVec[pr].name);
ui->tableWProcess->setColumnWidth(6,150);
}
ui->tableWProcess->setSortingEnabled(true); //quickly sort it
ui->tableWProcess->setSortingEnabled(false); //disable sorting again and adjust cell formants
ui->tableWProcess->horizontalHeader()->setSortIndicatorShown(true);
for(int pr=0; pr<dataVec.size();pr++){
//fix selection
if(ui->tableWProcess->item(pr,0)->data(Qt::DisplayRole).toInt()==currentSelectedPID){
//select row again
ui->tableWProcess->selectRow(pr);
//select->row
}
ui->tableWProcess->item(pr,3)->setData(Qt::DisplayRole,QString::number(ui->tableWProcess->item(pr,3)->data(Qt::DisplayRole).toFloat(),'f',1) + " MiB");
ui->tableWProcess->item(pr,2)->setData(Qt::DisplayRole,QString::number(ui->tableWProcess->item(pr,2)->data(Qt::DisplayRole).toFloat()*100,'f',1) + QString("%"));
ui->tableWProcess->item(pr,4)->setData(Qt::DisplayRole,QString::number(ui->tableWProcess->item(pr,4)->data(Qt::DisplayRole).toFloat(),'f',1) + QString(" MiB/s"));
if(!m_ProcessStatReaderT.noRootPriv)
ui->tableWProcess->item(pr,5)->setData(Qt::DisplayRole,QString::number(ui->tableWProcess->item(pr,5)->data(Qt::DisplayRole).toFloat(),'f',1) + QString(" MiB/s"));
else ui->tableWProcess->item(pr,5)->setText("need root");
}
// throw std::runtime_error("test process aseembling exception");
} catch (std::exception& e) {
qDebug() << "execption when assembling process data: " << e.what();
} catch (...){
qDebug() << "unknown execption occured when assembling process data";
}
}
void LTM::on_butKillProcess_clicked()
{
std::lock_guard<std::mutex> lck(m_ProcessStatReaderT.m_DataVecMutex); //jsut to be sure not to get current selcted item while table is being rebuilt
QString terminateID = ui->tableWProcess->selectedItems()[0]->data(Qt::DisplayRole).toString();
system((std::string("kill -9 ")+ terminateID.toStdString()).c_str());
}