获取linux系统信息,linux系统信息获取和上报

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

#include

#include

#include

#include

#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);

}

参考:

标签:char,获取,rate,awk,linux,print,上报,NULL,buf

来源: https://www.cnblogs.com/embedded-linux/p/11621593.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值