用C++和shell获取本机CPU、网卡、内存、磁盘等的基本信息;
由于对C++相关的函数没多少了解,但是觉得用shell反而相对简单一些:
一、shell脚本,用来辅助C++获取主机的资源使用信息
(1) cpurate.sh 获取cpu的使用率
#!/bin/sh ##echo user nice system idle iowait irq softirq CPULOG_1=$(cat /proc/stat | grep 'cpu ' | awk '{print $2" "$3" "$4" "$5" "$6" "$7" "$8}') SYS_IDLE_1=$(echo $CPULOG_1 | awk '{print $4}') Total_1=$(echo $CPULOG_1 | awk '{print $1+$2+$3+$4+$5+$6+$7}') sleep 1 CPULOG_2=$(cat /proc/stat | grep 'cpu ' | awk '{print $2" "$3" "$4" "$5" "$6" "$7" "$8}') SYS_IDLE_2=$(echo $CPULOG_2 | awk '{print $4}') Total_2=$(echo $CPULOG_2 | awk '{print $1+$2+$3+$4+$5+$6+$7}') SYS_IDLE=`expr $SYS_IDLE_2 - $SYS_IDLE_1` Total=`expr $Total_2 - $Total_1` SYS_USAGE=`expr $SYS_IDLE/$Total*100 |bc -l` SYS_Rate=`expr 100-$SYS_USAGE |bc -l` Disp_SYS_Rate=`expr "scale=3; a=$SYS_Rate/1; if(length(a)==scale(a) && a!=0) print 0;print a" |bc` echo $Disp_SYS_Rate%
(2)memrate.sh 获取内存的使用率
#!/bin/sh MemTotal=$(cat /proc/meminfo | grep 'MemTotal' | awk '{print $2}') MemFree=$(cat /proc/meminfo | grep 'MemFree' | awk '{print $2}') Disp_SYS_Rate=`expr "scale=3; a=100*$MemFree/$MemTotal; if(length(a)==scale(a)) print 0;print a" |bc` echo $Disp_SYS_Rate%
(3)network.sh 获取网卡的使用率
#!/bin/sh cat /proc/net/dev | grep 'eth' | awk '{ if($2!=0) print $1"/"$2}'
(4)getsize.sh 获取磁盘的可用与总共的大小
#!/bin/bash LISTEN_PATH=./ if [ -n $1 ]; then LISTEN_PATH=$1 fi echo `df -h $LISTEN_PATH | grep "dev" `| awk '{print $3"/"$2}'
二、C++ file用来调用上面的shell文件获取信息
#include<iostream> #include<string> #include <stdio.h> using namespace std; bool GetCpuRate(std::string& cpurate) { FILE *file; string cmd("./cpurate.sh"); file = popen(cmd.c_str(), "r"); if(file == NULL) { cout<<cmd<<" fail"<<endl; return false; } char buf[512] = {0}; while(fgets(buf, sizeof(buf), file) != NULL) { char tmpbuf[512]={0}; sscanf(buf,"%s",tmpbuf); cpurate=std::string(tmpbuf); } pclose(file); return true; } bool GetMemRate(std::string& memrate) { FILE *file; string cmd("./memrate.sh"); file = popen(cmd.c_str(), "r"); if(file == NULL) { cout<<cmd<<" fail"<<endl; return false; } char buf[512] = {0}; while(fgets(buf, sizeof(buf), file) != NULL) { char tmpbuf[512]={0}; sscanf(buf,"%s",tmpbuf); memrate=std::string(tmpbuf); } pclose(file); return true; } bool GetNetInfo(std::string& network) { FILE *file; string cmd("./network.sh"); file = popen(cmd.c_str(), "r"); if(file == NULL) { cout<<cmd<<" fail"<<endl; return false; } char buf[512] = {0}; while(fgets(buf, sizeof(buf), file) != NULL) { char tmpbuf[512]={0}; sscanf(buf,"%s",tmpbuf); network=std::string(tmpbuf); } pclose(file); return true; } bool GetDiskInfo(std::string& diskInfo,const std::string path) { FILE *file; string cmd("./getsize.sh "+path); file = popen(cmd.c_str(), "r"); if(file == NULL) { cout<<cmd<<" fail"<<endl; return false; } char buf[512] = {0}; while(fgets(buf, sizeof(buf), file) != NULL) { char tmpbuf[512]={0}; sscanf(buf,"%s",tmpbuf); diskInfo=std::string(tmpbuf); } pclose(file); return true; } int main() { std::string cpurate, memrate, network, diskInfo; GetCpuRate(cpurate); GetMemRate(memrate); GetNetInfo(network); GetDiskInfo(diskInfo, "/home/"); cout<<cpurate<<endl; cout<<memrate<<endl; cout<<network<<endl; cout<<diskInfo<<endl; return 0; }
三、获取当前活动的网卡和mac,C++file(这两个是抄别人的,但是时间有点久,找不到是谁的了)
(1) getethaddr.c 获取当前的网卡地址
#include <stdio.h> #include <ifaddrs.h> #include <netinet/in.h> #include <string.h> #include <arpa/inet.h> int get_local_ip(char *ip_list) { struct ifaddrs *ifAddrStruct; void *tmpAddrPtr; char ip[INET_ADDRSTRLEN]; int n = 0; getifaddrs(&ifAddrStruct); while (ifAddrStruct != NULL) { if (ifAddrStruct->ifa_addr->sa_family==AF_INET) { tmpAddrPtr=&((struct sockaddr_in *)ifAddrStruct->ifa_addr)->sin_addr; inet_ntop(AF_INET, tmpAddrPtr, ip, INET_ADDRSTRLEN); if (strcmp(ip, "127.0.0.1") != 0) { // printf("%s IP Address:%s ", ifAddrStruct->ifa_name, ip); if (n == 0){ memcpy(ip_list, ip, INET_ADDRSTRLEN); } else { memcpy(ip_list+INET_ADDRSTRLEN, ip, INET_ADDRSTRLEN); } n++; } } ifAddrStruct=ifAddrStruct->ifa_next; } //free ifaddrs freeifaddrs(ifAddrStruct); return n; } int main() { char ip[3][INET_ADDRSTRLEN]; memset(ip, 0, sizeof(ip)); int n; for (n=get_local_ip(*ip); n>0; n--) { printf("%s ", ip[n-1]); } return 0; }
(2) 获取mac地址getmac.c
#include <stdio.h> #include <fcntl.h> #include <stdlib.h> #include <string.h> #include <unistd.h> #include <sys/ioctl.h> #include <sys/types.h> #include <sys/socket.h> #include <netinet/in.h> #include <linux/if.h> #define IFNAMSIZ 16 char ifname_buf[2048]; char *ifnames = ifname_buf; int count = 0; void add_interface_name(const char * name) { int i; for (i=0;i<count;i++) { if (!strcmp(ifnames+i*IFNAMSIZ, name)) return; } strncpy(ifnames+(count++)*IFNAMSIZ, name, IFNAMSIZ-1); } char * get_name(char *name, char *p) { while (isspace(*p)) p++; while (*p) { if (isspace(*p)) break; if (*p == ':') { /* could be an alias */ char *dot = p, *dotname = name; *name++ = *p++; while (isdigit(*p)) *name++ = *p++; if (*p != ':') { /* it wasn't, backup */ p = dot; name = dotname; } if (*p == '