diff --git a/code/trunk/sys_nicmonitor/src/sys_nicmonitor/Makefile b/code/trunk/sys_nicmonitor/src/sys_nicmonitor/Makefile index 3d12f16..cb76a76 100644 --- a/code/trunk/sys_nicmonitor/src/sys_nicmonitor/Makefile +++ b/code/trunk/sys_nicmonitor/src/sys_nicmonitor/Makefile @@ -4,7 +4,7 @@ DEFAULTS = Makefile.config include $(DEFAULTS) -all:sys_nicmonitor libnic_shm.so +all:sys_nicmonitor libnic_shm.so test CFLAGS += -Wall -Wformat=2 -Wno-format-extra-args -Wformat-security -Wformat-nonliteral #-g diff --git a/code/trunk/sys_nicmonitor/src/sys_nicmonitor/mnic.c b/code/trunk/sys_nicmonitor/src/sys_nicmonitor/mnic.c index 12c5b8d..c33c922 100644 --- a/code/trunk/sys_nicmonitor/src/sys_nicmonitor/mnic.c +++ b/code/trunk/sys_nicmonitor/src/sys_nicmonitor/mnic.c @@ -376,15 +376,12 @@ static void init_shm(SHM **ptr, CONFIG_FILE_ST *p) void* shmptr = NULL; int size = 0; key_t share_key; -// char *ref_count = NULL; - char *flag_str = NULL; char *path = shm_path; char error_str[200]; size = sizeof(SHM); size = size + 16; -// share_key = ftok(path, 0x000d5000); share_key = 0x1d5010; #if 0 @@ -409,7 +406,7 @@ static void init_shm(SHM **ptr, CONFIG_FILE_ST *p) return; } - flag_str = (char *)shmptr+size-16; + //flag_str = (char *)shmptr+size-16; // if( strcmp( flag_str, "D5001" ) != 0 ) { //first, do init. @@ -603,7 +600,8 @@ 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=strstr(str,":"); + str+=1; get_dev_fields(str, net); break; } @@ -703,8 +701,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,24 +732,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*8*100/1024/1024/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\n", curr->info.charname, count_t, summit, conf.flow_limit, curr->info.average_flow); + 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){ memset(ptr->env.Malarm.tSysNetcardAlarm.fault_devname, 0, NIC_NAME_LEN); strncpy(ptr->env.Malarm.tSysNetcardAlarm.fault_devname, ptr->env.host_name, ptr->env.host_name_size); @@ -768,8 +773,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); } @@ -803,7 +808,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...) */ @@ -1214,6 +1220,7 @@ int get_conf() // int i; // struct passwd *user; configfile_t *configfile; + char tmp_str[256]; // memset(buf, 0, sizeof(buf)); // uid=getuid(); @@ -1226,12 +1233,14 @@ int get_conf() configfile = dotconf_create(conf_path, options, NULL, CASE_INSENSITIVE); if (!configfile) { - record_log("Warning can't reading config file\n"); + sprintf(tmp_str,"%s","Warning can't reading config file\n"); + record_log(tmp_str); return -1; } if (dotconf_command_loop(configfile) == 0){ - record_log("Warning can't reading config file\n"); + sprintf(tmp_str,"%s","Warning can't reading config file\n"); + record_log(tmp_str); return -1; } dotconf_cleanup(configfile); diff --git a/code/trunk/sys_nicmonitor/src/sys_nicmonitor/testread.c b/code/trunk/sys_nicmonitor/src/sys_nicmonitor/testread.c new file mode 100644 index 0000000..080a444 --- /dev/null +++ b/code/trunk/sys_nicmonitor/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); + } +}