linux系统信息获取和上报
通过调用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内存查看及释放
linux系统信息获取和上报的更多相关文章
- QT在linux下获取网络类型
开发中遇到这样一个需求,需要判断当前网络的类型(wifi或者4G或者网线),在这里给大家一块分享下: 1.这里有一个linux指令:nmcli(大家自行百度即可) 2.nmcli device sta ...
- linux C 获取当前目录的实现(转-Blossom)
linux C 获取当前目录的实现: //获取当前目录#include <stdlib.h>#include <stdio.h>#include <string.h> ...
- linux编程获取本机网络相关参数
getifaddrs()和struct ifaddrs的使用,获取本机IP 博客分类: Linux C编程 ifaddrs结构体定义如下: struct ifaddrs { struct ifad ...
- I.MX6 Linux 自动获取AR1020 event input节点
/*********************************************************************** * I.MX6 Linux 自动获取AR1020 ev ...
- Linux 下获取LAN中指定IP的网卡的MAC(物理地址)
// all.h// 2005/06/20,a.m. wenxy #ifndef _ALL_H#define _ALL_H #include <memory.h>#include < ...
- Linux下获取硬盘使用情况
Linux下获取硬盘使用情况[总结] 1.前言 在嵌入式设备中,硬盘空间非常有限,在涉及到经常写日志的进程时候,需要考虑日志的大小和删除,不然很快就硬盘写满,导致日志程序崩溃.为了捕获硬盘写满的异常场 ...
- # Linux Whois3获取 运营商信息
Linux Whois3获取 运营商信息 APNIC是管理亚太地区IP地址分配的机构,它有着丰富准确的IP地址分配库,同时这些信息也是对外公开的,并提供了一个查询工具,下面就让我们看看如何在Linux ...
- .net core在Linux下获取AD域信息
.net core在Linux下获取AD域信息 .net Core 2.1.4 .net core现在System.DirectoryServices只支持Windows平台下使用. 参考: http ...
- PHP获取网卡的MAC地址原码;目前支持WIN/LINUX系统 获取机器网卡的物理(MAC)地址
声明转换于其它博客当中的. <?php /** 获取网卡的MAC地址原码:目前支持WIN/LINUX系统 获取机器网卡的物理(MAC)地址 **/ class GetMacAddr{ var $ ...
随机推荐
- Deploy a plain HTTP registry
当docker pull a.b.com/ubuntu:14.04的时候,如果提示:“Docker x509 insecure registry”之类的,说这个registry不安全的,如果就是想在a ...
- Ubuntu下卸载anaconda
转载:https://blog.csdn.net/m0_37407756/article/details/77968724(一)删除整个anaconda目录: 由于Anaconda的安装文件都包含在一 ...
- windows系统下设置mtu值的方法
说起 MTU 值,可能许多朋友连听都没听说过.简单来说,我们上网时所进行的操作,都是通过传输一个又一个“数据包”来实现的,而 MTU 值就是用来设定可传输数据包的最大尺寸的.很显然,MTU 值设置得过 ...
- matlab学习笔记13_2匿名函数
一起来学matlab-matlab学习笔记13函数 13_2 匿名函数 觉得有用的话,欢迎一起讨论相互学习~Follow Me 参考文献 https://ww2.mathworks.cn/help/m ...
- EasyDSS高性能RTMP、HLS(m3u8)、HTTP-FLV、RTSP流媒体服务器出现no compatible source was found for this media问题的解决
背景分析 EasyDSS流媒体解决方案总体可划分成三个部分:前端视频源设备(PC.手机.摄像机)流媒体数据获取并即时回传.流媒体服务器端直播和录像与回放.客户端直播播放与录像检索回放.前端推流我们使用 ...
- 封装redis操作 php版本
<?php namespace App\Service; use App\Service\BaseService; use Illuminate\Support\Facades\Redis; / ...
- Grammar01 语法七要素之一_词类
1 词类 1.1 词类表格 实词 名词 -> n. -> noun -> 给所有人和物命名的词. 动词 -> v. (vt. vi.) -> verb ( transit ...
- 【转】Centos下编译升级安装Boost
https://www.xingchenw.cn/article/191 Centos下编译升级安装Boost 首先在官网现在相应的包 https://www.boost.org/users/down ...
- 机器学习之挖掘melb_data.csv数据
mel_data.csv是关于melb地区房屋的数据 mel_data.csv import pandas as pd melbourne_file_path = "E:\data\Melb ...
- 【剑指offer】1+….+n,不能使用相关关键字
题目描述 求1+2+3+...+n,要求不能使用乘除法.for.while.if.else.switch.case等关键字及条件判断语句(A?B:C). 分析:可以使用递归! class Soluti ...