Qt linux获取cpu使用率、内存、网络收发速度、磁盘读写速度、磁盘剩余空间等,实际上大部分都和qt无关的,用其他语言也可以获取。
code:
.h
#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
.cpp
#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")
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")
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
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
}
效果:

示例代码(Qt5.9工程):http://download.csdn.net/download/yangyang031213/10204938
https://github.com/yangyang0312/cpp/tree/master/Qt/resource_minitor