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 $ ...
随机推荐
- Nginx请求转发
1.比如说我要将127.0.0.1/topics上的所有请求转发到xxx:xxx/上 修改 sudo vim /etc/nginx/nginx.conf server { listen 80; ser ...
- LODOP打印图片水平居中
其他居中,查看本博客相关博文:LODOP中打印项水平居中简短问答.图片也属于超文本打印项,因此如果想把图片居中,也需要图片本身内容相对于图片打印项宽度居中,然后再设置打印项居中.如图,同一张图片,都设 ...
- K8S使用入门-添加一个node
上一篇博客我们已经将K8S部署起来了,现在我们就来介绍一下如何简单使用K8S (1)添加节点 注意事项:不能和k8s master节点的主机名一样.否则会导致k8s无法正常识别出该节点 添加节点是比较 ...
- 分割nginx日志
#!/bin/bash #此脚本用于自动分割Nginx的日志,包括access.log和error.log #每天00:00执行此脚本 将前一天的access.log重命名为access-xxxx-x ...
- The 10 Best Studio Headphones of 2019
The 10 Best Studio Headphones of 2019 https://www.outeraudio.com/category/list/ https://www.outeraud ...
- 032 SSM综合练习08--数据后台管理系统--jsp页面显示当前用户名
1. 页面端标签控制权限 在jsp页面中我们可以使用spring security提供的权限标签来进行权限控制. (1)在pom文件中导入依赖 <dependency> <group ...
- centos实现三个节点高可用
centos实现三个节点高可用 使用的资源为keepalived和nginx 高可用主机IP地址 192.168.136.131 192.168.136.133 192.168.136.134 ngi ...
- celery 分布式异步任务框架(celery简单使用、celery多任务结构、celery定时任务、celery计划任务、celery在Django项目中使用Python脚本调用Django环境)
一.celery简介: Celery 是一个强大的 分布式任务队列 的 异步处理框架,它可以让任务的执行完全脱离主程序,甚至可以被分配到其他主机上运行.我们通常使用它来实现异步任务(async tas ...
- go上传图片微信服务器<<临时素材
type WxImage struct { Type string `json:"type"` MediaId string `json:"media_id"` ...
- Golang-使用mysql
一.安装mysql-driver驱动 go get github.com/go-sql-driver/mysql 二.安装完毕之后,就可以通过go语言操作mysql了 const ( _selectU ...