2024-01-25 18:37:25 +08:00
|
|
|
|
#include "HxSystem.h"
|
2024-01-11 16:14:21 +08:00
|
|
|
|
#include "HxProcess.h"
|
|
|
|
|
|
|
|
|
|
#include <QSettings>
|
2024-01-21 22:06:55 +08:00
|
|
|
|
#ifdef Q_OS_LINUX
|
2024-01-11 16:14:21 +08:00
|
|
|
|
#include <unistd.h>
|
2024-01-21 22:06:55 +08:00
|
|
|
|
#endif
|
2024-01-11 16:14:21 +08:00
|
|
|
|
|
|
|
|
|
static double m_memory_total = 0;
|
|
|
|
|
static double m_cpu_total = 0, m_cpu_use = 0;
|
|
|
|
|
static QMap<QString, QList<double>> m_disk_rw_speed;
|
|
|
|
|
|
|
|
|
|
QString HxSystem::get_cpu_model()
|
|
|
|
|
{
|
|
|
|
|
#ifdef Q_OS_LINUX
|
|
|
|
|
return HxProcess::start("cat /proc/cpuinfo | grep name | cut -f2 -d: | uniq -c").simplified().remove(0, 1);
|
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
|
#ifdef Q_OS_WIN
|
|
|
|
|
QSettings *CPU = new QSettings("HKEY_LOCAL_MACHINE\\HARDWARE\\DESCRIPTION\\System\\CentralProcessor\\0", QSettings::NativeFormat);
|
|
|
|
|
return CPU->value("ProcessorNameString").toString();
|
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
|
return "";
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
int HxSystem::get_logical_cpu_number(){return HxProcess::start("cat /proc/cpuinfo| grep \"processor\"| wc -l").toInt();}
|
|
|
|
|
|
|
|
|
|
bool HxSystem::get_cpu_temp(double *temp)
|
|
|
|
|
{
|
|
|
|
|
Q_UNUSED(temp);
|
|
|
|
|
|
|
|
|
|
#ifdef Q_OS_LINUX
|
|
|
|
|
auto result = HxProcess::start("cat /sys/class/thermal/thermal_zone0/temp");
|
|
|
|
|
|
|
|
|
|
auto data = result.simplified();
|
|
|
|
|
|
|
|
|
|
if(data.isNull())
|
|
|
|
|
return false;
|
|
|
|
|
|
|
|
|
|
*temp = data.toDouble() / 1000;
|
|
|
|
|
|
|
|
|
|
return true;
|
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
bool HxSystem::get_cpu_status(double *cpu_rate)
|
|
|
|
|
{
|
|
|
|
|
Q_UNUSED(cpu_rate);
|
|
|
|
|
Q_UNUSED(m_cpu_use);
|
|
|
|
|
Q_UNUSED(m_cpu_total);
|
|
|
|
|
|
|
|
|
|
#ifdef Q_OS_LINUX
|
|
|
|
|
auto result = HxProcess::start("cat /proc/stat");
|
|
|
|
|
|
|
|
|
|
auto list = result.split("\n")[0].simplified().split(" ");
|
|
|
|
|
|
|
|
|
|
if(list.size() > 3)
|
|
|
|
|
{
|
|
|
|
|
double use = list[1].toDouble() + list[2].toDouble() + list[3].toDouble();
|
|
|
|
|
|
|
|
|
|
double total = 0;
|
|
|
|
|
|
|
|
|
|
for(int i = 1; i < list.size(); i++)
|
|
|
|
|
total += list[i].toDouble();
|
|
|
|
|
|
|
|
|
|
if(total - m_cpu_total > 0)
|
|
|
|
|
{
|
|
|
|
|
*cpu_rate = (use - m_cpu_use) / (total - m_cpu_total) * 100.0;
|
|
|
|
|
m_cpu_total = total;
|
|
|
|
|
m_cpu_use = use;
|
|
|
|
|
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
bool HxSystem::get_memory_status(double *memory_use, double *memory_total, double *swap_use, double *swap_total)
|
|
|
|
|
{
|
|
|
|
|
auto result = HxProcess::start("free -m");
|
|
|
|
|
|
|
|
|
|
auto list = result.split("\n");
|
|
|
|
|
|
|
|
|
|
if(list.size() >= 3)
|
|
|
|
|
{
|
|
|
|
|
auto memory_list = list[1].simplified().split(" ");
|
|
|
|
|
auto swap_list = list[2].simplified().split(" ");
|
|
|
|
|
|
|
|
|
|
if(memory_list.size() >=7)
|
|
|
|
|
{
|
|
|
|
|
*memory_use = memory_list[2].toDouble();
|
|
|
|
|
*memory_total = memory_list[1].toDouble();
|
|
|
|
|
|
|
|
|
|
m_memory_total = *memory_total;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if(swap_list.size() >= 4)
|
|
|
|
|
{
|
|
|
|
|
*swap_use = swap_list[2].toDouble();
|
|
|
|
|
*swap_total = swap_list[1].toDouble();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
bool HxSystem::get_program_status(double *cpu_usage, double *virtual_memory, double *resident_memory)
|
|
|
|
|
{
|
2024-01-25 18:37:25 +08:00
|
|
|
|
Q_UNUSED(cpu_usage);
|
|
|
|
|
Q_UNUSED(virtual_memory);
|
|
|
|
|
Q_UNUSED(resident_memory);
|
|
|
|
|
|
2024-01-21 22:06:55 +08:00
|
|
|
|
#ifdef Q_OS_LINUX
|
2024-01-11 16:14:21 +08:00
|
|
|
|
auto result = HxProcess::start(QString("ps u %1").arg(getpid()));
|
|
|
|
|
|
|
|
|
|
auto list = result.split("\n");
|
|
|
|
|
|
|
|
|
|
if(list.size() > 1)
|
|
|
|
|
{
|
|
|
|
|
auto info = list[1].simplified().split(" ");
|
|
|
|
|
|
|
|
|
|
*cpu_usage = info[2].toDouble();
|
|
|
|
|
|
|
|
|
|
*virtual_memory = info[4].toDouble() / 1024 / 1024;
|
|
|
|
|
|
|
|
|
|
*resident_memory = info[5].toDouble() / 1024;
|
|
|
|
|
|
|
|
|
|
return true;
|
|
|
|
|
}
|
2024-01-21 22:06:55 +08:00
|
|
|
|
#endif
|
2024-01-11 16:14:21 +08:00
|
|
|
|
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
bool HxSystem::get_harddisk_status(QString disk, QString *file_system, QString *size, QString *use, double *read_speed, double *write_speed)
|
|
|
|
|
{
|
|
|
|
|
auto result = HxProcess::start(QString("df -h %1").arg(disk));
|
|
|
|
|
|
|
|
|
|
auto list = result.split("\n");
|
|
|
|
|
|
|
|
|
|
if(list.size() > 1)
|
|
|
|
|
{
|
|
|
|
|
auto array = list[1].simplified().split(" ");
|
|
|
|
|
*file_system = array[0];
|
|
|
|
|
*size = array[1];
|
|
|
|
|
*use = array[4];
|
|
|
|
|
|
|
|
|
|
result = HxProcess::start(QString("iostat -k -d | grep %1").arg((*file_system).split("/").last()));
|
|
|
|
|
array = result.simplified().split(" ");
|
|
|
|
|
if(array.size() == 6)
|
|
|
|
|
{
|
|
|
|
|
if(m_disk_rw_speed.contains(*file_system))
|
|
|
|
|
{
|
|
|
|
|
auto rw_speed = m_disk_rw_speed.value(*file_system);
|
|
|
|
|
|
|
|
|
|
*read_speed = (array[4].toDouble() - rw_speed[0]);
|
|
|
|
|
*write_speed = (array[5].toDouble() - rw_speed[1]);
|
|
|
|
|
|
|
|
|
|
rw_speed[0] = array[4].toDouble();
|
|
|
|
|
rw_speed[1] = array[5].toDouble();
|
|
|
|
|
|
|
|
|
|
m_disk_rw_speed[*file_system] = rw_speed;
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
QList<double> values;
|
|
|
|
|
values.append(array[4].toDouble());
|
|
|
|
|
values.append(array[5].toDouble());
|
|
|
|
|
m_disk_rw_speed.insert(*file_system, values);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
bool HxSystem::get_harddisk_temperature(QString file_system, QString *temperature)
|
|
|
|
|
{
|
|
|
|
|
auto result = HxProcess::start(QString("echo tvis | sudo -S hddtemp '%1'").arg(file_system));
|
|
|
|
|
|
|
|
|
|
*temperature = result.simplified().split(":").last();
|
|
|
|
|
|
|
|
|
|
if((*temperature).isEmpty())
|
|
|
|
|
return false;
|
|
|
|
|
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
bool HxSystem::get_harddisk_smart(QString file_system, QString *smart)
|
|
|
|
|
{
|
2024-01-25 18:37:25 +08:00
|
|
|
|
Q_UNUSED(file_system);
|
|
|
|
|
Q_UNUSED(smart);
|
2024-01-21 22:06:55 +08:00
|
|
|
|
#ifdef Q_OS_LINUX
|
2024-01-11 16:14:21 +08:00
|
|
|
|
auto result = HxProcess::start(QString("echo tvis | sudo -S smartctl -i %1 | grep 'SMART support is'").arg(file_system));
|
|
|
|
|
if(result.indexOf("Unavailable") != -1)
|
|
|
|
|
{
|
|
|
|
|
*smart = QObject::tr("不支持");
|
|
|
|
|
}
|
|
|
|
|
else if(result.indexOf("Disabled") != -1)
|
|
|
|
|
{
|
|
|
|
|
*smart = QObject::tr("未启用");
|
|
|
|
|
}
|
|
|
|
|
else if(result.indexOf("Available") != -1)
|
|
|
|
|
{
|
|
|
|
|
result = HxProcess::start(QString("echo tvis | sudo smartctl -H %1 | grep 'SMART overall-health self-assessment test result'").arg(file_system));
|
|
|
|
|
if(result.indexOf("PASSED") != -1)
|
|
|
|
|
{
|
|
|
|
|
*smart = QObject::tr("良好");
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
*smart = QObject::tr("故障");
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
return false;
|
2024-01-21 22:06:55 +08:00
|
|
|
|
#endif
|
2024-01-11 16:14:21 +08:00
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|