Código: obtenga información básica del dispositivo en 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;
    }
}

Información del disco

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


Principio de uso de la CPU :
leer/proc/stat

La primera fila de datos Explicación
usuario El tiempo de ejecución en modo usuario acumulado desde el inicio del sistema hasta el momento actual, excluyendo los procesos con un valor negativo de nice
bonito El tiempo de CPU ocupado por procesos con un valor agradable negativo desde el inicio del sistema hasta el momento actual
sistema El tiempo de ejecución en el estado central desde el inicio del sistema hasta el momento actual
ocioso Se acumula desde el inicio del sistema hasta el momento actual, otro tiempo de espera excepto el tiempo de espera IO iowait (12256) Se acumula desde el inicio del sistema hasta el momento actual, el tiempo de espera IO
esperar io tiempo de espera
irq Acumulado desde el inicio del sistema hasta el momento actual, tiempo de interrupción difícil
softirq Desde el inicio del sistema hasta el momento actual, el tiempo de interrupción suave
// 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;
    }
}

Temperatura de la CPU
Nota: solo probado en la placa de desarrollo Raspberry Pi, 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);
    }
}

Uso de memoria

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

Supongo que te gusta

Origin blog.csdn.net/weixin_45579994/article/details/112578721
Recomendado
Clasificación