-
Notifications
You must be signed in to change notification settings - Fork 55
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Qt linux获取cpu使用率、内存、网络收发速度、磁盘读写速度、磁盘剩余空间等
- Loading branch information
1 parent
7494efa
commit 0b54ede
Showing
8 changed files
with
630 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
#include "mainwindow.h" | ||
#include <QApplication> | ||
|
||
int main(int argc, char *argv[]) | ||
{ | ||
QApplication a(argc, argv); | ||
MainWindow w; | ||
w.show(); | ||
|
||
return a.exec(); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
#include "mainwindow.h" | ||
#include "ui_mainwindow.h" | ||
|
||
MainWindow::MainWindow(QWidget *parent) : | ||
QMainWindow(parent), | ||
ui(new Ui::MainWindow) | ||
{ | ||
ui->setupUi(this); | ||
|
||
p_res = new resource_minitor(this); | ||
} | ||
|
||
MainWindow::~MainWindow() | ||
{ | ||
delete ui; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
#ifndef MAINWINDOW_H | ||
#define MAINWINDOW_H | ||
|
||
#include <QMainWindow> | ||
#include "resource_minitor.h" | ||
|
||
namespace Ui { | ||
class MainWindow; | ||
} | ||
|
||
class MainWindow : public QMainWindow | ||
{ | ||
Q_OBJECT | ||
|
||
public: | ||
explicit MainWindow(QWidget *parent = 0); | ||
~MainWindow(); | ||
|
||
private: | ||
Ui::MainWindow *ui; | ||
|
||
resource_minitor * p_res; | ||
}; | ||
|
||
#endif // MAINWINDOW_H |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
<ui version="4.0"> | ||
<class>MainWindow</class> | ||
<widget class="QMainWindow" name="MainWindow" > | ||
<property name="geometry" > | ||
<rect> | ||
<x>0</x> | ||
<y>0</y> | ||
<width>400</width> | ||
<height>300</height> | ||
</rect> | ||
</property> | ||
<property name="windowTitle" > | ||
<string>MainWindow</string> | ||
</property> | ||
<widget class="QMenuBar" name="menuBar" /> | ||
<widget class="QToolBar" name="mainToolBar" /> | ||
<widget class="QWidget" name="centralWidget" /> | ||
<widget class="QStatusBar" name="statusBar" /> | ||
</widget> | ||
<layoutDefault spacing="6" margin="11" /> | ||
<pixmapfunction></pixmapfunction> | ||
<resources/> | ||
<connections/> | ||
</ui> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,144 @@ | ||
#include "resource_minitor.h" | ||
#include "sys/statfs.h" | ||
|
||
resource_minitor::resource_minitor(QObject *parent) : QObject(parent) | ||
{ | ||
connect(&monitor_timer__, &QTimer::timeout, this, &resource_minitor::get_resource__); | ||
monitor_timer__.start(m_timer_interval__); | ||
} | ||
|
||
void resource_minitor::get_resource__() | ||
{ | ||
get_cpu_usage__ (); | ||
get_disk_speed__(); | ||
get_mem_usage__ (); | ||
get_net_usage__ (); | ||
get_disk_space__(); | ||
get_path_space("/"); | ||
qDebug()<<"\n"; | ||
} | ||
|
||
bool resource_minitor::get_mem_usage__() | ||
{ | ||
QProcess process; | ||
process.start("free -m"); //使用free完成获取 | ||
process.waitForFinished(); | ||
process.readLine(); | ||
QString str = process.readLine(); | ||
str.replace("\n",""); | ||
str.replace(QRegExp("( ){1,}")," ");//将连续空格替换为单个空格 用于分割 | ||
auto lst = str.split(" "); | ||
if(lst.size() > 6) | ||
{ | ||
qDebug("mem total:%.0lfMB free:%.0lfMB",lst[1].toDouble(),lst[6].toDouble()); | ||
return true; | ||
} | ||
return false; | ||
} | ||
|
||
bool resource_minitor::get_net_usage__() | ||
{ | ||
QProcess process; | ||
process.start("cat /proc/net/dev"); //读取文件/proc/net/dev获取网络收发包数量,再除取样时间得到网络速度 | ||
process.waitForFinished(); | ||
process.readLine(); | ||
process.readLine(); | ||
while(!process.atEnd()) | ||
{ | ||
QString str = process.readLine(); | ||
str.replace("\n",""); | ||
str.replace(QRegExp("( ){1,}")," "); | ||
auto lst = str.split(" "); | ||
if(lst.size() > 9 && lst[0] == "enp2s0:") | ||
{ | ||
double recv = 0; | ||
double send = 0; | ||
if(lst.size() > 1) | ||
recv = lst[1].toDouble(); | ||
if(lst.size() > 9) | ||
send = lst[9].toDouble(); | ||
qDebug("%s 接收速度:%.0lfbyte/s 发送速度:%.0lfbyte/s",lst[0].toStdString().c_str(),(recv - m_recv_bytes__) / (m_timer_interval__ / 1000.0),(send - m_send_bytes__) / (m_timer_interval__ / 1000.0)); | ||
m_recv_bytes__ = recv; | ||
m_send_bytes__ = send; | ||
} | ||
} | ||
return true; | ||
} | ||
|
||
bool resource_minitor::get_cpu_usage__() | ||
{ | ||
QProcess process; | ||
process.start("cat /proc/stat"); | ||
process.waitForFinished(); | ||
QString str = process.readLine(); | ||
str.replace("\n",""); | ||
str.replace(QRegExp("( ){1,}")," "); | ||
auto lst = str.split(" "); | ||
if(lst.size() > 3) | ||
{ | ||
double use = lst[1].toDouble() + lst[2].toDouble() + lst[3].toDouble(); | ||
double total = 0; | ||
for(int i = 1;i < lst.size();++i) | ||
total += lst[i].toDouble(); | ||
if(total - m_cpu_total__ > 0) | ||
{ | ||
qDebug("cpu rate:%.2lf%%",(use - m_cpu_use__) / (total - m_cpu_total__) * 100.0); | ||
m_cpu_total__ = total; | ||
m_cpu_use__ = use; | ||
return true; | ||
} | ||
} | ||
return false; | ||
} | ||
|
||
bool resource_minitor::get_disk_speed__() | ||
{ | ||
QProcess process; | ||
process.start("iostat -k -d"); | ||
process.waitForFinished(); | ||
process.readLine(); | ||
process.readLine(); | ||
process.readLine(); | ||
QString str = process.readLine(); | ||
str.replace("\n",""); | ||
str.replace(QRegExp("( ){1,}")," "); | ||
auto lst = str.split(" "); | ||
if(lst.size() > 5) | ||
{ | ||
qDebug("disk read:%.0lfkb/s disk write:%.0lfkb/s",(lst[4].toDouble() - m_disk_read__ ) / (m_timer_interval__ / 1000.0),(lst[5].toDouble() - m_disk_write__) / (m_timer_interval__ / 1000.0)); | ||
m_disk_read__ = lst[4].toDouble(); | ||
m_disk_write__ = lst[5].toDouble(); | ||
return true; | ||
} | ||
return false; | ||
} | ||
|
||
bool resource_minitor::get_disk_space__() | ||
{ | ||
QProcess process; | ||
process.start("df -k"); | ||
process.waitForFinished(); | ||
process.readLine(); | ||
while(!process.atEnd()) | ||
{ | ||
QString str = process.readLine(); | ||
if(str.startsWith("/dev/sda")) | ||
{ | ||
str.replace("\n",""); | ||
str.replace(QRegExp("( ){1,}")," "); | ||
auto lst = str.split(" "); | ||
if(lst.size() > 5) | ||
qDebug("挂载点:%s 已用:%.0lfMB 可用:%.0lfMB",lst[5].toStdString().c_str(),lst[2].toDouble()/1024.0,lst[3].toDouble()/1024.0); | ||
} | ||
} | ||
return true; | ||
} | ||
|
||
bool resource_minitor::get_path_space(const QString & path) | ||
{ | ||
struct statfs diskInfo; | ||
statfs(path.toUtf8().data(), &diskInfo); | ||
qDebug("%s 总大小:%.0lfMB 可用大小:%.0lfMB",path.toStdString().c_str(),(diskInfo.f_blocks * diskInfo.f_bsize)/1024.0/1024.0,(diskInfo.f_bavail * diskInfo.f_bsize)/1024.0/1024.0); | ||
return true; | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
#ifndef RESOURCE_MINITOR_H | ||
#define RESOURCE_MINITOR_H | ||
|
||
#include <QObject> | ||
#include <QTimer> | ||
#include <QProcess> | ||
#include <QDebug> | ||
|
||
class resource_minitor : public QObject | ||
{ | ||
Q_OBJECT | ||
public: | ||
|
||
explicit resource_minitor(QObject *parent = nullptr); | ||
|
||
private slots: | ||
void get_resource__(); | ||
|
||
private: | ||
bool get_mem_usage__(); | ||
bool get_net_usage__(); | ||
bool get_disk_speed__(); | ||
bool get_cpu_usage__(); | ||
bool get_disk_space__(); | ||
bool get_path_space(const QString & path); | ||
|
||
private: | ||
const int m_timer_interval__ = 1000; | ||
QTimer monitor_timer__; | ||
double m_send_bytes__ = 0; | ||
double m_recv_bytes__ = 0; | ||
double m_disk_read__ = 0; | ||
double m_disk_write__ = 0; | ||
double m_cpu_total__ = 0; | ||
double m_cpu_use__ = 0; | ||
}; | ||
|
||
#endif // RESOURCE_MINITOR_H |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
#------------------------------------------------- | ||
# | ||
# Project created by QtCreator 2018-01-10T13:07:58 | ||
# | ||
#------------------------------------------------- | ||
|
||
QT += core gui | ||
|
||
greaterThan(QT_MAJOR_VERSION, 4): QT += widgets | ||
|
||
TARGET = resource_minitor | ||
TEMPLATE = app | ||
|
||
# The following define makes your compiler emit warnings if you use | ||
# any feature of Qt which has been marked as deprecated (the exact warnings | ||
# depend on your compiler). Please consult the documentation of the | ||
# deprecated API in order to know how to port your code away from it. | ||
DEFINES += QT_DEPRECATED_WARNINGS | ||
|
||
# You can also make your code fail to compile if you use deprecated APIs. | ||
# In order to do so, uncomment the following line. | ||
# You can also select to disable deprecated APIs only up to a certain version of Qt. | ||
#DEFINES += QT_DISABLE_DEPRECATED_BEFORE=0x060000 # disables all the APIs deprecated before Qt 6.0.0 | ||
|
||
|
||
SOURCES += \ | ||
main.cpp \ | ||
mainwindow.cpp \ | ||
resource_minitor.cpp | ||
|
||
HEADERS += \ | ||
mainwindow.h \ | ||
resource_minitor.h | ||
|
||
FORMS += \ | ||
mainwindow.ui |
Oops, something went wrong.