• linux系统信息和容器信息获取和上报


    一. 命令行获取

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

    获取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, '', buf_size);
        if(NULL == fgets(buf, buf_size, f)){
            pclose(f);
            return -1;
        }
    
        pclose(f);
    
        len = strlen(buf);
        if(len > 0 && (buf[len-1] == '
    ')){
            buf[len-1] = '';
            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] = {''};
        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/# 

    二. cadvisor

    参考:

    1. linux内存查看及释放

    2. /proc/meminfo分析

    3. prometheus 容器监控:cAdvisor

  • 相关阅读:
    git查看某个文件的提交记录
    git修改文件夹名字
    Visual Studio 2013 在使用 razor无智能提示的解决办法
    git部分提交
    SQL SERVER与C#的数据类型对应表
    使用VBS实现SSH远程登录并自动执行命令
    安装并配置Samba
    Trouble and solution
    两台电脑间共享文件
    Astah类图中使用list<>
  • 原文地址:https://www.cnblogs.com/embedded-linux/p/11621593.html
Copyright © 2020-2023  润新知