代码: Linux下获取基本设备信息

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;
    }
}

磁盘信息

// 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使用率
原理:
读取/proc/stat

第一行的数据 解释
user 从系统启动开始累计到当前时刻,处于用户态的运行时间,不包含 nice值为负进程
nice 从系统启动开始累计到当前时刻,nice值为负的进程所占用的CPU时间
system 从系统启动开始累计到当前时刻,处于核心态的运行时间
idle 从系统启动开始累计到当前时刻,除IO等待时间以外的其它等待时间iowait (12256) 从系统启动开始累计到当前时刻,IO等待时间
iowait io等待时间
irq 从系统启动开始累计到当前时刻,硬中断时间
softirq 从系统启动开始累计到当前时刻,软中断时间
// 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温度
注: 仅在树莓派开发板,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);
    }
}

内存使用情况

// 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");
    }
}

猜你喜欢

转载自blog.csdn.net/weixin_45579994/article/details/112578721
今日推荐