#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); } }