diff --git a/src/sys_nicmonitor/Makefile b/src/sys_nicmonitor/Makefile index a674951..845da9a 100644 --- a/src/sys_nicmonitor/Makefile +++ b/src/sys_nicmonitor/Makefile @@ -6,7 +6,7 @@ include $(DEFAULTS) all:sys_nicmonitor libnic_shm.so test -CFLAGS += -Wall -Wformat=2 -Wno-format-extra-args -Wformat-security -Wformat-nonliteral #-g +CFLAGS += -D TESTINTERVAL -Wall -Wformat=2 -Wno-format-extra-args -Wformat-security -Wformat-nonliteral #-g sys_nicmonitor:mnic.o read_netcard.o send_alarm.o $(CC) -lman -ldotconf -lpthread -o $@ $^ diff --git a/src/sys_nicmonitor/mnic.c b/src/sys_nicmonitor/mnic.c index ba6dc3b..1f23b3b 100644 --- a/src/sys_nicmonitor/mnic.c +++ b/src/sys_nicmonitor/mnic.c @@ -603,7 +603,9 @@ static void get_inc_stats(NETCARD_INFO *net) memset(linebuf, 0, sizeof(linebuf)); while((retp = fgets(linebuf, sizeof(linebuf), dev_fp)) != NULL){ if((str = strstr(linebuf, net->charname)) != NULL){ - str +=strlen(net->charname) + 2; +// str +=strlen(net->charname) + 2; + str=strstr(str,":"); + str+=1; get_dev_fields(str, net); break; } @@ -703,8 +705,12 @@ static void *cacu_flow(void *p) int flags = 1; int count_ok[MAXNICNUM]; int count_no[MAXNICNUM]; - char error_str[200]; - unsigned long long bytes[MAXNICNUM]; + unsigned long long rx_flow = 0; + unsigned long long tx_flow = 0; + char error_str[2048]; + // tx_bytes tx_bytes + unsigned long long rx_bytes[MAXNICNUM]; + unsigned long long tx_bytes[MAXNICNUM]; THFLOW *ptr = (THFLOW *)p; NETCARD_INFO_ST *curr; struct timespec s_time, os_time; @@ -730,23 +736,27 @@ static void *cacu_flow(void *p) get_inc_info(&curr->info); get_inc_stats(&curr->info); if(flags){ - bytes[i] = curr->info.rx_bytes + curr->info.tx_bytes; + rx_bytes[i] = curr->info.rx_bytes; + tx_bytes[i] = curr->info.tx_bytes; } - curr->info.average_flow =(unsigned long long)((curr->info.rx_bytes + curr->info.tx_bytes - bytes[i])/ptr->mem.conf->flow_interval); + //tx_flow,rx_flow, + rx_flow=(curr->info.rx_bytes -rx_bytes[i])/ptr->mem.conf->flow_interval; + tx_flow=(curr->info.tx_bytes -tx_bytes[i])/ptr->mem.conf->flow_interval; + if(!strncmp(curr->info.charname, "bond", 4)){ count_t = get_netcard_count(curr->info.charname); }else { count_t = 1; } - summit = (unsigned int)(curr->info.average_flow/1024/1024/ptr->mem.conf->flow_peak/count_t*8*100); + // summit = (curr->info.average_flow*8/1024/1024*100/(ptr->mem.conf->flow_peak)/count_t); + summit = (tx_flow > rx_flow ? tx_flow :rx_flow)*8*100/1024/1024/(ptr->mem.conf->flow_peak)/count_t; if( sys_nic_debug ) { #if 1 - printf("%s:\tcount=%d\tsummit=%d\tflow_limit:%d\taverage_flow:%lld\n", curr->info.charname, count_t, summit, conf.flow_limit, curr->info.average_flow); + printf("%s:\tcount=%d\tsummit=%d\tflow_limit:%d\taverage_flow:%lld\trx_flow:%lld\ttx_flow:%lld\n", curr->info.charname, count_t, summit, conf.flow_limit, curr->info.average_flow,rx_flow,tx_flow); #endif - snprintf(error_str, sizeof(error_str), "%s:\tcount=%d\tsummit=%d\tflow_limit:%d\taverage_flow:%lld\tbytes[%d]:%lld\tcurr->info.rx_bytes:%lld\tcurr->info.tx_bytes\n", - curr->info.charname, count_t, summit, conf.flow_limit, curr->info.average_flow,i,bytes[i],curr->info.rx_bytes,curr->info.tx_bytes); + snprintf(error_str, sizeof(error_str), "%s:\tcount=%d\tsummit=%d\tflow_limit:%d\tcurr->info.rx_bytes:%lld\tcurr->info.tx_bytes:%lld\tflow_interval:%d\n", + curr->info.charname, count_t, summit, conf.flow_limit,curr->info.rx_bytes,curr->info.tx_bytes,ptr->mem.conf->flow_interval); record_log(error_str); - } if(summit >= ptr->mem.conf->flow_limit){ @@ -769,8 +779,8 @@ static void *cacu_flow(void *p) send_inc_info_one(&curr->info, ptr->mem.semid, ptr->mem.shm_ptr); if( sys_nic_debug ) { snprintf(error_str, sizeof(error_str), - "NOTICE: %s flow is abnormal, summit is %d, average flow is %lld !\n", - curr->info.charname, summit, curr->info.average_flow); + "NOTICE: %s flow is abnormal, summit is %d, average flow is %lld , rx_flow is %lld , tx_flow is %lld !\n", + curr->info.charname, summit, curr->info.average_flow,rx_flow,tx_flow); } else { snprintf(error_str, sizeof(error_str), "NOTICE: %s flow is abnormal !\n", curr->info.charname); } @@ -804,7 +814,8 @@ static void *cacu_flow(void *p) pthread_mutex_unlock(&mut); count_no[i] = 3; }/* end if(summit... */ - bytes[i] = curr->info.rx_bytes + curr->info.tx_bytes; + rx_bytes[i] = curr->info.rx_bytes; + tx_bytes[i] = curr->info.tx_bytes; i++; curr = curr->next; }/* end while(curr...) */ diff --git a/src/sys_nicmonitor/read_netcard.c b/src/sys_nicmonitor/read_netcard.c index c745f48..e8579e3 100644 --- a/src/sys_nicmonitor/read_netcard.c +++ b/src/sys_nicmonitor/read_netcard.c @@ -1,5 +1,6 @@ #include "mnic.h" +static int sys_nic_debug=0; static char *prase_digit(char **s) { char *retp = NULL; @@ -54,9 +55,13 @@ char *get_name(char *name, char *p) int get_dev_fields(char *str, NETCARD_INFO *ife) { + if( getenv("SYS_NIC_DEBUG") ) { + sys_nic_debug = 1; + } + int i = 0; char *retp[16]; - + char error_str[1024]; while((retp[i] = prase_digit(&str)) != NULL){i++;if(i > 15)break;} ife->rx_bytes = atoll(retp[0]); ife->rx_packets = atoll(retp[1]); @@ -71,6 +76,10 @@ int get_dev_fields(char *str, NETCARD_INFO *ife) ife->tx_fifo_errors = atol(retp[12]); ife->collisions = atol(retp[13]); ife->tx_carrier_errors = atol(retp[14]); + if( 0 ) { + snprintf(error_str, sizeof(error_str), "ife->rx_bytes%lld:\tife->tx_bytes=%lld\n",ife->rx_bytes,ife->tx_bytes); + record_log(error_str); + } return 0; } diff --git a/src/sys_nicmonitor/testread.c b/src/sys_nicmonitor/testread.c new file mode 100644 index 0000000..080a444 --- /dev/null +++ b/src/sys_nicmonitor/testread.c @@ -0,0 +1,181 @@ +#include +#include +#include +#include +#include + + +#include "sys_netcard.h" +#define IPSIZE 16 +#define THRNR 10 +#define DAYS 25 +#define LINE_SIZE 2048 +#define HOST_NAME_SIZE 40 +#define BOND_PATH "/proc/net/bonding/*" +#define LOG_PATH "/var/log/netcard/" +#define SHM_PATH "/share/sys_netcard_shm_path" +#define SEM_PATH "/share/sys_netcard_sem_path" +#define MAX_GW 32 +#define DEV_MAXLEN 32 +#define IPLEN 16 +#define CONF_FILE "/conf/nic/sys_netcard_conf.txt" + +//#define MNIC_VERSION "1.9" + +#define NIC_UNKNOWN 0 +#define NIC_DOWN 1 +#define NIC_UNRUNNING 2 +#define NIC_UNLINKABLE 3 +#define NIC_NORMAL 4 + +//网卡设备描述结构 +typedef struct __nic_dev{ + char dev_name[DEV_MAXLEN]; //网卡名称 + char ping_ip[MAX_GW][IPLEN]; //ping地址列表,最多32个 + int gw_num; //地址列表的长度 +}NIC_DEV; + +typedef struct __config_file_st{ + unsigned char domain; + short serv; + short event; + int udpport; + int monitor_interval; + int write_interval; + int flow_interval; + int flow_limit; + int flow_peak; + char udp[NIC_NAME_LEN]; + NIC_DEV nic[MAXNICNUM]; //被监视网卡的列表 + char ip[IPSIZE]; + int pingnum; //OPTIONAL!! How many times for once ping check? Default is 2. + int pinglap; //OPTIONAL!! How many seconds does ping wait for reply package? Default is 1. + int crazyping; +}CONFIG_FILE_ST; + +typedef struct inc_name_node{ + char name[NIC_NAME_LEN]; + struct inc_name_node *next; +}NET_NAME_ST; + +typedef struct inc_info_node{ + NETCARD_INFO info; + int status; + struct inc_info_node *next; +}NETCARD_INFO_ST; + +typedef struct net_info{ + NETCARD_INFO info[MAXNICNUM]; +}SHM; + +typedef struct __thread_env_st{ + D5000_NIC_ALARM Malarm; + char bond_file_path[64]; + char host_name[HOST_NAME_SIZE]; + int host_name_size; + CONFIG_FILE_ST *conf; +}THENV; + +typedef struct __thread_mem_st{ + int semid; + SHM *shm_ptr; + NETCARD_INFO_ST *listp; + CONFIG_FILE_ST *conf; +}THMEM; + +typedef struct __thread_flow_st{ + THENV env; + THMEM mem; +}THFLOW; + +int sys_nic_debug = 1; +static void get_inc_stats(NETCARD_INFO *net); +int get_dev_fields(char *str, NETCARD_INFO *ife); +static char *prase_digit(char **s); +static void get_inc_stats(NETCARD_INFO *net) +{ + char linebuf[LINE_SIZE]; + char *retp = NULL; + char *str = NULL; + FILE *dev_fp; + + if((dev_fp = fopen("/proc/net/dev", "r")) == NULL)return; + memset(linebuf, 0, sizeof(linebuf)); + + + while((retp = fgets(linebuf, sizeof(linebuf), dev_fp)) != NULL){ +// printf("linebuf: %s charname: %s\n", linebuf,net->charname); + if((str = strstr(linebuf, net->charname)) != NULL){ + str +=strlen(net->charname) + 2; + get_dev_fields(str, net); + break; + } + } + fclose(dev_fp); +} +static char *prase_digit(char **s) +{ + char *retp = NULL; + char *p = *s; + + if(!p)return NULL; + while(!isdigit(*p)){ + p++; + if(*p == '\0')return NULL; + } + retp = p; + while(isdigit(*p)){ + p++; + if(*p == '\0'){ + *s = NULL; + return retp; + } + } + *p = '\0'; + p++; + *s = p; + return retp; +} + + +int get_dev_fields(char *str, NETCARD_INFO *ife) +{ + if( getenv("SYS_NIC_DEBUG") ) { + sys_nic_debug = 1; + } + + fprintf(stdout, "str:%s\n",str); + int i = 0; + char *retp[16]; + while((retp[i] = prase_digit(&str)) != NULL){i++;if(i > 15)break;} + ife->rx_bytes = atoll(retp[0]); + ife->rx_packets = atoll(retp[1]); + ife->rx_errors = atol(retp[2]); + ife->rx_dropped = atol(retp[3]); + ife->rx_fifo_errors = atol(retp[4]); + ife->rx_multicast = atol(retp[7]); + ife->tx_bytes = atoll(retp[8]); + ife->tx_packets = atoll(retp[9]); + ife->tx_errors = atol(retp[10]); + ife->tx_dropped = atol(retp[11]); + ife->tx_fifo_errors = atol(retp[12]); + ife->collisions = atol(retp[13]); + ife->tx_carrier_errors = atol(retp[14]); + if( sys_nic_debug ) { + fprintf(stdout, "ife->rx_bytes:\t%lld\tife->tx_bytes:\t%lld\n",ife->rx_bytes,ife->tx_bytes); + } + return 0; +} +int main (int argc, char *argv[]){ + THMEM mem; + NETCARD_INFO_ST *curr; + NETCARD_INFO net; + + sprintf(net.charname,"%s","bond0"); + while (1){ + get_inc_stats(&net); + printf("ifname:%s, tx:%llu rx:%llu\n",net.charname, net.tx_bytes, net.rx_bytes); +// flush(NULL); + sleep(1); + } +} diff --git a/src/sys_nicmonitor/testt.c b/src/sys_nicmonitor/testt.c new file mode 100644 index 0000000..40467d9 --- /dev/null +++ b/src/sys_nicmonitor/testt.c @@ -0,0 +1,184 @@ +#include +#include +#include +#include +#include + + +#include "sys_netcard.h" +#define IPSIZE 16 +#define THRNR 10 +#define DAYS 25 +#define LINE_SIZE 2048 +#define HOST_NAME_SIZE 40 +#define BOND_PATH "/proc/net/bonding/*" +#define LOG_PATH "/var/log/netcard/" +#define SHM_PATH "/share/sys_netcard_shm_path" +#define SEM_PATH "/share/sys_netcard_sem_path" +#define MAX_GW 32 +#define DEV_MAXLEN 32 +#define IPLEN 16 +#define CONF_FILE "/conf/nic/sys_netcard_conf.txt" + +//#define MNIC_VERSION "1.9" + +#define NIC_UNKNOWN 0 +#define NIC_DOWN 1 +#define NIC_UNRUNNING 2 +#define NIC_UNLINKABLE 3 +#define NIC_NORMAL 4 + +//网卡设备描述结构 +typedef struct __nic_dev{ + char dev_name[DEV_MAXLEN]; //网卡名称 + char ping_ip[MAX_GW][IPLEN]; //ping地址列表,最多32个 + int gw_num; //地址列表的长度 +}NIC_DEV; + +typedef struct __config_file_st{ + unsigned char domain; + short serv; + short event; + int udpport; + int monitor_interval; + int write_interval; + int flow_interval; + int flow_limit; + int flow_peak; + char udp[NIC_NAME_LEN]; + NIC_DEV nic[MAXNICNUM]; //被监视网卡的列表 + char ip[IPSIZE]; + int pingnum; //OPTIONAL!! How many times for once ping check? Default is 2. + int pinglap; //OPTIONAL!! How many seconds does ping wait for reply package? Default is 1. + int crazyping; +}CONFIG_FILE_ST; + +typedef struct inc_name_node{ + char name[NIC_NAME_LEN]; + struct inc_name_node *next; +}NET_NAME_ST; + +typedef struct inc_info_node{ + NETCARD_INFO info; + int status; + struct inc_info_node *next; +}NETCARD_INFO_ST; + +typedef struct net_info{ + NETCARD_INFO info[MAXNICNUM]; +}SHM; + +typedef struct __thread_env_st{ + D5000_NIC_ALARM Malarm; + char bond_file_path[64]; + char host_name[HOST_NAME_SIZE]; + int host_name_size; + CONFIG_FILE_ST *conf; +}THENV; + +typedef struct __thread_mem_st{ + int semid; + SHM *shm_ptr; + NETCARD_INFO_ST *listp; + CONFIG_FILE_ST *conf; +}THMEM; + +typedef struct __thread_flow_st{ + THENV env; + THMEM mem; +}THFLOW; + +int sys_nic_debug = 1; +static void get_inc_stats(NETCARD_INFO *net); +int get_dev_fields(char *str, NETCARD_INFO *ife); +static char *prase_digit(char **s); +static void get_inc_stats(NETCARD_INFO *net) +{ + char linebuf[LINE_SIZE]; + char *retp = NULL; + char *str = malloc(sizeof(char) * 2048); + FILE *dev_fp; + + if((dev_fp = fopen("/proc/net/dev", "r")) == NULL)return; + memset(linebuf, 0, sizeof(linebuf)); + + + while((retp = fgets(linebuf, sizeof(linebuf), dev_fp)) != NULL){ + printf("linebuf: %s charname: %s\n", linebuf,net->charname); + + if((str = strstr(linebuf, net->charname)) != NULL){ + str=strstr(str, ":"); + str+=1; +// str +=strlen(net->charname) + 2; + get_dev_fields(str, net); + break; + } + } + fclose(dev_fp); +} +static char *prase_digit(char **s) +{ + char *retp = NULL; + char *p = *s; + + if(!p)return NULL; + while(!isdigit(*p)){ + p++; + if(*p == '\0')return NULL; + } + retp = p; + while(isdigit(*p)){ + p++; + if(*p == '\0'){ + *s = NULL; + return retp; + } + } + *p = '\0'; + p++; + *s = p; + return retp; +} + + +int get_dev_fields(char *str, NETCARD_INFO *ife) +{ + if( getenv("SYS_NIC_DEBUG") ) { + sys_nic_debug = 1; + } + fprintf(stdout, "str:%s\n",str); + + int i = 0; + char *retp[16]; + while((retp[i] = prase_digit(&str)) != NULL){i++;if(i > 15)break;} + ife->rx_bytes = atoll(retp[0]); + ife->rx_packets = atoll(retp[1]); + ife->rx_errors = atol(retp[2]); + ife->rx_dropped = atol(retp[3]); + ife->rx_fifo_errors = atol(retp[4]); + ife->rx_multicast = atol(retp[7]); + ife->tx_bytes = atoll(retp[8]); + ife->tx_packets = atoll(retp[9]); + ife->tx_errors = atol(retp[10]); + ife->tx_dropped = atol(retp[11]); + ife->tx_fifo_errors = atol(retp[12]); + ife->collisions = atol(retp[13]); + ife->tx_carrier_errors = atol(retp[14]); + if( sys_nic_debug ) { + fprintf(stdout, "ife->rx_bytes:\t%lld\tife->tx_bytes:\t%lld\n rx_str:\t%s\ttx_str:\t%s\n",ife->rx_bytes,ife->tx_bytes,retp[0],retp[8]); + } + return 0; +} +int main (int argc, char *argv[]){ + THMEM mem; + NETCARD_INFO_ST *curr; + NETCARD_INFO net; + + sprintf(net.charname,"%s","bond0"); + while (1){ + get_inc_stats(&net); + printf("ifname:%s, tx:%llu rx:%llu\n",net.charname, net.tx_bytes, net.rx_bytes); +// flush(NULL); + sleep(1); + } +}