linux系统信息获取和上报

Posted yuxi_o

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了linux系统信息获取和上报相关的知识,希望对你有一定的参考价值。

一. 命令行获取

通过调用shell命令获取系统信息,如cpu个数,cpu/内存磁盘使用情况,网络信息等。golang可通过gopsutil库获取。

获取IP地址:ifconfig ens33 | awk \'/inet addr/{ print \\$2; }\' | cut -d : -f 2
CPU一分钟平均使用率: cat /proc/loadavg | cut -d \' \' -f 1
CPU当前使用率:IDLE=$(top -b -n 1|grep Cpu|awk \'{print $8; }\' )     (100-IDLE)%
CPU当前用户使用率:top -b -n 1|grep Cpu|awk \'{print $2; }\'
总内存:cat /proc/meminfo | awk \'/MemTotal/{print $2}\'
空闲内存:cat /proc/meminfo | awk \'/MemFree/{print $2}\'
磁盘占用率:df -h | grep \'/dev/root\' | awk \'{print \\$5}\'

当前时间:date +%Y%m%d-%H:%M:%S 
系统运行时间:uptime | awk \'{ print $3; }\' | cut -d \',\' -f1
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>

#define CMD_BUF_SIZE    256 

typedef float (*func_trans)(char *buf, int buf_size);
static float trans_cpu_avg_rate(char *buf, int buf_size);
static float trans_cpu_now_rate(char *buf, int buf_size);
static float trans_mem_rate(char *buf, int buf_size);

static int exec_cmd(char *buf, int buf_size, char *cmd)
{
    if(NULL == cmd || NULL == buf || buf_size <= 0){
        return -1;
    }

    FILE *f = NULL;
    int len = 0;

    f= popen(cmd, "r");
    if(NULL == f){
        return -1;
    }

//    memset(buf, \'\\0\', buf_size);
    if(NULL == fgets(buf, buf_size, f)){
        pclose(f);
        return -1;
    }

    pclose(f);

    len = strlen(buf);
    if(len > 0 && (buf[len-1] == \'\\n\')){
        buf[len-1] = \'\\0\';
        len --;
    }

    return len;
}

struct st_cmd_gw{
    char *item;
    char format;
    char *cmd;
    func_trans trans;
    char *factor;
} cmd_gw[] = {
    {"ip1", \'s\', "ifconfig eth0 | awk \'/inet /{ print $2; }\' | cut -d : -f 2", NULL, NULL},
    {"wlp1s0", \'s\', "ifconfig wlp1s0 | awk \'/inet /{ print $2; }\' | cut -d : -f 2", NULL, NULL},
    {"pppoe", \'s\', "ifconfig ppp0 | awk \'/inet /{ print $2; }\' | cut -d : -f 2", NULL, NULL},    
    {"time_now", \'s\', "date \'+%Y-%m-\\%d %H:%M:\\%S\'", NULL, NULL},
    {"time_up", \'s\', "uptime | awk \'{ print $3; }\' | cut -d \',\' -f1", NULL, NULL},
    {"cpu_avg_rate", \'f\', "cat /proc/loadavg | cut -d \' \' -f 1", trans_cpu_avg_rate, "100*\\%f/4"},
    {"cpu_now_rate", \'f\', "top -b -n 1|grep Cpu|awk \'{print $8; }\' ", trans_cpu_now_rate, "100.00-\\%f"},
    {"cpu_usr_now_rate", \'f\', "top -b -n 1|grep Cpu|awk \'{print $2; }\'", NULL, NULL},
    {"disk_rate", \'f\', "df -h | grep \'/dev/root\' | awk \'{print $5}\'", NULL, NULL},
    {"disk_use_f", \'f\', "df -h | grep \'/dev/root\' | awk \'{print $3}\'", NULL, NULL},
    {"disk_avail_f", \'f\', "df -h | grep \'/dev/root\' | awk \'{print $4}\'", NULL, NULL},
    {"disk_use_s", \'s\', "df -h | grep \'/dev/root\' | awk \'{print $3}\'", NULL, NULL},
    {"disk_avail_s", \'s\', "df -h | grep \'/dev/root\' | awk \'{print $4}\'", NULL, NULL},
    {"ram_rate", \'f\', "cat /proc/meminfo | awk \'/MemTotal/{print $2}\'", trans_mem_rate, NULL},
    {"ram_all", \'f\', "cat /proc/meminfo | awk \'/MemTotal/{print $2}\'", NULL, NULL},
    {"ram_free", \'f\', "cat /proc/meminfo | awk \'/MemFree/{print $2}\'", NULL, NULL},
    {"cpu_num", \'s\', "cat /proc/cpuinfo | grep \\"processor\\" | wc -l | awk \'{print $1}\'", NULL, NULL}
};

// out: output the result of the string type
// f: output the result of the float type
int out_gw(char *out, int out_len, const char *in, float *f)
{
    if(NULL == in || NULL == out || out_len <= 0){
        return -1;
    }

    int i = 0;
    int len = strlen(in);
    int count = sizeof(cmd_gw)/sizeof(cmd_gw[0]);

    for(; i < count; i++){
        if(!strncasecmp(in, cmd_gw[i].item, len)){
            break;            
        }    
    }

    if(i >count){
        return -1;
    }

    int ret = exec_cmd(out, out_len, cmd_gw[i].cmd);
    if(ret <= 0){
        return -1;
    }
    
    *f = 0.0;
    if(\'f\' == cmd_gw[i].format){
        if(cmd_gw[i].trans){
            *f = (cmd_gw[i].trans)(out, out_len);    
        } else {
            *f = atof(out);
        }
    }
    
    return cmd_gw[i].format;
}

static float trans_cpu_avg_rate(char *buf, int buf_size)
{
    return 100 * atof(buf) / 4;
}

static float trans_cpu_now_rate(char *buf, int buf_size)
{
    return 100.00 - atof(buf);
}

static float trans_mem_rate(char *buf, int buf_size)
{
    float f = 0.0;
    char out_free[CMD_BUF_SIZE] = {\'\\0\'};
    int ret_free = out_gw(out_free, sizeof(out_free), "mem_free", &f);

    if(\'f\' != (char)ret_free){
        return -1;
    }

    return 100.00 - 100.00 * atof(out_free) / atof(buf);
}

容器信息也可通过docker stats获取到:

docker stats --no-stream --format "{\\"container\\":\\"{{ .Name }}\\",\\"memory\\":{\\"raw\\":\\"{{ .MemUsage }}\\",\\"percent\\":\\"{{ .MemPerc }}\\"},\\"cpu\\":\\"{{ .CPUPerc }}\\",\\"networkIO\\":\\"{{.NetIO}},\\"BlockIO\\":\\"{{.BlockIO}}\\"}"
{"container":"cadvisor","memory":{"raw":"40.45MiB / 3.701GiB","percent":"1.07%"},"cpu":"2.40%","networkIO":"11.9MB / 1.27GB,"BlockIO":"0B / 0B"}
{"container":"orderer.example.com","memory":{"raw":"21.18MiB / 3.701GiB","percent":"0.56%"},"cpu":"0.29%","networkIO":"57.7MB / 57.9MB,"BlockIO":"0B / 0B"}
{"container":"orderer4.example.com","memory":{"raw":"18.62MiB / 3.701GiB","percent":"0.49%"},"cpu":"0.28%","networkIO":"57.5MB / 57.4MB,"BlockIO":"0B / 0B"}
{"container":"orderer2.example.com","memory":{"raw":"18.6MiB / 3.701GiB","percent":"0.49%"},"cpu":"0.32%","networkIO":"57.5MB / 57.4MB,"BlockIO":"0B / 0B"}
{"container":"orderer5.example.com","memory":{"raw":"18.2MiB / 3.701GiB","percent":"0.48%"},"cpu":"0.35%","networkIO":"57.4MB / 57.3MB,"BlockIO":"0B / 0B"}
{"container":"orderer3.example.com","memory":{"raw":"25.19MiB / 3.701GiB","percent":"0.66%"},"cpu":"0.63%","networkIO":"228MB / 229MB,"BlockIO":"0B / 0B"}

参考:docker查询容器转态,并转成json数据

当然docker信息可通过dockerAPI或SDK获取,参考:Develop with Docker Engine SDKs and API    https://docs.docker.com/engine/api/v1.39/# 

cpu使用率:(cpu_stats.total_usage - precpu_stats.total_usage) / (cpu_stats.system_cpu_usage - precpu_stats.system_cpu_usage) * online_cpus * 100
内存:memory_stats.usage / Math.pow(1024,2)

{
    "Name": "flexedge-opcua-server",    容器名称
    "Ip": "172.17.6.222",    docker Ip地址
    "Image": "test:latest",    镜像地址
    "HostConfig": {
        "Binds": [ 非必填
            "/usr/local/test:/usr/local/test"    映射目录
        ],
        "Memory": 1073741824,    内存上限 MB*Math.pow(1024,2)
        "NanoCPUs": 200000000,    cpu上限 核*Math.pow(10,8)
        "NetworkMode": "host"    网络模式
    }
}

二. cadvisor

 

参考:

1. linux内存查看及释放

2. /proc/meminfo分析

3. prometheus 容器监控:cAdvisor

以上是关于linux系统信息获取和上报的主要内容,如果未能解决你的问题,请参考以下文章

上报系统对比,定位跟权限

python BrickstorOS片段用于获取各种文件系统信息。

Android GB28181接入端实时位置订阅和上报之-如何获取当前经纬度

如何获取jenkins的日志信息

C++实现查询本机信息并且上报

个人冲刺——体温上报app(一阶段)