通过调用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. sql简单存储过程分享

    很多程序员朋友都视sql为洪湖水猛兽,其实深入分析一下,多用些时间与耐心,sql还是可以理解的. 本文主要是针对刚刚接触sql的新手朋友,进行一个sql存储过程的简单分享. 小子第一次发布文章,也是借 ...

  2. Laravel实现from的curl文件转发

    文件的使用curl分发时发现不能直接将其传入curl,需要使用CURLFile()来实现 分发类 <?php /** * 请求转发控制器 * Created by PhpStorm. * Use ...

  3. Centos6 系统下源码方式安装Mysql 记录

    在运维工作中经常部署各种运维环境,涉及mysql数据库的安装也是时常需要的.mysql数据库安装可以选择yum在线安装,但是这种安装的mysql一般是系统自带的,版本方面可能跟需求不太匹配. #### ...

  4. python 必选参数、默认参数、可变参数和、关键字参数

    转自:https://www.liaoxuefeng.com/wiki/897692888725344/897693568201440 可变参数 在Python函数中,还可以定义可变参数.顾名思义,可 ...

  5. 【剑指offer】数组中只出现一次的数

    题目描述 一个整型数组里除了两个数字之外,其他的数字都出现了两次.请写程序找出这两个只出现一次的数字. 分析: 经典的异或技巧题 两个相同的数字异或的结果为0,一个数和0异或的结果是其本身,假设现在那 ...

  6. 009 SpringCloud 学习笔记5-----Hystrix保护机制

    1.概述 Hystrix,英文意思是豪猪,全身是刺,看起来就不好惹,是一种保护机制.Hystrix也是Netflix公司的一款组件.主页:https://github.com/Netflix/Hyst ...

  7. Unity 新手引导

    根据Shader动态生成遮罩 源码地址 圆形遮罩镂空处理脚本: using System; using System.Collections.Generic; using UnityEngine; u ...

  8. layui 动画 实现过程

    layui官方文档晦涩难懂,对小白特别不友好 为演示效果,js和css文件引用cdn 演示是ul套li标签进行演示,这不是固定的,你也可以div套div,div套span,外面和里面的标签类要一一对应 ...

  9. day56——http协议、MVC和MTV框架模式、django下载安装、url路由分发

    day56 昨日复习 今日内容 HTTP协议 网页:https://www.cnblogs.com/clschao/articles/9230431.html 老师整理的重点 老师整理的重点 请求信息 ...

  10. 带小伙伴手写 golang context

    前言 - context 源码 可以先了解官方 context.go 轮廓. 这里捎带保存一份当前 context 版本备份. // Copyright 2014 The Go Authors. Al ...