IP
// Auther: IYATT-yx
#include <iostream>
#include <cstring>
extern "C"
{
#include <arpa/inet.h>
#include <ifaddrs.h>
#include <netinet/in.h>
}
int main()
{
struct ifaddrs *ifAddrStruct = NULL;
void *tmpAddrPtr = NULL;
getifaddrs(&ifAddrStruct);
while (ifAddrStruct != NULL)
{
if (ifAddrStruct->ifa_addr->sa_family == AF_INET)
{
tmpAddrPtr = &((struct sockaddr_in *)ifAddrStruct->ifa_addr)->sin_addr;
char addressBuffer[INET_ADDRSTRLEN];
inet_ntop(AF_INET, tmpAddrPtr, addressBuffer, INET_ADDRSTRLEN);
if (strcmp(ifAddrStruct->ifa_name, "eth0") == 0)
{
std::cout << "eth0: " << addressBuffer << std::endl;
break;
}
else if (strcmp(ifAddrStruct->ifa_name, "wlan0") == 0)
{
std::cout << "wlan0: " << addressBuffer << std::endl;
break;
}
}
ifAddrStruct = ifAddrStruct->ifa_next;
}
}
Disk information
// Auther: IYATT-yx
#include <iostream>
extern "C"
{
#include <sys/vfs.h>
#include <unistd.h>
}
struct statfs diskInfo;
int main()
{
statfs("/", &diskInfo);
long long unsigned totalBlocks = diskInfo.f_bsize;
long long unsigned totalSize = (totalBlocks * diskInfo.f_blocks) >> 20;
long long unsigned freeDisk = (diskInfo.f_bfree * totalBlocks) >> 20;
long long unsigned usedDisk = totalSize - freeDisk;
double usage = (double)usedDisk / (double)totalSize * 100;
std::cout << "磁盘总大小: " << totalSize << " MB"
<< "\n空闲大小: " << freeDisk << " MB"
<< "\n已使用大小: " << usedDisk << " MB"
<< "\n使用率: " << usage << " %" << std::endl;
}
CPU usage
principle:
read/proc/stat
The first row of data | Explanation |
---|---|
user | The running time in user mode accumulated from the start of the system to the current moment, excluding processes with a negative value of nice |
nice | The CPU time occupied by processes with a negative nice value from the start of the system to the current moment |
system | The running time in the core state from the start of the system to the current moment |
idle | Accumulate from the start of the system to the current moment, other waiting time except IO wait time iowait (12256) Accumulate from the start of the system to the current moment, IO wait time |
iowait | io waiting time |
irq | Accumulated from the start of the system to the current moment, hard interruption time |
softirq | From the start of the system to the current moment, the soft interruption time |
// Auther: IYATT-yx
#include <iostream>
#include <fstream>
extern "C"
{
#include <unistd.h>
}
int main()
{
while (true)
{
std::string cpu;
std::ifstream readCPU;
readCPU.open("/proc/stat", std::ifstream::in);
long unsigned user1, nice1, sys1, idle1, iowait1, irq1, softirq1, total1;
readCPU >> cpu >> user1 >> nice1 >> sys1 >> idle1 >> iowait1 >> irq1 >> softirq1;
readCPU.close();
sleep(1);
readCPU.open("/proc/stat", std::ifstream::in);
long unsigned user2, nice2, sys2, idle2, iowait2, irq2, softirq2, total2;
readCPU >> cpu >> user2 >> nice2 >> sys2 >> idle2 >> iowait2 >> irq2 >> softirq2;
readCPU.close();
sleep(1);
total1 = user1 + nice1 + sys1 + idle1 + iowait1 + irq1 + softirq1;
total2 = user2 + nice2 + sys2 + idle2 + iowait2 + irq2 + softirq2;
double usage = (double)(total2 - total1 - (idle2 - idle1)) / (double)(total2 - total1) * 100;
std::cout << "CPU使用率: " << usage << " %" << std::endl;
}
}
CPU temperature
Note: Only tested on Raspberry Pi development board, Raspberry Pi OS
// Auther: IYATT-yx
#include <iostream>
#include <fstream>
extern "C"
{
#include <unistd.h>
}
int main()
{
std::ifstream cpuTemp;
long temp;
while (true)
{
cpuTemp.open("/sys/class/thermal/thermal_zone0/temp", std::ifstream::in);
cpuTemp >> temp;
std::cout << "CPU温度为: " << temp / 1000.0 << std::endl;
cpuTemp.close();
sleep(1);
}
}
Memory usage
// Auther: IYATT-yx
#include <iostream>
extern "C"
{
#include <sys/sysinfo.h>
#include <unistd.h>
}
struct sysinfo sysInfo;
int main()
{
system("clear");
while (true)
{
if (sysinfo(&sysInfo) != 0)
{
std::cerr << "sysinfo Error" << std::endl;
continue;
}
unsigned long totalRAM = sysInfo.totalram >> 20;
unsigned long freeRAM = sysInfo.freeram >> 20;
unsigned long usedRAM = totalRAM - freeRAM;
double usage = (double)usedRAM / (double)totalRAM * 100;
std::cout << "已使用内存大小: " << usedRAM << "MB"
<< "\n未使用内存大小: " << freeRAM << "MB"
<< "\n内存总大小: " << totalRAM << "MB"
<< "\n内存使用率: " << usage << " %" << std::endl;
sleep(1);
system("clear");
}
}