当前位置: 技术问答>linux和unix
通过proc文件系统获取系统性能监测参数-代码实现
来源: 互联网 发布时间:2016-06-04
本文导语: #include #include #include #include #include #include #include #include #include //打印调试信息 #define DBG_print(level, lpszFormat, ...) syslog(level|LOG_DAEMON, lpszFormat, ##__VA_ARGS__) //发送性能日志信息 #define LOG_print(lpszFormat, ...) ...
#include
#include
#include
#include
#include
#include
#include
#include
#include
//打印调试信息
#define DBG_print(level, lpszFormat, ...)
syslog(level|LOG_DAEMON, lpszFormat, ##__VA_ARGS__)
//发送性能日志信息
#define LOG_print(lpszFormat, ...)
syslog(LOG_INFO, lpszFormat, ##__VA_ARGS__)
//用于时间同步
time_t TargetTime;
//CPU使用
unsigned long long cpu_total = 0;
unsigned long long cpu_total_used = 0;
unsigned long long pre_cpu_total = 0;
unsigned long long pre_cpu_total_used = 0;
int cpu_percent_used = 0;
//内存
int mem_total = 0;
int mem_free = 0;
//网卡流量
unsigned long long eth0_in = 0;
unsigned long long eth0_out = 0;
unsigned long long eth1_in = 0;
unsigned long long eth1_out = 0;
unsigned long long pre_eth0_in = 0;
unsigned long long pre_eth0_out = 0;
unsigned long long pre_eth1_in = 0;
unsigned long long pre_eth1_out = 0;
time_t pre_time = 0;
int eth0_in_bps = 0;
int eth0_out_bps = 0;
int eth1_in_bps = 0;
int eth1_out_bps = 0;
//并发tcp连接数
#define MAX_CONN 40960
typedef enum {
TCP_ESTABLISHED = 1,
TCP_SYN_SENT,
TCP_SYN_RECV,
TCP_FIN_WAIT1,
TCP_FIN_WAIT2,
TCP_TIME_WAIT,
TCP_CLOSE,
TCP_CLOSE_WAIT,
TCP_LAST_ACK,
TCP_LISTEN
} TCP_STATE;
struct TCP_CONN {
TCP_STATE st;
unsigned long local_ip;
unsigned long local_port;
unsigned long remote_ip;
unsigned long remote_port;
} tcp_conns[MAX_CONN];
int nTcpConns = 0;
int daemon_init()
{
pid_t pid;
struct rlimit rlim;
int i;
if( (pid=fork())