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

#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 <= ){
return -;
} FILE *f = NULL;
int len = ; f= popen(cmd, "r");
if(NULL == f){
return -;
} // memset(buf, '\0', buf_size);
if(NULL == fgets(buf, buf_size, f)){
pclose(f);
return -;
} pclose(f); len = strlen(buf);
if(len > && (buf[len-] == '\n')){
buf[len-] = '\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 <= ){
return -;
} int i = ;
int len = strlen(in);
int count = sizeof(cmd_gw)/sizeof(cmd_gw[]); for(; i < count; i++){
if(!strncasecmp(in, cmd_gw[i].item, len)){
break;
}
} if(i >count){
return -;
} int ret = exec_cmd(out, out_len, cmd_gw[i].cmd);
if(ret <= ){
return -;
} *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 * atof(buf) / ;
} 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 -;
} return 100.00 - 100.00 * atof(out_free) / atof(buf);
}

参考:

1. linux内存查看及释放

2. /proc/meminfo分析

linux系统信息获取和上报的更多相关文章

  1. QT在linux下获取网络类型

    开发中遇到这样一个需求,需要判断当前网络的类型(wifi或者4G或者网线),在这里给大家一块分享下: 1.这里有一个linux指令:nmcli(大家自行百度即可) 2.nmcli device sta ...

  2. linux C 获取当前目录的实现(转-Blossom)

    linux C 获取当前目录的实现: //获取当前目录#include <stdlib.h>#include <stdio.h>#include <string.h> ...

  3. linux编程获取本机网络相关参数

    getifaddrs()和struct ifaddrs的使用,获取本机IP 博客分类: Linux C编程   ifaddrs结构体定义如下: struct ifaddrs { struct ifad ...

  4. I.MX6 Linux 自动获取AR1020 event input节点

    /*********************************************************************** * I.MX6 Linux 自动获取AR1020 ev ...

  5. Linux 下获取LAN中指定IP的网卡的MAC(物理地址)

    // all.h// 2005/06/20,a.m. wenxy #ifndef _ALL_H#define _ALL_H #include <memory.h>#include < ...

  6. Linux下获取硬盘使用情况

    Linux下获取硬盘使用情况[总结] 1.前言 在嵌入式设备中,硬盘空间非常有限,在涉及到经常写日志的进程时候,需要考虑日志的大小和删除,不然很快就硬盘写满,导致日志程序崩溃.为了捕获硬盘写满的异常场 ...

  7. # Linux Whois3获取 运营商信息

    Linux Whois3获取 运营商信息 APNIC是管理亚太地区IP地址分配的机构,它有着丰富准确的IP地址分配库,同时这些信息也是对外公开的,并提供了一个查询工具,下面就让我们看看如何在Linux ...

  8. .net core在Linux下获取AD域信息

    .net core在Linux下获取AD域信息 .net Core 2.1.4 .net core现在System.DirectoryServices只支持Windows平台下使用. 参考: http ...

  9. PHP获取网卡的MAC地址原码;目前支持WIN/LINUX系统 获取机器网卡的物理(MAC)地址

    声明转换于其它博客当中的. <?php /** 获取网卡的MAC地址原码:目前支持WIN/LINUX系统 获取机器网卡的物理(MAC)地址 **/ class GetMacAddr{ var $ ...

随机推荐

  1. 动手制作 java版本切换 多版本JDK安装 windows JDK版本 切换

    [参考]windows下JDK版本之间的切换 1.下载各版本安装包,指定安装位置顺序安装 2.删除注册表,文件和环境变量 文件: C:\Windows\System32  下java相关文件如 jav ...

  2. django web问题

    django生命周期 1. 当用户在浏览器中输入url时,浏览器会生成请求头和请求体发给服务端 请求头和请求体中会包含浏览器的动作(action),这个动作通常为get或者post,体现在url之中. ...

  3. Docker是什么?

    Docker是什么? Docker是一个虚拟环境容器,可以将你的环境.代码.配置文件等一并打包到这个容器中,并发布和应用到任意平台中.比如,你在本地部署了git,jenkins等,可以将其与插件一并打 ...

  4. ThinkPHP 中入口文件中的APP_DEBUG为TRUE时不报错,改为FALSE时报错

    今天好不容易将一个新闻网做好了(ThinkPHP框架做的),但是,当我将入口文件中定义调试模式设为FALSE,即define('APP_DEBUG',False),然后再刷新网站的时候,就提示报错,报 ...

  5. Appium之Toast元素识别

    问题思考 在日常使用App过程中,经常会看到App界面有一些弹窗提示(如下图所示)这些提示元素出现后等待3秒左右就会自动消失,那么我们该如何获取这些元素文字内容呢? Toast简介 Android中的 ...

  6. React 使用 if else 判断语句

    今天在写 React 时,在 render 的return中既然不能使用if判断语句,所以就整理一些在react中使用if 的方式,可根据自己的实际情况选择: 方式一: class LLL exten ...

  7. 破解MySQL登录密码的几种方法

    工具列表 Medusa Ncrack Hydra Metasploit Medusa medusa  -h 192.168.1.106 –U /root/Desktop/user.txt –P /ro ...

  8. cube-ui 重构饿了吗Webapp的 scroll-nav域名插槽问题

    Vue2.6 将 slot-scope 废弃了. 推荐使用 v-slot: 其使用方法大致如下: 注意多个插槽的情况下,最好都基于 <template> default插槽用法还是一样的, ...

  9. 【bat】【windows】win10查看所有wifi密码

    win10的可以,win7的好像不行 @echo off & setlocal EnableDelayedExpansion title 查看所有wifi和密码 for /f "us ...

  10. 绘制UML图的工具

    目前在用processOn网站进行绘制:https://www.processon.com/ 学习其简单的入门教程: https://www.processon.com/support https:/ ...