diff --git a/code/trunk/README b/README similarity index 100% rename from code/trunk/README rename to README diff --git a/branches/sys_nicmonitor-1.4-龙芯-32/Makefile b/branches/sys_nicmonitor-1.4-龙芯-32/Makefile deleted file mode 100644 index 95ab1ad..0000000 --- a/branches/sys_nicmonitor-1.4-龙芯-32/Makefile +++ /dev/null @@ -1,28 +0,0 @@ -#! /usr/bin/make -f - -DEFAULTS = Makefile.config - -include $(DEFAULTS) - -all:sys_nicmonitor libnic_shm.so - -CFLAGS += -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 $@ $^ -libnic_shm.so:nicinfo_shm.c - $(CC) -fpic -shared -o $@ $^ -clean: - $(RM) *.o - $(RM) libnic_shm.so sys_nicmonitor -install: - $(MKDIR) -p $(LIBDIR) - $(MKDIR) -p $(BINDIR) - $(MKDIR) -p $(INCDIR) - $(CP) libnic_shm.so $(LIBDIR) - $(CP) sys_nicmonitor $(BINDIR) - $(CP) nicinfo_shm.h $(INCDIR) -uninstall: - $(RM) -f $(LIBDIR)/libnic_shm.so - $(RM) -f $(BINDIR)/sys_nicmonitor - $(RM) -f $(INCDIR)/nicinfo_shm.h diff --git a/branches/sys_nicmonitor-1.4-龙芯-32/mnic.c b/branches/sys_nicmonitor-1.4-龙芯-32/mnic.c deleted file mode 100644 index 70021a7..0000000 --- a/branches/sys_nicmonitor-1.4-龙芯-32/mnic.c +++ /dev/null @@ -1,1513 +0,0 @@ -#include "mnic.h" -#include "dotconf.h" -#include -#include -#include "proc_inv.h" - -#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 "/tmp/sys_netcard_shm_path" -#define SEM_PATH "/tmp/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.4" - -#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. -}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 find_nic_config(char *); - -static pthread_mutex_t mut = PTHREAD_MUTEX_INITIALIZER; -static short seqno = 1; -static char log_flag = 1; -static int sem_id = 0; -static char process_name[64]; -char log_path[1024]; -char shm_path[1024]; -char sem_path[1024]; -char conf_path[1024]; - -int host_name_size = 0; -char host_name[HOST_NAME_SIZE]; -int semid = -1; -SHM *shm_ptr; - -//20100510 -int total_ping_time, elapsed_ping_time; -int get_netcard_status(NETCARD_INFO_ST *); - -/****dotconf****/ -static DOTCONF_CB(cb_str); -static DOTCONF_CB(cb_int); -int nicnum = 0; -CONFIG_FILE_ST conf; - -char myconf[][64] = { - "domain", // 0 - "serv", // 1 - "event", // 2 - "udpport", // 3 - "sys_netcard_shm_path", // 4 - "sys_netcard_sem_path", // 5 - "monitor_interval", // 6 - "write_interval", // 7 - "flow_interval", // 8 - "flow_limit", // 9 - "flow_peak", // 10 - "udp", // 11 - "nic", // 12 - "ping", // 13 - "pingnum", // 14 - "pinglap", // 15 - "" -}; - -static const configoption_t options[] = { - {myconf[0], ARG_INT, cb_int, NULL, CTX_ALL}, - {myconf[1], ARG_INT, cb_int, NULL, CTX_ALL}, - {myconf[2], ARG_INT, cb_int, NULL, CTX_ALL}, - {myconf[3], ARG_INT, cb_int, NULL, CTX_ALL}, - {myconf[4], ARG_STR, cb_str, NULL, CTX_ALL}, - {myconf[5], ARG_STR, cb_str, NULL, CTX_ALL}, - {myconf[6], ARG_INT, cb_int, NULL, CTX_ALL}, - {myconf[7], ARG_INT, cb_int, NULL, CTX_ALL}, - {myconf[8], ARG_INT, cb_int, NULL, CTX_ALL}, - {myconf[9], ARG_INT, cb_int, NULL, CTX_ALL}, - {myconf[10], ARG_INT, cb_int, NULL, CTX_ALL}, - {myconf[11], ARG_STR, cb_str, NULL, CTX_ALL}, - {myconf[12], ARG_STR, cb_str, NULL, CTX_ALL}, - {myconf[13], ARG_STR, cb_str, NULL, CTX_ALL}, - {myconf[14], ARG_INT, cb_int, NULL, CTX_ALL}, - {myconf[15], ARG_INT, cb_int, NULL, CTX_ALL}, - LAST_OPTION -}; -/****dotconf****/ - -/* write error in logfile */ -void record_log(char *str) -{ - time_t tamp; - char str_tm[4]; - char log_str[512]; - FILE *log_fp = NULL; - struct tm tmptr; - struct timeval tv; - struct timezone tz; - - if((log_fp = fopen(log_path, "a")) != NULL){ - tamp = time(NULL); - memset(str_tm, 0, sizeof(str_tm)); - memset(log_str, 0, sizeof(log_str)); - localtime_r(&tamp, &tmptr); - gettimeofday(&tv, &tz); - snprintf(str_tm, sizeof(str_tm), "%d", (int)tv.tv_usec/1000); - if(str_tm[1] == '\0')str_tm[1] = '0'; - if(str_tm[2] == '\0')str_tm[2] = '0'; - strftime(log_str, sizeof(log_str), "%F %T.", &tmptr); - strcat(log_str, str_tm); - strcat(log_str, " "); - strcat(log_str, process_name); - strcat(log_str, " "); - strcat(log_str, str); - if(fwrite(log_str, 1, strlen(log_str), log_fp) < 1){ - } - fclose(log_fp); - } -} - -static void init_sem(int *semid, CONFIG_FILE_ST *ptr) -{ - key_t key; - char error_str[200]; - -/* write error in logfile */ - memset(error_str, 0, sizeof(error_str)); - if ((key = ftok(sem_path, SEM_PROJ_ID)) == -1) { - snprintf(error_str, sizeof(error_str), "EMERG: ftok():%s\n", strerror(errno)); - record_log(error_str); - return; - } - if((*semid = semget(key, 1, IPC_CREAT|0666)) == -1){ - snprintf(error_str, sizeof(error_str), "EMERG: semget():%s\n", strerror(errno)); - record_log(error_str); - return; - } - if(semctl(*semid, 0, SETVAL, 0) == -1){ - snprintf(error_str, sizeof(error_str), "EMERG: semctl: %s !\n", strerror(errno)); - record_log(error_str); - semctl(*semid, 0, IPC_RMID); - return; - } -} - -static void init_shm(SHM **ptr, CONFIG_FILE_ST *p) -{ - int fd = -1; - char error_str[200]; - - mode_t mask_tmp; - mask_tmp = umask(0000); - if((fd = open(shm_path, O_RDWR|O_CREAT|O_TRUNC, 0666)) == -1){ - printf("could not create %s\n", shm_path); - return; - } - umask(mask_tmp); - lseek(fd, sizeof(SHM), SEEK_SET); - write(fd, "\0", 1); - *ptr = (SHM *)mmap(NULL, sizeof(SHM), PROT_READ|PROT_WRITE, MAP_SHARED, fd, 0); - if(*ptr == MAP_FAILED){ - snprintf(error_str, sizeof(error_str), "EMERG: mmap():%s\n", strerror(errno)); - record_log(error_str); - return; - } - close(fd); - -} - -static void get_sem(int semid) -{ - struct sembuf lock[2]; - char error_str[200]; - - lock[0].sem_num = 0; - lock[0].sem_op = 0; - lock[0].sem_flg = SEM_UNDO; - - lock[1].sem_num = 0; - lock[1].sem_op = 1; - lock[1].sem_flg = SEM_UNDO; - - while(semop(semid, lock, 2)){ - if(errno == EINTR)continue; - snprintf(error_str, sizeof(error_str), "EMERG: get_sem semop():%s\n", strerror(errno)); - record_log(error_str); - return; - } -} - -static void release_sem(int semid) -{ - int ret; - char error_str[200]; - struct sembuf unlock; - - unlock.sem_num = 0; - unlock.sem_op = -1; - unlock.sem_flg = SEM_UNDO; - - if((ret = semop(semid, &unlock, 1)) == -1){ - snprintf(error_str, sizeof(error_str), "EMERG: rel_sem semop():%s\n", strerror(errno)); - record_log(error_str); - return; - } -} - -static void proc_get_name(NET_NAME_ST *nodep) -{ - char linebuf[LINE_SIZE]; - char error_str[200]; - char *retp = NULL; - int err_val=0; - NET_NAME_ST *node = NULL; - FILE *dev_fp; - - if((dev_fp = fopen("/proc/net/dev", "rb")) == NULL){ - snprintf(error_str, sizeof(error_str), "EMERG: Can't open dev file:%s\n", strerror(errno)); - record_log(error_str); - return; - } - memset(linebuf, 0, sizeof(linebuf)); - fgets(linebuf, sizeof(linebuf), dev_fp); - fgets(linebuf, sizeof(linebuf), dev_fp); - while((retp = fgets(linebuf, sizeof(linebuf), dev_fp)) != NULL){ - if((node = (NET_NAME_ST *)malloc(sizeof(NET_NAME_ST))) == NULL){ - err_val = 1; - break; - } - memset(node->name, 0, NIC_NAME_LEN); - get_name(node->name, retp); - node->next = nodep->next; - nodep->next = node; - } - if(err_val){ - NET_NAME_ST *curr = nodep->next; - while(curr != NULL){ - node = curr->next; - free(curr); - curr = node; - } - } - fclose(dev_fp); -} - -static void ioctl_get_name(NET_NAME_ST *nodep) -{ - int count = 0; - int inuse = 0; - int err_val = 0; - NETCARD_INFO net_name[MAXNICNUM]; - NET_NAME_ST *node = NULL, *curr = NULL; - - memset(net_name, 0, sizeof(net_name)); - if((count = ioc_get_name(net_name)) == 0)return; - if(nodep->next == NULL) - inuse = 1; - while(count > 0){ - curr = nodep->next; - while(curr != NULL){ - if(!strncmp(net_name[count - 1].charname, curr->name, NIC_NAME_LEN)){ - inuse = 0; - break; - } - inuse = 1; - curr = curr->next; - } - if(inuse){ - if((node = (NET_NAME_ST *)malloc(sizeof(NET_NAME_ST))) == NULL){ - err_val = 1; - break; - } - memset(node->name, 0, NIC_NAME_LEN); - strncpy(node->name, net_name[count - 1].charname, NIC_NAME_LEN); - node->next = nodep->next; - nodep->next = node; - } - count--; - } - if(err_val){ - curr = nodep->next; - while(curr != NULL){ - node = curr->next; - free(curr); - curr = node; - } - } -} - -static void get_all_name(NET_NAME_ST *nodep) -{ - proc_get_name(nodep); - ioctl_get_name(nodep); -} - -static void create_monit_list(NET_NAME_ST *nodep, NETCARD_INFO_ST *info_list, char *bond) -{ - int err_val = 0; - NETCARD_INFO_ST *info_node = NULL; - NET_NAME_ST *curr, *node; - - curr = nodep->next; - while(curr != NULL){ - if((info_node = (NETCARD_INFO_ST *)malloc(sizeof(NETCARD_INFO_ST))) == NULL){ - err_val = 1; - break; - } - memset(info_node, 0, sizeof(NETCARD_INFO_ST)); - strncpy(info_node->info.charname, curr->name, NIC_NAME_LEN); - strncpy(info_node->info.descr, bond, strlen(bond)); - info_node->next = info_list->next; - info_list->next = info_node; - curr = curr->next; - } - if(err_val){ - NETCARD_INFO_ST *info_curr = info_list->next; - while(info_curr != NULL){ - info_node = info_curr->next; - free(info_curr); - info_curr = info_node; - } - curr = nodep->next; - while(curr != NULL){ - node = curr->next; - free(curr); - curr = node; - } - } -} - -static int get_inc_info(NETCARD_INFO *net) -{ - return if_fetch(net); -} - -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){ - if((str = strstr(linebuf, net->charname)) != NULL){ - str +=strlen(net->charname) + 2; - get_dev_fields(str, net); - break; - } - } - fclose(dev_fp); -} - -static void send_inc_info_one(NETCARD_INFO *net, int semid, SHM *ptr) -{ - int i; - for(i = 0; ptr->info[i].charname[0] != 0; i++){ - if(!strncmp(ptr->info[i].charname, net->charname, NIC_NAME_LEN)){ - get_sem(semid); - memcpy(&ptr->info[i], net, sizeof(NETCARD_INFO)); - release_sem(semid); - } - } -} - -static void *send_inc_info(void *p) -{ - int i = 0; - int flags = 1; - char error_str[200]; - unsigned long long bytes[MAXNICNUM]; - THMEM *ptr = (THMEM *)p; - NETCARD_INFO_ST *curr; - struct timespec s_time, os_time; - - if((curr = ptr->listp->next) == NULL){ - pthread_mutex_lock(&mut); - snprintf(error_str, sizeof(error_str), "EMERG: Empty Netcard list!\n"); - record_log(error_str); - pthread_mutex_unlock(&mut); - return NULL; - } - s_time.tv_sec = ptr->conf->write_interval; - s_time.tv_nsec = 0; - - /* copy all net_info*/ - while(1){ - i = 0; - get_sem(ptr->semid); - memset(ptr->shm_ptr, 0, sizeof(SHM)); - curr = ptr->listp->next; - while(curr != NULL){ - get_inc_info(&curr->info); - if(curr->status == NIC_UNLINKABLE){ - curr->info.flags &= (~IFF_UP); - curr->info.flags &= (~IFF_RUNNING); - } - get_inc_stats(&curr->info); - if(flags){ - bytes[i] = curr->info.rx_bytes + curr->info.tx_bytes; - } - curr->info.average_flow =(unsigned long long)((curr->info.rx_bytes + curr->info.tx_bytes - bytes[i])/(ptr->conf->write_interval)); - memcpy(&ptr->shm_ptr->info[i], &curr->info, sizeof(NETCARD_INFO)); - bytes[i] = curr->info.rx_bytes + curr->info.tx_bytes; - i++; - curr = curr->next; - } - release_sem(ptr->semid); - flags = 0; - nanosleep(&s_time, &os_time); - } -} - -static int get_netcard_count(char *name) -{ - int count = 0; - FILE *fp = NULL; - char path[64] = "/proc/net/bonding/"; - char buf[LINE_SIZE]; - char *retp = NULL; - char *str = NULL; - - strncat(path, name, strlen(name)); - if((fp = fopen(path, "r")) == NULL)return 1; - - memset(buf, 0, sizeof(buf)); - fseek(fp, 65, SEEK_SET); - while((retp = fgets(buf, sizeof(buf), fp)) != NULL){ - if((str = strstr(buf, "active-backup")) != NULL){ - fclose(fp); - return 1; - } - if(!strncmp(buf, "Slave Interface:", 16))count++; - } - fclose(fp); - return count; -} - -static void *cacu_flow(void *p) -{ - int i = 0, count_t = 1; - int summit = 0; - int flags = 1; - int count_ok[MAXNICNUM]; - int count_no[MAXNICNUM]; - char error_str[200]; - unsigned long long bytes[MAXNICNUM]; - THFLOW *ptr = (THFLOW *)p; - NETCARD_INFO_ST *curr; - struct timespec s_time, os_time; - - if((curr = ptr->mem.listp->next) == NULL){ - pthread_mutex_lock(&mut); - snprintf(error_str, sizeof(error_str), "EMERG: Empty Netcard list!\n"); - record_log(error_str); - pthread_mutex_unlock(&mut); - return NULL; - } - s_time.tv_sec = ptr->mem.conf->flow_interval; - s_time.tv_nsec = 0; - - for(i = 0; i < MAXNICNUM ; i++){ - count_ok[i] = 0; - count_no[i] = 0; - } - while(1){ - i = 0; - curr = ptr->mem.listp->next; - while(curr != NULL){ - get_inc_info(&curr->info); - get_inc_stats(&curr->info); - if(flags){ - bytes[i] = curr->info.rx_bytes + 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); - 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); -#if 0 - printf("%s:\tcount=%d\tsummit=%d\tflow_limit:%d\taverage_flow:%d\n", curr->info.charname, count_t, summit, conf.flow_limit, curr->info.average_flow); -#endif - 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); - strncat(ptr->env.Malarm.tSysNetcardAlarm.fault_devname, "-", 1); - strncat(ptr->env.Malarm.tSysNetcardAlarm.fault_devname, curr->info.charname, (NIC_NAME_LEN - ptr->env.host_name_size - 1)); - ptr->env.Malarm.tSysNetcardAlarm.flags = NETCARD_ALARM_ABNORMAL; - ptr->env.Malarm.tSysNetcardAlarm.retrytimes = 0; - ptr->env.Malarm.tMsgFrame.len = sizeof(ptr->env.Malarm); - ptr->env.Malarm.tMsgFrame.seqno = seqno; - pthread_mutex_lock(&mut); - while(count_no[i] > 0){ - send_alarm(&ptr->env.Malarm, ptr->mem.conf->udpport, ptr->mem.conf->ip); - seqno++; - ptr->env.Malarm.tMsgFrame.seqno = seqno; - ptr->env.Malarm.tSysNetcardAlarm.retrytimes++; - count_no[i]--; - if(count_no[i] == 0){ - send_inc_info_one(&curr->info, ptr->mem.semid, ptr->mem.shm_ptr); - //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); - snprintf(error_str, sizeof(error_str), "NOTICE: %s flow is abnormal !\n", curr->info.charname); - record_log(error_str); - } - } - pthread_mutex_unlock(&mut); - count_ok[i] = 3; - }else{ - 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); - strncat(ptr->env.Malarm.tSysNetcardAlarm.fault_devname, "-", 1); - strncat(ptr->env.Malarm.tSysNetcardAlarm.fault_devname, curr->info.charname, (NIC_NAME_LEN - ptr->env.host_name_size - 1)); - ptr->env.Malarm.tSysNetcardAlarm.flags = NETCARD_ALARM_NORMAL; - ptr->env.Malarm.tSysNetcardAlarm.retrytimes = 0; - ptr->env.Malarm.tMsgFrame.len = sizeof(ptr->env.Malarm); - ptr->env.Malarm.tMsgFrame.seqno = seqno; - pthread_mutex_lock(&mut); - while(count_ok[i] > 0){ - send_alarm(&ptr->env.Malarm, ptr->mem.conf->udpport, ptr->mem.conf->ip); - seqno++; - ptr->env.Malarm.tMsgFrame.seqno = seqno; - ptr->env.Malarm.tSysNetcardAlarm.retrytimes++; - count_ok[i]--; - if(count_ok[i] == 0){ - send_inc_info_one(&curr->info, ptr->mem.semid, ptr->mem.shm_ptr); - snprintf(error_str, sizeof(error_str), "NOTICE: %s flow is normal now !\n", curr->info.charname); - record_log(error_str); - } - } - pthread_mutex_unlock(&mut); - count_no[i] = 3; - }/* end if(summit... */ - bytes[i] = curr->info.rx_bytes + curr->info.tx_bytes; - i++; - curr = curr->next; - }/* end while(curr...) */ - flags = 0; - nanosleep(&s_time, &os_time); - }/* end while(1) */ -} -static char *parse_str(char **s) -{ - char *retp = NULL; - char *p = *s; - - while(isspace(*p))p++; - retp = p; - while(!isspace(*p))p++; - *p = '\0'; - - return retp; -} - -int ping_gw(char *devname) -{ - int i, ret = -1, j, k; - char buf[64]; - - i = find_nic_config(devname); - if( i == -1 ) - return 0; - - if(conf.nic[i].gw_num == 0) - return 0; - - for(k=0; k /dev/null 2>&1", conf.pinglap,conf.nic[i].ping_ip[j]); - ret=system(buf); -#if 0 - if (WIFSIGNALED(ret) && - (WTERMSIG(ret) == SIGINT || WTERMSIG(ret) == SIGQUIT)) - exit(0); -#endif - if(ret == 0) - return 0; - - } - } - - return -1; -} - -static void *check_inc_switch(void *ptr) -{ - THENV *arg = (THENV *)ptr; - FILE *fp = NULL; - char name[NIC_NAME_LEN]; - struct timespec s_time, os_time; - int count = 0; - char linebuf[LINE_SIZE]; - char error_str[200]; - char *str = NULL; - char *retp = NULL; - - if((fp = fopen(arg->bond_file_path, "r")) == NULL){ - snprintf(error_str, sizeof(error_str), "EMERG: fopen():%s\n", strerror(errno)); - record_log(error_str); - free(ptr); - return NULL; - } - fseek(fp, 65, SEEK_SET); - memset(name, 0, sizeof(name)); - memset(linebuf, 0, sizeof(linebuf)); - if((retp = fgets(linebuf, sizeof(linebuf), fp)) != NULL){ - if((retp = strstr(linebuf, "active-backup")) == NULL){ - free(ptr); - fclose(fp); - return NULL; - } - } - - s_time.tv_sec = 0; - s_time.tv_nsec = 100000000; - - while(1){ - while((str = fgets(linebuf, sizeof(linebuf), fp)) != NULL){ - if(!strncmp(linebuf, "Currently Active Slave:", 23)){ - str += 23; - retp = parse_str(&str); - break; - } - } - if(name[0] == 0){ - strncpy(name, retp, strlen(retp)); - }else if(strncmp(name, retp, strlen(retp)) != 0){ - memset(arg->Malarm.tSysNetcardAlarm.switch_devname, 0, NIC_NAME_LEN); - strncpy(arg->Malarm.tSysNetcardAlarm.switch_devname, arg->host_name, arg->host_name_size); - strncat(arg->Malarm.tSysNetcardAlarm.switch_devname, "-", 1); - strncat(arg->Malarm.tSysNetcardAlarm.switch_devname, retp, (NIC_NAME_LEN - arg->host_name_size - 1)); - arg->Malarm.tSysNetcardAlarm.flags = NETCARD_ALARM_SWITCH; - arg->Malarm.tSysNetcardAlarm.retrytimes = 0; - arg->Malarm.tMsgFrame.len = sizeof(arg->Malarm); - arg->Malarm.tMsgFrame.seqno = seqno; - count = 3; - pthread_mutex_lock(&mut); - snprintf(error_str, sizeof(error_str), "NOTICE: Net card checking to %s!\n", retp); - record_log(error_str); - while(count > 0){ - send_alarm(&arg->Malarm, arg->conf->udpport, arg->conf->ip); - seqno++; - arg->Malarm.tMsgFrame.seqno = seqno; - arg->Malarm.tSysNetcardAlarm.retrytimes++; - count--; - } - pthread_mutex_unlock(&mut); - memset(name, 0, NIC_NAME_LEN); - strncpy(name, retp, strlen(retp)); - } - fclose(fp); - nanosleep(&s_time, &os_time); - if((fp = fopen(arg->bond_file_path, "r")) == NULL){ - free(ptr); - return NULL; - } - fseek(fp, 65, SEEK_SET); - } - free(ptr); - fclose(fp); - return NULL; -} - -static void *create_logfile(void *ptr) -{ - char buf[128]; - char del_file[DAYS][1024]; - char *s; - int head, tail, fd; - int flag = 0, del_flag = 0; - int old_mon = 0; - int old_day = 0; - time_t tamp; - struct tm tmptr; - struct timespec s_time, os_time; - - s_time.tv_sec = 1; - s_time.tv_nsec = 0; - head = tail = 0; - while(1){ - tamp = time(NULL); - localtime_r(&tamp, &tmptr); - if(old_mon != (tmptr.tm_mon + 1))flag = 1; - else if(old_day != tmptr.tm_mday)flag = 1; - else flag = 0; - if(flag){ - s = strrchr(log_path, '/'); - s++; - *s = '\0'; - memset(buf, 0, sizeof(buf)); - strftime(buf, sizeof(buf), "%Y%m%d_", &tmptr); - strcat(buf, process_name); - strcat(buf, ".log"); - strcat(log_path, buf); - mode_t mask_tmp; - mask_tmp = umask(0000); - if((fd = open(log_path, O_RDWR|O_CREAT, 0666)) != -1){ - log_flag = 0; - close(fd); - } - umask(mask_tmp); - old_mon = tmptr.tm_mon+1; - old_day = tmptr.tm_mday; - strcpy(del_file[head++], log_path); - if(head == DAYS){ - head = 0; - del_flag = 1; - } - if(del_flag){ - unlink(del_file[tail]); - tail++; - if(tail == DAYS)tail = 0; - } - } - nanosleep(&s_time, &os_time); - } -} - -void write_time(int semid, SHM *ptr) -{ - int i = 0; - int flag_time_stamp; - - flag_time_stamp = time(NULL); - - while(ptr->info[i].charname[0] != 0){ - ptr->info[i].time_stamp = flag_time_stamp; - i++; - } -} -#if 0 -char *parse(char *line) -{ - char *begin = NULL; - char *end = NULL; - - if((begin = strchr(line, '/')) != NULL){ - if((end = strstr(line, "bin")) != NULL){ - *end='\0'; - return begin; - } - } - return NULL; -} -#endif -void release_name_list(NET_NAME_ST *head) -{ - NET_NAME_ST *curr, *save; - - curr = head->next; - while(curr != NULL){ - save = curr->next; - free(curr); - curr = save; - } -} -int get_netcard(char *line, char *nic) -{ - char *begin, *retp; - - while(isspace(*line))line++; - begin = line; - while(!isspace(*line))line++; - *line++ = '\0'; - if(!strncmp(begin, "nic", 4)){ - if((retp = parse_str(&line)) != NULL){ - strncpy(nic, retp, MAXNICNUM); - return 1; - } - } - return 0; -} - -void sig_handler(int sig) -{ -// char err_str[200]; - -#if 0 - if(unlink(SHM_PATH) == -1){ - snprintf(err_str, sizeof(err_str), "NOTICE: error delete %s\n", strerror(errno)); - record_log(err_str); - } - if(unlink(SEM_PATH) == -1){ - snprintf(err_str, sizeof(err_str), "NOTICE: error delete %s\n", strerror(errno)); - record_log(err_str); - } -#endif -// semctl(sem_id, 0, IPC_RMID); - -// snprintf(err_str, sizeof(err_str), "NOTICE: process exit by signal %d\n", sig); -// record_log(err_str); - exit(0); -} - -/* - * 查看数据结构中是否有给定的网卡 - * 输入:网卡名称 - * 返回值:-1 表示没有 - * 非0值表示设备列表中的索引 - * - */ -int find_nic_config(char *dev_name){ - int i, index=-1; - - for(i=0; iname, myconf[13]) == 0) && (nicnum <= MAXNICNUM)){ - i = 1; - if(cmd->data.list[0] && cmd->data.list[1]){ - //查找设备列表中是否已有该网卡 - //如果没有则创建一项 - //如果已有则只要增加ping的地址和地址数量 - //一般都能找到,因为配置文件中一般总是先定义"nic",后定义"ping" - //如果没找到,则表明先定义了"ping",未定义"nic"或后定义"nic",而未定义"nic"应该是错误的 - index = find_nic_config(cmd->data.list[0]); - if(index == -1){ - strncpy(conf.nic[nicnum].dev_name, cmd->data.list[0], strlen(cmd->data.list[0]) >= IPLEN ? IPLEN-1 : strlen(cmd->data.list[0])); - card_info_index = nicnum; - nicnum++; - } - else - card_info_index = index; - - while(cmd->data.list[i]){ - if((int)(inet_addr(cmd->data.list[i])) == -1){ - sprintf(wrong_ip,"The wrong IP address:'%s'",cmd->data.list[i]); - strcat(wrong_ip,"\n"); - record_log(wrong_ip); - } else{ - strncpy(conf.nic[card_info_index].ping_ip[conf.nic[card_info_index].gw_num++], cmd->data.list[i], strlen(cmd->data.list[i]) >= IPLEN ? IPLEN-1 : strlen(cmd->data.list[i])); - } - i++; - } - } - } - - //"nic"选项的处理 - if((strcmp(cmd ->name, myconf[12]) == 0) && (nicnum <= MAXNICNUM)){ - //设备列表中没有给定的网卡设备,则添加一项 - //否则不必添加 - //对应配置文件中先指定"ping"后指定"nic"的情况 - index = find_nic_config(cmd->data.str); - if(index == -1){ - strncpy(conf.nic[nicnum].dev_name, cmd->data.str, strlen(cmd->data.str) >= NIC_NAME_LEN ? NIC_NAME_LEN-1 : strlen(cmd->data.str)); - nicnum++; - } - } - - if((strcmp(cmd ->name, myconf[11]) == 0)){ - strncpy(conf.udp, cmd->data.str, strlen(cmd->data.str)); - } - - return NULL; -} - -DOTCONF_CB(cb_int) -{ - if(strcmp(cmd ->name, myconf[0]) == 0){ - conf.domain = cmd->data.value; - } - if(strcmp(cmd ->name, myconf[1]) == 0){ - conf.serv = cmd->data.value; - } - if(strcmp(cmd ->name, myconf[2]) == 0){ - conf.event = cmd->data.value; - } - if(strcmp(cmd ->name, myconf[3]) == 0){ - conf.udpport = cmd->data.value; - } - if(strcmp(cmd ->name, myconf[6]) == 0){ - conf.monitor_interval = cmd->data.value; - } - if(strcmp(cmd ->name, myconf[7]) == 0){ - conf.write_interval = cmd->data.value; - } - if(strcmp(cmd ->name, myconf[8]) == 0){ - conf.flow_interval = cmd->data.value; - } - if(strcmp(cmd ->name, myconf[9]) == 0){ - conf.flow_limit = cmd->data.value; - } - if(strcmp(cmd ->name, myconf[10]) == 0){ - conf.flow_peak = cmd->data.value; - } - if(strcmp(cmd ->name, myconf[14]) == 0){ - conf.pingnum = cmd->data.value; - } - if(strcmp(cmd ->name, myconf[15]) == 0){ - conf.pinglap = cmd->data.value; - } - - return NULL; -} - -DOTCONF_CB(cb_list) -{ - return NULL; -} - -int get_conf() -{ -// int uid; -// char buf[1024]; -// int i; -// struct passwd *user; - configfile_t *configfile; - -// memset(buf, 0, sizeof(buf)); -// uid=getuid(); -// user = getpwnam("d5000"); -// user = getpwuid(uid); - -// sprintf(buf,"%s", user->pw_dir); -// strcat(buf,conf_path); - - configfile = dotconf_create(conf_path, options, NULL, CASE_INSENSITIVE); - if (!configfile) - { - record_log("Warning can't reading config file\n"); - return -1; - } - - if (dotconf_command_loop(configfile) == 0){ - record_log("Warning can't reading config file\n"); - return -1; - } - dotconf_cleanup(configfile); - - if(conf.domain == 0) - conf.domain = 10; - if(conf.serv == 0) - conf.serv = 1; - if(conf.event == 0) - conf.event = 2; - if(conf.udpport == 0) - conf.udpport = 15000; - if(conf.monitor_interval == 0) - conf.monitor_interval = 100; - if(conf.write_interval == 0) - conf.write_interval = 300; - if(conf.flow_interval == 0) - conf.flow_interval = 60; - if(conf.flow_limit == 0) - conf.flow_limit = 30; - if(conf.flow_peak == 0) - conf.flow_peak = 1000; - if(conf.udp[0] == 0) - strcpy(conf.udp, "bond0"); - if(conf.nic[0].dev_name[0] == 0) - strcpy(conf.nic[0].dev_name, "bond0"); - if(conf.pingnum == 0) - conf.pingnum = 2; - if(conf.pinglap == 0) - conf.pinglap = 1; - - return 0; -} - -int isrun(char *p) -{ - char buf[16]; - char cmdbuf[1024]; - FILE *fp; - - memset(buf, 0, sizeof(buf)); - memset(cmdbuf, 0, sizeof(cmdbuf)); - - snprintf(cmdbuf, sizeof(cmdbuf), "pidof %s", p); - fp=popen(cmdbuf, "r"); - fread(buf, sizeof(char), sizeof(buf), fp); - pclose(fp); - if(strchr(buf, ' ') != NULL) - return 1; - else - return 0; -} - -int get_broad_ip(void) -{ - char ip_p[4][4]; - char error_str[200]; - int skfd; - struct ifreq ifr; - - memset(&ifr, 0, sizeof(ifr)); - memset(ip_p, 0, sizeof(ip_p)); - - if((skfd = socket(PF_INET, SOCK_DGRAM, 0)) == -1)return -1; - strcpy(ifr.ifr_name, conf.udp); - if (!ioctl(skfd, SIOCGIFBRDADDR, &ifr)){ - sprintf(ip_p[0], "%d", (unsigned char)ifr.ifr_broadaddr.sa_data[2]); - sprintf(ip_p[1], "%d", (unsigned char)ifr.ifr_broadaddr.sa_data[3]); - sprintf(ip_p[2], "%d", (unsigned char)ifr.ifr_broadaddr.sa_data[4]); - sprintf(ip_p[3], "%d", (unsigned char)ifr.ifr_broadaddr.sa_data[5]); - }else{ - snprintf(error_str, sizeof(error_str), "EMERG: Invalid broadcast device -- %s\n", conf.udp); - record_log(error_str); - close(skfd); - return -1; - } - close(skfd); - strcat(conf.ip, ip_p[0]); - strcat(conf.ip, "."); - strcat(conf.ip, ip_p[1]); - strcat(conf.ip, "."); - strcat(conf.ip, ip_p[2]); - strcat(conf.ip, "."); - strcat(conf.ip, ip_p[3]); - - return 0; -} - -void create_dir(char *create_path) -{ - int i,len = strlen(create_path); - - len = strlen(create_path); - - for(i=1; inext; - while(curr != NULL){ - if(!strncmp(curr->name, conf.nic[i].dev_name, NIC_NAME_LEN)){ - if((node = (NET_NAME_ST *)malloc(sizeof(NET_NAME_ST))) == NULL){ - err_val = 1; - break; - } - memset(node->name, 0, NIC_NAME_LEN); - strncpy(node->name, conf.nic[i].dev_name, NIC_NAME_LEN); - node->next = opt_name_list->next; - opt_name_list->next = node; - count_t ++; - break; - } - curr = curr->next; - } - if(err_val)break; - } - if(count_t != i || err_val){ - for(curr = opt_name_list->next; curr != NULL; curr = curr->next)free(curr); - snprintf(error_str, sizeof(error_str), "EMERG: Invalid netcard name --\n"); - record_log(error_str); - return -1; - } - err_val = 0; - - return 0; -} - -int get_netcard_status(NETCARD_INFO_ST *listp) -{ - if((listp->info.flags & IFF_UP) != IFF_UP) - return NIC_DOWN; - - if((listp->info.flags & IFF_RUNNING) != IFF_RUNNING) - return NIC_UNRUNNING; - - //20100510 - if(elapsed_ping_time == total_ping_time){ - if(ping_gw(listp->info.charname) != 0) - return NIC_UNLINKABLE; - } - else{ - if(listp->status == NIC_UNLINKABLE) - return NIC_UNLINKABLE; - } - - return NIC_NORMAL; -} - -int do_alarm(NETCARD_INFO_ST *listp, int nic_status, D5000_NIC_ALARM *Malarm) -{ - int i; - char error_str[200]; - - memset(Malarm->tSysNetcardAlarm.fault_devname, 0, NIC_NAME_LEN); - strncpy(Malarm->tSysNetcardAlarm.fault_devname, host_name, host_name_size); - strncat(Malarm->tSysNetcardAlarm.fault_devname, "-", 1); - strncat(Malarm->tSysNetcardAlarm.fault_devname, listp->info.charname, NIC_NAME_LEN - host_name_size - 1); - Malarm->tSysNetcardAlarm.retrytimes = 0; - Malarm->tMsgFrame.len = sizeof(Malarm); - Malarm->tMsgFrame.seqno = seqno; - - switch(nic_status){ - case NIC_DOWN: - Malarm->tSysNetcardAlarm.flags = NETCARD_ALARM_FAULT; - snprintf(error_str, sizeof(error_str), "NOTICE: %s is DOWN!\n", listp->info.charname); - break; - case NIC_UNRUNNING: - Malarm->tSysNetcardAlarm.flags = NETCARD_ALARM_FAULT; - snprintf(error_str, sizeof(error_str), "NOTICE: %s is UP but UNRUNNING!\n", listp->info.charname); - break; - case NIC_UNLINKABLE: - Malarm->tSysNetcardAlarm.flags = NETCARD_ALARM_FAULT; - listp->info.flags &= (~IFF_UP); - listp->info.flags &= (~IFF_RUNNING); - snprintf(error_str, sizeof(error_str), "NOTICE: %s is RUNNING but UNLINKABLE!\n", listp->info.charname); - break; - case NIC_NORMAL: - Malarm->tSysNetcardAlarm.flags = NETCARD_ALARM_RESUME; - snprintf(error_str, sizeof(error_str), "NOTICE: %s is NORMAL!\n", listp->info.charname); - break; - } - pthread_mutex_lock(&mut); - for(i = 0; i < 3; i++){ - send_alarm(Malarm, conf.udpport, conf.ip); - seqno++; - Malarm->tMsgFrame.seqno = seqno; - Malarm->tSysNetcardAlarm.retrytimes++; - } - send_inc_info_one(&listp->info, semid, shm_ptr); - pthread_mutex_unlock(&mut); - record_log(error_str); - - return 0; -} - -int main(int argc, char ** argv) -{ - int ret = 0, proc_stat = 0; - unsigned int i = 0; - int nic_status; - int sys_time_stamp = 0; - char *retp = NULL; - char *str = NULL; - char bond[128]; - char error_str[200]; - char linebuf[LINE_SIZE]; - THENV *arg; - glob_t res; - FILE *bond_fp = NULL; - pthread_t tid[THRNR]; - pthread_t tid_mem; - pthread_t tid_flow; - pthread_t tid_time; - THMEM mem; - THFLOW flow; - struct timespec s_time, os_time, log_time; - NET_NAME_ST all_name_list, opt_name_list; - NETCARD_INFO_ST inc_info_list, *listp = NULL; - D5000_NIC_ALARM Malarm; - struct sigaction sig; - struct passwd *user; - proc_invocation prcm; - - memset(&conf, 0, sizeof(CONFIG_FILE_ST)); - if(isrun(argv[0]) == 1){ - printf("the program is already started!\n"); - exit(0); - } -//process register -#if 1 - if((proc_stat=prcm.proc_init("sys","base_srv","sys_nicmonitor"))==-1){ - perror("proc_init()"); - exit(-1); - } -#endif - - sig.sa_handler = sig_handler; - sigemptyset(&sig.sa_mask); - sigaddset(&sig.sa_mask, SIGINT); - sigaddset(&sig.sa_mask, SIGTERM); - sig.sa_flags = 0; - sigaction(SIGTERM, &sig, NULL); - sigaction(SIGINT, &sig, NULL); - sigaction(SIGSEGV, &sig, NULL); - - /* create dir file path */ - if((user = getpwnam("d5000")) != NULL){ - sprintf(log_path,"%s",user->pw_dir); - sprintf(shm_path,"%s",user->pw_dir); - sprintf(sem_path,"%s",user->pw_dir); - sprintf(conf_path,"%s",user->pw_dir); - } - strcat(log_path,LOG_PATH); - strcat(shm_path,SHM_PATH); - strcat(sem_path,SEM_PATH); - strcat(conf_path,CONF_FILE); - - create_semdir(); - create_shmdir(); - create_logdir(); - create_confdir(); - - /* get process name */ - memset(process_name, 0, sizeof(process_name)); - if((retp = strrchr(argv[0], '/')) != NULL){ - retp++; - strcpy(process_name, retp); - }else{ - strcpy(process_name, argv[0]); - } - /*create logfile thread */ - pthread_create(&tid_time, NULL, create_logfile, NULL); - - /* initialize some....*/ - memset(&all_name_list, 0, sizeof(NET_NAME_ST)); - all_name_list.next = NULL; - memset(&opt_name_list, 0, sizeof(NET_NAME_ST)); - opt_name_list.next = NULL; - - memset(&inc_info_list, 0, sizeof(NETCARD_INFO_ST)); - inc_info_list.next = NULL; - - - /* get all netcard by proc/net/dev and ioctl */ - get_all_name(&all_name_list); - - /* wait for create log thread */ - log_time.tv_sec = 0; - log_time.tv_nsec = 1000000; - while(log_flag)nanosleep(&log_time, NULL); - - /* sure the process is started */ - snprintf(error_str, sizeof(error_str), "NOTICE: %s V%s is started !\n", process_name, MNIC_VERSION); - record_log(error_str); - - get_conf(); - - /* get broadcast address*/ - if((ret = get_broad_ip()) != 0) - return -1; - if((ret = create_name_list(&all_name_list, &opt_name_list)) != 0) - return -1; - /* get hostname */ - if((ret = gethostname(host_name, sizeof(host_name))) != -1){ - host_name_size = strlen(host_name); - }else host_name_size = HOST_NAME_SIZE; - /* initialize Malarm */ - Malarm.tMsgFrame.serv = conf.serv; - Malarm.tMsgFrame.event = conf.event; - Malarm.tMsgFrame.domain = conf.domain; - - /* get bonding file */ - memset(bond, 0, sizeof(bond)); - memset(linebuf, 0, sizeof(linebuf)); - if((ret = glob(BOND_PATH, 0, NULL, &res)) != 0){ - snprintf(error_str, sizeof(error_str), "NOTICE: No bonding netcard !\n"); - record_log(error_str); - strcpy(bond, "No bonding netcard!"); - } - for (i = 0; i < res.gl_pathc; ++i) { - if((arg = (THENV *)malloc(sizeof(THENV))) != NULL){ - memset(arg, 0, sizeof(THENV)); - strncpy(arg->bond_file_path, res.gl_pathv[i], strlen(res.gl_pathv[i])); - arg->Malarm = Malarm; - strncpy(arg->host_name, host_name, host_name_size); - arg->host_name_size = host_name_size; - arg->conf = &conf; - if(pthread_create(tid+i, NULL, check_inc_switch, (void*)arg)){ - snprintf(error_str, sizeof(error_str), "EMERG: pthread_create(): %s\n", strerror(errno)); - record_log(error_str); - } - } - } - for(i = 0; i < res.gl_pathc; ++i){ - if((bond_fp = fopen(res.gl_pathv[i], "r")) == NULL){ - snprintf(error_str, sizeof(error_str), "EMERG: fopen(): %s\n", strerror(errno)); - record_log(error_str); - }else{ - retp = strrchr(res.gl_pathv[i], '/'); - retp++; - strcat(bond, retp); - strcat(bond, ":"); - while((str = fgets(linebuf, sizeof(linebuf), bond_fp)) != NULL){ - if(!strncmp(linebuf, "Slave Interface:", 16)){ - str += 16; - retp = parse_str(&str); - strcat(bond, retp); - strcat(bond, " "); - } - } - fclose(bond_fp); - } - } - - /* initialize time*/ - s_time.tv_sec = 0; - s_time.tv_nsec = conf.monitor_interval*1000*1000; - - /* initialize sem*/ - init_sem(&semid, &conf); - init_shm(&shm_ptr, &conf); - /* for IPC_RMID */ - sem_id = semid; - - /* create monitor netcard list */ - create_monit_list(&opt_name_list, &inc_info_list, bond); - release_name_list(&all_name_list); - release_name_list(&opt_name_list); - - mem.semid = semid; - mem.shm_ptr = shm_ptr; - mem.listp = &inc_info_list; - mem.conf = &conf; - if(pthread_create(&tid_mem, NULL, send_inc_info, (void *)&mem)){ - snprintf(error_str, sizeof(error_str), "EMERG: pthread_create(): %s\n", strerror(errno)); - record_log(error_str); - } - - /* create caculate flow thread*/ - flow.env.Malarm = Malarm; - strncpy(flow.env.host_name, host_name, host_name_size); - flow.env.host_name_size = host_name_size; - flow.mem.semid = semid; - flow.mem.shm_ptr = shm_ptr; - flow.mem.listp = &inc_info_list; - flow.mem.conf = &conf; - if(pthread_create(&tid_flow, NULL, cacu_flow, (void *)&flow)){ - snprintf(error_str, sizeof(error_str), "EMERG: pthread_create(): %s\n", strerror(errno)); - record_log(error_str); - } - - total_ping_time = 1000 / conf.monitor_interval; - if(total_ping_time == 0) - total_ping_time = 1; - elapsed_ping_time = 1; - - while(1){ - i = 0; - listp = inc_info_list.next; - for(i = 0; listp != NULL; i++){ - get_inc_info(&listp->info); - get_inc_stats(&listp->info); - nic_status = get_netcard_status(listp); - if(nic_status != listp->status){ - do_alarm(listp, nic_status, &Malarm); - listp->status = nic_status; - } - listp = listp->next; - } - - if(seqno > 32765)seqno = 0; - nanosleep(&s_time, &os_time); - - - //20100510 - ++elapsed_ping_time; - if(elapsed_ping_time > total_ping_time) - elapsed_ping_time = 1; - - sys_time_stamp += conf.monitor_interval; - if(sys_time_stamp >= 1000){ - sys_time_stamp = 0; - write_time(semid, shm_ptr); - } - } - for (i = 0; i < res.gl_pathc; i++) { - pthread_join(tid[i], NULL); - } - pthread_join(tid_mem, NULL); - pthread_join(tid_flow, NULL); - pthread_join(tid_time, NULL); - pthread_mutex_destroy(&mut); - globfree(&res); - munmap(shm_ptr, sizeof(SHM)); - return 0; -} diff --git a/branches/sys_nicmonitor-1.4-龙芯-32/nicinfo_shm.c b/branches/sys_nicmonitor-1.4-龙芯-32/nicinfo_shm.c deleted file mode 100644 index c8a8bf2..0000000 --- a/branches/sys_nicmonitor-1.4-龙芯-32/nicinfo_shm.c +++ /dev/null @@ -1,209 +0,0 @@ -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include - -#include "nicinfo_shm.h" - -#define SHM_PATH "/tmp/sys_netcard_shm_path" -#define SEM_PATH "/tmp/sys_netcard_sem_path" -#define LOG_PATH "/var/log/netcard/" - -typedef struct net_info{ - NETCARD_INFO info[MAXNICNUM]; -}SHM; - -static int semid = 0; -static char log_path[1024]; -static char shm_path[1024]; -static char sem_path[1024]; -SHM *ptr = NULL; - -/* write error in logfile */ -void record_log(char *str) -{ - time_t tamp; - char str_tm[4]; - char log_str[512]; - FILE *log_fp = NULL; - struct tm tmptr; - struct timeval tv; - struct timezone tz; - - if((log_fp = fopen(log_path, "a")) != NULL){ - tamp = time(NULL); - memset(str_tm, 0, sizeof(str_tm)); - memset(log_str, 0, sizeof(log_str)); - localtime_r(&tamp, &tmptr); - gettimeofday(&tv, &tz); - snprintf(str_tm, sizeof(str_tm), "%d", (int)tv.tv_usec/1000); - if(str_tm[1] == '\0')str_tm[1] = '0'; - if(str_tm[2] == '\0')str_tm[2] = '0'; - strftime(log_str, sizeof(log_str), "%F %T.", &tmptr); - strcat(log_str, str_tm); - strcat(log_str, " "); - strcat(log_str, "get_nic_info"); - strcat(log_str, " "); - strcat(log_str, str); - if(fwrite(log_str, 1, strlen(log_str), log_fp) < 1){ - } - fclose(log_fp); - } -} - -void get_sem(int semid) -{ - char err_str[200]; - struct sembuf lock[2]; - - lock[0].sem_num = 0; - lock[0].sem_op = 0; - lock[0].sem_flg = SEM_UNDO; - - lock[1].sem_num = 0; - lock[1].sem_op = 1; - lock[1].sem_flg = SEM_UNDO; - - while(semop(semid, lock, 2)){ - if (errno == EINTR) { - continue; - } - snprintf(err_str, sizeof(err_str), "EMERG: semop():\n", strerror(errno)); - record_log(err_str); - return; - } -} - -void release_sem(int semid) -{ - int ret; - char err_str[200]; - struct sembuf unlock; - - unlock.sem_num = 0; - unlock.sem_op = -1; - unlock.sem_flg = SEM_UNDO; - - if((ret = semop(semid, &unlock, 1)) == -1){ - snprintf(err_str, sizeof(err_str), "EMERG: semop():\n", strerror(errno)); - record_log(err_str); - return; - } -} - - -int init_nic_info(void) -{ - int fd = -1; - int sem_val; - char err_str[200]; - key_t key; - - - if ((key=ftok(sem_path, SEM_PROJ_ID)) == -1) { - snprintf(err_str, sizeof(err_str), "EMERG: ftok():%s\n", strerror(errno)); - record_log(err_str); - return -1; - } - if((semid = semget(key, 1, IPC_CREAT|0666)) == -1){ - snprintf(err_str, sizeof(err_str), "EMERG: Create semaphore error: %s\n", strerror(errno)); - record_log(err_str); - return -1; - } - /* - if((sem_val = semctl(semid, 0, GETVAL, 0)) == -1){ - snprintf(err_str, sizeof(err_str), "EMERG: semctl: %s !\n", strerror(errno)); - record_log(err_str); - semctl(semid, 0, IPC_RMID); - return -1; - } - if(!sem_val){ - if(semctl(semid, 0, SETVAL, 1) == -1){ - snprintf(err_str, sizeof(err_str), "EMERG: semctl: %s !\n", strerror(errno)); - record_log(err_str); - semctl(semid, 0, IPC_RMID); - return -1; - } - } - */ - if((fd = open(shm_path, O_RDONLY)) == -1){ - snprintf(err_str, sizeof(err_str), "EMERG: Error open %s: %s!\n",shm_path, strerror(errno)); - record_log(err_str); - return -1; - } - ptr = (SHM*)mmap(NULL, sizeof(SHM), PROT_READ, MAP_SHARED, fd, 0); - if(ptr == MAP_FAILED){ - snprintf(err_str, sizeof(err_str), "EMERG: mmap():%s\n", strerror(errno)); - record_log(err_str); - return -1; - } - close(fd); - - return 0; -} -int get_nic_info(char *nic_name, NETCARD_INFO *net_info) -{ - int i,ret; - time_t tamp; - struct tm tmptr; - DIR *dir; - char *s; - char buf[128]; - char err_str[200]; - struct passwd *user; - - tamp = time(NULL); - localtime_r(&tamp, &tmptr); - - if((user = getpwnam("d5000"))!= NULL){ - sprintf(log_path,"%s",user->pw_dir); - sprintf(shm_path,"%s",user->pw_dir); - sprintf(sem_path,"%s",user->pw_dir); - } - strcat(log_path,LOG_PATH); - strcat(shm_path,SHM_PATH); - strcat(sem_path,SEM_PATH); - - if((dir = opendir(log_path)) == NULL){ - if(errno == ENOENT){ - strcpy(log_path, "/tmp/"); - } - } - closedir(dir); - s = strrchr(log_path, '/'); - s++; - *s = '\0'; - memset(buf, 0, sizeof(buf)); - strftime(buf, sizeof(buf), "%Y%m%d_", &tmptr); - strcat(buf, "sys_nicmonitor"); - strcat(buf, ".log"); - strcat(log_path, buf); - if((ret = init_nic_info()) == -1){ - return -1; - } - get_sem(semid); - for(i = 0; ptr->info[i].charname[0] != 0; i++){ - if(!strcmp(ptr->info[i].charname, nic_name)){ - // get_sem(semid); - memcpy(net_info, &ptr->info[i], sizeof(NETCARD_INFO)); - release_sem(semid); - munmap(ptr, sizeof(SHM)); - return 0; - } - } - release_sem(semid); - snprintf(err_str, sizeof(err_str), "NOTICE: No information of %s !\n", nic_name); - record_log(err_str); - munmap(ptr, sizeof(SHM)); - return -1; -} diff --git a/branches/sys_nicmonitor-1.4-龙芯-32/proc_inv.h b/branches/sys_nicmonitor-1.4-龙芯-32/proc_inv.h deleted file mode 100644 index 945622c..0000000 --- a/branches/sys_nicmonitor-1.4-龙芯-32/proc_inv.h +++ /dev/null @@ -1,198 +0,0 @@ -//////////////////////////////////////////////////////////////////////////////////// -// -// Copyright (c) Comets' Grp. of Kedong Corp 2008. All Rights Reserved. -// -// FileName : procconf.h -// -// Function : this class realize some basic functions for process managerment, -// such as initiate process, report status of process, check status of process, -// update status of process, get information of process -// -// Author : -// -// Date : -// -// Modify by : -// -// Mod Date : -// -//////////////////////////////////////////////////////////////////////////////////// - -#ifndef _PROCCONF_H -#define _PROCCONF_H - -#include -#include -#include -#include -#include -#include -#include -//#include -#include - -#include "const.h" - -//for alarm ................................ -typedef struct MESS_BH -{ - unsigned char mtype; - int length; -}; -typedef struct PROCESS_ALM -{ - char context_name[MAX_STRING_LEN]; - char app_name[MAX_STRING_LEN]; - char proc_name[MAX_STRING_LEN]; - unsigned char status; -}; -//for alarm end ............................. - -//for mmi.................................... -const int MAX_BUFFER_LEN = 500; -typedef struct MESS_BLOCK -{ - unsigned char num; - char buffer[MAX_BUFFER_LEN]; -}; -typedef struct MESS_PROC -{ - char context_name[MAX_STRING_LEN]; - char app_name[MAX_STRING_LEN]; - char proc_name[MAX_STRING_LEN]; - char status; -}; -//for mmi end ................................ - -const int DEFAULT_PERIOD = 3; -const int COUNT_LIMIT = 2; -const int START_DEFAULT_PERIOD = 60; -const int START_COUNT_LIMIT = 5; -const int APP_COUNT_LIMIT = 1; - -//process critical level -//const int WST_CRITICAL = 1; // Shutdown and reboot workstation when failed -//const int SYS_CRITICAL = 2; // Shutdown and reboot the system on the wst when failed -//const int USER_CRITICAL = 3; // Shutdown and reboot the subsystem when failed -//const int GENERAL = 4; // reboot the process -const int CRUCIAL = 1; // crucial process -const int GENERAL = 0; // general process - - -extern int srv_init(char *service,int port); -extern int Tcp_close(int sockfd); -extern int Tcp_read(int fd,char *ptr,int nbytes); -extern int Tcp_write(int fd,char *ptr,int nbytes); -extern int srv_accept(int fd,struct sockaddr *cli_addr,int *clilen); -extern int client_tcp_open(char *host,char *service,int port); - -typedef struct -{ - char context_name[MAX_STRING_LEN]; - char app_name[MAX_STRING_LEN]; - char proc_name[MAX_STRING_LEN]; - pid_t proc_pid; -}PROC_ADM_INFO; - -//*************************************************************** -// structure name : PROC_INFO -// function : store process informatin -// author : -// date : -// modify by : -// modification : -// mod date : -//*************************************************************** -typedef struct -{ - int position; - char node_name[MAX_STRING_LEN]; - char context_name[MAX_STRING_LEN]; - char app_name[MAX_STRING_LEN]; - char proc_name[MAX_STRING_LEN]; - - unsigned char active_flag; - unsigned char master_flag; - - time_t startup_time; - time_t refresh_time; - short refresh_peri; - unsigned char monitor_type; - - pid_t proc_pid; - unsigned char auto_start; - unsigned char act_timer; - unsigned char start_timer; - unsigned char critical_level; - - char exefile_path[MAX_EXECMD_LEN]; - -}PROC_INFO; - -//*************************************************************** -// structure name : APP_INFO -// function : store application informatin -// author : -// date : -// modify by : -// modification : -// mod date : -//*************************************************************** -typedef struct -{ - int position; - char context_name[MAX_STRING_LEN]; - int context_id; - char app_name[MAX_STRING_LEN]; - int app_id; - unsigned char act_timer; - unsigned char active_flag; -}APP_INFO; - - -typedef struct -{ - int no_proc; - int semdes_cfg; - PROC_INFO proc[MAX_LOCAL_PROCESS]; - APP_INFO app[MAX_LOCAL_APP]; -}PROCCFG; - -class proc_invocation -{ - public: - int m_init; - PROCCFG *proccfg_p; - - public: - proc_invocation(); - ~proc_invocation(); - - int conf_create(); - //int check_proc_status(); - //int update_rtdb(); - //int kill_proc(pid_t pid); - //int start_proc(char *cmdline); - //int send_alarm(char *context_name, char *app_name, char *proc_name, unsigned char status); - //int update_proc_status(char *context_name, char *app_name, char *proc_name, char status); - - //int proc_init(char *context_name, char *app_name, char *proc_name, int critical_level);//exefile_path,auto_start - int proc_init(char *context_name, char *app_name, char *proc_name); - int proc_report(int pos, char status, int intertime=3); - int proc_exit(int proc_pos); - - int get_pos(char *context_name, char *app_name, char *proc_name); - int is_proc_exist(char *context_name, char *app_name, char *proc_name); - int conf_map(); - - int get_procinfo(int position, PROC_ADM_INFO *p_info); - int get_active_pid(int &num, int *p_pidlist); - int is_proc_run(pid_t pid); - int is_proc_run(char *context_name, char *app_name, char *proc_name); - -}; - -#endif - - - diff --git a/branches/sys_nicmonitor-1.4-龙芯-32/read_netcard.c b/branches/sys_nicmonitor-1.4-龙芯-32/read_netcard.c deleted file mode 100644 index c745f48..0000000 --- a/branches/sys_nicmonitor-1.4-龙芯-32/read_netcard.c +++ /dev/null @@ -1,133 +0,0 @@ -#include "mnic.h" - -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; -} - - -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 == '\0') - return NULL; - p++; - break; - } - *name++ = *p++; - } - *name++ = '\0'; - return p; -} - -int get_dev_fields(char *str, NETCARD_INFO *ife) -{ - 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]); - return 0; -} - -int if_fetch(NETCARD_INFO *ife) -{ - int skfd; - struct ifreq ifr; - - if((skfd = socket(PF_INET, SOCK_DGRAM, 0)) == -1)return -1; - - strcpy(ifr.ifr_name, ife->charname); - if (!ioctl(skfd, SIOCGIFFLAGS, &ifr)) - ife->flags = ifr.ifr_flags; - - if (!ioctl(skfd, SIOCGIFHWADDR, &ifr)) - ife->hwaddr = ifr.ifr_hwaddr; - - if (!ioctl(skfd, SIOCGIFMTU, &ifr)) - ife->mtu = ifr.ifr_mtu; - - if (!ioctl(skfd, SIOCGIFTXQLEN, &ifr)) - ife->tx_queue_len = ifr.ifr_qlen; - - if (!ioctl(skfd, SIOCGIFADDR, &ifr)) - ife->addr = ifr.ifr_addr; - - if (!ioctl(skfd, SIOCGIFBRDADDR, &ifr)) - ife->broadaddr = ifr.ifr_broadaddr; - - if (!ioctl(skfd, SIOCGIFNETMASK, &ifr)) - ife->netmask = ifr.ifr_netmask; - - ife->time_stamp = time(NULL); - close(skfd); - return 0; -} - - -int ioc_get_name(NETCARD_INFO *name) -{ - int skfd; - int ifrnum; - int i = 0; - struct ifconf ifc; - struct ifreq buf[MAXNICNUM]; - - if((skfd = socket(PF_INET, SOCK_DGRAM, 0)) == -1)return -1; - memset(buf, 0, sizeof(buf)); - ifc.ifc_len = sizeof(buf); - ifc.ifc_buf = (caddr_t) buf; - ioctl(skfd, SIOCGIFCONF, &ifc); - ifrnum = ifc.ifc_len / sizeof(struct ifreq); - while(ifrnum-- > 0){ - strcpy(name[i].charname, buf[ifrnum].ifr_name); - i++; - } - close(skfd); - return i; -} - diff --git a/branches/sys_nicmonitor-1.4-龙芯-32/sys_nicmonitor b/branches/sys_nicmonitor-1.4-龙芯-32/sys_nicmonitor deleted file mode 100644 index 3f094ea..0000000 Binary files a/branches/sys_nicmonitor-1.4-龙芯-32/sys_nicmonitor and /dev/null differ diff --git a/branches/sys_nicmonitor-1.5/Makefile b/branches/sys_nicmonitor-1.5/Makefile deleted file mode 100644 index 5cccc77..0000000 --- a/branches/sys_nicmonitor-1.5/Makefile +++ /dev/null @@ -1,28 +0,0 @@ -#! /usr/bin/make -f - -DEFAULTS = Makefile.config - -include $(DEFAULTS) - -all:sys_nicmonitor libnic_shm.so - -CFLAGS += -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 $@ $^ -libnic_shm.so:nicinfo_shm.c - $(CC) -fpic -shared -o $@ $^ -clean: - $(RM) *.o - $(RM) libnic_shm.so sys_nicmonitor -install: - $(MKDIR) -p $(LIBDIR) - $(MKDIR) -p $(BINDIR) - $(MKDIR) -p $(INCDIR) - $(CP) libnic_shm.so $(LIBDIR) - $(CP) sys_nicmonitor $(BINDIR) - $(CP) nicinfo_shm.h $(INCDIR) -uninstall: - $(RM) -f $(LIBDIR)/libnic_shm.so - $(RM) -f $(BINDIR)/sys_nicmonitor - $(RM) -f $(INCDIR)/nicinfo_shm.h diff --git a/branches/sys_nicmonitor-1.5/Makefile.config b/branches/sys_nicmonitor-1.5/Makefile.config deleted file mode 100644 index 7a2d8e6..0000000 --- a/branches/sys_nicmonitor-1.5/Makefile.config +++ /dev/null @@ -1,16 +0,0 @@ - -PREFIX = $(DESTDIR) - -BINDIR = $(PREFIX)/usr/bin - -LIBDIR = $(PREFIX)/usr/lib64 - -INCDIR = $(PREFIX)/usr/include - -INSTALL_SH = ./install.sh - -CP = cp -RM = rm -#CC = gcc -CC = g++ -MKDIR = mkdir diff --git a/branches/sys_nicmonitor-1.5/README1.5 b/branches/sys_nicmonitor-1.5/README1.5 deleted file mode 100644 index fa1087e..0000000 --- a/branches/sys_nicmonitor-1.5/README1.5 +++ /dev/null @@ -1,32 +0,0 @@ - -lijin:/home/d5000/lijin/sys_nicmonitor # ./sys_nicmonitor -shm_path:/home/d5000/tmp/sys_netcard_shm_path -log_path:/home/d5000/var/log/netcard/20110110_sys_nicmonitor.log -udp    bond0 -nic    bond0 -nic    eth0 -nic    eth1 - -d5000@lijin:~ > cat var/log/netcard/20110110_sys_nicmonitor.log -2011-01-10 11:10:37.761 sys_nicmonitor NOTICE: sys_nicmonitor V1.5 is -started ! -2011-01-10 11:10:37.761 sys_nicmonitor -shm_path:/home/d5000/tmp/sys_netcard_shm_path -2011-01-10 11:10:37.761 sys_nicmonitor -log_path:/home/d5000/var/log/netcard/20110110_sys_nicmonitor.log -2011-01-10 11:10:37.761 sys_nicmonitor udp    bond0 -2011-01-10 11:10:37.761 sys_nicmonitor nic    bond0 -2011-01-10 11:10:37.761 sys_nicmonitor nic    eth0 -2011-01-10 11:10:37.762 sys_nicmonitor nic    eth1 -2011-01-10 11:10:37.762 sys_nicmonitor NOTICE: bond0 is NORMAL! -2011-01-10 11:10:37.763 sys_nicmonitor NOTICE: eth0 is NORMAL! -2011-01-10 11:10:37.763 sys_nicmonitor NOTICE: eth1 is NORMAL! - -与1.4不一样的地方在与增加log文件中的打印语句 -sys_nicmonitor started! version v1.4 -shm_path: /home/d5000/tmp -log_path: /home/d5000/ var/log/netcard -udp bond0 -nic bond0 -nic eth0 -nic eth1 diff --git a/branches/sys_nicmonitor-1.5/const.h b/branches/sys_nicmonitor-1.5/const.h deleted file mode 100644 index e2ebfa5..0000000 --- a/branches/sys_nicmonitor-1.5/const.h +++ /dev/null @@ -1,51 +0,0 @@ -#ifndef _RTE_CONST_H -#define _RTE_CONST_H - -#include - - -#define MAX_STRING_LEN 24 -#define MAX_EXECMD_LEN 200 -#define MAX_FILENAME_LEN 200 - -#define MAX_CONTEXT 8 -#define MAX_HOSTNAME_LEN 24 -#define MAX_LOCAL_MESPROC 512 -#define MAX_LOCAL_PROCESS MAX_LOCAL_MESPROC -#define MAX_LOCAL_APP 32 -#define MAX_LOCAL_NODE 256 - -#define MAX_SET 256 //max num of event set -#define MAX_EVENT 1300 -#define MAX_REG_PROC 20 - -#define LEN_SHMBLK_BIG 4096 -#define MAX_MSGBUF_LEN 32767 -#define FREE_PAGE_SIZE 65536 -#define MAX_PAGE_NUM 1024 - -#define MAX_QUE MAX_LOCAL_MESPROC -#define MAX_SEMPHORE_SET MAX_LOCAL_MESPROC*2 -#define RTE_HAN 0 // queue number for event handeler0(RTE) -#define EX_EVENT_HAN RTE_HAN // queue number for extrnal event handeler - -#define DOMAIN_I 1 -#define DOMAIN_II 2 -#define DOMAIN_III 3 - -//Add 20090225 -const int PROC_TYPE_RPT =1; -const int PROC_TYPE_UNRPT =0; -//add end - - -//status for proc and app -const int NON_ACTIVE = 0; -const int ACTIVE = 1; -const int HANGUP = 2; -const int FAILURE = 5; -const int START = 6; -const int STOP = 7; - -#endif - diff --git a/branches/sys_nicmonitor-1.5/mnic.c b/branches/sys_nicmonitor-1.5/mnic.c deleted file mode 100644 index c3bcf62..0000000 --- a/branches/sys_nicmonitor-1.5/mnic.c +++ /dev/null @@ -1,1541 +0,0 @@ -#include "mnic.h" -#include "dotconf.h" -#include -#include -#include "proc_inv.h" - -#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 "/tmp/sys_netcard_shm_path" -#define SEM_PATH "/tmp/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.5" - -#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. -}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 find_nic_config(char *); - -static pthread_mutex_t mut = PTHREAD_MUTEX_INITIALIZER; -static short seqno = 1; -static char log_flag = 1; -static int sem_id = 0; -static char process_name[64]; -char log_path[1024]; -char shm_path[1024]; -char sem_path[1024]; -char conf_path[1024]; - -int host_name_size = 0; -char host_name[HOST_NAME_SIZE]; -int semid = -1; -SHM *shm_ptr; - -//20100510 -int total_ping_time, elapsed_ping_time; -int get_netcard_status(NETCARD_INFO_ST *); - -/****dotconf****/ -static DOTCONF_CB(cb_str); -static DOTCONF_CB(cb_int); -int nicnum = 0; -CONFIG_FILE_ST conf; - -char myconf[][64] = { - "domain", // 0 - "serv", // 1 - "event", // 2 - "udpport", // 3 - "sys_netcard_shm_path", // 4 - "sys_netcard_sem_path", // 5 - "monitor_interval", // 6 - "write_interval", // 7 - "flow_interval", // 8 - "flow_limit", // 9 - "flow_peak", // 10 - "udp", // 11 - "nic", // 12 - "ping", // 13 - "pingnum", // 14 - "pinglap", // 15 - "" -}; - -static const configoption_t options[] = { - {myconf[0], ARG_INT, cb_int, NULL, CTX_ALL}, - {myconf[1], ARG_INT, cb_int, NULL, CTX_ALL}, - {myconf[2], ARG_INT, cb_int, NULL, CTX_ALL}, - {myconf[3], ARG_INT, cb_int, NULL, CTX_ALL}, - {myconf[4], ARG_STR, cb_str, NULL, CTX_ALL}, - {myconf[5], ARG_STR, cb_str, NULL, CTX_ALL}, - {myconf[6], ARG_INT, cb_int, NULL, CTX_ALL}, - {myconf[7], ARG_INT, cb_int, NULL, CTX_ALL}, - {myconf[8], ARG_INT, cb_int, NULL, CTX_ALL}, - {myconf[9], ARG_INT, cb_int, NULL, CTX_ALL}, - {myconf[10], ARG_INT, cb_int, NULL, CTX_ALL}, - {myconf[11], ARG_STR, cb_str, NULL, CTX_ALL}, - {myconf[12], ARG_STR, cb_str, NULL, CTX_ALL}, - {myconf[13], ARG_STR, cb_str, NULL, CTX_ALL}, - {myconf[14], ARG_INT, cb_int, NULL, CTX_ALL}, - {myconf[15], ARG_INT, cb_int, NULL, CTX_ALL}, - LAST_OPTION -}; -/****dotconf****/ - -/* write error in logfile */ -void record_log(char *str) -{ - time_t tamp; - char str_tm[4]; - char log_str[512]; - FILE *log_fp = NULL; - struct tm tmptr; - struct timeval tv; - struct timezone tz; - - if((log_fp = fopen(log_path, "a")) != NULL){ - tamp = time(NULL); - memset(str_tm, 0, sizeof(str_tm)); - memset(log_str, 0, sizeof(log_str)); - localtime_r(&tamp, &tmptr); - gettimeofday(&tv, &tz); - snprintf(str_tm, sizeof(str_tm), "%d", (int)tv.tv_usec/1000); - if(str_tm[1] == '\0')str_tm[1] = '0'; - if(str_tm[2] == '\0')str_tm[2] = '0'; - strftime(log_str, sizeof(log_str), "%F %T.", &tmptr); - strcat(log_str, str_tm); - strcat(log_str, " "); - strcat(log_str, process_name); - strcat(log_str, " "); - strcat(log_str, str); - if(fwrite(log_str, 1, strlen(log_str), log_fp) < 1){ - } - fclose(log_fp); - } -} - -static void init_sem(int *semid, CONFIG_FILE_ST *ptr) -{ - key_t key; - char error_str[200]; - -/* write error in logfile */ - memset(error_str, 0, sizeof(error_str)); - if ((key = ftok(sem_path, SEM_PROJ_ID)) == -1) { - snprintf(error_str, sizeof(error_str), "EMERG: ftok():%s\n", strerror(errno)); - record_log(error_str); - return; - } - if((*semid = semget(key, 1, IPC_CREAT|0666)) == -1){ - snprintf(error_str, sizeof(error_str), "EMERG: semget():%s\n", strerror(errno)); - record_log(error_str); - return; - } - if(semctl(*semid, 0, SETVAL, 0) == -1){ - snprintf(error_str, sizeof(error_str), "EMERG: semctl: %s !\n", strerror(errno)); - record_log(error_str); - semctl(*semid, 0, IPC_RMID); - return; - } -} - -static void init_shm(SHM **ptr, CONFIG_FILE_ST *p) -{ - int fd = -1; - char error_str[200]; - - mode_t mask_tmp; - mask_tmp = umask(0000); - if((fd = open(shm_path, O_RDWR|O_CREAT|O_TRUNC, 0666)) == -1){ - printf("could not create %s\n", shm_path); - return; - } - umask(mask_tmp); - lseek(fd, sizeof(SHM), SEEK_SET); - write(fd, "\0", 1); - *ptr = (SHM *)mmap(NULL, sizeof(SHM), PROT_READ|PROT_WRITE, MAP_SHARED, fd, 0); - if(*ptr == MAP_FAILED){ - snprintf(error_str, sizeof(error_str), "EMERG: mmap():%s\n", strerror(errno)); - record_log(error_str); - return; - } - close(fd); - -} - -static void get_sem(int semid) -{ - struct sembuf lock[2]; - char error_str[200]; - - lock[0].sem_num = 0; - lock[0].sem_op = 0; - lock[0].sem_flg = SEM_UNDO; - - lock[1].sem_num = 0; - lock[1].sem_op = 1; - lock[1].sem_flg = SEM_UNDO; - - while(semop(semid, lock, 2)){ - if(errno == EINTR)continue; - snprintf(error_str, sizeof(error_str), "EMERG: get_sem semop():%s\n", strerror(errno)); - record_log(error_str); - return; - } -} - -static void release_sem(int semid) -{ - int ret; - char error_str[200]; - struct sembuf unlock; - - unlock.sem_num = 0; - unlock.sem_op = -1; - unlock.sem_flg = SEM_UNDO; - - if((ret = semop(semid, &unlock, 1)) == -1){ - snprintf(error_str, sizeof(error_str), "EMERG: rel_sem semop():%s\n", strerror(errno)); - record_log(error_str); - return; - } -} - -static void proc_get_name(NET_NAME_ST *nodep) -{ - char linebuf[LINE_SIZE]; - char error_str[200]; - char *retp = NULL; - int err_val=0; - NET_NAME_ST *node = NULL; - FILE *dev_fp; - - if((dev_fp = fopen("/proc/net/dev", "rb")) == NULL){ - snprintf(error_str, sizeof(error_str), "EMERG: Can't open dev file:%s\n", strerror(errno)); - record_log(error_str); - return; - } - memset(linebuf, 0, sizeof(linebuf)); - fgets(linebuf, sizeof(linebuf), dev_fp); - fgets(linebuf, sizeof(linebuf), dev_fp); - while((retp = fgets(linebuf, sizeof(linebuf), dev_fp)) != NULL){ - if((node = (NET_NAME_ST *)malloc(sizeof(NET_NAME_ST))) == NULL){ - err_val = 1; - break; - } - memset(node->name, 0, NIC_NAME_LEN); - get_name(node->name, retp); - node->next = nodep->next; - nodep->next = node; - } - if(err_val){ - NET_NAME_ST *curr = nodep->next; - while(curr != NULL){ - node = curr->next; - free(curr); - curr = node; - } - } - fclose(dev_fp); -} - -static void ioctl_get_name(NET_NAME_ST *nodep) -{ - int count = 0; - int inuse = 0; - int err_val = 0; - NETCARD_INFO net_name[MAXNICNUM]; - NET_NAME_ST *node = NULL, *curr = NULL; - - memset(net_name, 0, sizeof(net_name)); - if((count = ioc_get_name(net_name)) == 0)return; - if(nodep->next == NULL) - inuse = 1; - while(count > 0){ - curr = nodep->next; - while(curr != NULL){ - if(!strncmp(net_name[count - 1].charname, curr->name, NIC_NAME_LEN)){ - inuse = 0; - break; - } - inuse = 1; - curr = curr->next; - } - if(inuse){ - if((node = (NET_NAME_ST *)malloc(sizeof(NET_NAME_ST))) == NULL){ - err_val = 1; - break; - } - memset(node->name, 0, NIC_NAME_LEN); - strncpy(node->name, net_name[count - 1].charname, NIC_NAME_LEN); - node->next = nodep->next; - nodep->next = node; - } - count--; - } - if(err_val){ - curr = nodep->next; - while(curr != NULL){ - node = curr->next; - free(curr); - curr = node; - } - } -} - -static void get_all_name(NET_NAME_ST *nodep) -{ - proc_get_name(nodep); - ioctl_get_name(nodep); -} - -static void create_monit_list(NET_NAME_ST *nodep, NETCARD_INFO_ST *info_list, char *bond) -{ - int err_val = 0; - NETCARD_INFO_ST *info_node = NULL; - NET_NAME_ST *curr, *node; - - curr = nodep->next; - while(curr != NULL){ - if((info_node = (NETCARD_INFO_ST *)malloc(sizeof(NETCARD_INFO_ST))) == NULL){ - err_val = 1; - break; - } - memset(info_node, 0, sizeof(NETCARD_INFO_ST)); - strncpy(info_node->info.charname, curr->name, NIC_NAME_LEN); - strncpy(info_node->info.descr, bond, strlen(bond)); - info_node->next = info_list->next; - info_list->next = info_node; - curr = curr->next; - } - if(err_val){ - NETCARD_INFO_ST *info_curr = info_list->next; - while(info_curr != NULL){ - info_node = info_curr->next; - free(info_curr); - info_curr = info_node; - } - curr = nodep->next; - while(curr != NULL){ - node = curr->next; - free(curr); - curr = node; - } - } -} - -static int get_inc_info(NETCARD_INFO *net) -{ - return if_fetch(net); -} - -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){ - if((str = strstr(linebuf, net->charname)) != NULL){ - str +=strlen(net->charname) + 2; - get_dev_fields(str, net); - break; - } - } - fclose(dev_fp); -} - -static void send_inc_info_one(NETCARD_INFO *net, int semid, SHM *ptr) -{ - int i; - for(i = 0; ptr->info[i].charname[0] != 0; i++){ - if(!strncmp(ptr->info[i].charname, net->charname, NIC_NAME_LEN)){ - get_sem(semid); - memcpy(&ptr->info[i], net, sizeof(NETCARD_INFO)); - release_sem(semid); - } - } -} - -static void *send_inc_info(void *p) -{ - int i = 0; - int flags = 1; - char error_str[200]; - unsigned long long bytes[MAXNICNUM]; - THMEM *ptr = (THMEM *)p; - NETCARD_INFO_ST *curr; - struct timespec s_time, os_time; - - if((curr = ptr->listp->next) == NULL){ - pthread_mutex_lock(&mut); - snprintf(error_str, sizeof(error_str), "EMERG: Empty Netcard list!\n"); - record_log(error_str); - pthread_mutex_unlock(&mut); - return NULL; - } - s_time.tv_sec = ptr->conf->write_interval; - s_time.tv_nsec = 0; - - /* copy all net_info*/ - while(1){ - i = 0; - get_sem(ptr->semid); - memset(ptr->shm_ptr, 0, sizeof(SHM)); - curr = ptr->listp->next; - while(curr != NULL){ - get_inc_info(&curr->info); - if(curr->status == NIC_UNLINKABLE){ - curr->info.flags &= (~IFF_UP); - curr->info.flags &= (~IFF_RUNNING); - } - get_inc_stats(&curr->info); - if(flags){ - bytes[i] = curr->info.rx_bytes + curr->info.tx_bytes; - } - curr->info.average_flow =(unsigned long long)((curr->info.rx_bytes + curr->info.tx_bytes - bytes[i])/(ptr->conf->write_interval)); - memcpy(&ptr->shm_ptr->info[i], &curr->info, sizeof(NETCARD_INFO)); - bytes[i] = curr->info.rx_bytes + curr->info.tx_bytes; - i++; - curr = curr->next; - } - release_sem(ptr->semid); - flags = 0; - nanosleep(&s_time, &os_time); - } -} - -static int get_netcard_count(char *name) -{ - int count = 0; - FILE *fp = NULL; - char path[64] = "/proc/net/bonding/"; - char buf[LINE_SIZE]; - char *retp = NULL; - char *str = NULL; - - strncat(path, name, strlen(name)); - if((fp = fopen(path, "r")) == NULL)return 1; - - memset(buf, 0, sizeof(buf)); - fseek(fp, 65, SEEK_SET); - while((retp = fgets(buf, sizeof(buf), fp)) != NULL){ - if((str = strstr(buf, "active-backup")) != NULL){ - fclose(fp); - return 1; - } - if(!strncmp(buf, "Slave Interface:", 16))count++; - } - fclose(fp); - return count; -} - -static void *cacu_flow(void *p) -{ - int i = 0, count_t = 1; - int summit = 0; - int flags = 1; - int count_ok[MAXNICNUM]; - int count_no[MAXNICNUM]; - char error_str[200]; - unsigned long long bytes[MAXNICNUM]; - THFLOW *ptr = (THFLOW *)p; - NETCARD_INFO_ST *curr; - struct timespec s_time, os_time; - - if((curr = ptr->mem.listp->next) == NULL){ - pthread_mutex_lock(&mut); - snprintf(error_str, sizeof(error_str), "EMERG: Empty Netcard list!\n"); - record_log(error_str); - pthread_mutex_unlock(&mut); - return NULL; - } - s_time.tv_sec = ptr->mem.conf->flow_interval; - s_time.tv_nsec = 0; - - for(i = 0; i < MAXNICNUM ; i++){ - count_ok[i] = 0; - count_no[i] = 0; - } - while(1){ - i = 0; - curr = ptr->mem.listp->next; - while(curr != NULL){ - get_inc_info(&curr->info); - get_inc_stats(&curr->info); - if(flags){ - bytes[i] = curr->info.rx_bytes + 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); - 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); -#if 0 - printf("%s:\tcount=%d\tsummit=%d\tflow_limit:%d\taverage_flow:%d\n", curr->info.charname, count_t, summit, conf.flow_limit, curr->info.average_flow); -#endif - 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); - strncat(ptr->env.Malarm.tSysNetcardAlarm.fault_devname, "-", 1); - strncat(ptr->env.Malarm.tSysNetcardAlarm.fault_devname, curr->info.charname, (NIC_NAME_LEN - ptr->env.host_name_size - 1)); - ptr->env.Malarm.tSysNetcardAlarm.flags = NETCARD_ALARM_ABNORMAL; - ptr->env.Malarm.tSysNetcardAlarm.retrytimes = 0; - ptr->env.Malarm.tMsgFrame.len = sizeof(ptr->env.Malarm); - ptr->env.Malarm.tMsgFrame.seqno = seqno; - pthread_mutex_lock(&mut); - while(count_no[i] > 0){ - send_alarm(&ptr->env.Malarm, ptr->mem.conf->udpport, ptr->mem.conf->ip); - seqno++; - ptr->env.Malarm.tMsgFrame.seqno = seqno; - ptr->env.Malarm.tSysNetcardAlarm.retrytimes++; - count_no[i]--; - if(count_no[i] == 0){ - send_inc_info_one(&curr->info, ptr->mem.semid, ptr->mem.shm_ptr); - //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); - snprintf(error_str, sizeof(error_str), "NOTICE: %s flow is abnormal !\n", curr->info.charname); - record_log(error_str); - } - } - pthread_mutex_unlock(&mut); - count_ok[i] = 3; - }else{ - 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); - strncat(ptr->env.Malarm.tSysNetcardAlarm.fault_devname, "-", 1); - strncat(ptr->env.Malarm.tSysNetcardAlarm.fault_devname, curr->info.charname, (NIC_NAME_LEN - ptr->env.host_name_size - 1)); - ptr->env.Malarm.tSysNetcardAlarm.flags = NETCARD_ALARM_NORMAL; - ptr->env.Malarm.tSysNetcardAlarm.retrytimes = 0; - ptr->env.Malarm.tMsgFrame.len = sizeof(ptr->env.Malarm); - ptr->env.Malarm.tMsgFrame.seqno = seqno; - pthread_mutex_lock(&mut); - while(count_ok[i] > 0){ - send_alarm(&ptr->env.Malarm, ptr->mem.conf->udpport, ptr->mem.conf->ip); - seqno++; - ptr->env.Malarm.tMsgFrame.seqno = seqno; - ptr->env.Malarm.tSysNetcardAlarm.retrytimes++; - count_ok[i]--; - if(count_ok[i] == 0){ - send_inc_info_one(&curr->info, ptr->mem.semid, ptr->mem.shm_ptr); - snprintf(error_str, sizeof(error_str), "NOTICE: %s flow is normal now !\n", curr->info.charname); - record_log(error_str); - } - } - pthread_mutex_unlock(&mut); - count_no[i] = 3; - }/* end if(summit... */ - bytes[i] = curr->info.rx_bytes + curr->info.tx_bytes; - i++; - curr = curr->next; - }/* end while(curr...) */ - flags = 0; - nanosleep(&s_time, &os_time); - }/* end while(1) */ -} -static char *parse_str(char **s) -{ - char *retp = NULL; - char *p = *s; - - while(isspace(*p))p++; - retp = p; - while(!isspace(*p))p++; - *p = '\0'; - - return retp; -} - -int ping_gw(char *devname) -{ - int i, ret = -1, j, k; - char buf[64]; - - i = find_nic_config(devname); - if( i == -1 ) - return 0; - - if(conf.nic[i].gw_num == 0) - return 0; - - for(k=0; k /dev/null 2>&1", conf.pinglap,conf.nic[i].ping_ip[j]); - ret=system(buf); -#if 0 - if (WIFSIGNALED(ret) && - (WTERMSIG(ret) == SIGINT || WTERMSIG(ret) == SIGQUIT)) - exit(0); -#endif - if(ret == 0) - return 0; - - } - } - - return -1; -} - -static void *check_inc_switch(void *ptr) -{ - THENV *arg = (THENV *)ptr; - FILE *fp = NULL; - char name[NIC_NAME_LEN]; - struct timespec s_time, os_time; - int count = 0; - char linebuf[LINE_SIZE]; - char error_str[200]; - char *str = NULL; - char *retp = NULL; - - if((fp = fopen(arg->bond_file_path, "r")) == NULL){ - snprintf(error_str, sizeof(error_str), "EMERG: fopen():%s\n", strerror(errno)); - record_log(error_str); - free(ptr); - return NULL; - } - fseek(fp, 65, SEEK_SET); - memset(name, 0, sizeof(name)); - memset(linebuf, 0, sizeof(linebuf)); - if((retp = fgets(linebuf, sizeof(linebuf), fp)) != NULL){ - if((retp = strstr(linebuf, "active-backup")) == NULL){ - free(ptr); - fclose(fp); - return NULL; - } - } - - s_time.tv_sec = 0; - s_time.tv_nsec = 100000000; - - while(1){ - while((str = fgets(linebuf, sizeof(linebuf), fp)) != NULL){ - if(!strncmp(linebuf, "Currently Active Slave:", 23)){ - str += 23; - retp = parse_str(&str); - break; - } - } - if(name[0] == 0){ - strncpy(name, retp, strlen(retp)); - }else if(strncmp(name, retp, strlen(retp)) != 0){ - memset(arg->Malarm.tSysNetcardAlarm.switch_devname, 0, NIC_NAME_LEN); - strncpy(arg->Malarm.tSysNetcardAlarm.switch_devname, arg->host_name, arg->host_name_size); - strncat(arg->Malarm.tSysNetcardAlarm.switch_devname, "-", 1); - strncat(arg->Malarm.tSysNetcardAlarm.switch_devname, retp, (NIC_NAME_LEN - arg->host_name_size - 1)); - arg->Malarm.tSysNetcardAlarm.flags = NETCARD_ALARM_SWITCH; - arg->Malarm.tSysNetcardAlarm.retrytimes = 0; - arg->Malarm.tMsgFrame.len = sizeof(arg->Malarm); - arg->Malarm.tMsgFrame.seqno = seqno; - count = 3; - pthread_mutex_lock(&mut); - snprintf(error_str, sizeof(error_str), "NOTICE: Net card checking to %s!\n", retp); - record_log(error_str); - while(count > 0){ - send_alarm(&arg->Malarm, arg->conf->udpport, arg->conf->ip); - seqno++; - arg->Malarm.tMsgFrame.seqno = seqno; - arg->Malarm.tSysNetcardAlarm.retrytimes++; - count--; - } - pthread_mutex_unlock(&mut); - memset(name, 0, NIC_NAME_LEN); - strncpy(name, retp, strlen(retp)); - } - fclose(fp); - nanosleep(&s_time, &os_time); - if((fp = fopen(arg->bond_file_path, "r")) == NULL){ - free(ptr); - return NULL; - } - fseek(fp, 65, SEEK_SET); - } - free(ptr); - fclose(fp); - return NULL; -} - -static void *create_logfile(void *ptr) -{ - char buf[128]; - char del_file[DAYS][1024]; - char *s; - int head, tail, fd; - int flag = 0, del_flag = 0; - int old_mon = 0; - int old_day = 0; - time_t tamp; - struct tm tmptr; - struct timespec s_time, os_time; - - s_time.tv_sec = 1; - s_time.tv_nsec = 0; - head = tail = 0; - while(1){ - tamp = time(NULL); - localtime_r(&tamp, &tmptr); - if(old_mon != (tmptr.tm_mon + 1))flag = 1; - else if(old_day != tmptr.tm_mday)flag = 1; - else flag = 0; - if(flag){ - s = strrchr(log_path, '/'); - s++; - *s = '\0'; - memset(buf, 0, sizeof(buf)); - strftime(buf, sizeof(buf), "%Y%m%d_", &tmptr); - strcat(buf, process_name); - strcat(buf, ".log"); - strcat(log_path, buf); - mode_t mask_tmp; - mask_tmp = umask(0000); - if((fd = open(log_path, O_RDWR|O_CREAT, 0666)) != -1){ - log_flag = 0; - close(fd); - } - umask(mask_tmp); - old_mon = tmptr.tm_mon+1; - old_day = tmptr.tm_mday; - strcpy(del_file[head++], log_path); - if(head == DAYS){ - head = 0; - del_flag = 1; - } - if(del_flag){ - unlink(del_file[tail]); - tail++; - if(tail == DAYS)tail = 0; - } - } - nanosleep(&s_time, &os_time); - } -} - -void write_time(int semid, SHM *ptr) -{ - int i = 0; - int flag_time_stamp; - - flag_time_stamp = time(NULL); - - while(ptr->info[i].charname[0] != 0){ - ptr->info[i].time_stamp = flag_time_stamp; - i++; - } -} -#if 0 -char *parse(char *line) -{ - char *begin = NULL; - char *end = NULL; - - if((begin = strchr(line, '/')) != NULL){ - if((end = strstr(line, "bin")) != NULL){ - *end='\0'; - return begin; - } - } - return NULL; -} -#endif -void release_name_list(NET_NAME_ST *head) -{ - NET_NAME_ST *curr, *save; - - curr = head->next; - while(curr != NULL){ - save = curr->next; - free(curr); - curr = save; - } -} -int get_netcard(char *line, char *nic) -{ - char *begin, *retp; - - while(isspace(*line))line++; - begin = line; - while(!isspace(*line))line++; - *line++ = '\0'; - if(!strncmp(begin, "nic", 4)){ - if((retp = parse_str(&line)) != NULL){ - strncpy(nic, retp, MAXNICNUM); - return 1; - } - } - return 0; -} - -void sig_handler(int sig) -{ -// char err_str[200]; - -#if 0 - if(unlink(SHM_PATH) == -1){ - snprintf(err_str, sizeof(err_str), "NOTICE: error delete %s\n", strerror(errno)); - record_log(err_str); - } - if(unlink(SEM_PATH) == -1){ - snprintf(err_str, sizeof(err_str), "NOTICE: error delete %s\n", strerror(errno)); - record_log(err_str); - } -#endif -// semctl(sem_id, 0, IPC_RMID); - -// snprintf(err_str, sizeof(err_str), "NOTICE: process exit by signal %d\n", sig); -// record_log(err_str); - exit(0); -} - -/* - * 查看数据结构中是否有给定的网卡 - * 输入:网卡名称 - * 返回值:-1 表示没有 - * 非0值表示设备列表中的索引 - * - */ -int find_nic_config(char *dev_name){ - int i, index=-1; - - for(i=0; iname, myconf[13]) == 0) && (nicnum <= MAXNICNUM)){ - i = 1; - if(cmd->data.list[0] && cmd->data.list[1]){ - //查找设备列表中是否已有该网卡 - //如果没有则创建一项 - //如果已有则只要增加ping的地址和地址数量 - //一般都能找到,因为配置文件中一般总是先定义"nic",后定义"ping" - //如果没找到,则表明先定义了"ping",未定义"nic"或后定义"nic",而未定义"nic"应该是错误的 - index = find_nic_config(cmd->data.list[0]); - if(index == -1){ - strncpy(conf.nic[nicnum].dev_name, cmd->data.list[0], strlen(cmd->data.list[0]) >= IPLEN ? IPLEN-1 : strlen(cmd->data.list[0])); - card_info_index = nicnum; - nicnum++; - } - else - card_info_index = index; - - while(cmd->data.list[i]){ - if((int)(inet_addr(cmd->data.list[i])) == -1){ - sprintf(wrong_ip,"The wrong IP address:'%s'",cmd->data.list[i]); - strcat(wrong_ip,"\n"); - record_log(wrong_ip); - } else{ - strncpy(conf.nic[card_info_index].ping_ip[conf.nic[card_info_index].gw_num++], cmd->data.list[i], strlen(cmd->data.list[i]) >= IPLEN ? IPLEN-1 : strlen(cmd->data.list[i])); - } - i++; - } - } - } - - //"nic"选项的处理 - if((strcmp(cmd ->name, myconf[12]) == 0) && (nicnum <= MAXNICNUM)){ - //设备列表中没有给定的网卡设备,则添加一项 - //否则不必添加 - //对应配置文件中先指定"ping"后指定"nic"的情况 - index = find_nic_config(cmd->data.str); - if(index == -1){ - strncpy(conf.nic[nicnum].dev_name, cmd->data.str, strlen(cmd->data.str) >= NIC_NAME_LEN ? NIC_NAME_LEN-1 : strlen(cmd->data.str)); - nicnum++; - } - } - - if((strcmp(cmd ->name, myconf[11]) == 0)){ - strncpy(conf.udp, cmd->data.str, strlen(cmd->data.str)); - } - - return NULL; -} - -DOTCONF_CB(cb_int) -{ - if(strcmp(cmd ->name, myconf[0]) == 0){ - conf.domain = cmd->data.value; - } - if(strcmp(cmd ->name, myconf[1]) == 0){ - conf.serv = cmd->data.value; - } - if(strcmp(cmd ->name, myconf[2]) == 0){ - conf.event = cmd->data.value; - } - if(strcmp(cmd ->name, myconf[3]) == 0){ - conf.udpport = cmd->data.value; - } - if(strcmp(cmd ->name, myconf[6]) == 0){ - conf.monitor_interval = cmd->data.value; - } - if(strcmp(cmd ->name, myconf[7]) == 0){ - conf.write_interval = cmd->data.value; - } - if(strcmp(cmd ->name, myconf[8]) == 0){ - conf.flow_interval = cmd->data.value; - } - if(strcmp(cmd ->name, myconf[9]) == 0){ - conf.flow_limit = cmd->data.value; - } - if(strcmp(cmd ->name, myconf[10]) == 0){ - conf.flow_peak = cmd->data.value; - } - if(strcmp(cmd ->name, myconf[14]) == 0){ - conf.pingnum = cmd->data.value; - } - if(strcmp(cmd ->name, myconf[15]) == 0){ - conf.pinglap = cmd->data.value; - } - - return NULL; -} - -DOTCONF_CB(cb_list) -{ - return NULL; -} - -int get_conf() -{ -// int uid; -// char buf[1024]; -// int i; -// struct passwd *user; - configfile_t *configfile; - -// memset(buf, 0, sizeof(buf)); -// uid=getuid(); -// user = getpwnam("d5000"); -// user = getpwuid(uid); - -// sprintf(buf,"%s", user->pw_dir); -// strcat(buf,conf_path); - - configfile = dotconf_create(conf_path, options, NULL, CASE_INSENSITIVE); - if (!configfile) - { - record_log("Warning can't reading config file\n"); - return -1; - } - - if (dotconf_command_loop(configfile) == 0){ - record_log("Warning can't reading config file\n"); - return -1; - } - dotconf_cleanup(configfile); - - if(conf.domain == 0) - conf.domain = 10; - if(conf.serv == 0) - conf.serv = 1; - if(conf.event == 0) - conf.event = 2; - if(conf.udpport == 0) - conf.udpport = 15000; - if(conf.monitor_interval == 0) - conf.monitor_interval = 100; - if(conf.write_interval == 0) - conf.write_interval = 300; - if(conf.flow_interval == 0) - conf.flow_interval = 60; - if(conf.flow_limit == 0) - conf.flow_limit = 30; - if(conf.flow_peak == 0) - conf.flow_peak = 1000; - if(conf.udp[0] == 0) - strcpy(conf.udp, "bond0"); - if(conf.nic[0].dev_name[0] == 0) - strcpy(conf.nic[0].dev_name, "bond0"); - if(conf.pingnum == 0) - conf.pingnum = 2; - if(conf.pinglap == 0) - conf.pinglap = 1; - - return 0; -} - -int isrun(char *p) -{ - char buf[16]; - char cmdbuf[1024]; - FILE *fp; - - memset(buf, 0, sizeof(buf)); - memset(cmdbuf, 0, sizeof(cmdbuf)); - - snprintf(cmdbuf, sizeof(cmdbuf), "pidof %s", p); - fp=popen(cmdbuf, "r"); - fread(buf, sizeof(char), sizeof(buf), fp); - pclose(fp); - if(strchr(buf, ' ') != NULL) - return 1; - else - return 0; -} - -int get_broad_ip(void) -{ - char ip_p[4][4]; - char error_str[200]; - int skfd; - struct ifreq ifr; - - memset(&ifr, 0, sizeof(ifr)); - memset(ip_p, 0, sizeof(ip_p)); - - if((skfd = socket(PF_INET, SOCK_DGRAM, 0)) == -1)return -1; - strcpy(ifr.ifr_name, conf.udp); - if (!ioctl(skfd, SIOCGIFBRDADDR, &ifr)){ - sprintf(ip_p[0], "%d", (unsigned char)ifr.ifr_broadaddr.sa_data[2]); - sprintf(ip_p[1], "%d", (unsigned char)ifr.ifr_broadaddr.sa_data[3]); - sprintf(ip_p[2], "%d", (unsigned char)ifr.ifr_broadaddr.sa_data[4]); - sprintf(ip_p[3], "%d", (unsigned char)ifr.ifr_broadaddr.sa_data[5]); - }else{ - snprintf(error_str, sizeof(error_str), "EMERG: Invalid broadcast device -- %s\n", conf.udp); - record_log(error_str); - close(skfd); - return -1; - } - close(skfd); - strcat(conf.ip, ip_p[0]); - strcat(conf.ip, "."); - strcat(conf.ip, ip_p[1]); - strcat(conf.ip, "."); - strcat(conf.ip, ip_p[2]); - strcat(conf.ip, "."); - strcat(conf.ip, ip_p[3]); - - return 0; -} - -void create_dir(char *create_path) -{ - int i,len = strlen(create_path); - - len = strlen(create_path); - - for(i=1; inext; - while(curr != NULL){ - if(!strncmp(curr->name, conf.nic[i].dev_name, NIC_NAME_LEN)){ - if((node = (NET_NAME_ST *)malloc(sizeof(NET_NAME_ST))) == NULL){ - err_val = 1; - break; - } - memset(node->name, 0, NIC_NAME_LEN); - strncpy(node->name, conf.nic[i].dev_name, NIC_NAME_LEN); - node->next = opt_name_list->next; - opt_name_list->next = node; - count_t ++; - break; - } - curr = curr->next; - } - if(err_val)break; - } - if(count_t != i || err_val){ - for(curr = opt_name_list->next; curr != NULL; curr = curr->next)free(curr); - snprintf(error_str, sizeof(error_str), "EMERG: Invalid netcard name --\n"); - record_log(error_str); - return -1; - } - err_val = 0; - - return 0; -} - -int get_netcard_status(NETCARD_INFO_ST *listp) -{ - if((listp->info.flags & IFF_UP) != IFF_UP) - return NIC_DOWN; - - if((listp->info.flags & IFF_RUNNING) != IFF_RUNNING) - return NIC_UNRUNNING; - - //20100510 - if(elapsed_ping_time == total_ping_time){ - if(ping_gw(listp->info.charname) != 0) - return NIC_UNLINKABLE; - } - else{ - if(listp->status == NIC_UNLINKABLE) - return NIC_UNLINKABLE; - } - - return NIC_NORMAL; -} - -int do_alarm(NETCARD_INFO_ST *listp, int nic_status, D5000_NIC_ALARM *Malarm) -{ - int i; - char error_str[200]; - - memset(Malarm->tSysNetcardAlarm.fault_devname, 0, NIC_NAME_LEN); - strncpy(Malarm->tSysNetcardAlarm.fault_devname, host_name, host_name_size); - strncat(Malarm->tSysNetcardAlarm.fault_devname, "-", 1); - strncat(Malarm->tSysNetcardAlarm.fault_devname, listp->info.charname, NIC_NAME_LEN - host_name_size - 1); - Malarm->tSysNetcardAlarm.retrytimes = 0; - Malarm->tMsgFrame.len = sizeof(Malarm); - Malarm->tMsgFrame.seqno = seqno; - - switch(nic_status){ - case NIC_DOWN: - Malarm->tSysNetcardAlarm.flags = NETCARD_ALARM_FAULT; - snprintf(error_str, sizeof(error_str), "NOTICE: %s is DOWN!\n", listp->info.charname); - break; - case NIC_UNRUNNING: - Malarm->tSysNetcardAlarm.flags = NETCARD_ALARM_FAULT; - snprintf(error_str, sizeof(error_str), "NOTICE: %s is UP but UNRUNNING!\n", listp->info.charname); - break; - case NIC_UNLINKABLE: - Malarm->tSysNetcardAlarm.flags = NETCARD_ALARM_FAULT; - listp->info.flags &= (~IFF_UP); - listp->info.flags &= (~IFF_RUNNING); - snprintf(error_str, sizeof(error_str), "NOTICE: %s is RUNNING but UNLINKABLE!\n", listp->info.charname); - break; - case NIC_NORMAL: - Malarm->tSysNetcardAlarm.flags = NETCARD_ALARM_RESUME; - snprintf(error_str, sizeof(error_str), "NOTICE: %s is NORMAL!\n", listp->info.charname); - break; - } - pthread_mutex_lock(&mut); - for(i = 0; i < 3; i++){ - send_alarm(Malarm, conf.udpport, conf.ip); - seqno++; - Malarm->tMsgFrame.seqno = seqno; - Malarm->tSysNetcardAlarm.retrytimes++; - } - send_inc_info_one(&listp->info, semid, shm_ptr); - pthread_mutex_unlock(&mut); - record_log(error_str); - - return 0; -} - -int main(int argc, char ** argv) -{ - int ret = 0, proc_stat = 0; - unsigned int i = 0; - int nic_status; - int sys_time_stamp = 0; - char *retp = NULL; - char *str = NULL; - char bond[128]; - char error_str[200]; - char linebuf[LINE_SIZE]; - THENV *arg; - glob_t res; - FILE *bond_fp = NULL; - pthread_t tid[THRNR]; - pthread_t tid_mem; - pthread_t tid_flow; - pthread_t tid_time; - THMEM mem; - THFLOW flow; - struct timespec s_time, os_time, log_time; - NET_NAME_ST all_name_list, opt_name_list; - NETCARD_INFO_ST inc_info_list, *listp = NULL; - D5000_NIC_ALARM Malarm; - struct sigaction sig; - struct passwd *user; - proc_invocation prcm; - - char log_record[125]={0}; - - memset(&conf, 0, sizeof(CONFIG_FILE_ST)); - if(isrun(argv[0]) == 1){ - printf("the program is already started!\n"); - exit(0); - } -//process register -#if 1 - if((proc_stat=prcm.proc_init("sys","base_srv","sys_nicmonitor"))==-1){ - perror("proc_init()"); - exit(-1); - } -#endif - - sig.sa_handler = sig_handler; - sigemptyset(&sig.sa_mask); - sigaddset(&sig.sa_mask, SIGINT); - sigaddset(&sig.sa_mask, SIGTERM); - sig.sa_flags = 0; - sigaction(SIGTERM, &sig, NULL); - sigaction(SIGINT, &sig, NULL); - sigaction(SIGSEGV, &sig, NULL); - - /* create dir file path */ - if((user = getpwnam("d5000")) != NULL){ - sprintf(log_path,"%s",user->pw_dir); - sprintf(shm_path,"%s",user->pw_dir); - sprintf(sem_path,"%s",user->pw_dir); - sprintf(conf_path,"%s",user->pw_dir); - } - - strcat(log_path,LOG_PATH); - strcat(shm_path,SHM_PATH); - strcat(sem_path,SEM_PATH); - strcat(conf_path,CONF_FILE); - - create_semdir(); - create_shmdir(); - create_logdir(); - create_confdir(); - - /* get process name */ - memset(process_name, 0, sizeof(process_name)); - if((retp = strrchr(argv[0], '/')) != NULL){ - retp++; - strcpy(process_name, retp); - }else{ - strcpy(process_name, argv[0]); - } - /*create logfile thread */ - pthread_create(&tid_time, NULL, create_logfile, NULL); - - /* initialize some....*/ - memset(&all_name_list, 0, sizeof(NET_NAME_ST)); - all_name_list.next = NULL; - memset(&opt_name_list, 0, sizeof(NET_NAME_ST)); - opt_name_list.next = NULL; - - memset(&inc_info_list, 0, sizeof(NETCARD_INFO_ST)); - inc_info_list.next = NULL; - - - /* get all netcard by proc/net/dev and ioctl */ - get_all_name(&all_name_list); - - /* wait for create log thread */ - log_time.tv_sec = 0; - log_time.tv_nsec = 1000000; - while(log_flag)nanosleep(&log_time, NULL); - - /* sure the process is started */ - snprintf(error_str, sizeof(error_str), "NOTICE: %s V%s is started !\n", process_name, MNIC_VERSION); - record_log(error_str); - - get_conf(); - - if(shm_path){ - printf("shm_path:%s\n",shm_path); - sprintf(log_record,"shm_path:%s\n",shm_path); - record_log(log_record); - } - - if(log_path){ - printf("log_path:%s\n",log_path); - sprintf(log_record,"log_path:%s\n",log_path); - record_log(log_record); - } - - - if(conf.udp){ - printf("udp %s\n",conf.udp); - sprintf(log_record,"udp %s\n",conf.udp); - record_log(log_record); - } - - for(i=0;conf.nic[i].dev_name[0]!=0;i++){ - printf("nic %s\n",conf.nic[i].dev_name); - sprintf(log_record,"nic %s\n",conf.nic[i].dev_name); - record_log(log_record); - } - - /* get broadcast address*/ - if((ret = get_broad_ip()) != 0) - return -1; - if((ret = create_name_list(&all_name_list, &opt_name_list)) != 0) - return -1; - /* get hostname */ - if((ret = gethostname(host_name, sizeof(host_name))) != -1){ - host_name_size = strlen(host_name); - }else host_name_size = HOST_NAME_SIZE; - /* initialize Malarm */ - Malarm.tMsgFrame.serv = conf.serv; - Malarm.tMsgFrame.event = conf.event; - Malarm.tMsgFrame.domain = conf.domain; - - /* get bonding file */ - memset(bond, 0, sizeof(bond)); - memset(linebuf, 0, sizeof(linebuf)); - if((ret = glob(BOND_PATH, 0, NULL, &res)) != 0){ - snprintf(error_str, sizeof(error_str), "NOTICE: No bonding netcard !\n"); - record_log(error_str); - strcpy(bond, "No bonding netcard!"); - } - for (i = 0; i < res.gl_pathc; ++i) { - if((arg = (THENV *)malloc(sizeof(THENV))) != NULL){ - memset(arg, 0, sizeof(THENV)); - strncpy(arg->bond_file_path, res.gl_pathv[i], strlen(res.gl_pathv[i])); - arg->Malarm = Malarm; - strncpy(arg->host_name, host_name, host_name_size); - arg->host_name_size = host_name_size; - arg->conf = &conf; - if(pthread_create(tid+i, NULL, check_inc_switch, (void*)arg)){ - snprintf(error_str, sizeof(error_str), "EMERG: pthread_create(): %s\n", strerror(errno)); - record_log(error_str); - } - } - } - for(i = 0; i < res.gl_pathc; ++i){ - if((bond_fp = fopen(res.gl_pathv[i], "r")) == NULL){ - snprintf(error_str, sizeof(error_str), "EMERG: fopen(): %s\n", strerror(errno)); - record_log(error_str); - }else{ - retp = strrchr(res.gl_pathv[i], '/'); - retp++; - strcat(bond, retp); - strcat(bond, ":"); - while((str = fgets(linebuf, sizeof(linebuf), bond_fp)) != NULL){ - if(!strncmp(linebuf, "Slave Interface:", 16)){ - str += 16; - retp = parse_str(&str); - strcat(bond, retp); - strcat(bond, " "); - } - } - fclose(bond_fp); - } - } - - /* initialize time*/ - s_time.tv_sec = 0; - s_time.tv_nsec = conf.monitor_interval*1000*1000; - - /* initialize sem*/ - init_sem(&semid, &conf); - init_shm(&shm_ptr, &conf); - /* for IPC_RMID */ - sem_id = semid; - - /* create monitor netcard list */ - create_monit_list(&opt_name_list, &inc_info_list, bond); - release_name_list(&all_name_list); - release_name_list(&opt_name_list); - - mem.semid = semid; - mem.shm_ptr = shm_ptr; - mem.listp = &inc_info_list; - mem.conf = &conf; - if(pthread_create(&tid_mem, NULL, send_inc_info, (void *)&mem)){ - snprintf(error_str, sizeof(error_str), "EMERG: pthread_create(): %s\n", strerror(errno)); - record_log(error_str); - } - - /* create caculate flow thread*/ - flow.env.Malarm = Malarm; - strncpy(flow.env.host_name, host_name, host_name_size); - flow.env.host_name_size = host_name_size; - flow.mem.semid = semid; - flow.mem.shm_ptr = shm_ptr; - flow.mem.listp = &inc_info_list; - flow.mem.conf = &conf; - if(pthread_create(&tid_flow, NULL, cacu_flow, (void *)&flow)){ - snprintf(error_str, sizeof(error_str), "EMERG: pthread_create(): %s\n", strerror(errno)); - record_log(error_str); - } - - total_ping_time = 1000 / conf.monitor_interval; - if(total_ping_time == 0) - total_ping_time = 1; - elapsed_ping_time = 1; - - while(1){ - i = 0; - listp = inc_info_list.next; - for(i = 0; listp != NULL; i++){ - get_inc_info(&listp->info); - get_inc_stats(&listp->info); - nic_status = get_netcard_status(listp); - if(nic_status != listp->status){ - do_alarm(listp, nic_status, &Malarm); - listp->status = nic_status; - } - listp = listp->next; - } - - if(seqno > 32765)seqno = 0; - nanosleep(&s_time, &os_time); - - - //20100510 - ++elapsed_ping_time; - if(elapsed_ping_time > total_ping_time) - elapsed_ping_time = 1; - - sys_time_stamp += conf.monitor_interval; - if(sys_time_stamp >= 1000){ - sys_time_stamp = 0; - write_time(semid, shm_ptr); - } - } - for (i = 0; i < res.gl_pathc; i++) { - pthread_join(tid[i], NULL); - } - pthread_join(tid_mem, NULL); - pthread_join(tid_flow, NULL); - pthread_join(tid_time, NULL); - pthread_mutex_destroy(&mut); - globfree(&res); - munmap(shm_ptr, sizeof(SHM)); - return 0; -} diff --git a/branches/sys_nicmonitor-1.5/mnic.h b/branches/sys_nicmonitor-1.5/mnic.h deleted file mode 100644 index d7a6390..0000000 --- a/branches/sys_nicmonitor-1.5/mnic.h +++ /dev/null @@ -1,44 +0,0 @@ -#ifndef __MNIC_H -#define __MNIC_H - -//#define _GNU_SOURCE -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include - -#include "sys_netcard.h" -#include -#include -#define IPSIZE 16 - -char *get_name(char *, char *); -int if_fetch(NETCARD_INFO *); -int ioc_get_name(NETCARD_INFO *); -int get_dev_fields(char *p, NETCARD_INFO *); -int get_user_home(int , char *); -void send_alarm(D5000_NIC_ALARM *, int , char *); -void record_log(char *); - -#endif diff --git a/branches/sys_nicmonitor-1.5/nicinfo_shm.c b/branches/sys_nicmonitor-1.5/nicinfo_shm.c deleted file mode 100644 index c8a8bf2..0000000 --- a/branches/sys_nicmonitor-1.5/nicinfo_shm.c +++ /dev/null @@ -1,209 +0,0 @@ -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include - -#include "nicinfo_shm.h" - -#define SHM_PATH "/tmp/sys_netcard_shm_path" -#define SEM_PATH "/tmp/sys_netcard_sem_path" -#define LOG_PATH "/var/log/netcard/" - -typedef struct net_info{ - NETCARD_INFO info[MAXNICNUM]; -}SHM; - -static int semid = 0; -static char log_path[1024]; -static char shm_path[1024]; -static char sem_path[1024]; -SHM *ptr = NULL; - -/* write error in logfile */ -void record_log(char *str) -{ - time_t tamp; - char str_tm[4]; - char log_str[512]; - FILE *log_fp = NULL; - struct tm tmptr; - struct timeval tv; - struct timezone tz; - - if((log_fp = fopen(log_path, "a")) != NULL){ - tamp = time(NULL); - memset(str_tm, 0, sizeof(str_tm)); - memset(log_str, 0, sizeof(log_str)); - localtime_r(&tamp, &tmptr); - gettimeofday(&tv, &tz); - snprintf(str_tm, sizeof(str_tm), "%d", (int)tv.tv_usec/1000); - if(str_tm[1] == '\0')str_tm[1] = '0'; - if(str_tm[2] == '\0')str_tm[2] = '0'; - strftime(log_str, sizeof(log_str), "%F %T.", &tmptr); - strcat(log_str, str_tm); - strcat(log_str, " "); - strcat(log_str, "get_nic_info"); - strcat(log_str, " "); - strcat(log_str, str); - if(fwrite(log_str, 1, strlen(log_str), log_fp) < 1){ - } - fclose(log_fp); - } -} - -void get_sem(int semid) -{ - char err_str[200]; - struct sembuf lock[2]; - - lock[0].sem_num = 0; - lock[0].sem_op = 0; - lock[0].sem_flg = SEM_UNDO; - - lock[1].sem_num = 0; - lock[1].sem_op = 1; - lock[1].sem_flg = SEM_UNDO; - - while(semop(semid, lock, 2)){ - if (errno == EINTR) { - continue; - } - snprintf(err_str, sizeof(err_str), "EMERG: semop():\n", strerror(errno)); - record_log(err_str); - return; - } -} - -void release_sem(int semid) -{ - int ret; - char err_str[200]; - struct sembuf unlock; - - unlock.sem_num = 0; - unlock.sem_op = -1; - unlock.sem_flg = SEM_UNDO; - - if((ret = semop(semid, &unlock, 1)) == -1){ - snprintf(err_str, sizeof(err_str), "EMERG: semop():\n", strerror(errno)); - record_log(err_str); - return; - } -} - - -int init_nic_info(void) -{ - int fd = -1; - int sem_val; - char err_str[200]; - key_t key; - - - if ((key=ftok(sem_path, SEM_PROJ_ID)) == -1) { - snprintf(err_str, sizeof(err_str), "EMERG: ftok():%s\n", strerror(errno)); - record_log(err_str); - return -1; - } - if((semid = semget(key, 1, IPC_CREAT|0666)) == -1){ - snprintf(err_str, sizeof(err_str), "EMERG: Create semaphore error: %s\n", strerror(errno)); - record_log(err_str); - return -1; - } - /* - if((sem_val = semctl(semid, 0, GETVAL, 0)) == -1){ - snprintf(err_str, sizeof(err_str), "EMERG: semctl: %s !\n", strerror(errno)); - record_log(err_str); - semctl(semid, 0, IPC_RMID); - return -1; - } - if(!sem_val){ - if(semctl(semid, 0, SETVAL, 1) == -1){ - snprintf(err_str, sizeof(err_str), "EMERG: semctl: %s !\n", strerror(errno)); - record_log(err_str); - semctl(semid, 0, IPC_RMID); - return -1; - } - } - */ - if((fd = open(shm_path, O_RDONLY)) == -1){ - snprintf(err_str, sizeof(err_str), "EMERG: Error open %s: %s!\n",shm_path, strerror(errno)); - record_log(err_str); - return -1; - } - ptr = (SHM*)mmap(NULL, sizeof(SHM), PROT_READ, MAP_SHARED, fd, 0); - if(ptr == MAP_FAILED){ - snprintf(err_str, sizeof(err_str), "EMERG: mmap():%s\n", strerror(errno)); - record_log(err_str); - return -1; - } - close(fd); - - return 0; -} -int get_nic_info(char *nic_name, NETCARD_INFO *net_info) -{ - int i,ret; - time_t tamp; - struct tm tmptr; - DIR *dir; - char *s; - char buf[128]; - char err_str[200]; - struct passwd *user; - - tamp = time(NULL); - localtime_r(&tamp, &tmptr); - - if((user = getpwnam("d5000"))!= NULL){ - sprintf(log_path,"%s",user->pw_dir); - sprintf(shm_path,"%s",user->pw_dir); - sprintf(sem_path,"%s",user->pw_dir); - } - strcat(log_path,LOG_PATH); - strcat(shm_path,SHM_PATH); - strcat(sem_path,SEM_PATH); - - if((dir = opendir(log_path)) == NULL){ - if(errno == ENOENT){ - strcpy(log_path, "/tmp/"); - } - } - closedir(dir); - s = strrchr(log_path, '/'); - s++; - *s = '\0'; - memset(buf, 0, sizeof(buf)); - strftime(buf, sizeof(buf), "%Y%m%d_", &tmptr); - strcat(buf, "sys_nicmonitor"); - strcat(buf, ".log"); - strcat(log_path, buf); - if((ret = init_nic_info()) == -1){ - return -1; - } - get_sem(semid); - for(i = 0; ptr->info[i].charname[0] != 0; i++){ - if(!strcmp(ptr->info[i].charname, nic_name)){ - // get_sem(semid); - memcpy(net_info, &ptr->info[i], sizeof(NETCARD_INFO)); - release_sem(semid); - munmap(ptr, sizeof(SHM)); - return 0; - } - } - release_sem(semid); - snprintf(err_str, sizeof(err_str), "NOTICE: No information of %s !\n", nic_name); - record_log(err_str); - munmap(ptr, sizeof(SHM)); - return -1; -} diff --git a/branches/sys_nicmonitor-1.5/nicinfo_shm.h b/branches/sys_nicmonitor-1.5/nicinfo_shm.h deleted file mode 100644 index cd74135..0000000 --- a/branches/sys_nicmonitor-1.5/nicinfo_shm.h +++ /dev/null @@ -1,52 +0,0 @@ -#ifndef NIC_SHM_H -#define NIC_SHM_H - -#include - -#ifdef __cplusplus -extern "C" { -#endif - -#ifndef NIC_NAME_LEN -#define NIC_NAME_LEN 64 -#endif -#define SEM_PROJ_ID 's' -#define MAXNICNUM 32 - -typedef long KEYID; -typedef struct _net_info -{ - KEYID ID; - char charname[NIC_NAME_LEN]; - char descr[128]; - struct sockaddr addr; - struct sockaddr broadaddr; - struct sockaddr netmask; - struct sockaddr hwaddr; - int time_stamp; - short flags; - int mtu; - int tx_queue_len; - unsigned long long average_flow; - unsigned long long rx_packets; - unsigned long long tx_packets; - unsigned long long rx_bytes; - unsigned long long tx_bytes; - unsigned long rx_errors; - unsigned long tx_errors; - unsigned long rx_dropped; - unsigned long tx_dropped; - unsigned long rx_multicast; - unsigned long collisions; - unsigned long rx_fifo_errors; - unsigned long tx_carrier_errors; - unsigned long tx_fifo_errors; -}NETCARD_INFO; - -int get_nic_info(char *nic_name, NETCARD_INFO *net_info); - - -#ifdef __cplusplus -} -#endif -#endif diff --git a/branches/sys_nicmonitor-1.5/proc_inv.h b/branches/sys_nicmonitor-1.5/proc_inv.h deleted file mode 100644 index 945622c..0000000 --- a/branches/sys_nicmonitor-1.5/proc_inv.h +++ /dev/null @@ -1,198 +0,0 @@ -//////////////////////////////////////////////////////////////////////////////////// -// -// Copyright (c) Comets' Grp. of Kedong Corp 2008. All Rights Reserved. -// -// FileName : procconf.h -// -// Function : this class realize some basic functions for process managerment, -// such as initiate process, report status of process, check status of process, -// update status of process, get information of process -// -// Author : -// -// Date : -// -// Modify by : -// -// Mod Date : -// -//////////////////////////////////////////////////////////////////////////////////// - -#ifndef _PROCCONF_H -#define _PROCCONF_H - -#include -#include -#include -#include -#include -#include -#include -//#include -#include - -#include "const.h" - -//for alarm ................................ -typedef struct MESS_BH -{ - unsigned char mtype; - int length; -}; -typedef struct PROCESS_ALM -{ - char context_name[MAX_STRING_LEN]; - char app_name[MAX_STRING_LEN]; - char proc_name[MAX_STRING_LEN]; - unsigned char status; -}; -//for alarm end ............................. - -//for mmi.................................... -const int MAX_BUFFER_LEN = 500; -typedef struct MESS_BLOCK -{ - unsigned char num; - char buffer[MAX_BUFFER_LEN]; -}; -typedef struct MESS_PROC -{ - char context_name[MAX_STRING_LEN]; - char app_name[MAX_STRING_LEN]; - char proc_name[MAX_STRING_LEN]; - char status; -}; -//for mmi end ................................ - -const int DEFAULT_PERIOD = 3; -const int COUNT_LIMIT = 2; -const int START_DEFAULT_PERIOD = 60; -const int START_COUNT_LIMIT = 5; -const int APP_COUNT_LIMIT = 1; - -//process critical level -//const int WST_CRITICAL = 1; // Shutdown and reboot workstation when failed -//const int SYS_CRITICAL = 2; // Shutdown and reboot the system on the wst when failed -//const int USER_CRITICAL = 3; // Shutdown and reboot the subsystem when failed -//const int GENERAL = 4; // reboot the process -const int CRUCIAL = 1; // crucial process -const int GENERAL = 0; // general process - - -extern int srv_init(char *service,int port); -extern int Tcp_close(int sockfd); -extern int Tcp_read(int fd,char *ptr,int nbytes); -extern int Tcp_write(int fd,char *ptr,int nbytes); -extern int srv_accept(int fd,struct sockaddr *cli_addr,int *clilen); -extern int client_tcp_open(char *host,char *service,int port); - -typedef struct -{ - char context_name[MAX_STRING_LEN]; - char app_name[MAX_STRING_LEN]; - char proc_name[MAX_STRING_LEN]; - pid_t proc_pid; -}PROC_ADM_INFO; - -//*************************************************************** -// structure name : PROC_INFO -// function : store process informatin -// author : -// date : -// modify by : -// modification : -// mod date : -//*************************************************************** -typedef struct -{ - int position; - char node_name[MAX_STRING_LEN]; - char context_name[MAX_STRING_LEN]; - char app_name[MAX_STRING_LEN]; - char proc_name[MAX_STRING_LEN]; - - unsigned char active_flag; - unsigned char master_flag; - - time_t startup_time; - time_t refresh_time; - short refresh_peri; - unsigned char monitor_type; - - pid_t proc_pid; - unsigned char auto_start; - unsigned char act_timer; - unsigned char start_timer; - unsigned char critical_level; - - char exefile_path[MAX_EXECMD_LEN]; - -}PROC_INFO; - -//*************************************************************** -// structure name : APP_INFO -// function : store application informatin -// author : -// date : -// modify by : -// modification : -// mod date : -//*************************************************************** -typedef struct -{ - int position; - char context_name[MAX_STRING_LEN]; - int context_id; - char app_name[MAX_STRING_LEN]; - int app_id; - unsigned char act_timer; - unsigned char active_flag; -}APP_INFO; - - -typedef struct -{ - int no_proc; - int semdes_cfg; - PROC_INFO proc[MAX_LOCAL_PROCESS]; - APP_INFO app[MAX_LOCAL_APP]; -}PROCCFG; - -class proc_invocation -{ - public: - int m_init; - PROCCFG *proccfg_p; - - public: - proc_invocation(); - ~proc_invocation(); - - int conf_create(); - //int check_proc_status(); - //int update_rtdb(); - //int kill_proc(pid_t pid); - //int start_proc(char *cmdline); - //int send_alarm(char *context_name, char *app_name, char *proc_name, unsigned char status); - //int update_proc_status(char *context_name, char *app_name, char *proc_name, char status); - - //int proc_init(char *context_name, char *app_name, char *proc_name, int critical_level);//exefile_path,auto_start - int proc_init(char *context_name, char *app_name, char *proc_name); - int proc_report(int pos, char status, int intertime=3); - int proc_exit(int proc_pos); - - int get_pos(char *context_name, char *app_name, char *proc_name); - int is_proc_exist(char *context_name, char *app_name, char *proc_name); - int conf_map(); - - int get_procinfo(int position, PROC_ADM_INFO *p_info); - int get_active_pid(int &num, int *p_pidlist); - int is_proc_run(pid_t pid); - int is_proc_run(char *context_name, char *app_name, char *proc_name); - -}; - -#endif - - - diff --git a/branches/sys_nicmonitor-1.5/read_netcard.c b/branches/sys_nicmonitor-1.5/read_netcard.c deleted file mode 100644 index c745f48..0000000 --- a/branches/sys_nicmonitor-1.5/read_netcard.c +++ /dev/null @@ -1,133 +0,0 @@ -#include "mnic.h" - -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; -} - - -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 == '\0') - return NULL; - p++; - break; - } - *name++ = *p++; - } - *name++ = '\0'; - return p; -} - -int get_dev_fields(char *str, NETCARD_INFO *ife) -{ - 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]); - return 0; -} - -int if_fetch(NETCARD_INFO *ife) -{ - int skfd; - struct ifreq ifr; - - if((skfd = socket(PF_INET, SOCK_DGRAM, 0)) == -1)return -1; - - strcpy(ifr.ifr_name, ife->charname); - if (!ioctl(skfd, SIOCGIFFLAGS, &ifr)) - ife->flags = ifr.ifr_flags; - - if (!ioctl(skfd, SIOCGIFHWADDR, &ifr)) - ife->hwaddr = ifr.ifr_hwaddr; - - if (!ioctl(skfd, SIOCGIFMTU, &ifr)) - ife->mtu = ifr.ifr_mtu; - - if (!ioctl(skfd, SIOCGIFTXQLEN, &ifr)) - ife->tx_queue_len = ifr.ifr_qlen; - - if (!ioctl(skfd, SIOCGIFADDR, &ifr)) - ife->addr = ifr.ifr_addr; - - if (!ioctl(skfd, SIOCGIFBRDADDR, &ifr)) - ife->broadaddr = ifr.ifr_broadaddr; - - if (!ioctl(skfd, SIOCGIFNETMASK, &ifr)) - ife->netmask = ifr.ifr_netmask; - - ife->time_stamp = time(NULL); - close(skfd); - return 0; -} - - -int ioc_get_name(NETCARD_INFO *name) -{ - int skfd; - int ifrnum; - int i = 0; - struct ifconf ifc; - struct ifreq buf[MAXNICNUM]; - - if((skfd = socket(PF_INET, SOCK_DGRAM, 0)) == -1)return -1; - memset(buf, 0, sizeof(buf)); - ifc.ifc_len = sizeof(buf); - ifc.ifc_buf = (caddr_t) buf; - ioctl(skfd, SIOCGIFCONF, &ifc); - ifrnum = ifc.ifc_len / sizeof(struct ifreq); - while(ifrnum-- > 0){ - strcpy(name[i].charname, buf[ifrnum].ifr_name); - i++; - } - close(skfd); - return i; -} - diff --git a/branches/sys_nicmonitor-1.5/send_alarm.c b/branches/sys_nicmonitor-1.5/send_alarm.c deleted file mode 100644 index e88635f..0000000 --- a/branches/sys_nicmonitor-1.5/send_alarm.c +++ /dev/null @@ -1,37 +0,0 @@ -#include "mnic.h" - -void send_alarm(D5000_NIC_ALARM *mesg, int socket_port, char *ipv4) -{ - int bytes; - int sock_sd; - int val; - char error_str[200]; - struct sockaddr_in recever; - socklen_t sock_len; - - if((sock_sd = socket(PF_INET, SOCK_DGRAM, IPPROTO_UDP)) == -1){ - snprintf(error_str, sizeof(error_str), "EMERG: socket(): %s\n", strerror(errno)); - record_log(error_str); - return; - } - val=1; - if ((setsockopt(sock_sd, SOL_SOCKET, SO_BROADCAST, &val, sizeof(val))) == -1) { - snprintf(error_str, sizeof(error_str), "EMERG: setsockopt(): %s\n", strerror(errno)); - record_log(error_str); - return; - } - /* init socket*/ - recever.sin_family = AF_INET; - recever.sin_port = htons(socket_port); - inet_pton(AF_INET, ipv4, &recever.sin_addr); - sock_len = sizeof(recever); - /* send alarm*/ - if((bytes = sendto(sock_sd, mesg, sizeof(D5000_NIC_ALARM), 0, (const struct sockaddr *)&recever, sock_len)) < 1){ - snprintf(error_str, sizeof(error_str), "EMERG: sendto(): %s\n", strerror(errno)); - record_log(error_str); - return; - } - //snprintf(error_str, sizeof(error_str), "NOTICE: Alarm information has already send!\n"); - //record_log(error_str); - close(sock_sd); -} diff --git a/branches/sys_nicmonitor-1.5/sys_netcard.h b/branches/sys_nicmonitor-1.5/sys_netcard.h deleted file mode 100644 index 0d7746c..0000000 --- a/branches/sys_nicmonitor-1.5/sys_netcard.h +++ /dev/null @@ -1,88 +0,0 @@ -#ifndef _SYS_NETCARD_H -#define _SYS_NETCARD_H - -#include - -#ifndef NIC_NAME_LEN -#define NIC_NAME_LEN 64 -#endif - -#define SEM_PROJ_ID 's' -#define MAXNICNUM 32 - -#ifdef __cplusplus -extern "C"{ -#endif - -typedef long KEYID; - -typedef struct _msg_frame // message frame -{ - short len; // message length - short seqno; // send sequence - short serv; // services ID - short event; // event ID - unsigned char domain; // domain ID - unsigned char ctxt; // Context ID - short stid; // SOURCE task id - short dtid; // DESTINATION task ID - unsigned char ver_coding; // 版本号 + 编码 - unsigned char mes_type; // 帧类型 -}MSG_FRAME, *LPMSG_FRAME; - -typedef struct _net_info -{ - KEYID ID; - char charname[NIC_NAME_LEN]; - char descr[128]; - struct sockaddr addr; - struct sockaddr broadaddr; - struct sockaddr netmask; - struct sockaddr hwaddr; - int time_stamp; - short flags; - int mtu; - int tx_queue_len; - unsigned long long average_flow; - unsigned long long rx_packets; - unsigned long long tx_packets; - unsigned long long rx_bytes; - unsigned long long tx_bytes; - unsigned long rx_errors; - unsigned long tx_errors; - unsigned long rx_dropped; - unsigned long tx_dropped; - unsigned long rx_multicast; - unsigned long collisions; - unsigned long rx_fifo_errors; - unsigned long tx_carrier_errors; - unsigned long tx_fifo_errors; -}NETCARD_INFO, *LPNETCARD_INFO; - -#define NETCARD_ALARM_FAULT 1 -#define NETCARD_ALARM_RESUME 2 -#define NETCARD_ALARM_SWITCH 3 -#define NETCARD_ALARM_ABNORMAL 4 -#define NETCARD_ALARM_NORMAL 5 -typedef struct _sys_netcard_alarm -{ - char fault_devname[NIC_NAME_LEN]; /*故障设备名称 */ - char switch_devname[NIC_NAME_LEN]; /*切换设备名称 */ - short flags; /* 状态标记 : 1:网卡故障。2:网卡恢复。3:网卡切换。4:流量异常。*/ - short retrytimes; //0,1,2 重发次数 -}SYS_NETCARD_ALARM, *LPSYS_NETCARD_ALARM; - - -typedef struct _d5000_nic_alarm -{ - MSG_FRAME tMsgFrame ; - SYS_NETCARD_ALARM tSysNetcardAlarm ; -}D5000_NIC_ALARM, *LPD5000_NIC_ALARM; - - -#ifdef __cplusplus -} -#endif - -#endif - diff --git a/branches/sys_nicmonitor-1.5/sys_nicmonitor b/branches/sys_nicmonitor-1.5/sys_nicmonitor deleted file mode 100755 index 5e5d31c..0000000 Binary files a/branches/sys_nicmonitor-1.5/sys_nicmonitor and /dev/null differ diff --git a/branches/sys_nicmonitor-1.6/Makefile b/branches/sys_nicmonitor-1.6/Makefile deleted file mode 100644 index 5cccc77..0000000 --- a/branches/sys_nicmonitor-1.6/Makefile +++ /dev/null @@ -1,28 +0,0 @@ -#! /usr/bin/make -f - -DEFAULTS = Makefile.config - -include $(DEFAULTS) - -all:sys_nicmonitor libnic_shm.so - -CFLAGS += -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 $@ $^ -libnic_shm.so:nicinfo_shm.c - $(CC) -fpic -shared -o $@ $^ -clean: - $(RM) *.o - $(RM) libnic_shm.so sys_nicmonitor -install: - $(MKDIR) -p $(LIBDIR) - $(MKDIR) -p $(BINDIR) - $(MKDIR) -p $(INCDIR) - $(CP) libnic_shm.so $(LIBDIR) - $(CP) sys_nicmonitor $(BINDIR) - $(CP) nicinfo_shm.h $(INCDIR) -uninstall: - $(RM) -f $(LIBDIR)/libnic_shm.so - $(RM) -f $(BINDIR)/sys_nicmonitor - $(RM) -f $(INCDIR)/nicinfo_shm.h diff --git a/branches/sys_nicmonitor-1.6/Makefile.config b/branches/sys_nicmonitor-1.6/Makefile.config deleted file mode 100644 index 7a2d8e6..0000000 --- a/branches/sys_nicmonitor-1.6/Makefile.config +++ /dev/null @@ -1,16 +0,0 @@ - -PREFIX = $(DESTDIR) - -BINDIR = $(PREFIX)/usr/bin - -LIBDIR = $(PREFIX)/usr/lib64 - -INCDIR = $(PREFIX)/usr/include - -INSTALL_SH = ./install.sh - -CP = cp -RM = rm -#CC = gcc -CC = g++ -MKDIR = mkdir diff --git a/branches/sys_nicmonitor-1.6/README1.5 b/branches/sys_nicmonitor-1.6/README1.5 deleted file mode 100644 index fa1087e..0000000 --- a/branches/sys_nicmonitor-1.6/README1.5 +++ /dev/null @@ -1,32 +0,0 @@ - -lijin:/home/d5000/lijin/sys_nicmonitor # ./sys_nicmonitor -shm_path:/home/d5000/tmp/sys_netcard_shm_path -log_path:/home/d5000/var/log/netcard/20110110_sys_nicmonitor.log -udp    bond0 -nic    bond0 -nic    eth0 -nic    eth1 - -d5000@lijin:~ > cat var/log/netcard/20110110_sys_nicmonitor.log -2011-01-10 11:10:37.761 sys_nicmonitor NOTICE: sys_nicmonitor V1.5 is -started ! -2011-01-10 11:10:37.761 sys_nicmonitor -shm_path:/home/d5000/tmp/sys_netcard_shm_path -2011-01-10 11:10:37.761 sys_nicmonitor -log_path:/home/d5000/var/log/netcard/20110110_sys_nicmonitor.log -2011-01-10 11:10:37.761 sys_nicmonitor udp    bond0 -2011-01-10 11:10:37.761 sys_nicmonitor nic    bond0 -2011-01-10 11:10:37.761 sys_nicmonitor nic    eth0 -2011-01-10 11:10:37.762 sys_nicmonitor nic    eth1 -2011-01-10 11:10:37.762 sys_nicmonitor NOTICE: bond0 is NORMAL! -2011-01-10 11:10:37.763 sys_nicmonitor NOTICE: eth0 is NORMAL! -2011-01-10 11:10:37.763 sys_nicmonitor NOTICE: eth1 is NORMAL! - -与1.4不一样的地方在与增加log文件中的打印语句 -sys_nicmonitor started! version v1.4 -shm_path: /home/d5000/tmp -log_path: /home/d5000/ var/log/netcard -udp bond0 -nic bond0 -nic eth0 -nic eth1 diff --git a/branches/sys_nicmonitor-1.6/README1.6 b/branches/sys_nicmonitor-1.6/README1.6 deleted file mode 100644 index da22610..0000000 --- a/branches/sys_nicmonitor-1.6/README1.6 +++ /dev/null @@ -1,2 +0,0 @@ -V1.5与V1.6差异 -将ping_gw中的system调用改为popen减少系统误报 diff --git a/branches/sys_nicmonitor-1.6/const.h b/branches/sys_nicmonitor-1.6/const.h deleted file mode 100644 index e2ebfa5..0000000 --- a/branches/sys_nicmonitor-1.6/const.h +++ /dev/null @@ -1,51 +0,0 @@ -#ifndef _RTE_CONST_H -#define _RTE_CONST_H - -#include - - -#define MAX_STRING_LEN 24 -#define MAX_EXECMD_LEN 200 -#define MAX_FILENAME_LEN 200 - -#define MAX_CONTEXT 8 -#define MAX_HOSTNAME_LEN 24 -#define MAX_LOCAL_MESPROC 512 -#define MAX_LOCAL_PROCESS MAX_LOCAL_MESPROC -#define MAX_LOCAL_APP 32 -#define MAX_LOCAL_NODE 256 - -#define MAX_SET 256 //max num of event set -#define MAX_EVENT 1300 -#define MAX_REG_PROC 20 - -#define LEN_SHMBLK_BIG 4096 -#define MAX_MSGBUF_LEN 32767 -#define FREE_PAGE_SIZE 65536 -#define MAX_PAGE_NUM 1024 - -#define MAX_QUE MAX_LOCAL_MESPROC -#define MAX_SEMPHORE_SET MAX_LOCAL_MESPROC*2 -#define RTE_HAN 0 // queue number for event handeler0(RTE) -#define EX_EVENT_HAN RTE_HAN // queue number for extrnal event handeler - -#define DOMAIN_I 1 -#define DOMAIN_II 2 -#define DOMAIN_III 3 - -//Add 20090225 -const int PROC_TYPE_RPT =1; -const int PROC_TYPE_UNRPT =0; -//add end - - -//status for proc and app -const int NON_ACTIVE = 0; -const int ACTIVE = 1; -const int HANGUP = 2; -const int FAILURE = 5; -const int START = 6; -const int STOP = 7; - -#endif - diff --git a/branches/sys_nicmonitor-1.6/mnic.c b/branches/sys_nicmonitor-1.6/mnic.c deleted file mode 100755 index ec574f2..0000000 --- a/branches/sys_nicmonitor-1.6/mnic.c +++ /dev/null @@ -1,1552 +0,0 @@ -#include "mnic.h" -#include "dotconf.h" -#include -#include -#include "proc_inv.h" - -#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 "/tmp/sys_netcard_shm_path" -#define SEM_PATH "/tmp/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.6" - -#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. -}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 find_nic_config(char *); - -static pthread_mutex_t mut = PTHREAD_MUTEX_INITIALIZER; -static short seqno = 1; -static char log_flag = 1; -static int sem_id = 0; -static char process_name[64]; -char log_path[1024]; -char shm_path[1024]; -char sem_path[1024]; -char conf_path[1024]; - -int host_name_size = 0; -char host_name[HOST_NAME_SIZE]; -int semid = -1; -SHM *shm_ptr; - -//20100510 -int total_ping_time, elapsed_ping_time; -int get_netcard_status(NETCARD_INFO_ST *); - -/****dotconf****/ -static DOTCONF_CB(cb_str); -static DOTCONF_CB(cb_int); -int nicnum = 0; -CONFIG_FILE_ST conf; - -char myconf[][64] = { - "domain", // 0 - "serv", // 1 - "event", // 2 - "udpport", // 3 - "sys_netcard_shm_path", // 4 - "sys_netcard_sem_path", // 5 - "monitor_interval", // 6 - "write_interval", // 7 - "flow_interval", // 8 - "flow_limit", // 9 - "flow_peak", // 10 - "udp", // 11 - "nic", // 12 - "ping", // 13 - "pingnum", // 14 - "pinglap", // 15 - "" -}; - -static const configoption_t options[] = { - {myconf[0], ARG_INT, cb_int, NULL, CTX_ALL}, - {myconf[1], ARG_INT, cb_int, NULL, CTX_ALL}, - {myconf[2], ARG_INT, cb_int, NULL, CTX_ALL}, - {myconf[3], ARG_INT, cb_int, NULL, CTX_ALL}, - {myconf[4], ARG_STR, cb_str, NULL, CTX_ALL}, - {myconf[5], ARG_STR, cb_str, NULL, CTX_ALL}, - {myconf[6], ARG_INT, cb_int, NULL, CTX_ALL}, - {myconf[7], ARG_INT, cb_int, NULL, CTX_ALL}, - {myconf[8], ARG_INT, cb_int, NULL, CTX_ALL}, - {myconf[9], ARG_INT, cb_int, NULL, CTX_ALL}, - {myconf[10], ARG_INT, cb_int, NULL, CTX_ALL}, - {myconf[11], ARG_STR, cb_str, NULL, CTX_ALL}, - {myconf[12], ARG_STR, cb_str, NULL, CTX_ALL}, - {myconf[13], ARG_STR, cb_str, NULL, CTX_ALL}, - {myconf[14], ARG_INT, cb_int, NULL, CTX_ALL}, - {myconf[15], ARG_INT, cb_int, NULL, CTX_ALL}, - LAST_OPTION -}; -/****dotconf****/ - -/* write error in logfile */ -void record_log(char *str) -{ - time_t tamp; - char str_tm[4]; - char log_str[512]; - FILE *log_fp = NULL; - struct tm tmptr; - struct timeval tv; - struct timezone tz; - - if((log_fp = fopen(log_path, "a")) != NULL){ - tamp = time(NULL); - memset(str_tm, 0, sizeof(str_tm)); - memset(log_str, 0, sizeof(log_str)); - localtime_r(&tamp, &tmptr); - gettimeofday(&tv, &tz); - snprintf(str_tm, sizeof(str_tm), "%d", (int)tv.tv_usec/1000); - if(str_tm[1] == '\0')str_tm[1] = '0'; - if(str_tm[2] == '\0')str_tm[2] = '0'; - strftime(log_str, sizeof(log_str), "%F %T.", &tmptr); - strcat(log_str, str_tm); - strcat(log_str, " "); - strcat(log_str, process_name); - strcat(log_str, " "); - strcat(log_str, str); - if(fwrite(log_str, 1, strlen(log_str), log_fp) < 1){ - } - fclose(log_fp); - } -} - -static void init_sem(int *semid, CONFIG_FILE_ST *ptr) -{ - key_t key; - char error_str[200]; - -/* write error in logfile */ - memset(error_str, 0, sizeof(error_str)); - if ((key = ftok(sem_path, SEM_PROJ_ID)) == -1) { - snprintf(error_str, sizeof(error_str), "EMERG: ftok():%s\n", strerror(errno)); - record_log(error_str); - return; - } - if((*semid = semget(key, 1, IPC_CREAT|0666)) == -1){ - snprintf(error_str, sizeof(error_str), "EMERG: semget():%s\n", strerror(errno)); - record_log(error_str); - return; - } - if(semctl(*semid, 0, SETVAL, 0) == -1){ - snprintf(error_str, sizeof(error_str), "EMERG: semctl: %s !\n", strerror(errno)); - record_log(error_str); - semctl(*semid, 0, IPC_RMID); - return; - } -} - -static void init_shm(SHM **ptr, CONFIG_FILE_ST *p) -{ - int fd = -1; - char error_str[200]; - - mode_t mask_tmp; - mask_tmp = umask(0000); - if((fd = open(shm_path, O_RDWR|O_CREAT|O_TRUNC, 0666)) == -1){ - printf("could not create %s\n", shm_path); - return; - } - umask(mask_tmp); - lseek(fd, sizeof(SHM), SEEK_SET); - write(fd, "\0", 1); - *ptr = (SHM *)mmap(NULL, sizeof(SHM), PROT_READ|PROT_WRITE, MAP_SHARED, fd, 0); - if(*ptr == MAP_FAILED){ - snprintf(error_str, sizeof(error_str), "EMERG: mmap():%s\n", strerror(errno)); - record_log(error_str); - return; - } - close(fd); - -} - -static void get_sem(int semid) -{ - struct sembuf lock[2]; - char error_str[200]; - - lock[0].sem_num = 0; - lock[0].sem_op = 0; - lock[0].sem_flg = SEM_UNDO; - - lock[1].sem_num = 0; - lock[1].sem_op = 1; - lock[1].sem_flg = SEM_UNDO; - - while(semop(semid, lock, 2)){ - if(errno == EINTR)continue; - snprintf(error_str, sizeof(error_str), "EMERG: get_sem semop():%s\n", strerror(errno)); - record_log(error_str); - return; - } -} - -static void release_sem(int semid) -{ - int ret; - char error_str[200]; - struct sembuf unlock; - - unlock.sem_num = 0; - unlock.sem_op = -1; - unlock.sem_flg = SEM_UNDO; - - if((ret = semop(semid, &unlock, 1)) == -1){ - snprintf(error_str, sizeof(error_str), "EMERG: rel_sem semop():%s\n", strerror(errno)); - record_log(error_str); - return; - } -} - -static void proc_get_name(NET_NAME_ST *nodep) -{ - char linebuf[LINE_SIZE]; - char error_str[200]; - char *retp = NULL; - int err_val=0; - NET_NAME_ST *node = NULL; - FILE *dev_fp; - - if((dev_fp = fopen("/proc/net/dev", "rb")) == NULL){ - snprintf(error_str, sizeof(error_str), "EMERG: Can't open dev file:%s\n", strerror(errno)); - record_log(error_str); - return; - } - memset(linebuf, 0, sizeof(linebuf)); - fgets(linebuf, sizeof(linebuf), dev_fp); - fgets(linebuf, sizeof(linebuf), dev_fp); - while((retp = fgets(linebuf, sizeof(linebuf), dev_fp)) != NULL){ - if((node = (NET_NAME_ST *)malloc(sizeof(NET_NAME_ST))) == NULL){ - err_val = 1; - break; - } - memset(node->name, 0, NIC_NAME_LEN); - get_name(node->name, retp); - node->next = nodep->next; - nodep->next = node; - } - if(err_val){ - NET_NAME_ST *curr = nodep->next; - while(curr != NULL){ - node = curr->next; - free(curr); - curr = node; - } - } - fclose(dev_fp); -} - -static void ioctl_get_name(NET_NAME_ST *nodep) -{ - int count = 0; - int inuse = 0; - int err_val = 0; - NETCARD_INFO net_name[MAXNICNUM]; - NET_NAME_ST *node = NULL, *curr = NULL; - - memset(net_name, 0, sizeof(net_name)); - if((count = ioc_get_name(net_name)) == 0)return; - if(nodep->next == NULL) - inuse = 1; - while(count > 0){ - curr = nodep->next; - while(curr != NULL){ - if(!strncmp(net_name[count - 1].charname, curr->name, NIC_NAME_LEN)){ - inuse = 0; - break; - } - inuse = 1; - curr = curr->next; - } - if(inuse){ - if((node = (NET_NAME_ST *)malloc(sizeof(NET_NAME_ST))) == NULL){ - err_val = 1; - break; - } - memset(node->name, 0, NIC_NAME_LEN); - strncpy(node->name, net_name[count - 1].charname, NIC_NAME_LEN); - node->next = nodep->next; - nodep->next = node; - } - count--; - } - if(err_val){ - curr = nodep->next; - while(curr != NULL){ - node = curr->next; - free(curr); - curr = node; - } - } -} - -static void get_all_name(NET_NAME_ST *nodep) -{ - proc_get_name(nodep); - ioctl_get_name(nodep); -} - -static void create_monit_list(NET_NAME_ST *nodep, NETCARD_INFO_ST *info_list, char *bond) -{ - int err_val = 0; - NETCARD_INFO_ST *info_node = NULL; - NET_NAME_ST *curr, *node; - - curr = nodep->next; - while(curr != NULL){ - if((info_node = (NETCARD_INFO_ST *)malloc(sizeof(NETCARD_INFO_ST))) == NULL){ - err_val = 1; - break; - } - memset(info_node, 0, sizeof(NETCARD_INFO_ST)); - strncpy(info_node->info.charname, curr->name, NIC_NAME_LEN); - strncpy(info_node->info.descr, bond, strlen(bond)); - info_node->next = info_list->next; - info_list->next = info_node; - curr = curr->next; - } - if(err_val){ - NETCARD_INFO_ST *info_curr = info_list->next; - while(info_curr != NULL){ - info_node = info_curr->next; - free(info_curr); - info_curr = info_node; - } - curr = nodep->next; - while(curr != NULL){ - node = curr->next; - free(curr); - curr = node; - } - } -} - -static int get_inc_info(NETCARD_INFO *net) -{ - return if_fetch(net); -} - -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){ - if((str = strstr(linebuf, net->charname)) != NULL){ - str +=strlen(net->charname) + 2; - get_dev_fields(str, net); - break; - } - } - fclose(dev_fp); -} - -static void send_inc_info_one(NETCARD_INFO *net, int semid, SHM *ptr) -{ - int i; - for(i = 0; ptr->info[i].charname[0] != 0; i++){ - if(!strncmp(ptr->info[i].charname, net->charname, NIC_NAME_LEN)){ - get_sem(semid); - memcpy(&ptr->info[i], net, sizeof(NETCARD_INFO)); - release_sem(semid); - } - } -} - -static void *send_inc_info(void *p) -{ - int i = 0; - int flags = 1; - char error_str[200]; - unsigned long long bytes[MAXNICNUM]; - THMEM *ptr = (THMEM *)p; - NETCARD_INFO_ST *curr; - struct timespec s_time, os_time; - - if((curr = ptr->listp->next) == NULL){ - pthread_mutex_lock(&mut); - snprintf(error_str, sizeof(error_str), "EMERG: Empty Netcard list!\n"); - record_log(error_str); - pthread_mutex_unlock(&mut); - return NULL; - } - s_time.tv_sec = ptr->conf->write_interval; - s_time.tv_nsec = 0; - - /* copy all net_info*/ - while(1){ - i = 0; - get_sem(ptr->semid); - // memset(ptr->shm_ptr, 0, sizeof(SHM)); - curr = ptr->listp->next; - while(curr != NULL){ - get_inc_info(&curr->info); - if(curr->status == NIC_UNLINKABLE){ - curr->info.flags &= (~IFF_UP); - curr->info.flags &= (~IFF_RUNNING); - } - get_inc_stats(&curr->info); - if(flags){ - bytes[i] = curr->info.rx_bytes + curr->info.tx_bytes; - } - curr->info.average_flow =(unsigned long long)((curr->info.rx_bytes + curr->info.tx_bytes - bytes[i])/(ptr->conf->write_interval)); - memcpy(&ptr->shm_ptr->info[i], &curr->info, sizeof(NETCARD_INFO)); - bytes[i] = curr->info.rx_bytes + curr->info.tx_bytes; - i++; - curr = curr->next; - } - release_sem(ptr->semid); - flags = 0; - nanosleep(&s_time, &os_time); - } -} - -static int get_netcard_count(char *name) -{ - int count = 0; - FILE *fp = NULL; - char path[64] = "/proc/net/bonding/"; - char buf[LINE_SIZE]; - char *retp = NULL; - char *str = NULL; - - strncat(path, name, strlen(name)); - if((fp = fopen(path, "r")) == NULL)return 1; - - memset(buf, 0, sizeof(buf)); - fseek(fp, 65, SEEK_SET); - while((retp = fgets(buf, sizeof(buf), fp)) != NULL){ - if((str = strstr(buf, "active-backup")) != NULL){ - fclose(fp); - return 1; - } - if(!strncmp(buf, "Slave Interface:", 16))count++; - } - fclose(fp); - return count; -} - -static void *cacu_flow(void *p) -{ - int i = 0, count_t = 1; - int summit = 0; - int flags = 1; - int count_ok[MAXNICNUM]; - int count_no[MAXNICNUM]; - char error_str[200]; - unsigned long long bytes[MAXNICNUM]; - THFLOW *ptr = (THFLOW *)p; - NETCARD_INFO_ST *curr; - struct timespec s_time, os_time; - - if((curr = ptr->mem.listp->next) == NULL){ - pthread_mutex_lock(&mut); - snprintf(error_str, sizeof(error_str), "EMERG: Empty Netcard list!\n"); - record_log(error_str); - pthread_mutex_unlock(&mut); - return NULL; - } - s_time.tv_sec = ptr->mem.conf->flow_interval; - s_time.tv_nsec = 0; - - for(i = 0; i < MAXNICNUM ; i++){ - count_ok[i] = 0; - count_no[i] = 0; - } - while(1){ - i = 0; - curr = ptr->mem.listp->next; - while(curr != NULL){ - get_inc_info(&curr->info); - get_inc_stats(&curr->info); - if(flags){ - bytes[i] = curr->info.rx_bytes + 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); - 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); -#if 0 - printf("%s:\tcount=%d\tsummit=%d\tflow_limit:%d\taverage_flow:%d\n", curr->info.charname, count_t, summit, conf.flow_limit, curr->info.average_flow); -#endif - 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); - strncat(ptr->env.Malarm.tSysNetcardAlarm.fault_devname, "-", 1); - strncat(ptr->env.Malarm.tSysNetcardAlarm.fault_devname, curr->info.charname, (NIC_NAME_LEN - ptr->env.host_name_size - 1)); - ptr->env.Malarm.tSysNetcardAlarm.flags = NETCARD_ALARM_ABNORMAL; - ptr->env.Malarm.tSysNetcardAlarm.retrytimes = 0; - ptr->env.Malarm.tMsgFrame.len = sizeof(ptr->env.Malarm); - ptr->env.Malarm.tMsgFrame.seqno = seqno; - pthread_mutex_lock(&mut); - while(count_no[i] > 0){ - send_alarm(&ptr->env.Malarm, ptr->mem.conf->udpport, ptr->mem.conf->ip); - seqno++; - ptr->env.Malarm.tMsgFrame.seqno = seqno; - ptr->env.Malarm.tSysNetcardAlarm.retrytimes++; - count_no[i]--; - if(count_no[i] == 0){ - send_inc_info_one(&curr->info, ptr->mem.semid, ptr->mem.shm_ptr); - //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); - snprintf(error_str, sizeof(error_str), "NOTICE: %s flow is abnormal !\n", curr->info.charname); - record_log(error_str); - } - } - pthread_mutex_unlock(&mut); - count_ok[i] = 3; - }else{ - 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); - strncat(ptr->env.Malarm.tSysNetcardAlarm.fault_devname, "-", 1); - strncat(ptr->env.Malarm.tSysNetcardAlarm.fault_devname, curr->info.charname, (NIC_NAME_LEN - ptr->env.host_name_size - 1)); - ptr->env.Malarm.tSysNetcardAlarm.flags = NETCARD_ALARM_NORMAL; - ptr->env.Malarm.tSysNetcardAlarm.retrytimes = 0; - ptr->env.Malarm.tMsgFrame.len = sizeof(ptr->env.Malarm); - ptr->env.Malarm.tMsgFrame.seqno = seqno; - pthread_mutex_lock(&mut); - while(count_ok[i] > 0){ - send_alarm(&ptr->env.Malarm, ptr->mem.conf->udpport, ptr->mem.conf->ip); - seqno++; - ptr->env.Malarm.tMsgFrame.seqno = seqno; - ptr->env.Malarm.tSysNetcardAlarm.retrytimes++; - count_ok[i]--; - if(count_ok[i] == 0){ - send_inc_info_one(&curr->info, ptr->mem.semid, ptr->mem.shm_ptr); - snprintf(error_str, sizeof(error_str), "NOTICE: %s flow is normal now !\n", curr->info.charname); - record_log(error_str); - } - } - pthread_mutex_unlock(&mut); - count_no[i] = 3; - }/* end if(summit... */ - bytes[i] = curr->info.rx_bytes + curr->info.tx_bytes; - i++; - curr = curr->next; - }/* end while(curr...) */ - flags = 0; - nanosleep(&s_time, &os_time); - }/* end while(1) */ -} -static char *parse_str(char **s) -{ - char *retp = NULL; - char *p = *s; - - while(isspace(*p))p++; - retp = p; - while(!isspace(*p))p++; - *p = '\0'; - - return retp; -} - -int ping_gw(char *devname) -{ - FILE *fp; - int i, j, k; - char buf[128]; - char getbuf[64]; - - i = find_nic_config(devname); - if( i == -1 ) - return 0; - - if(conf.nic[i].gw_num == 0) - return 0; - - for(k=0; kbond_file_path, "r")) == NULL){ - snprintf(error_str, sizeof(error_str), "EMERG: fopen():%s\n", strerror(errno)); - record_log(error_str); - free(ptr); - return NULL; - } - fseek(fp, 65, SEEK_SET); - memset(name, 0, sizeof(name)); - memset(linebuf, 0, sizeof(linebuf)); - if((retp = fgets(linebuf, sizeof(linebuf), fp)) != NULL){ - if((retp = strstr(linebuf, "active-backup")) == NULL){ - free(ptr); - fclose(fp); - return NULL; - } - } - - s_time.tv_sec = 0; - s_time.tv_nsec = 100000000; - - while(1){ - while((str = fgets(linebuf, sizeof(linebuf), fp)) != NULL){ - if(!strncmp(linebuf, "Currently Active Slave:", 23)){ - str += 23; - retp = parse_str(&str); - break; - } - } - if(name[0] == 0){ - strncpy(name, retp, strlen(retp)); - }else if(strncmp(name, retp, strlen(retp)) != 0){ - memset(arg->Malarm.tSysNetcardAlarm.switch_devname, 0, NIC_NAME_LEN); - strncpy(arg->Malarm.tSysNetcardAlarm.switch_devname, arg->host_name, arg->host_name_size); - strncat(arg->Malarm.tSysNetcardAlarm.switch_devname, "-", 1); - strncat(arg->Malarm.tSysNetcardAlarm.switch_devname, retp, (NIC_NAME_LEN - arg->host_name_size - 1)); - arg->Malarm.tSysNetcardAlarm.flags = NETCARD_ALARM_SWITCH; - arg->Malarm.tSysNetcardAlarm.retrytimes = 0; - arg->Malarm.tMsgFrame.len = sizeof(arg->Malarm); - arg->Malarm.tMsgFrame.seqno = seqno; - count = 3; - pthread_mutex_lock(&mut); - snprintf(error_str, sizeof(error_str), "NOTICE: Net card checking to %s!\n", retp); - record_log(error_str); - while(count > 0){ - send_alarm(&arg->Malarm, arg->conf->udpport, arg->conf->ip); - seqno++; - arg->Malarm.tMsgFrame.seqno = seqno; - arg->Malarm.tSysNetcardAlarm.retrytimes++; - count--; - } - pthread_mutex_unlock(&mut); - memset(name, 0, NIC_NAME_LEN); - strncpy(name, retp, strlen(retp)); - } - fclose(fp); - nanosleep(&s_time, &os_time); - if((fp = fopen(arg->bond_file_path, "r")) == NULL){ - free(ptr); - return NULL; - } - fseek(fp, 65, SEEK_SET); - } - free(ptr); - fclose(fp); - return NULL; -} - -static void *create_logfile(void *ptr) -{ - char buf[128]; - char del_file[DAYS][1024]; - char *s; - int head, tail, fd; - int flag = 0, del_flag = 0; - int old_mon = 0; - int old_day = 0; - time_t tamp; - struct tm tmptr; - struct timespec s_time, os_time; - - s_time.tv_sec = 1; - s_time.tv_nsec = 0; - head = tail = 0; - while(1){ - tamp = time(NULL); - localtime_r(&tamp, &tmptr); - if(old_mon != (tmptr.tm_mon + 1))flag = 1; - else if(old_day != tmptr.tm_mday)flag = 1; - else flag = 0; - if(flag){ - s = strrchr(log_path, '/'); - s++; - *s = '\0'; - memset(buf, 0, sizeof(buf)); - strftime(buf, sizeof(buf), "%Y%m%d_", &tmptr); - strcat(buf, process_name); - strcat(buf, ".log"); - strcat(log_path, buf); - mode_t mask_tmp; - mask_tmp = umask(0000); - if((fd = open(log_path, O_RDWR|O_CREAT, 0666)) != -1){ - log_flag = 0; - close(fd); - } - umask(mask_tmp); - old_mon = tmptr.tm_mon+1; - old_day = tmptr.tm_mday; - strcpy(del_file[head++], log_path); - if(head == DAYS){ - head = 0; - del_flag = 1; - } - if(del_flag){ - unlink(del_file[tail]); - tail++; - if(tail == DAYS)tail = 0; - } - } - nanosleep(&s_time, &os_time); - } -} - -void write_time(int semid, SHM *ptr) -{ - int i = 0; - int flag_time_stamp; - - flag_time_stamp = time(NULL); - - while(ptr->info[i].charname[0] != 0){ - ptr->info[i].time_stamp = flag_time_stamp; - i++; - } -} -#if 0 -char *parse(char *line) -{ - char *begin = NULL; - char *end = NULL; - - if((begin = strchr(line, '/')) != NULL){ - if((end = strstr(line, "bin")) != NULL){ - *end='\0'; - return begin; - } - } - return NULL; -} -#endif -void release_name_list(NET_NAME_ST *head) -{ - NET_NAME_ST *curr, *save; - - curr = head->next; - while(curr != NULL){ - save = curr->next; - free(curr); - curr = save; - } -} -int get_netcard(char *line, char *nic) -{ - char *begin, *retp; - - while(isspace(*line))line++; - begin = line; - while(!isspace(*line))line++; - *line++ = '\0'; - if(!strncmp(begin, "nic", 4)){ - if((retp = parse_str(&line)) != NULL){ - strncpy(nic, retp, MAXNICNUM); - return 1; - } - } - return 0; -} - -void sig_handler(int sig) -{ -// char err_str[200]; - -#if 0 - if(unlink(SHM_PATH) == -1){ - snprintf(err_str, sizeof(err_str), "NOTICE: error delete %s\n", strerror(errno)); - record_log(err_str); - } - if(unlink(SEM_PATH) == -1){ - snprintf(err_str, sizeof(err_str), "NOTICE: error delete %s\n", strerror(errno)); - record_log(err_str); - } -#endif -// semctl(sem_id, 0, IPC_RMID); - -// snprintf(err_str, sizeof(err_str), "NOTICE: process exit by signal %d\n", sig); -// record_log(err_str); - exit(0); -} - -/* - * 查看数据结构中是否有给定的网卡 - * 输入:网卡名称 - * 返回值:-1 表示没有 - * 非0值表示设备列表中的索引 - * - */ -int find_nic_config(char *dev_name){ - int i, index=-1; - - for(i=0; iname, myconf[13]) == 0) && (nicnum <= MAXNICNUM)){ - i = 1; - if(cmd->data.list[0] && cmd->data.list[1]){ - //查找设备列表中是否已有该网卡 - //如果没有则创建一项 - //如果已有则只要增加ping的地址和地址数量 - //一般都能找到,因为配置文件中一般总是先定义"nic",后定义"ping" - //如果没找到,则表明先定义了"ping",未定义"nic"或后定义"nic",而未定义"nic"应该是错误的 - index = find_nic_config(cmd->data.list[0]); - if(index == -1){ - strncpy(conf.nic[nicnum].dev_name, cmd->data.list[0], strlen(cmd->data.list[0]) >= IPLEN ? IPLEN-1 : strlen(cmd->data.list[0])); - card_info_index = nicnum; - nicnum++; - } - else - card_info_index = index; - - while(cmd->data.list[i]){ - if((int)(inet_addr(cmd->data.list[i])) == -1){ - sprintf(wrong_ip,"The wrong IP address:'%s'",cmd->data.list[i]); - strcat(wrong_ip,"\n"); - record_log(wrong_ip); - } else{ - strncpy(conf.nic[card_info_index].ping_ip[conf.nic[card_info_index].gw_num++], cmd->data.list[i], strlen(cmd->data.list[i]) >= IPLEN ? IPLEN-1 : strlen(cmd->data.list[i])); - } - i++; - } - } - } - - //"nic"选项的处理 - if((strcmp(cmd ->name, myconf[12]) == 0) && (nicnum <= MAXNICNUM)){ - //设备列表中没有给定的网卡设备,则添加一项 - //否则不必添加 - //对应配置文件中先指定"ping"后指定"nic"的情况 - index = find_nic_config(cmd->data.str); - if(index == -1){ - strncpy(conf.nic[nicnum].dev_name, cmd->data.str, strlen(cmd->data.str) >= NIC_NAME_LEN ? NIC_NAME_LEN-1 : strlen(cmd->data.str)); - nicnum++; - } - } - - if((strcmp(cmd ->name, myconf[11]) == 0)){ - strncpy(conf.udp, cmd->data.str, strlen(cmd->data.str)); - } - - return NULL; -} - -DOTCONF_CB(cb_int) -{ - if(strcmp(cmd ->name, myconf[0]) == 0){ - conf.domain = cmd->data.value; - } - if(strcmp(cmd ->name, myconf[1]) == 0){ - conf.serv = cmd->data.value; - } - if(strcmp(cmd ->name, myconf[2]) == 0){ - conf.event = cmd->data.value; - } - if(strcmp(cmd ->name, myconf[3]) == 0){ - conf.udpport = cmd->data.value; - } - if(strcmp(cmd ->name, myconf[6]) == 0){ - conf.monitor_interval = cmd->data.value; - } - if(strcmp(cmd ->name, myconf[7]) == 0){ - conf.write_interval = cmd->data.value; - } - if(strcmp(cmd ->name, myconf[8]) == 0){ - conf.flow_interval = cmd->data.value; - } - if(strcmp(cmd ->name, myconf[9]) == 0){ - conf.flow_limit = cmd->data.value; - } - if(strcmp(cmd ->name, myconf[10]) == 0){ - conf.flow_peak = cmd->data.value; - } - if(strcmp(cmd ->name, myconf[14]) == 0){ - conf.pingnum = cmd->data.value; - } - if(strcmp(cmd ->name, myconf[15]) == 0){ - conf.pinglap = cmd->data.value; - } - - return NULL; -} - -DOTCONF_CB(cb_list) -{ - return NULL; -} - -int get_conf() -{ -// int uid; -// char buf[1024]; -// int i; -// struct passwd *user; - configfile_t *configfile; - -// memset(buf, 0, sizeof(buf)); -// uid=getuid(); -// user = getpwnam("d5000"); -// user = getpwuid(uid); - -// sprintf(buf,"%s", user->pw_dir); -// strcat(buf,conf_path); - - configfile = dotconf_create(conf_path, options, NULL, CASE_INSENSITIVE); - if (!configfile) - { - record_log("Warning can't reading config file\n"); - return -1; - } - - if (dotconf_command_loop(configfile) == 0){ - record_log("Warning can't reading config file\n"); - return -1; - } - dotconf_cleanup(configfile); - - if(conf.domain == 0) - conf.domain = 10; - if(conf.serv == 0) - conf.serv = 1; - if(conf.event == 0) - conf.event = 2; - if(conf.udpport == 0) - conf.udpport = 15000; - if(conf.monitor_interval == 0) - conf.monitor_interval = 100; - if(conf.write_interval == 0) - conf.write_interval = 300; - if(conf.flow_interval == 0) - conf.flow_interval = 60; - if(conf.flow_limit == 0) - conf.flow_limit = 30; - if(conf.flow_peak == 0) - conf.flow_peak = 1000; - if(conf.udp[0] == 0) - strcpy(conf.udp, "bond0"); - if(conf.nic[0].dev_name[0] == 0) - strcpy(conf.nic[0].dev_name, "bond0"); - if(conf.pingnum == 0) - conf.pingnum = 2; - if(conf.pinglap == 0) - conf.pinglap = 1; - - return 0; -} - -int isrun(char *p) -{ - char buf[16]; - char cmdbuf[1024]; - FILE *fp; - - memset(buf, 0, sizeof(buf)); - memset(cmdbuf, 0, sizeof(cmdbuf)); - - snprintf(cmdbuf, sizeof(cmdbuf), "pidof %s", p); - fp=popen(cmdbuf, "r"); - fread(buf, sizeof(char), sizeof(buf), fp); - pclose(fp); - if(strchr(buf, ' ') != NULL) - return 1; - else - return 0; -} - -int get_broad_ip(void) -{ - char ip_p[4][4]; - char error_str[200]; - int skfd; - struct ifreq ifr; - - memset(&ifr, 0, sizeof(ifr)); - memset(ip_p, 0, sizeof(ip_p)); - - if((skfd = socket(PF_INET, SOCK_DGRAM, 0)) == -1)return -1; - strcpy(ifr.ifr_name, conf.udp); - if (!ioctl(skfd, SIOCGIFBRDADDR, &ifr)){ - sprintf(ip_p[0], "%d", (unsigned char)ifr.ifr_broadaddr.sa_data[2]); - sprintf(ip_p[1], "%d", (unsigned char)ifr.ifr_broadaddr.sa_data[3]); - sprintf(ip_p[2], "%d", (unsigned char)ifr.ifr_broadaddr.sa_data[4]); - sprintf(ip_p[3], "%d", (unsigned char)ifr.ifr_broadaddr.sa_data[5]); - }else{ - snprintf(error_str, sizeof(error_str), "EMERG: Invalid broadcast device -- %s\n", conf.udp); - record_log(error_str); - close(skfd); - return -1; - } - close(skfd); - strcat(conf.ip, ip_p[0]); - strcat(conf.ip, "."); - strcat(conf.ip, ip_p[1]); - strcat(conf.ip, "."); - strcat(conf.ip, ip_p[2]); - strcat(conf.ip, "."); - strcat(conf.ip, ip_p[3]); - - return 0; -} - -void create_dir(char *create_path) -{ - int i,len = strlen(create_path); - - len = strlen(create_path); - - for(i=1; inext; - while(curr != NULL){ - if(!strncmp(curr->name, conf.nic[i].dev_name, NIC_NAME_LEN)){ - if((node = (NET_NAME_ST *)malloc(sizeof(NET_NAME_ST))) == NULL){ - err_val = 1; - break; - } - memset(node->name, 0, NIC_NAME_LEN); - strncpy(node->name, conf.nic[i].dev_name, NIC_NAME_LEN); - node->next = opt_name_list->next; - opt_name_list->next = node; - count_t ++; - break; - } - curr = curr->next; - } - if(err_val)break; - } - if(count_t != i || err_val){ - for(curr = opt_name_list->next; curr != NULL; curr = curr->next)free(curr); - snprintf(error_str, sizeof(error_str), "EMERG: Invalid netcard name --\n"); - record_log(error_str); - return -1; - } - err_val = 0; - - return 0; -} - -int get_netcard_status(NETCARD_INFO_ST *listp) -{ - if((listp->info.flags & IFF_UP) != IFF_UP) - return NIC_DOWN; - - if((listp->info.flags & IFF_RUNNING) != IFF_RUNNING) - return NIC_UNRUNNING; - - //20100510 - if(elapsed_ping_time == total_ping_time){ - if(ping_gw(listp->info.charname) != 0) - return NIC_UNLINKABLE; - } - else{ - if(listp->status == NIC_UNLINKABLE) - return NIC_UNLINKABLE; - } - - return NIC_NORMAL; -} - -int do_alarm(NETCARD_INFO_ST *listp, int nic_status, D5000_NIC_ALARM *Malarm) -{ - int i; - char error_str[200]; - - memset(Malarm->tSysNetcardAlarm.fault_devname, 0, NIC_NAME_LEN); - strncpy(Malarm->tSysNetcardAlarm.fault_devname, host_name, host_name_size); - strncat(Malarm->tSysNetcardAlarm.fault_devname, "-", 1); - strncat(Malarm->tSysNetcardAlarm.fault_devname, listp->info.charname, NIC_NAME_LEN - host_name_size - 1); - Malarm->tSysNetcardAlarm.retrytimes = 0; - Malarm->tMsgFrame.len = sizeof(Malarm); - Malarm->tMsgFrame.seqno = seqno; - - switch(nic_status){ - case NIC_DOWN: - Malarm->tSysNetcardAlarm.flags = NETCARD_ALARM_FAULT; - snprintf(error_str, sizeof(error_str), "NOTICE: %s is DOWN!\n", listp->info.charname); - break; - case NIC_UNRUNNING: - Malarm->tSysNetcardAlarm.flags = NETCARD_ALARM_FAULT; - snprintf(error_str, sizeof(error_str), "NOTICE: %s is UP but UNRUNNING!\n", listp->info.charname); - break; - case NIC_UNLINKABLE: - Malarm->tSysNetcardAlarm.flags = NETCARD_ALARM_FAULT; - listp->info.flags &= (~IFF_UP); - listp->info.flags &= (~IFF_RUNNING); - snprintf(error_str, sizeof(error_str), "NOTICE: %s is RUNNING but UNLINKABLE!\n", listp->info.charname); - break; - case NIC_NORMAL: - Malarm->tSysNetcardAlarm.flags = NETCARD_ALARM_RESUME; - snprintf(error_str, sizeof(error_str), "NOTICE: %s is NORMAL!\n", listp->info.charname); - break; - } - pthread_mutex_lock(&mut); - for(i = 0; i < 3; i++){ - send_alarm(Malarm, conf.udpport, conf.ip); - seqno++; - Malarm->tMsgFrame.seqno = seqno; - Malarm->tSysNetcardAlarm.retrytimes++; - } - send_inc_info_one(&listp->info, semid, shm_ptr); - pthread_mutex_unlock(&mut); - record_log(error_str); - - return 0; -} - -int main(int argc, char ** argv) -{ - int ret = 0, proc_stat = 0; - unsigned int i = 0; - int nic_status; - int sys_time_stamp = 0; - char *retp = NULL; - char *str = NULL; - char bond[128]; - char error_str[200]; - char linebuf[LINE_SIZE]; - THENV *arg; - glob_t res; - FILE *bond_fp = NULL; - pthread_t tid[THRNR]; - pthread_t tid_mem; - pthread_t tid_flow; - pthread_t tid_time; - THMEM mem; - THFLOW flow; - struct timespec s_time, os_time, log_time; - NET_NAME_ST all_name_list, opt_name_list; - NETCARD_INFO_ST inc_info_list, *listp = NULL; - D5000_NIC_ALARM Malarm; - struct sigaction sig; - struct passwd *user; - proc_invocation prcm; - - char log_record[125]={0}; - - memset(&conf, 0, sizeof(CONFIG_FILE_ST)); - if(isrun(argv[0]) == 1){ - printf("the program is already started!\n"); - exit(0); - } -//process register -#if 0 - if((proc_stat=prcm.proc_init("sys","base_srv","sys_nicmonitor"))==-1){ - perror("proc_init()"); - exit(-1); - } -#endif - - sig.sa_handler = sig_handler; - sigemptyset(&sig.sa_mask); - sigaddset(&sig.sa_mask, SIGINT); - sigaddset(&sig.sa_mask, SIGTERM); - sig.sa_flags = 0; - sigaction(SIGTERM, &sig, NULL); - sigaction(SIGINT, &sig, NULL); - sigaction(SIGSEGV, &sig, NULL); - - /* create dir file path */ - if((user = getpwnam("d5000")) != NULL){ - sprintf(log_path,"%s",user->pw_dir); - sprintf(shm_path,"%s",user->pw_dir); - sprintf(sem_path,"%s",user->pw_dir); - sprintf(conf_path,"%s",user->pw_dir); - } - - strcat(log_path,LOG_PATH); - strcat(shm_path,SHM_PATH); - strcat(sem_path,SEM_PATH); - strcat(conf_path,CONF_FILE); - - create_semdir(); - create_shmdir(); - create_logdir(); - create_confdir(); - - /* get process name */ - memset(process_name, 0, sizeof(process_name)); - if((retp = strrchr(argv[0], '/')) != NULL){ - retp++; - strcpy(process_name, retp); - }else{ - strcpy(process_name, argv[0]); - } - /*create logfile thread */ - pthread_create(&tid_time, NULL, create_logfile, NULL); - - /* initialize some....*/ - memset(&all_name_list, 0, sizeof(NET_NAME_ST)); - all_name_list.next = NULL; - memset(&opt_name_list, 0, sizeof(NET_NAME_ST)); - opt_name_list.next = NULL; - - memset(&inc_info_list, 0, sizeof(NETCARD_INFO_ST)); - inc_info_list.next = NULL; - - - /* get all netcard by proc/net/dev and ioctl */ - get_all_name(&all_name_list); - - /* wait for create log thread */ - log_time.tv_sec = 0; - log_time.tv_nsec = 1000000; - while(log_flag)nanosleep(&log_time, NULL); - - /* sure the process is started */ - snprintf(error_str, sizeof(error_str), "NOTICE: %s V%s is started !\n", process_name, MNIC_VERSION); - record_log(error_str); - - get_conf(); - - if(shm_path){ - printf("shm_path:%s\n",shm_path); - sprintf(log_record,"shm_path:%s\n",shm_path); - record_log(log_record); - } - - if(log_path){ - printf("log_path:%s\n",log_path); - sprintf(log_record,"log_path:%s\n",log_path); - record_log(log_record); - } - - - if(conf.udp){ - printf("udp %s\n",conf.udp); - sprintf(log_record,"udp %s\n",conf.udp); - record_log(log_record); - } - - for(i=0;conf.nic[i].dev_name[0]!=0;i++){ - printf("nic %s\n",conf.nic[i].dev_name); - sprintf(log_record,"nic %s\n",conf.nic[i].dev_name); - record_log(log_record); - } - - /* get broadcast address*/ - if((ret = get_broad_ip()) != 0) - return -1; - if((ret = create_name_list(&all_name_list, &opt_name_list)) != 0) - return -1; - /* get hostname */ - if((ret = gethostname(host_name, sizeof(host_name))) != -1){ - host_name_size = strlen(host_name); - }else host_name_size = HOST_NAME_SIZE; - /* initialize Malarm */ - Malarm.tMsgFrame.serv = conf.serv; - Malarm.tMsgFrame.event = conf.event; - Malarm.tMsgFrame.domain = conf.domain; - - /* get bonding file */ - memset(bond, 0, sizeof(bond)); - memset(linebuf, 0, sizeof(linebuf)); - if((ret = glob(BOND_PATH, 0, NULL, &res)) != 0){ - snprintf(error_str, sizeof(error_str), "NOTICE: No bonding netcard !\n"); - record_log(error_str); - strcpy(bond, "No bonding netcard!"); - } - for (i = 0; i < res.gl_pathc; ++i) { - if((arg = (THENV *)malloc(sizeof(THENV))) != NULL){ - memset(arg, 0, sizeof(THENV)); - strncpy(arg->bond_file_path, res.gl_pathv[i], strlen(res.gl_pathv[i])); - arg->Malarm = Malarm; - strncpy(arg->host_name, host_name, host_name_size); - arg->host_name_size = host_name_size; - arg->conf = &conf; - if(pthread_create(tid+i, NULL, check_inc_switch, (void*)arg)){ - snprintf(error_str, sizeof(error_str), "EMERG: pthread_create(): %s\n", strerror(errno)); - record_log(error_str); - } - } - } - for(i = 0; i < res.gl_pathc; ++i){ - if((bond_fp = fopen(res.gl_pathv[i], "r")) == NULL){ - snprintf(error_str, sizeof(error_str), "EMERG: fopen(): %s\n", strerror(errno)); - record_log(error_str); - }else{ - retp = strrchr(res.gl_pathv[i], '/'); - retp++; - strcat(bond, retp); - strcat(bond, ":"); - while((str = fgets(linebuf, sizeof(linebuf), bond_fp)) != NULL){ - if(!strncmp(linebuf, "Slave Interface:", 16)){ - str += 16; - retp = parse_str(&str); - strcat(bond, retp); - strcat(bond, " "); - } - } - fclose(bond_fp); - } - } - - /* initialize time*/ - s_time.tv_sec = 0; - s_time.tv_nsec = conf.monitor_interval*1000*1000; - - /* initialize sem*/ - init_sem(&semid, &conf); - init_shm(&shm_ptr, &conf); - /* for IPC_RMID */ - sem_id = semid; - - /* create monitor netcard list */ - create_monit_list(&opt_name_list, &inc_info_list, bond); - release_name_list(&all_name_list); - release_name_list(&opt_name_list); - - mem.semid = semid; - mem.shm_ptr = shm_ptr; - mem.listp = &inc_info_list; - mem.conf = &conf; - if(pthread_create(&tid_mem, NULL, send_inc_info, (void *)&mem)){ - snprintf(error_str, sizeof(error_str), "EMERG: pthread_create(): %s\n", strerror(errno)); - record_log(error_str); - } - - /* create caculate flow thread*/ - flow.env.Malarm = Malarm; - strncpy(flow.env.host_name, host_name, host_name_size); - flow.env.host_name_size = host_name_size; - flow.mem.semid = semid; - flow.mem.shm_ptr = shm_ptr; - flow.mem.listp = &inc_info_list; - flow.mem.conf = &conf; - if(pthread_create(&tid_flow, NULL, cacu_flow, (void *)&flow)){ - snprintf(error_str, sizeof(error_str), "EMERG: pthread_create(): %s\n", strerror(errno)); - record_log(error_str); - } - - total_ping_time = 1000 / conf.monitor_interval; - if(total_ping_time == 0) - total_ping_time = 1; - elapsed_ping_time = 1; - - while(1){ - i = 0; - listp = inc_info_list.next; - for(i = 0; listp != NULL; i++){ - get_inc_info(&listp->info); - get_inc_stats(&listp->info); - nic_status = get_netcard_status(listp); - if(nic_status != listp->status){ - do_alarm(listp, nic_status, &Malarm); - listp->status = nic_status; - } - listp = listp->next; - } - - if(seqno > 32765)seqno = 0; - nanosleep(&s_time, &os_time); - - - //20100510 - ++elapsed_ping_time; - if(elapsed_ping_time > total_ping_time) - elapsed_ping_time = 1; - - sys_time_stamp += conf.monitor_interval; - if(sys_time_stamp >= 1000){ - sys_time_stamp = 0; - write_time(semid, shm_ptr); - } - } - for (i = 0; i < res.gl_pathc; i++) { - pthread_join(tid[i], NULL); - } - pthread_join(tid_mem, NULL); - pthread_join(tid_flow, NULL); - pthread_join(tid_time, NULL); - pthread_mutex_destroy(&mut); - globfree(&res); - munmap(shm_ptr, sizeof(SHM)); - return 0; -} diff --git a/branches/sys_nicmonitor-1.6/mnic.h b/branches/sys_nicmonitor-1.6/mnic.h deleted file mode 100644 index d7a6390..0000000 --- a/branches/sys_nicmonitor-1.6/mnic.h +++ /dev/null @@ -1,44 +0,0 @@ -#ifndef __MNIC_H -#define __MNIC_H - -//#define _GNU_SOURCE -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include - -#include "sys_netcard.h" -#include -#include -#define IPSIZE 16 - -char *get_name(char *, char *); -int if_fetch(NETCARD_INFO *); -int ioc_get_name(NETCARD_INFO *); -int get_dev_fields(char *p, NETCARD_INFO *); -int get_user_home(int , char *); -void send_alarm(D5000_NIC_ALARM *, int , char *); -void record_log(char *); - -#endif diff --git a/branches/sys_nicmonitor-1.6/nicinfo_shm.c b/branches/sys_nicmonitor-1.6/nicinfo_shm.c deleted file mode 100644 index c8a8bf2..0000000 --- a/branches/sys_nicmonitor-1.6/nicinfo_shm.c +++ /dev/null @@ -1,209 +0,0 @@ -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include - -#include "nicinfo_shm.h" - -#define SHM_PATH "/tmp/sys_netcard_shm_path" -#define SEM_PATH "/tmp/sys_netcard_sem_path" -#define LOG_PATH "/var/log/netcard/" - -typedef struct net_info{ - NETCARD_INFO info[MAXNICNUM]; -}SHM; - -static int semid = 0; -static char log_path[1024]; -static char shm_path[1024]; -static char sem_path[1024]; -SHM *ptr = NULL; - -/* write error in logfile */ -void record_log(char *str) -{ - time_t tamp; - char str_tm[4]; - char log_str[512]; - FILE *log_fp = NULL; - struct tm tmptr; - struct timeval tv; - struct timezone tz; - - if((log_fp = fopen(log_path, "a")) != NULL){ - tamp = time(NULL); - memset(str_tm, 0, sizeof(str_tm)); - memset(log_str, 0, sizeof(log_str)); - localtime_r(&tamp, &tmptr); - gettimeofday(&tv, &tz); - snprintf(str_tm, sizeof(str_tm), "%d", (int)tv.tv_usec/1000); - if(str_tm[1] == '\0')str_tm[1] = '0'; - if(str_tm[2] == '\0')str_tm[2] = '0'; - strftime(log_str, sizeof(log_str), "%F %T.", &tmptr); - strcat(log_str, str_tm); - strcat(log_str, " "); - strcat(log_str, "get_nic_info"); - strcat(log_str, " "); - strcat(log_str, str); - if(fwrite(log_str, 1, strlen(log_str), log_fp) < 1){ - } - fclose(log_fp); - } -} - -void get_sem(int semid) -{ - char err_str[200]; - struct sembuf lock[2]; - - lock[0].sem_num = 0; - lock[0].sem_op = 0; - lock[0].sem_flg = SEM_UNDO; - - lock[1].sem_num = 0; - lock[1].sem_op = 1; - lock[1].sem_flg = SEM_UNDO; - - while(semop(semid, lock, 2)){ - if (errno == EINTR) { - continue; - } - snprintf(err_str, sizeof(err_str), "EMERG: semop():\n", strerror(errno)); - record_log(err_str); - return; - } -} - -void release_sem(int semid) -{ - int ret; - char err_str[200]; - struct sembuf unlock; - - unlock.sem_num = 0; - unlock.sem_op = -1; - unlock.sem_flg = SEM_UNDO; - - if((ret = semop(semid, &unlock, 1)) == -1){ - snprintf(err_str, sizeof(err_str), "EMERG: semop():\n", strerror(errno)); - record_log(err_str); - return; - } -} - - -int init_nic_info(void) -{ - int fd = -1; - int sem_val; - char err_str[200]; - key_t key; - - - if ((key=ftok(sem_path, SEM_PROJ_ID)) == -1) { - snprintf(err_str, sizeof(err_str), "EMERG: ftok():%s\n", strerror(errno)); - record_log(err_str); - return -1; - } - if((semid = semget(key, 1, IPC_CREAT|0666)) == -1){ - snprintf(err_str, sizeof(err_str), "EMERG: Create semaphore error: %s\n", strerror(errno)); - record_log(err_str); - return -1; - } - /* - if((sem_val = semctl(semid, 0, GETVAL, 0)) == -1){ - snprintf(err_str, sizeof(err_str), "EMERG: semctl: %s !\n", strerror(errno)); - record_log(err_str); - semctl(semid, 0, IPC_RMID); - return -1; - } - if(!sem_val){ - if(semctl(semid, 0, SETVAL, 1) == -1){ - snprintf(err_str, sizeof(err_str), "EMERG: semctl: %s !\n", strerror(errno)); - record_log(err_str); - semctl(semid, 0, IPC_RMID); - return -1; - } - } - */ - if((fd = open(shm_path, O_RDONLY)) == -1){ - snprintf(err_str, sizeof(err_str), "EMERG: Error open %s: %s!\n",shm_path, strerror(errno)); - record_log(err_str); - return -1; - } - ptr = (SHM*)mmap(NULL, sizeof(SHM), PROT_READ, MAP_SHARED, fd, 0); - if(ptr == MAP_FAILED){ - snprintf(err_str, sizeof(err_str), "EMERG: mmap():%s\n", strerror(errno)); - record_log(err_str); - return -1; - } - close(fd); - - return 0; -} -int get_nic_info(char *nic_name, NETCARD_INFO *net_info) -{ - int i,ret; - time_t tamp; - struct tm tmptr; - DIR *dir; - char *s; - char buf[128]; - char err_str[200]; - struct passwd *user; - - tamp = time(NULL); - localtime_r(&tamp, &tmptr); - - if((user = getpwnam("d5000"))!= NULL){ - sprintf(log_path,"%s",user->pw_dir); - sprintf(shm_path,"%s",user->pw_dir); - sprintf(sem_path,"%s",user->pw_dir); - } - strcat(log_path,LOG_PATH); - strcat(shm_path,SHM_PATH); - strcat(sem_path,SEM_PATH); - - if((dir = opendir(log_path)) == NULL){ - if(errno == ENOENT){ - strcpy(log_path, "/tmp/"); - } - } - closedir(dir); - s = strrchr(log_path, '/'); - s++; - *s = '\0'; - memset(buf, 0, sizeof(buf)); - strftime(buf, sizeof(buf), "%Y%m%d_", &tmptr); - strcat(buf, "sys_nicmonitor"); - strcat(buf, ".log"); - strcat(log_path, buf); - if((ret = init_nic_info()) == -1){ - return -1; - } - get_sem(semid); - for(i = 0; ptr->info[i].charname[0] != 0; i++){ - if(!strcmp(ptr->info[i].charname, nic_name)){ - // get_sem(semid); - memcpy(net_info, &ptr->info[i], sizeof(NETCARD_INFO)); - release_sem(semid); - munmap(ptr, sizeof(SHM)); - return 0; - } - } - release_sem(semid); - snprintf(err_str, sizeof(err_str), "NOTICE: No information of %s !\n", nic_name); - record_log(err_str); - munmap(ptr, sizeof(SHM)); - return -1; -} diff --git a/branches/sys_nicmonitor-1.6/nicinfo_shm.h b/branches/sys_nicmonitor-1.6/nicinfo_shm.h deleted file mode 100644 index cd74135..0000000 --- a/branches/sys_nicmonitor-1.6/nicinfo_shm.h +++ /dev/null @@ -1,52 +0,0 @@ -#ifndef NIC_SHM_H -#define NIC_SHM_H - -#include - -#ifdef __cplusplus -extern "C" { -#endif - -#ifndef NIC_NAME_LEN -#define NIC_NAME_LEN 64 -#endif -#define SEM_PROJ_ID 's' -#define MAXNICNUM 32 - -typedef long KEYID; -typedef struct _net_info -{ - KEYID ID; - char charname[NIC_NAME_LEN]; - char descr[128]; - struct sockaddr addr; - struct sockaddr broadaddr; - struct sockaddr netmask; - struct sockaddr hwaddr; - int time_stamp; - short flags; - int mtu; - int tx_queue_len; - unsigned long long average_flow; - unsigned long long rx_packets; - unsigned long long tx_packets; - unsigned long long rx_bytes; - unsigned long long tx_bytes; - unsigned long rx_errors; - unsigned long tx_errors; - unsigned long rx_dropped; - unsigned long tx_dropped; - unsigned long rx_multicast; - unsigned long collisions; - unsigned long rx_fifo_errors; - unsigned long tx_carrier_errors; - unsigned long tx_fifo_errors; -}NETCARD_INFO; - -int get_nic_info(char *nic_name, NETCARD_INFO *net_info); - - -#ifdef __cplusplus -} -#endif -#endif diff --git a/branches/sys_nicmonitor-1.6/proc_inv.h b/branches/sys_nicmonitor-1.6/proc_inv.h deleted file mode 100644 index 945622c..0000000 --- a/branches/sys_nicmonitor-1.6/proc_inv.h +++ /dev/null @@ -1,198 +0,0 @@ -//////////////////////////////////////////////////////////////////////////////////// -// -// Copyright (c) Comets' Grp. of Kedong Corp 2008. All Rights Reserved. -// -// FileName : procconf.h -// -// Function : this class realize some basic functions for process managerment, -// such as initiate process, report status of process, check status of process, -// update status of process, get information of process -// -// Author : -// -// Date : -// -// Modify by : -// -// Mod Date : -// -//////////////////////////////////////////////////////////////////////////////////// - -#ifndef _PROCCONF_H -#define _PROCCONF_H - -#include -#include -#include -#include -#include -#include -#include -//#include -#include - -#include "const.h" - -//for alarm ................................ -typedef struct MESS_BH -{ - unsigned char mtype; - int length; -}; -typedef struct PROCESS_ALM -{ - char context_name[MAX_STRING_LEN]; - char app_name[MAX_STRING_LEN]; - char proc_name[MAX_STRING_LEN]; - unsigned char status; -}; -//for alarm end ............................. - -//for mmi.................................... -const int MAX_BUFFER_LEN = 500; -typedef struct MESS_BLOCK -{ - unsigned char num; - char buffer[MAX_BUFFER_LEN]; -}; -typedef struct MESS_PROC -{ - char context_name[MAX_STRING_LEN]; - char app_name[MAX_STRING_LEN]; - char proc_name[MAX_STRING_LEN]; - char status; -}; -//for mmi end ................................ - -const int DEFAULT_PERIOD = 3; -const int COUNT_LIMIT = 2; -const int START_DEFAULT_PERIOD = 60; -const int START_COUNT_LIMIT = 5; -const int APP_COUNT_LIMIT = 1; - -//process critical level -//const int WST_CRITICAL = 1; // Shutdown and reboot workstation when failed -//const int SYS_CRITICAL = 2; // Shutdown and reboot the system on the wst when failed -//const int USER_CRITICAL = 3; // Shutdown and reboot the subsystem when failed -//const int GENERAL = 4; // reboot the process -const int CRUCIAL = 1; // crucial process -const int GENERAL = 0; // general process - - -extern int srv_init(char *service,int port); -extern int Tcp_close(int sockfd); -extern int Tcp_read(int fd,char *ptr,int nbytes); -extern int Tcp_write(int fd,char *ptr,int nbytes); -extern int srv_accept(int fd,struct sockaddr *cli_addr,int *clilen); -extern int client_tcp_open(char *host,char *service,int port); - -typedef struct -{ - char context_name[MAX_STRING_LEN]; - char app_name[MAX_STRING_LEN]; - char proc_name[MAX_STRING_LEN]; - pid_t proc_pid; -}PROC_ADM_INFO; - -//*************************************************************** -// structure name : PROC_INFO -// function : store process informatin -// author : -// date : -// modify by : -// modification : -// mod date : -//*************************************************************** -typedef struct -{ - int position; - char node_name[MAX_STRING_LEN]; - char context_name[MAX_STRING_LEN]; - char app_name[MAX_STRING_LEN]; - char proc_name[MAX_STRING_LEN]; - - unsigned char active_flag; - unsigned char master_flag; - - time_t startup_time; - time_t refresh_time; - short refresh_peri; - unsigned char monitor_type; - - pid_t proc_pid; - unsigned char auto_start; - unsigned char act_timer; - unsigned char start_timer; - unsigned char critical_level; - - char exefile_path[MAX_EXECMD_LEN]; - -}PROC_INFO; - -//*************************************************************** -// structure name : APP_INFO -// function : store application informatin -// author : -// date : -// modify by : -// modification : -// mod date : -//*************************************************************** -typedef struct -{ - int position; - char context_name[MAX_STRING_LEN]; - int context_id; - char app_name[MAX_STRING_LEN]; - int app_id; - unsigned char act_timer; - unsigned char active_flag; -}APP_INFO; - - -typedef struct -{ - int no_proc; - int semdes_cfg; - PROC_INFO proc[MAX_LOCAL_PROCESS]; - APP_INFO app[MAX_LOCAL_APP]; -}PROCCFG; - -class proc_invocation -{ - public: - int m_init; - PROCCFG *proccfg_p; - - public: - proc_invocation(); - ~proc_invocation(); - - int conf_create(); - //int check_proc_status(); - //int update_rtdb(); - //int kill_proc(pid_t pid); - //int start_proc(char *cmdline); - //int send_alarm(char *context_name, char *app_name, char *proc_name, unsigned char status); - //int update_proc_status(char *context_name, char *app_name, char *proc_name, char status); - - //int proc_init(char *context_name, char *app_name, char *proc_name, int critical_level);//exefile_path,auto_start - int proc_init(char *context_name, char *app_name, char *proc_name); - int proc_report(int pos, char status, int intertime=3); - int proc_exit(int proc_pos); - - int get_pos(char *context_name, char *app_name, char *proc_name); - int is_proc_exist(char *context_name, char *app_name, char *proc_name); - int conf_map(); - - int get_procinfo(int position, PROC_ADM_INFO *p_info); - int get_active_pid(int &num, int *p_pidlist); - int is_proc_run(pid_t pid); - int is_proc_run(char *context_name, char *app_name, char *proc_name); - -}; - -#endif - - - diff --git a/branches/sys_nicmonitor-1.6/read_netcard.c b/branches/sys_nicmonitor-1.6/read_netcard.c deleted file mode 100644 index c745f48..0000000 --- a/branches/sys_nicmonitor-1.6/read_netcard.c +++ /dev/null @@ -1,133 +0,0 @@ -#include "mnic.h" - -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; -} - - -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 == '\0') - return NULL; - p++; - break; - } - *name++ = *p++; - } - *name++ = '\0'; - return p; -} - -int get_dev_fields(char *str, NETCARD_INFO *ife) -{ - 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]); - return 0; -} - -int if_fetch(NETCARD_INFO *ife) -{ - int skfd; - struct ifreq ifr; - - if((skfd = socket(PF_INET, SOCK_DGRAM, 0)) == -1)return -1; - - strcpy(ifr.ifr_name, ife->charname); - if (!ioctl(skfd, SIOCGIFFLAGS, &ifr)) - ife->flags = ifr.ifr_flags; - - if (!ioctl(skfd, SIOCGIFHWADDR, &ifr)) - ife->hwaddr = ifr.ifr_hwaddr; - - if (!ioctl(skfd, SIOCGIFMTU, &ifr)) - ife->mtu = ifr.ifr_mtu; - - if (!ioctl(skfd, SIOCGIFTXQLEN, &ifr)) - ife->tx_queue_len = ifr.ifr_qlen; - - if (!ioctl(skfd, SIOCGIFADDR, &ifr)) - ife->addr = ifr.ifr_addr; - - if (!ioctl(skfd, SIOCGIFBRDADDR, &ifr)) - ife->broadaddr = ifr.ifr_broadaddr; - - if (!ioctl(skfd, SIOCGIFNETMASK, &ifr)) - ife->netmask = ifr.ifr_netmask; - - ife->time_stamp = time(NULL); - close(skfd); - return 0; -} - - -int ioc_get_name(NETCARD_INFO *name) -{ - int skfd; - int ifrnum; - int i = 0; - struct ifconf ifc; - struct ifreq buf[MAXNICNUM]; - - if((skfd = socket(PF_INET, SOCK_DGRAM, 0)) == -1)return -1; - memset(buf, 0, sizeof(buf)); - ifc.ifc_len = sizeof(buf); - ifc.ifc_buf = (caddr_t) buf; - ioctl(skfd, SIOCGIFCONF, &ifc); - ifrnum = ifc.ifc_len / sizeof(struct ifreq); - while(ifrnum-- > 0){ - strcpy(name[i].charname, buf[ifrnum].ifr_name); - i++; - } - close(skfd); - return i; -} - diff --git a/branches/sys_nicmonitor-1.6/send_alarm.c b/branches/sys_nicmonitor-1.6/send_alarm.c deleted file mode 100644 index e88635f..0000000 --- a/branches/sys_nicmonitor-1.6/send_alarm.c +++ /dev/null @@ -1,37 +0,0 @@ -#include "mnic.h" - -void send_alarm(D5000_NIC_ALARM *mesg, int socket_port, char *ipv4) -{ - int bytes; - int sock_sd; - int val; - char error_str[200]; - struct sockaddr_in recever; - socklen_t sock_len; - - if((sock_sd = socket(PF_INET, SOCK_DGRAM, IPPROTO_UDP)) == -1){ - snprintf(error_str, sizeof(error_str), "EMERG: socket(): %s\n", strerror(errno)); - record_log(error_str); - return; - } - val=1; - if ((setsockopt(sock_sd, SOL_SOCKET, SO_BROADCAST, &val, sizeof(val))) == -1) { - snprintf(error_str, sizeof(error_str), "EMERG: setsockopt(): %s\n", strerror(errno)); - record_log(error_str); - return; - } - /* init socket*/ - recever.sin_family = AF_INET; - recever.sin_port = htons(socket_port); - inet_pton(AF_INET, ipv4, &recever.sin_addr); - sock_len = sizeof(recever); - /* send alarm*/ - if((bytes = sendto(sock_sd, mesg, sizeof(D5000_NIC_ALARM), 0, (const struct sockaddr *)&recever, sock_len)) < 1){ - snprintf(error_str, sizeof(error_str), "EMERG: sendto(): %s\n", strerror(errno)); - record_log(error_str); - return; - } - //snprintf(error_str, sizeof(error_str), "NOTICE: Alarm information has already send!\n"); - //record_log(error_str); - close(sock_sd); -} diff --git a/branches/sys_nicmonitor-1.6/sys_netcard.h b/branches/sys_nicmonitor-1.6/sys_netcard.h deleted file mode 100644 index 0d7746c..0000000 --- a/branches/sys_nicmonitor-1.6/sys_netcard.h +++ /dev/null @@ -1,88 +0,0 @@ -#ifndef _SYS_NETCARD_H -#define _SYS_NETCARD_H - -#include - -#ifndef NIC_NAME_LEN -#define NIC_NAME_LEN 64 -#endif - -#define SEM_PROJ_ID 's' -#define MAXNICNUM 32 - -#ifdef __cplusplus -extern "C"{ -#endif - -typedef long KEYID; - -typedef struct _msg_frame // message frame -{ - short len; // message length - short seqno; // send sequence - short serv; // services ID - short event; // event ID - unsigned char domain; // domain ID - unsigned char ctxt; // Context ID - short stid; // SOURCE task id - short dtid; // DESTINATION task ID - unsigned char ver_coding; // 版本号 + 编码 - unsigned char mes_type; // 帧类型 -}MSG_FRAME, *LPMSG_FRAME; - -typedef struct _net_info -{ - KEYID ID; - char charname[NIC_NAME_LEN]; - char descr[128]; - struct sockaddr addr; - struct sockaddr broadaddr; - struct sockaddr netmask; - struct sockaddr hwaddr; - int time_stamp; - short flags; - int mtu; - int tx_queue_len; - unsigned long long average_flow; - unsigned long long rx_packets; - unsigned long long tx_packets; - unsigned long long rx_bytes; - unsigned long long tx_bytes; - unsigned long rx_errors; - unsigned long tx_errors; - unsigned long rx_dropped; - unsigned long tx_dropped; - unsigned long rx_multicast; - unsigned long collisions; - unsigned long rx_fifo_errors; - unsigned long tx_carrier_errors; - unsigned long tx_fifo_errors; -}NETCARD_INFO, *LPNETCARD_INFO; - -#define NETCARD_ALARM_FAULT 1 -#define NETCARD_ALARM_RESUME 2 -#define NETCARD_ALARM_SWITCH 3 -#define NETCARD_ALARM_ABNORMAL 4 -#define NETCARD_ALARM_NORMAL 5 -typedef struct _sys_netcard_alarm -{ - char fault_devname[NIC_NAME_LEN]; /*故障设备名称 */ - char switch_devname[NIC_NAME_LEN]; /*切换设备名称 */ - short flags; /* 状态标记 : 1:网卡故障。2:网卡恢复。3:网卡切换。4:流量异常。*/ - short retrytimes; //0,1,2 重发次数 -}SYS_NETCARD_ALARM, *LPSYS_NETCARD_ALARM; - - -typedef struct _d5000_nic_alarm -{ - MSG_FRAME tMsgFrame ; - SYS_NETCARD_ALARM tSysNetcardAlarm ; -}D5000_NIC_ALARM, *LPD5000_NIC_ALARM; - - -#ifdef __cplusplus -} -#endif - -#endif - diff --git a/branches/sys_nicmonitor-1.6/sys_nicmonitor b/branches/sys_nicmonitor-1.6/sys_nicmonitor deleted file mode 100755 index 5fa9b9a..0000000 Binary files a/branches/sys_nicmonitor-1.6/sys_nicmonitor and /dev/null differ diff --git a/code/trunk/sys_nicmonitor/src/Pkgfile b/code/Pkgfile similarity index 100% rename from code/trunk/sys_nicmonitor/src/Pkgfile rename to code/Pkgfile diff --git a/code/trunk/sys_nicmonitor/src/README b/code/README similarity index 100% rename from code/trunk/sys_nicmonitor/src/README rename to code/README diff --git a/code/branches/sys_nicmonitor-1.0/Makefile b/code/branches/sys_nicmonitor-1.0/Makefile deleted file mode 100644 index 5cccc77..0000000 --- a/code/branches/sys_nicmonitor-1.0/Makefile +++ /dev/null @@ -1,28 +0,0 @@ -#! /usr/bin/make -f - -DEFAULTS = Makefile.config - -include $(DEFAULTS) - -all:sys_nicmonitor libnic_shm.so - -CFLAGS += -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 $@ $^ -libnic_shm.so:nicinfo_shm.c - $(CC) -fpic -shared -o $@ $^ -clean: - $(RM) *.o - $(RM) libnic_shm.so sys_nicmonitor -install: - $(MKDIR) -p $(LIBDIR) - $(MKDIR) -p $(BINDIR) - $(MKDIR) -p $(INCDIR) - $(CP) libnic_shm.so $(LIBDIR) - $(CP) sys_nicmonitor $(BINDIR) - $(CP) nicinfo_shm.h $(INCDIR) -uninstall: - $(RM) -f $(LIBDIR)/libnic_shm.so - $(RM) -f $(BINDIR)/sys_nicmonitor - $(RM) -f $(INCDIR)/nicinfo_shm.h diff --git a/code/branches/sys_nicmonitor-1.0/Makefile.config b/code/branches/sys_nicmonitor-1.0/Makefile.config deleted file mode 100644 index 7a2d8e6..0000000 --- a/code/branches/sys_nicmonitor-1.0/Makefile.config +++ /dev/null @@ -1,16 +0,0 @@ - -PREFIX = $(DESTDIR) - -BINDIR = $(PREFIX)/usr/bin - -LIBDIR = $(PREFIX)/usr/lib64 - -INCDIR = $(PREFIX)/usr/include - -INSTALL_SH = ./install.sh - -CP = cp -RM = rm -#CC = gcc -CC = g++ -MKDIR = mkdir diff --git a/code/branches/sys_nicmonitor-1.0/const.h b/code/branches/sys_nicmonitor-1.0/const.h deleted file mode 100644 index e2ebfa5..0000000 --- a/code/branches/sys_nicmonitor-1.0/const.h +++ /dev/null @@ -1,51 +0,0 @@ -#ifndef _RTE_CONST_H -#define _RTE_CONST_H - -#include - - -#define MAX_STRING_LEN 24 -#define MAX_EXECMD_LEN 200 -#define MAX_FILENAME_LEN 200 - -#define MAX_CONTEXT 8 -#define MAX_HOSTNAME_LEN 24 -#define MAX_LOCAL_MESPROC 512 -#define MAX_LOCAL_PROCESS MAX_LOCAL_MESPROC -#define MAX_LOCAL_APP 32 -#define MAX_LOCAL_NODE 256 - -#define MAX_SET 256 //max num of event set -#define MAX_EVENT 1300 -#define MAX_REG_PROC 20 - -#define LEN_SHMBLK_BIG 4096 -#define MAX_MSGBUF_LEN 32767 -#define FREE_PAGE_SIZE 65536 -#define MAX_PAGE_NUM 1024 - -#define MAX_QUE MAX_LOCAL_MESPROC -#define MAX_SEMPHORE_SET MAX_LOCAL_MESPROC*2 -#define RTE_HAN 0 // queue number for event handeler0(RTE) -#define EX_EVENT_HAN RTE_HAN // queue number for extrnal event handeler - -#define DOMAIN_I 1 -#define DOMAIN_II 2 -#define DOMAIN_III 3 - -//Add 20090225 -const int PROC_TYPE_RPT =1; -const int PROC_TYPE_UNRPT =0; -//add end - - -//status for proc and app -const int NON_ACTIVE = 0; -const int ACTIVE = 1; -const int HANGUP = 2; -const int FAILURE = 5; -const int START = 6; -const int STOP = 7; - -#endif - diff --git a/code/branches/sys_nicmonitor-1.0/mnic.c b/code/branches/sys_nicmonitor-1.0/mnic.c deleted file mode 100644 index 8491e73..0000000 --- a/code/branches/sys_nicmonitor-1.0/mnic.c +++ /dev/null @@ -1,1430 +0,0 @@ -#include "mnic.h" -#include "dotconf.h" -#include -#include -#include "proc_inv.h" - -#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 "/tmp/sys_netcard_shm_path" -#define SEM_PATH "/tmp/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.2" - -#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. -}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 find_nic_config(char *); - -static pthread_mutex_t mut = PTHREAD_MUTEX_INITIALIZER; -static short seqno = 1; -static char log_flag = 1; -static int sem_id = 0; -static char process_name[64]; -char log_path[1024]; - -int host_name_size = 0; -char host_name[HOST_NAME_SIZE]; -int semid = -1; -SHM *shm_ptr; - -//20100510 -int total_ping_time, elapsed_ping_time; -int get_netcard_status(NETCARD_INFO_ST *); - -/****dotconf****/ -static DOTCONF_CB(cb_str); -static DOTCONF_CB(cb_int); -int nicnum = 0; -CONFIG_FILE_ST conf; - -char myconf[][64] = { - "domain", // 0 - "serv", // 1 - "event", // 2 - "udpport", // 3 - "sys_netcard_shm_path", // 4 - "sys_netcard_sem_path", // 5 - "monitor_interval", // 6 - "write_interval", // 7 - "flow_interval", // 8 - "flow_limit", // 9 - "flow_peak", // 10 - "udp", // 11 - "nic", // 12 - "ping", // 13 - "pingnum", // 14 - "pinglap", // 15 - "" -}; - -static const configoption_t options[] = { - {myconf[0], ARG_INT, cb_int, NULL, CTX_ALL}, - {myconf[1], ARG_INT, cb_int, NULL, CTX_ALL}, - {myconf[2], ARG_INT, cb_int, NULL, CTX_ALL}, - {myconf[3], ARG_INT, cb_int, NULL, CTX_ALL}, - {myconf[4], ARG_STR, cb_str, NULL, CTX_ALL}, - {myconf[5], ARG_STR, cb_str, NULL, CTX_ALL}, - {myconf[6], ARG_INT, cb_int, NULL, CTX_ALL}, - {myconf[7], ARG_INT, cb_int, NULL, CTX_ALL}, - {myconf[8], ARG_INT, cb_int, NULL, CTX_ALL}, - {myconf[9], ARG_INT, cb_int, NULL, CTX_ALL}, - {myconf[10], ARG_INT, cb_int, NULL, CTX_ALL}, - {myconf[11], ARG_STR, cb_str, NULL, CTX_ALL}, - {myconf[12], ARG_STR, cb_str, NULL, CTX_ALL}, - {myconf[13], ARG_STR, cb_str, NULL, CTX_ALL}, - {myconf[14], ARG_INT, cb_int, NULL, CTX_ALL}, - {myconf[15], ARG_INT, cb_int, NULL, CTX_ALL}, - LAST_OPTION -}; -/****dotconf****/ - -/* write error in logfile */ -void record_log(char *str) -{ - time_t tamp; - char str_tm[4]; - char log_str[512]; - FILE *log_fp = NULL; - struct tm tmptr; - struct timeval tv; - struct timezone tz; - - if((log_fp = fopen(log_path, "a")) != NULL){ - tamp = time(NULL); - memset(str_tm, 0, sizeof(str_tm)); - memset(log_str, 0, sizeof(log_str)); - localtime_r(&tamp, &tmptr); - gettimeofday(&tv, &tz); - snprintf(str_tm, sizeof(str_tm), "%d", (int)tv.tv_usec/1000); - if(str_tm[1] == '\0')str_tm[1] = '0'; - if(str_tm[2] == '\0')str_tm[2] = '0'; - strftime(log_str, sizeof(log_str), "%F %T.", &tmptr); - strcat(log_str, str_tm); - strcat(log_str, " "); - strcat(log_str, process_name); - strcat(log_str, " "); - strcat(log_str, str); - if(fwrite(log_str, 1, strlen(log_str), log_fp) < 1){ - } - fclose(log_fp); - } -} - -static void init_sem(int *semid, CONFIG_FILE_ST *ptr) -{ - key_t key; - char error_str[200]; - -/* write error in logfile */ - memset(error_str, 0, sizeof(error_str)); - if ((key = ftok(SEM_PATH, SEM_PROJ_ID)) == -1) { - snprintf(error_str, sizeof(error_str), "EMERG: ftok():%s\n", strerror(errno)); - record_log(error_str); - return; - } - if((*semid = semget(key, 1, IPC_CREAT|0666)) == -1){ - snprintf(error_str, sizeof(error_str), "EMERG: semget():%s\n", strerror(errno)); - record_log(error_str); - return; - } - if(semctl(*semid, 0, SETVAL, 1) == -1){ - snprintf(error_str, sizeof(error_str), "EMERG: semctl: %s !\n", strerror(errno)); - record_log(error_str); - semctl(*semid, 0, IPC_RMID); - return; - } -} - -static void init_shm(SHM **ptr, CONFIG_FILE_ST *p) -{ - int fd = -1; - char error_str[200]; - - mode_t mask_tmp; - mask_tmp = umask(0000); - if((fd = open(SHM_PATH, O_RDWR|O_CREAT|O_TRUNC, 0666)) == -1){ - printf("could not create %s\n", SHM_PATH); - return; - } - umask(mask_tmp); - lseek(fd, sizeof(SHM), SEEK_SET); - write(fd, "\0", 1); - *ptr = (SHM *)mmap(NULL, sizeof(SHM), PROT_READ|PROT_WRITE, MAP_SHARED, fd, 0); - if(*ptr == MAP_FAILED){ - snprintf(error_str, sizeof(error_str), "EMERG: mmap():%s\n", strerror(errno)); - record_log(error_str); - return; - } - close(fd); - -} - -static void get_sem(int semid) -{ - struct sembuf lock; - char error_str[200]; - - lock.sem_num = 0; - lock.sem_op = -1; - lock.sem_flg = SEM_UNDO; - - while(semop(semid, &lock, 1)){ - if(errno == EINTR)continue; - snprintf(error_str, sizeof(error_str), "EMERG: get_sem semop():%s\n", strerror(errno)); - record_log(error_str); - return; - } -} - -static void release_sem(int semid) -{ - int ret; - char error_str[200]; - struct sembuf unlock; - - unlock.sem_num = 0; - unlock.sem_op = 1; - unlock.sem_flg = SEM_UNDO; - - if((ret = semop(semid, &unlock, 1)) == -1){ - snprintf(error_str, sizeof(error_str), "EMERG: rel_sem semop():%s\n", strerror(errno)); - record_log(error_str); - return; - } -} - -static void proc_get_name(NET_NAME_ST *nodep) -{ - char linebuf[LINE_SIZE]; - char error_str[200]; - char *retp = NULL; - int err_val=0; - NET_NAME_ST *node = NULL; - FILE *dev_fp; - - if((dev_fp = fopen("/proc/net/dev", "rb")) == NULL){ - snprintf(error_str, sizeof(error_str), "EMERG: Can't open dev file:%s\n", strerror(errno)); - record_log(error_str); - return; - } - memset(linebuf, 0, sizeof(linebuf)); - fgets(linebuf, sizeof(linebuf), dev_fp); - fgets(linebuf, sizeof(linebuf), dev_fp); - while((retp = fgets(linebuf, sizeof(linebuf), dev_fp)) != NULL){ - if((node = (NET_NAME_ST *)malloc(sizeof(NET_NAME_ST))) == NULL){ - err_val = 1; - break; - } - memset(node->name, 0, NIC_NAME_LEN); - get_name(node->name, retp); - node->next = nodep->next; - nodep->next = node; - } - if(err_val){ - NET_NAME_ST *curr = nodep->next; - while(curr != NULL){ - node = curr->next; - free(curr); - curr = node; - } - } - fclose(dev_fp); -} - -static void ioctl_get_name(NET_NAME_ST *nodep) -{ - int count = 0; - int inuse = 0; - int err_val = 0; - NETCARD_INFO net_name[MAXNICNUM]; - NET_NAME_ST *node = NULL, *curr = NULL; - - memset(net_name, 0, sizeof(net_name)); - if((count = ioc_get_name(net_name)) == 0)return; - if(nodep->next == NULL) - inuse = 1; - while(count > 0){ - curr = nodep->next; - while(curr != NULL){ - if(!strncmp(net_name[count - 1].charname, curr->name, NIC_NAME_LEN)){ - inuse = 0; - break; - } - inuse = 1; - curr = curr->next; - } - if(inuse){ - if((node = (NET_NAME_ST *)malloc(sizeof(NET_NAME_ST))) == NULL){ - err_val = 1; - break; - } - memset(node->name, 0, NIC_NAME_LEN); - strncpy(node->name, net_name[count - 1].charname, NIC_NAME_LEN); - node->next = nodep->next; - nodep->next = node; - } - count--; - } - if(err_val){ - curr = nodep->next; - while(curr != NULL){ - node = curr->next; - free(curr); - curr = node; - } - } -} - -static void get_all_name(NET_NAME_ST *nodep) -{ - proc_get_name(nodep); - ioctl_get_name(nodep); -} - -static void create_monit_list(NET_NAME_ST *nodep, NETCARD_INFO_ST *info_list, char *bond) -{ - int err_val = 0; - NETCARD_INFO_ST *info_node = NULL; - NET_NAME_ST *curr, *node; - - curr = nodep->next; - while(curr != NULL){ - if((info_node = (NETCARD_INFO_ST *)malloc(sizeof(NETCARD_INFO_ST))) == NULL){ - err_val = 1; - break; - } - memset(info_node, 0, sizeof(NETCARD_INFO_ST)); - strncpy(info_node->info.charname, curr->name, NIC_NAME_LEN); - strncpy(info_node->info.descr, bond, strlen(bond)); - info_node->next = info_list->next; - info_list->next = info_node; - curr = curr->next; - } - if(err_val){ - NETCARD_INFO_ST *info_curr = info_list->next; - while(info_curr != NULL){ - info_node = info_curr->next; - free(info_curr); - info_curr = info_node; - } - curr = nodep->next; - while(curr != NULL){ - node = curr->next; - free(curr); - curr = node; - } - } -} - -static int get_inc_info(NETCARD_INFO *net) -{ - return if_fetch(net); -} - -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){ - if((str = strstr(linebuf, net->charname)) != NULL){ - str +=strlen(net->charname) + 2; - get_dev_fields(str, net); - break; - } - } - fclose(dev_fp); -} - -static void send_inc_info_one(NETCARD_INFO *net, int semid, SHM *ptr) -{ - int i; - for(i = 0; ptr->info[i].charname[0] != 0; i++){ - if(!strncmp(ptr->info[i].charname, net->charname, NIC_NAME_LEN)){ - get_sem(semid); - memcpy(&ptr->info[i], net, sizeof(NETCARD_INFO)); - release_sem(semid); - } - } -} - -static void *send_inc_info(void *p) -{ - int i = 0; - int flags = 1; - char error_str[200]; - unsigned long long bytes[MAXNICNUM]; - THMEM *ptr = (THMEM *)p; - NETCARD_INFO_ST *curr; - struct timespec s_time, os_time; - - if((curr = ptr->listp->next) == NULL){ - pthread_mutex_lock(&mut); - snprintf(error_str, sizeof(error_str), "EMERG: Empty Netcard list!\n"); - record_log(error_str); - pthread_mutex_unlock(&mut); - return NULL; - } - s_time.tv_sec = ptr->conf->write_interval; - s_time.tv_nsec = 0; - - /* copy all net_info*/ - while(1){ - i = 0; - get_sem(ptr->semid); - memset(ptr->shm_ptr, 0, sizeof(SHM)); - curr = ptr->listp->next; - while(curr != NULL){ - get_inc_info(&curr->info); - if(curr->status == NIC_UNLINKABLE){ - curr->info.flags &= (~IFF_UP); - curr->info.flags &= (~IFF_RUNNING); - } - get_inc_stats(&curr->info); - if(flags){ - bytes[i] = curr->info.rx_bytes + curr->info.tx_bytes; - } - curr->info.average_flow =(unsigned long long)((curr->info.rx_bytes + curr->info.tx_bytes - bytes[i])/(ptr->conf->write_interval)); - memcpy(&ptr->shm_ptr->info[i], &curr->info, sizeof(NETCARD_INFO)); - bytes[i] = curr->info.rx_bytes + curr->info.tx_bytes; - i++; - curr = curr->next; - } - release_sem(ptr->semid); - flags = 0; - nanosleep(&s_time, &os_time); - } -} - -static int get_netcard_count(char *name) -{ - int count = 0; - FILE *fp = NULL; - char path[64] = "/proc/net/bonding/"; - char buf[LINE_SIZE]; - char *retp = NULL; - char *str = NULL; - - strncat(path, name, strlen(name)); - if((fp = fopen(path, "r")) == NULL)return 1; - - memset(buf, 0, sizeof(buf)); - fseek(fp, 65, SEEK_SET); - while((retp = fgets(buf, sizeof(buf), fp)) != NULL){ - if((str = strstr(buf, "active-backup")) != NULL){ - fclose(fp); - return 1; - } - if(!strncmp(buf, "Slave Interface:", 16))count++; - } - fclose(fp); - return count; -} - -static void *cacu_flow(void *p) -{ - int i = 0, count_t = 1; - int summit = 0; - int flags = 1; - int count_ok[MAXNICNUM]; - int count_no[MAXNICNUM]; - char error_str[200]; - unsigned long long bytes[MAXNICNUM]; - THFLOW *ptr = (THFLOW *)p; - NETCARD_INFO_ST *curr; - struct timespec s_time, os_time; - - if((curr = ptr->mem.listp->next) == NULL){ - pthread_mutex_lock(&mut); - snprintf(error_str, sizeof(error_str), "EMERG: Empty Netcard list!\n"); - record_log(error_str); - pthread_mutex_unlock(&mut); - return NULL; - } - s_time.tv_sec = ptr->mem.conf->flow_interval; - s_time.tv_nsec = 0; - - for(i = 0; i < MAXNICNUM ; i++){ - count_ok[i] = 0; - count_no[i] = 0; - } - while(1){ - i = 0; - curr = ptr->mem.listp->next; - while(curr != NULL){ - get_inc_info(&curr->info); - get_inc_stats(&curr->info); - if(flags){ - bytes[i] = curr->info.rx_bytes + 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); - 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); -#if 0 - printf("%s:\tcount=%d\tsummit=%d\tflow_limit:%d\taverage_flow:%d\n", curr->info.charname, count_t, summit, conf.flow_limit, curr->info.average_flow); -#endif - 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); - strncat(ptr->env.Malarm.tSysNetcardAlarm.fault_devname, "-", 1); - strncat(ptr->env.Malarm.tSysNetcardAlarm.fault_devname, curr->info.charname, (NIC_NAME_LEN - ptr->env.host_name_size - 1)); - ptr->env.Malarm.tSysNetcardAlarm.flags = NETCARD_ALARM_ABNORMAL; - ptr->env.Malarm.tSysNetcardAlarm.retrytimes = 0; - ptr->env.Malarm.tMsgFrame.len = sizeof(ptr->env.Malarm); - ptr->env.Malarm.tMsgFrame.seqno = seqno; - pthread_mutex_lock(&mut); - while(count_no[i] > 0){ - send_alarm(&ptr->env.Malarm, ptr->mem.conf->udpport, ptr->mem.conf->ip); - seqno++; - ptr->env.Malarm.tMsgFrame.seqno = seqno; - ptr->env.Malarm.tSysNetcardAlarm.retrytimes++; - count_no[i]--; - if(count_no[i] == 0){ - send_inc_info_one(&curr->info, ptr->mem.semid, ptr->mem.shm_ptr); - //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); - snprintf(error_str, sizeof(error_str), "NOTICE: %s flow is abnormal !\n", curr->info.charname); - record_log(error_str); - } - } - pthread_mutex_unlock(&mut); - count_ok[i] = 3; - }else{ - 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); - strncat(ptr->env.Malarm.tSysNetcardAlarm.fault_devname, "-", 1); - strncat(ptr->env.Malarm.tSysNetcardAlarm.fault_devname, curr->info.charname, (NIC_NAME_LEN - ptr->env.host_name_size - 1)); - ptr->env.Malarm.tSysNetcardAlarm.flags = NETCARD_ALARM_NORMAL; - ptr->env.Malarm.tSysNetcardAlarm.retrytimes = 0; - ptr->env.Malarm.tMsgFrame.len = sizeof(ptr->env.Malarm); - ptr->env.Malarm.tMsgFrame.seqno = seqno; - pthread_mutex_lock(&mut); - while(count_ok[i] > 0){ - send_alarm(&ptr->env.Malarm, ptr->mem.conf->udpport, ptr->mem.conf->ip); - seqno++; - ptr->env.Malarm.tMsgFrame.seqno = seqno; - ptr->env.Malarm.tSysNetcardAlarm.retrytimes++; - count_ok[i]--; - if(count_ok[i] == 0){ - send_inc_info_one(&curr->info, ptr->mem.semid, ptr->mem.shm_ptr); - snprintf(error_str, sizeof(error_str), "NOTICE: %s flow is normal now !\n", curr->info.charname); - record_log(error_str); - } - } - pthread_mutex_unlock(&mut); - count_no[i] = 3; - }/* end if(summit... */ - bytes[i] = curr->info.rx_bytes + curr->info.tx_bytes; - i++; - curr = curr->next; - }/* end while(curr...) */ - flags = 0; - nanosleep(&s_time, &os_time); - }/* end while(1) */ -} -static char *parse_str(char **s) -{ - char *retp = NULL; - char *p = *s; - - while(isspace(*p))p++; - retp = p; - while(!isspace(*p))p++; - *p = '\0'; - - return retp; -} - -int ping_gw(char *devname) -{ - int i, ret = -1, j, k; - char buf[64]; - - i = find_nic_config(devname); - if( i == -1 ) - return 0; - - if(conf.nic[i].gw_num == 0) - return 0; - - for(k=0; k /dev/null 2>&1", conf.pinglap,conf.nic[i].ping_ip[j]); - ret=system(buf); -#if 0 - if (WIFSIGNALED(ret) && - (WTERMSIG(ret) == SIGINT || WTERMSIG(ret) == SIGQUIT)) - exit(0); -#endif - if(ret == 0) - return 0; - - } - } - - return -1; -} - -static void *check_inc_switch(void *ptr) -{ - THENV *arg = (THENV *)ptr; - FILE *fp = NULL; - char name[NIC_NAME_LEN]; - struct timespec s_time, os_time; - int count = 0; - char linebuf[LINE_SIZE]; - char error_str[200]; - char *str = NULL; - char *retp = NULL; - - if((fp = fopen(arg->bond_file_path, "r")) == NULL){ - snprintf(error_str, sizeof(error_str), "EMERG: fopen():%s\n", strerror(errno)); - record_log(error_str); - free(ptr); - return NULL; - } - fseek(fp, 65, SEEK_SET); - memset(name, 0, sizeof(name)); - memset(linebuf, 0, sizeof(linebuf)); - if((retp = fgets(linebuf, sizeof(linebuf), fp)) != NULL){ - if((retp = strstr(linebuf, "active-backup")) == NULL){ - free(ptr); - fclose(fp); - return NULL; - } - } - - s_time.tv_sec = 0; - s_time.tv_nsec = 100000000; - - while(1){ - while((str = fgets(linebuf, sizeof(linebuf), fp)) != NULL){ - if(!strncmp(linebuf, "Currently Active Slave:", 23)){ - str += 23; - retp = parse_str(&str); - break; - } - } - if(name[0] == 0){ - strncpy(name, retp, strlen(retp)); - }else if(strncmp(name, retp, strlen(retp)) != 0){ - memset(arg->Malarm.tSysNetcardAlarm.switch_devname, 0, NIC_NAME_LEN); - strncpy(arg->Malarm.tSysNetcardAlarm.switch_devname, arg->host_name, arg->host_name_size); - strncat(arg->Malarm.tSysNetcardAlarm.switch_devname, "-", 1); - strncat(arg->Malarm.tSysNetcardAlarm.switch_devname, retp, (NIC_NAME_LEN - arg->host_name_size - 1)); - arg->Malarm.tSysNetcardAlarm.flags = NETCARD_ALARM_SWITCH; - arg->Malarm.tSysNetcardAlarm.retrytimes = 0; - arg->Malarm.tMsgFrame.len = sizeof(arg->Malarm); - arg->Malarm.tMsgFrame.seqno = seqno; - count = 3; - pthread_mutex_lock(&mut); - snprintf(error_str, sizeof(error_str), "NOTICE: Net card checking to %s!\n", retp); - record_log(error_str); - while(count > 0){ - send_alarm(&arg->Malarm, arg->conf->udpport, arg->conf->ip); - seqno++; - arg->Malarm.tMsgFrame.seqno = seqno; - arg->Malarm.tSysNetcardAlarm.retrytimes++; - count--; - } - pthread_mutex_unlock(&mut); - memset(name, 0, NIC_NAME_LEN); - strncpy(name, retp, strlen(retp)); - } - fclose(fp); - nanosleep(&s_time, &os_time); - if((fp = fopen(arg->bond_file_path, "r")) == NULL){ - free(ptr); - return NULL; - } - fseek(fp, 65, SEEK_SET); - } - free(ptr); - fclose(fp); - return NULL; -} - -static void *create_logfile(void *ptr) -{ - char buf[128]; - char del_file[DAYS][1024]; - char *s; - int head, tail, fd; - int flag = 0, del_flag = 0; - int old_mon = 0; - int old_day = 0; - time_t tamp; - struct tm tmptr; - struct timespec s_time, os_time; - - s_time.tv_sec = 1; - s_time.tv_nsec = 0; - head = tail = 0; - while(1){ - tamp = time(NULL); - localtime_r(&tamp, &tmptr); - if(old_mon != (tmptr.tm_mon + 1))flag = 1; - else if(old_day != tmptr.tm_mday)flag = 1; - else flag = 0; - if(flag){ - s = strrchr(log_path, '/'); - s++; - *s = '\0'; - memset(buf, 0, sizeof(buf)); - strftime(buf, sizeof(buf), "%Y%m%d_", &tmptr); - strcat(buf, process_name); - strcat(buf, ".log"); - strcat(log_path, buf); - mode_t mask_tmp; - mask_tmp = umask(0000); - if((fd = open(log_path, O_RDWR|O_CREAT, 0666)) != -1){ - log_flag = 0; - close(fd); - } - umask(mask_tmp); - old_mon = tmptr.tm_mon+1; - old_day = tmptr.tm_mday; - strcpy(del_file[head++], log_path); - if(head == DAYS){ - head = 0; - del_flag = 1; - } - if(del_flag){ - unlink(del_file[tail]); - tail++; - if(tail == DAYS)tail = 0; - } - } - nanosleep(&s_time, &os_time); - } -} - -void write_time(int semid, SHM *ptr) -{ - int i = 0; - int flag_time_stamp; - - flag_time_stamp = time(NULL); - - while(ptr->info[i].charname[0] != 0){ - ptr->info[i].time_stamp = flag_time_stamp; - i++; - } -} -#if 0 -char *parse(char *line) -{ - char *begin = NULL; - char *end = NULL; - - if((begin = strchr(line, '/')) != NULL){ - if((end = strstr(line, "bin")) != NULL){ - *end='\0'; - return begin; - } - } - return NULL; -} -#endif -void release_name_list(NET_NAME_ST *head) -{ - NET_NAME_ST *curr, *save; - - curr = head->next; - while(curr != NULL){ - save = curr->next; - free(curr); - curr = save; - } -} -int get_netcard(char *line, char *nic) -{ - char *begin, *retp; - - while(isspace(*line))line++; - begin = line; - while(!isspace(*line))line++; - *line++ = '\0'; - if(!strncmp(begin, "nic", 4)){ - if((retp = parse_str(&line)) != NULL){ - strncpy(nic, retp, MAXNICNUM); - return 1; - } - } - return 0; -} - -void sig_handler(int sig) -{ -// char err_str[200]; - -#if 0 - if(unlink(SHM_PATH) == -1){ - snprintf(err_str, sizeof(err_str), "NOTICE: error delete %s\n", strerror(errno)); - record_log(err_str); - } - if(unlink(SEM_PATH) == -1){ - snprintf(err_str, sizeof(err_str), "NOTICE: error delete %s\n", strerror(errno)); - record_log(err_str); - } -#endif -// semctl(sem_id, 0, IPC_RMID); - -// snprintf(err_str, sizeof(err_str), "NOTICE: process exit by signal %d\n", sig); -// record_log(err_str); - exit(0); -} - -/* - * 查看数据结构中是否有给定的网卡 - * 输入:网卡名称 - * 返回值:-1 表示没有 - * 非0值表示设备列表中的索引 - * - */ -int find_nic_config(char *dev_name){ - int i, index=-1; - - for(i=0; iname, myconf[13]) == 0) && (nicnum <= MAXNICNUM)){ - i = 1; - if(cmd->data.list[0] && cmd->data.list[1]){ - //查找设备列表中是否已有该网卡 - //如果没有则创建一项 - //如果已有则只要增加ping的地址和地址数量 - //一般都能找到,因为配置文件中一般总是先定义"nic",后定义"ping" - //如果没找到,则表明先定义了"ping",未定义"nic"或后定义"nic",而未定义"nic"应该是错误的 - index = find_nic_config(cmd->data.list[0]); - if(index == -1){ - strncpy(conf.nic[nicnum].dev_name, cmd->data.list[0], strlen(cmd->data.list[0]) >= IPLEN ? IPLEN-1 : strlen(cmd->data.list[0])); - card_info_index = nicnum; - nicnum++; - } - else - card_info_index = index; - - while(cmd->data.list[i]){ - if((int)(inet_addr(cmd->data.list[i])) != -1){ - strncpy(conf.nic[card_info_index].ping_ip[i-1], cmd->data.list[i], strlen(cmd->data.list[i]) >= IPLEN ? IPLEN-1 : strlen(cmd->data.list[i])); - i++; - } - } - conf.nic[card_info_index].gw_num = i-1; - } - } - - //"nic"选项的处理 - if((strcmp(cmd ->name, myconf[12]) == 0) && (nicnum <= MAXNICNUM)){ - //设备列表中没有给定的网卡设备,则添加一项 - //否则不必添加 - //对应配置文件中先指定"ping"后指定"nic"的情况 - index = find_nic_config(cmd->data.str); - if(index == -1){ - strncpy(conf.nic[nicnum].dev_name, cmd->data.str, strlen(cmd->data.str) >= NIC_NAME_LEN ? NIC_NAME_LEN-1 : strlen(cmd->data.str)); - nicnum++; - } - } - - if((strcmp(cmd ->name, myconf[11]) == 0)){ - strncpy(conf.udp, cmd->data.str, strlen(cmd->data.str)); - } - - return NULL; -} - -DOTCONF_CB(cb_int) -{ - if(strcmp(cmd ->name, myconf[0]) == 0){ - conf.domain = cmd->data.value; - } - if(strcmp(cmd ->name, myconf[1]) == 0){ - conf.serv = cmd->data.value; - } - if(strcmp(cmd ->name, myconf[2]) == 0){ - conf.event = cmd->data.value; - } - if(strcmp(cmd ->name, myconf[3]) == 0){ - conf.udpport = cmd->data.value; - } - if(strcmp(cmd ->name, myconf[6]) == 0){ - conf.monitor_interval = cmd->data.value; - } - if(strcmp(cmd ->name, myconf[7]) == 0){ - conf.write_interval = cmd->data.value; - } - if(strcmp(cmd ->name, myconf[8]) == 0){ - conf.flow_interval = cmd->data.value; - } - if(strcmp(cmd ->name, myconf[9]) == 0){ - conf.flow_limit = cmd->data.value; - } - if(strcmp(cmd ->name, myconf[10]) == 0){ - conf.flow_peak = cmd->data.value; - } - if(strcmp(cmd ->name, myconf[14]) == 0){ - conf.pingnum = cmd->data.value; - } - if(strcmp(cmd ->name, myconf[15]) == 0){ - conf.pinglap = cmd->data.value; - } - - return NULL; -} - -DOTCONF_CB(cb_list) -{ - return NULL; -} - -int get_conf() -{ -// int uid; - char buf[1024]; -// int i; - struct passwd *user; - configfile_t *configfile; - - memset(buf, 0, sizeof(buf)); -// uid=getuid(); - user = getpwnam("d5000"); -// user = getpwuid(uid); - - sprintf(buf,"%s", user->pw_dir); - strcat(buf,CONF_FILE); - - configfile = dotconf_create(buf, options, NULL, CASE_INSENSITIVE); - if (!configfile) - { - record_log("Warning can't reading config file\n"); - return -1; - } - - if (dotconf_command_loop(configfile) == 0){ - record_log("Warning can't reading config file\n"); - return -1; - } - dotconf_cleanup(configfile); - - if(conf.domain == 0) - conf.domain = 10; - if(conf.serv == 0) - conf.serv = 1; - if(conf.event == 0) - conf.event = 2; - if(conf.udpport == 0) - conf.udpport = 15000; - if(conf.monitor_interval == 0) - conf.monitor_interval = 100; - if(conf.write_interval == 0) - conf.write_interval = 300; - if(conf.flow_interval == 0) - conf.flow_interval = 60; - if(conf.flow_limit == 0) - conf.flow_limit = 30; - if(conf.flow_peak == 0) - conf.flow_peak = 1000; - if(conf.udp[0] == 0) - strcpy(conf.udp, "bond0"); - if(conf.nic[0].dev_name[0] == 0) - strcpy(conf.nic[0].dev_name, "bond0"); - if(conf.pingnum == 0) - conf.pingnum = 2; - if(conf.pinglap == 0) - conf.pinglap = 1; - - return 0; -} - -int isrun(char *p) -{ - char buf[16]; - char cmdbuf[1024]; - FILE *fp; - - memset(buf, 0, sizeof(buf)); - memset(cmdbuf, 0, sizeof(cmdbuf)); - - snprintf(cmdbuf, sizeof(cmdbuf), "pidof %s", p); - fp=popen(cmdbuf, "r"); - fread(buf, sizeof(char), sizeof(buf), fp); - pclose(fp); - if(strchr(buf, ' ') != NULL) - return 1; - else - return 0; -} - -int get_broad_ip(void) -{ - char ip_p[4][4]; - char error_str[200]; - int skfd; - struct ifreq ifr; - - memset(&ifr, 0, sizeof(ifr)); - memset(ip_p, 0, sizeof(ip_p)); - - if((skfd = socket(PF_INET, SOCK_DGRAM, 0)) == -1)return -1; - strcpy(ifr.ifr_name, conf.udp); - if (!ioctl(skfd, SIOCGIFBRDADDR, &ifr)){ - sprintf(ip_p[0], "%d", (unsigned char)ifr.ifr_broadaddr.sa_data[2]); - sprintf(ip_p[1], "%d", (unsigned char)ifr.ifr_broadaddr.sa_data[3]); - sprintf(ip_p[2], "%d", (unsigned char)ifr.ifr_broadaddr.sa_data[4]); - sprintf(ip_p[3], "%d", (unsigned char)ifr.ifr_broadaddr.sa_data[5]); - }else{ - snprintf(error_str, sizeof(error_str), "EMERG: Invalid broadcast device -- %s\n", conf.udp); - record_log(error_str); - close(skfd); - return -1; - } - close(skfd); - strcat(conf.ip, ip_p[0]); - strcat(conf.ip, "."); - strcat(conf.ip, ip_p[1]); - strcat(conf.ip, "."); - strcat(conf.ip, ip_p[2]); - strcat(conf.ip, "."); - strcat(conf.ip, ip_p[3]); - - return 0; -} - -void create_logdir(void) -{ - int ret = 0; - - strcpy(log_path, LOG_PATH); - mode_t mask_tmp; - mask_tmp = umask(0000); - if(mkdir(log_path, 0777) != 0){ - if(errno != EEXIST){ - strcpy(log_path, "/tmp/"); - } - } - if((ret = open(SEM_PATH, O_RDWR | O_CREAT, 0666)) != -1){ - close(ret); - } - umask(mask_tmp); -} - - /* create netcard name list */ -int create_name_list(NET_NAME_ST *all_name_list, NET_NAME_ST *opt_name_list) -{ - int i, count_t, err_val = 0; - NET_NAME_ST *curr, *node; - char error_str[200]; - - for(i = 0, count_t = 0; conf.nic[i].dev_name[0] != 0 && (i < MAXNICNUM); i++){ - curr = all_name_list->next; - while(curr != NULL){ - if(!strncmp(curr->name, conf.nic[i].dev_name, NIC_NAME_LEN)){ - if((node = (NET_NAME_ST *)malloc(sizeof(NET_NAME_ST))) == NULL){ - err_val = 1; - break; - } - memset(node->name, 0, NIC_NAME_LEN); - strncpy(node->name, conf.nic[i].dev_name, NIC_NAME_LEN); - node->next = opt_name_list->next; - opt_name_list->next = node; - count_t ++; - break; - } - curr = curr->next; - } - if(err_val)break; - } - if(count_t != i || err_val){ - for(curr = opt_name_list->next; curr != NULL; curr = curr->next)free(curr); - snprintf(error_str, sizeof(error_str), "EMERG: Invalid netcard name --\n"); - record_log(error_str); - return -1; - } - err_val = 0; - - return 0; -} - -int get_netcard_status(NETCARD_INFO_ST *listp) -{ - if((listp->info.flags & IFF_UP) != IFF_UP) - return NIC_DOWN; - - if((listp->info.flags & IFF_RUNNING) != IFF_RUNNING) - return NIC_UNRUNNING; - - //20100510 - if(elapsed_ping_time == total_ping_time){ - if(ping_gw(listp->info.charname) != 0) - return NIC_UNLINKABLE; - } - else{ - if(listp->status == NIC_UNLINKABLE) - return NIC_UNLINKABLE; - } - - return NIC_NORMAL; -} - -int do_alarm(NETCARD_INFO_ST *listp, int nic_status, D5000_NIC_ALARM *Malarm) -{ - int i; - char error_str[200]; - - memset(Malarm->tSysNetcardAlarm.fault_devname, 0, NIC_NAME_LEN); - strncpy(Malarm->tSysNetcardAlarm.fault_devname, host_name, host_name_size); - strncat(Malarm->tSysNetcardAlarm.fault_devname, "-", 1); - strncat(Malarm->tSysNetcardAlarm.fault_devname, listp->info.charname, NIC_NAME_LEN - host_name_size - 1); - Malarm->tSysNetcardAlarm.retrytimes = 0; - Malarm->tMsgFrame.len = sizeof(Malarm); - Malarm->tMsgFrame.seqno = seqno; - - switch(nic_status){ - case NIC_DOWN: - Malarm->tSysNetcardAlarm.flags = NETCARD_ALARM_FAULT; - snprintf(error_str, sizeof(error_str), "NOTICE: %s is DOWN!\n", listp->info.charname); - break; - case NIC_UNRUNNING: - Malarm->tSysNetcardAlarm.flags = NETCARD_ALARM_FAULT; - snprintf(error_str, sizeof(error_str), "NOTICE: %s is UP but UNRUNNING!\n", listp->info.charname); - break; - case NIC_UNLINKABLE: - Malarm->tSysNetcardAlarm.flags = NETCARD_ALARM_FAULT; - listp->info.flags &= (~IFF_UP); - listp->info.flags &= (~IFF_RUNNING); - snprintf(error_str, sizeof(error_str), "NOTICE: %s is RUNNING but UNLINKABLE!\n", listp->info.charname); - break; - case NIC_NORMAL: - Malarm->tSysNetcardAlarm.flags = NETCARD_ALARM_RESUME; - snprintf(error_str, sizeof(error_str), "NOTICE: %s is NORMAL!\n", listp->info.charname); - break; - } - pthread_mutex_lock(&mut); - for(i = 0; i < 3; i++){ - send_alarm(Malarm, conf.udpport, conf.ip); - seqno++; - Malarm->tMsgFrame.seqno = seqno; - Malarm->tSysNetcardAlarm.retrytimes++; - } - send_inc_info_one(&listp->info, semid, shm_ptr); - pthread_mutex_unlock(&mut); - record_log(error_str); - - return 0; -} - -int main(int argc, char ** argv) -{ - int ret = 0, proc_stat = 0; - unsigned int i = 0; - int nic_status; - int sys_time_stamp = 0; - char *retp = NULL; - char *str = NULL; - char bond[128]; - char error_str[200]; - char linebuf[LINE_SIZE]; - THENV *arg; - glob_t res; - FILE *bond_fp = NULL; - pthread_t tid[THRNR]; - pthread_t tid_mem; - pthread_t tid_flow; - pthread_t tid_time; - THMEM mem; - THFLOW flow; - struct timespec s_time, os_time, log_time; - NET_NAME_ST all_name_list, opt_name_list; - NETCARD_INFO_ST inc_info_list, *listp = NULL; - D5000_NIC_ALARM Malarm; - struct sigaction sig; - proc_invocation prcm; - - memset(&conf, 0, sizeof(CONFIG_FILE_ST)); - if(isrun(argv[0]) == 1){ - printf("the program is already started!\n"); - exit(0); - } -//process register -#if 1 - if((proc_stat=prcm.proc_init("sys","base_srv","sys_nicmonitor"))==-1){ - perror("proc_init()"); - exit(-1); - } -#endif - - sig.sa_handler = sig_handler; - sigemptyset(&sig.sa_mask); - sigaddset(&sig.sa_mask, SIGINT); - sigaddset(&sig.sa_mask, SIGTERM); - sig.sa_flags = 0; - sigaction(SIGTERM, &sig, NULL); - sigaction(SIGINT, &sig, NULL); - sigaction(SIGSEGV, &sig, NULL); - - /* log file path */ - create_logdir(); - - /* get process name */ - memset(process_name, 0, sizeof(process_name)); - if((retp = strrchr(argv[0], '/')) != NULL){ - retp++; - strcpy(process_name, retp); - }else{ - strcpy(process_name, argv[0]); - } - /*create logfile thread */ - pthread_create(&tid_time, NULL, create_logfile, NULL); - - /* initialize some....*/ - memset(&all_name_list, 0, sizeof(NET_NAME_ST)); - all_name_list.next = NULL; - memset(&opt_name_list, 0, sizeof(NET_NAME_ST)); - opt_name_list.next = NULL; - - memset(&inc_info_list, 0, sizeof(NETCARD_INFO_ST)); - inc_info_list.next = NULL; - - - /* get all netcard by proc/net/dev and ioctl */ - get_all_name(&all_name_list); - - /* wait for create log thread */ - log_time.tv_sec = 0; - log_time.tv_nsec = 1000000; - while(log_flag)nanosleep(&log_time, NULL); - - /* sure the process is started */ - snprintf(error_str, sizeof(error_str), "NOTICE: %s V%s is started !\n", process_name, MNIC_VERSION); - record_log(error_str); - - get_conf(); - - /* get broadcast address*/ - if((ret = get_broad_ip()) != 0) - return -1; - if((ret = create_name_list(&all_name_list, &opt_name_list)) != 0) - return -1; - /* get hostname */ - if((ret = gethostname(host_name, sizeof(host_name))) != -1){ - host_name_size = strlen(host_name); - }else host_name_size = HOST_NAME_SIZE; - /* initialize Malarm */ - Malarm.tMsgFrame.serv = conf.serv; - Malarm.tMsgFrame.event = conf.event; - Malarm.tMsgFrame.domain = conf.domain; - - /* get bonding file */ - memset(bond, 0, sizeof(bond)); - memset(linebuf, 0, sizeof(linebuf)); - if((ret = glob(BOND_PATH, 0, NULL, &res)) != 0){ - snprintf(error_str, sizeof(error_str), "NOTICE: No bonding netcard !\n"); - record_log(error_str); - strcpy(bond, "No bonding netcard!"); - } - for (i = 0; i < res.gl_pathc; ++i) { - if((arg = (THENV *)malloc(sizeof(THENV))) != NULL){ - memset(arg, 0, sizeof(THENV)); - strncpy(arg->bond_file_path, res.gl_pathv[i], strlen(res.gl_pathv[i])); - arg->Malarm = Malarm; - strncpy(arg->host_name, host_name, host_name_size); - arg->host_name_size = host_name_size; - arg->conf = &conf; - if(pthread_create(tid+i, NULL, check_inc_switch, (void*)arg)){ - snprintf(error_str, sizeof(error_str), "EMERG: pthread_create(): %s\n", strerror(errno)); - record_log(error_str); - } - } - } - for(i = 0; i < res.gl_pathc; ++i){ - if((bond_fp = fopen(res.gl_pathv[i], "r")) == NULL){ - snprintf(error_str, sizeof(error_str), "EMERG: fopen(): %s\n", strerror(errno)); - record_log(error_str); - }else{ - retp = strrchr(res.gl_pathv[i], '/'); - retp++; - strcat(bond, retp); - strcat(bond, ":"); - while((str = fgets(linebuf, sizeof(linebuf), bond_fp)) != NULL){ - if(!strncmp(linebuf, "Slave Interface:", 16)){ - str += 16; - retp = parse_str(&str); - strcat(bond, retp); - strcat(bond, " "); - } - } - fclose(bond_fp); - } - } - - /* initialize time*/ - s_time.tv_sec = 0; - s_time.tv_nsec = conf.monitor_interval*1000*1000; - - /* initialize sem*/ - init_sem(&semid, &conf); - init_shm(&shm_ptr, &conf); - /* for IPC_RMID */ - sem_id = semid; - - /* create monitor netcard list */ - create_monit_list(&opt_name_list, &inc_info_list, bond); - release_name_list(&all_name_list); - release_name_list(&opt_name_list); - - mem.semid = semid; - mem.shm_ptr = shm_ptr; - mem.listp = &inc_info_list; - mem.conf = &conf; - if(pthread_create(&tid_mem, NULL, send_inc_info, (void *)&mem)){ - snprintf(error_str, sizeof(error_str), "EMERG: pthread_create(): %s\n", strerror(errno)); - record_log(error_str); - } - - /* create caculate flow thread*/ - flow.env.Malarm = Malarm; - strncpy(flow.env.host_name, host_name, host_name_size); - flow.env.host_name_size = host_name_size; - flow.mem.semid = semid; - flow.mem.shm_ptr = shm_ptr; - flow.mem.listp = &inc_info_list; - flow.mem.conf = &conf; - if(pthread_create(&tid_flow, NULL, cacu_flow, (void *)&flow)){ - snprintf(error_str, sizeof(error_str), "EMERG: pthread_create(): %s\n", strerror(errno)); - record_log(error_str); - } - - total_ping_time = 1000 / conf.monitor_interval; - if(total_ping_time == 0) - total_ping_time = 1; - elapsed_ping_time = 1; - - while(1){ - i = 0; - listp = inc_info_list.next; - for(i = 0; listp != NULL; i++){ - get_inc_info(&listp->info); - get_inc_stats(&listp->info); - nic_status = get_netcard_status(listp); - if(nic_status != listp->status){ - do_alarm(listp, nic_status, &Malarm); - listp->status = nic_status; - } - listp = listp->next; - } - - if(seqno > 32765)seqno = 0; - nanosleep(&s_time, &os_time); - - - //20100510 - ++elapsed_ping_time; - if(elapsed_ping_time > total_ping_time) - elapsed_ping_time = 1; - - sys_time_stamp += conf.monitor_interval; - if(sys_time_stamp >= 1000){ - sys_time_stamp = 0; - write_time(semid, shm_ptr); - } - } - for (i = 0; i < res.gl_pathc; i++) { - pthread_join(tid[i], NULL); - } - pthread_join(tid_mem, NULL); - pthread_join(tid_flow, NULL); - pthread_join(tid_time, NULL); - pthread_mutex_destroy(&mut); - globfree(&res); - munmap(shm_ptr, sizeof(SHM)); - return 0; -} diff --git a/code/branches/sys_nicmonitor-1.0/mnic.h b/code/branches/sys_nicmonitor-1.0/mnic.h deleted file mode 100644 index d7a6390..0000000 --- a/code/branches/sys_nicmonitor-1.0/mnic.h +++ /dev/null @@ -1,44 +0,0 @@ -#ifndef __MNIC_H -#define __MNIC_H - -//#define _GNU_SOURCE -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include - -#include "sys_netcard.h" -#include -#include -#define IPSIZE 16 - -char *get_name(char *, char *); -int if_fetch(NETCARD_INFO *); -int ioc_get_name(NETCARD_INFO *); -int get_dev_fields(char *p, NETCARD_INFO *); -int get_user_home(int , char *); -void send_alarm(D5000_NIC_ALARM *, int , char *); -void record_log(char *); - -#endif diff --git a/code/branches/sys_nicmonitor-1.0/nicinfo_shm.c b/code/branches/sys_nicmonitor-1.0/nicinfo_shm.c deleted file mode 100644 index 9eb5b45..0000000 --- a/code/branches/sys_nicmonitor-1.0/nicinfo_shm.c +++ /dev/null @@ -1,188 +0,0 @@ -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include - -#include "nicinfo_shm.h" - -#define SHM_PATH "/tmp/sys_netcard_shm_path" -#define SEM_PATH "/tmp/sys_netcard_sem_path" -#define LOG_PATH "/var/log/netcard/" - -typedef struct net_info{ - NETCARD_INFO info[MAXNICNUM]; -}SHM; - -static int semid = 0; -static char log_path[1024]; -SHM *ptr = NULL; - -/* write error in logfile */ -void record_log(char *str) -{ - time_t tamp; - char str_tm[4]; - char log_str[512]; - FILE *log_fp = NULL; - struct tm tmptr; - struct timeval tv; - struct timezone tz; - - if((log_fp = fopen(log_path, "a")) != NULL){ - tamp = time(NULL); - memset(str_tm, 0, sizeof(str_tm)); - memset(log_str, 0, sizeof(log_str)); - localtime_r(&tamp, &tmptr); - gettimeofday(&tv, &tz); - snprintf(str_tm, sizeof(str_tm), "%d", (int)tv.tv_usec/1000); - if(str_tm[1] == '\0')str_tm[1] = '0'; - if(str_tm[2] == '\0')str_tm[2] = '0'; - strftime(log_str, sizeof(log_str), "%F %T.", &tmptr); - strcat(log_str, str_tm); - strcat(log_str, " "); - strcat(log_str, "get_nic_info"); - strcat(log_str, " "); - strcat(log_str, str); - if(fwrite(log_str, 1, strlen(log_str), log_fp) < 1){ - } - fclose(log_fp); - } -} - -void get_sem(int semid) -{ - char err_str[200]; - struct sembuf lock; - - lock.sem_num = 0; - lock.sem_op = -1; - lock.sem_flg = SEM_UNDO; - - while(semop(semid, &lock, 1)){ - if (errno == EINTR) { - continue; - } - snprintf(err_str, sizeof(err_str), "EMERG: semop():\n", strerror(errno)); - record_log(err_str); - return; - } -} - -void release_sem(int semid) -{ - int ret; - char err_str[200]; - struct sembuf unlock; - - unlock.sem_num = 0; - unlock.sem_op = 1; - unlock.sem_flg = SEM_UNDO; - - if((ret = semop(semid, &unlock, 1)) == -1){ - snprintf(err_str, sizeof(err_str), "EMERG: semop():\n", strerror(errno)); - record_log(err_str); - return; - } -} - - -int init_nic_info(void) -{ - int fd = -1; - int sem_val; - char err_str[200]; - key_t key; - - - if ((key=ftok(SEM_PATH, SEM_PROJ_ID)) == -1) { - snprintf(err_str, sizeof(err_str), "EMERG: ftok():%s\n", strerror(errno)); - record_log(err_str); - return -1; - } - if((semid = semget(key, 1, IPC_CREAT|0666)) == -1){ - snprintf(err_str, sizeof(err_str), "EMERG: Create semaphore error: %s\n", strerror(errno)); - record_log(err_str); - return -1; - } - if((sem_val = semctl(semid, 0, GETVAL, 0)) == -1){ - snprintf(err_str, sizeof(err_str), "EMERG: semctl: %s !\n", strerror(errno)); - record_log(err_str); - semctl(semid, 0, IPC_RMID); - return -1; - } - if(!sem_val){ - if(semctl(semid, 0, SETVAL, 1) == -1){ - snprintf(err_str, sizeof(err_str), "EMERG: semctl: %s !\n", strerror(errno)); - record_log(err_str); - semctl(semid, 0, IPC_RMID); - return -1; - } - } - if((fd = open(SHM_PATH, O_RDONLY)) == -1){ - snprintf(err_str, sizeof(err_str), "EMERG: Error open %s: %s!\n", SHM_PATH, strerror(errno)); - record_log(err_str); - return -1; - } - ptr = (SHM*)mmap(NULL, sizeof(SHM), PROT_READ, MAP_SHARED, fd, 0); - if(ptr == MAP_FAILED){ - snprintf(err_str, sizeof(err_str), "EMERG: mmap():%s\n", strerror(errno)); - record_log(err_str); - return -1; - } - close(fd); - - return 0; -} -int get_nic_info(char *nic_name, NETCARD_INFO *net_info) -{ - int i,ret; - time_t tamp; - struct tm tmptr; - DIR *dir; - char *s; - char buf[128]; - char err_str[200]; - - tamp = time(NULL); - localtime_r(&tamp, &tmptr); - strcpy(log_path, LOG_PATH); - if((dir = opendir(log_path)) == NULL){ - if(errno == ENOENT){ - strcpy(log_path, "/tmp/"); - } - } - closedir(dir); - s = strrchr(log_path, '/'); - s++; - *s = '\0'; - memset(buf, 0, sizeof(buf)); - strftime(buf, sizeof(buf), "%Y%m%d_", &tmptr); - strcat(buf, "sys_nicmonitor"); - strcat(buf, ".log"); - strcat(log_path, buf); - if((ret = init_nic_info()) == -1){ - return -1; - } - for(i = 0; ptr->info[i].charname[0] != 0; i++){ - if(!strcmp(ptr->info[i].charname, nic_name)){ - get_sem(semid); - memcpy(net_info, &ptr->info[i], sizeof(NETCARD_INFO)); - release_sem(semid); - munmap(ptr, sizeof(SHM)); - return 0; - } - } - snprintf(err_str, sizeof(err_str), "NOTICE: No information of %s !\n", nic_name); - record_log(err_str); - munmap(ptr, sizeof(SHM)); - return -1; -} diff --git a/code/branches/sys_nicmonitor-1.0/nicinfo_shm.h b/code/branches/sys_nicmonitor-1.0/nicinfo_shm.h deleted file mode 100644 index cd74135..0000000 --- a/code/branches/sys_nicmonitor-1.0/nicinfo_shm.h +++ /dev/null @@ -1,52 +0,0 @@ -#ifndef NIC_SHM_H -#define NIC_SHM_H - -#include - -#ifdef __cplusplus -extern "C" { -#endif - -#ifndef NIC_NAME_LEN -#define NIC_NAME_LEN 64 -#endif -#define SEM_PROJ_ID 's' -#define MAXNICNUM 32 - -typedef long KEYID; -typedef struct _net_info -{ - KEYID ID; - char charname[NIC_NAME_LEN]; - char descr[128]; - struct sockaddr addr; - struct sockaddr broadaddr; - struct sockaddr netmask; - struct sockaddr hwaddr; - int time_stamp; - short flags; - int mtu; - int tx_queue_len; - unsigned long long average_flow; - unsigned long long rx_packets; - unsigned long long tx_packets; - unsigned long long rx_bytes; - unsigned long long tx_bytes; - unsigned long rx_errors; - unsigned long tx_errors; - unsigned long rx_dropped; - unsigned long tx_dropped; - unsigned long rx_multicast; - unsigned long collisions; - unsigned long rx_fifo_errors; - unsigned long tx_carrier_errors; - unsigned long tx_fifo_errors; -}NETCARD_INFO; - -int get_nic_info(char *nic_name, NETCARD_INFO *net_info); - - -#ifdef __cplusplus -} -#endif -#endif diff --git a/code/branches/sys_nicmonitor-1.0/proc_inv.h b/code/branches/sys_nicmonitor-1.0/proc_inv.h deleted file mode 100644 index 945622c..0000000 --- a/code/branches/sys_nicmonitor-1.0/proc_inv.h +++ /dev/null @@ -1,198 +0,0 @@ -//////////////////////////////////////////////////////////////////////////////////// -// -// Copyright (c) Comets' Grp. of Kedong Corp 2008. All Rights Reserved. -// -// FileName : procconf.h -// -// Function : this class realize some basic functions for process managerment, -// such as initiate process, report status of process, check status of process, -// update status of process, get information of process -// -// Author : -// -// Date : -// -// Modify by : -// -// Mod Date : -// -//////////////////////////////////////////////////////////////////////////////////// - -#ifndef _PROCCONF_H -#define _PROCCONF_H - -#include -#include -#include -#include -#include -#include -#include -//#include -#include - -#include "const.h" - -//for alarm ................................ -typedef struct MESS_BH -{ - unsigned char mtype; - int length; -}; -typedef struct PROCESS_ALM -{ - char context_name[MAX_STRING_LEN]; - char app_name[MAX_STRING_LEN]; - char proc_name[MAX_STRING_LEN]; - unsigned char status; -}; -//for alarm end ............................. - -//for mmi.................................... -const int MAX_BUFFER_LEN = 500; -typedef struct MESS_BLOCK -{ - unsigned char num; - char buffer[MAX_BUFFER_LEN]; -}; -typedef struct MESS_PROC -{ - char context_name[MAX_STRING_LEN]; - char app_name[MAX_STRING_LEN]; - char proc_name[MAX_STRING_LEN]; - char status; -}; -//for mmi end ................................ - -const int DEFAULT_PERIOD = 3; -const int COUNT_LIMIT = 2; -const int START_DEFAULT_PERIOD = 60; -const int START_COUNT_LIMIT = 5; -const int APP_COUNT_LIMIT = 1; - -//process critical level -//const int WST_CRITICAL = 1; // Shutdown and reboot workstation when failed -//const int SYS_CRITICAL = 2; // Shutdown and reboot the system on the wst when failed -//const int USER_CRITICAL = 3; // Shutdown and reboot the subsystem when failed -//const int GENERAL = 4; // reboot the process -const int CRUCIAL = 1; // crucial process -const int GENERAL = 0; // general process - - -extern int srv_init(char *service,int port); -extern int Tcp_close(int sockfd); -extern int Tcp_read(int fd,char *ptr,int nbytes); -extern int Tcp_write(int fd,char *ptr,int nbytes); -extern int srv_accept(int fd,struct sockaddr *cli_addr,int *clilen); -extern int client_tcp_open(char *host,char *service,int port); - -typedef struct -{ - char context_name[MAX_STRING_LEN]; - char app_name[MAX_STRING_LEN]; - char proc_name[MAX_STRING_LEN]; - pid_t proc_pid; -}PROC_ADM_INFO; - -//*************************************************************** -// structure name : PROC_INFO -// function : store process informatin -// author : -// date : -// modify by : -// modification : -// mod date : -//*************************************************************** -typedef struct -{ - int position; - char node_name[MAX_STRING_LEN]; - char context_name[MAX_STRING_LEN]; - char app_name[MAX_STRING_LEN]; - char proc_name[MAX_STRING_LEN]; - - unsigned char active_flag; - unsigned char master_flag; - - time_t startup_time; - time_t refresh_time; - short refresh_peri; - unsigned char monitor_type; - - pid_t proc_pid; - unsigned char auto_start; - unsigned char act_timer; - unsigned char start_timer; - unsigned char critical_level; - - char exefile_path[MAX_EXECMD_LEN]; - -}PROC_INFO; - -//*************************************************************** -// structure name : APP_INFO -// function : store application informatin -// author : -// date : -// modify by : -// modification : -// mod date : -//*************************************************************** -typedef struct -{ - int position; - char context_name[MAX_STRING_LEN]; - int context_id; - char app_name[MAX_STRING_LEN]; - int app_id; - unsigned char act_timer; - unsigned char active_flag; -}APP_INFO; - - -typedef struct -{ - int no_proc; - int semdes_cfg; - PROC_INFO proc[MAX_LOCAL_PROCESS]; - APP_INFO app[MAX_LOCAL_APP]; -}PROCCFG; - -class proc_invocation -{ - public: - int m_init; - PROCCFG *proccfg_p; - - public: - proc_invocation(); - ~proc_invocation(); - - int conf_create(); - //int check_proc_status(); - //int update_rtdb(); - //int kill_proc(pid_t pid); - //int start_proc(char *cmdline); - //int send_alarm(char *context_name, char *app_name, char *proc_name, unsigned char status); - //int update_proc_status(char *context_name, char *app_name, char *proc_name, char status); - - //int proc_init(char *context_name, char *app_name, char *proc_name, int critical_level);//exefile_path,auto_start - int proc_init(char *context_name, char *app_name, char *proc_name); - int proc_report(int pos, char status, int intertime=3); - int proc_exit(int proc_pos); - - int get_pos(char *context_name, char *app_name, char *proc_name); - int is_proc_exist(char *context_name, char *app_name, char *proc_name); - int conf_map(); - - int get_procinfo(int position, PROC_ADM_INFO *p_info); - int get_active_pid(int &num, int *p_pidlist); - int is_proc_run(pid_t pid); - int is_proc_run(char *context_name, char *app_name, char *proc_name); - -}; - -#endif - - - diff --git a/code/branches/sys_nicmonitor-1.0/read_netcard.c b/code/branches/sys_nicmonitor-1.0/read_netcard.c deleted file mode 100644 index c745f48..0000000 --- a/code/branches/sys_nicmonitor-1.0/read_netcard.c +++ /dev/null @@ -1,133 +0,0 @@ -#include "mnic.h" - -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; -} - - -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 == '\0') - return NULL; - p++; - break; - } - *name++ = *p++; - } - *name++ = '\0'; - return p; -} - -int get_dev_fields(char *str, NETCARD_INFO *ife) -{ - 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]); - return 0; -} - -int if_fetch(NETCARD_INFO *ife) -{ - int skfd; - struct ifreq ifr; - - if((skfd = socket(PF_INET, SOCK_DGRAM, 0)) == -1)return -1; - - strcpy(ifr.ifr_name, ife->charname); - if (!ioctl(skfd, SIOCGIFFLAGS, &ifr)) - ife->flags = ifr.ifr_flags; - - if (!ioctl(skfd, SIOCGIFHWADDR, &ifr)) - ife->hwaddr = ifr.ifr_hwaddr; - - if (!ioctl(skfd, SIOCGIFMTU, &ifr)) - ife->mtu = ifr.ifr_mtu; - - if (!ioctl(skfd, SIOCGIFTXQLEN, &ifr)) - ife->tx_queue_len = ifr.ifr_qlen; - - if (!ioctl(skfd, SIOCGIFADDR, &ifr)) - ife->addr = ifr.ifr_addr; - - if (!ioctl(skfd, SIOCGIFBRDADDR, &ifr)) - ife->broadaddr = ifr.ifr_broadaddr; - - if (!ioctl(skfd, SIOCGIFNETMASK, &ifr)) - ife->netmask = ifr.ifr_netmask; - - ife->time_stamp = time(NULL); - close(skfd); - return 0; -} - - -int ioc_get_name(NETCARD_INFO *name) -{ - int skfd; - int ifrnum; - int i = 0; - struct ifconf ifc; - struct ifreq buf[MAXNICNUM]; - - if((skfd = socket(PF_INET, SOCK_DGRAM, 0)) == -1)return -1; - memset(buf, 0, sizeof(buf)); - ifc.ifc_len = sizeof(buf); - ifc.ifc_buf = (caddr_t) buf; - ioctl(skfd, SIOCGIFCONF, &ifc); - ifrnum = ifc.ifc_len / sizeof(struct ifreq); - while(ifrnum-- > 0){ - strcpy(name[i].charname, buf[ifrnum].ifr_name); - i++; - } - close(skfd); - return i; -} - diff --git a/code/branches/sys_nicmonitor-1.0/send_alarm.c b/code/branches/sys_nicmonitor-1.0/send_alarm.c deleted file mode 100644 index e88635f..0000000 --- a/code/branches/sys_nicmonitor-1.0/send_alarm.c +++ /dev/null @@ -1,37 +0,0 @@ -#include "mnic.h" - -void send_alarm(D5000_NIC_ALARM *mesg, int socket_port, char *ipv4) -{ - int bytes; - int sock_sd; - int val; - char error_str[200]; - struct sockaddr_in recever; - socklen_t sock_len; - - if((sock_sd = socket(PF_INET, SOCK_DGRAM, IPPROTO_UDP)) == -1){ - snprintf(error_str, sizeof(error_str), "EMERG: socket(): %s\n", strerror(errno)); - record_log(error_str); - return; - } - val=1; - if ((setsockopt(sock_sd, SOL_SOCKET, SO_BROADCAST, &val, sizeof(val))) == -1) { - snprintf(error_str, sizeof(error_str), "EMERG: setsockopt(): %s\n", strerror(errno)); - record_log(error_str); - return; - } - /* init socket*/ - recever.sin_family = AF_INET; - recever.sin_port = htons(socket_port); - inet_pton(AF_INET, ipv4, &recever.sin_addr); - sock_len = sizeof(recever); - /* send alarm*/ - if((bytes = sendto(sock_sd, mesg, sizeof(D5000_NIC_ALARM), 0, (const struct sockaddr *)&recever, sock_len)) < 1){ - snprintf(error_str, sizeof(error_str), "EMERG: sendto(): %s\n", strerror(errno)); - record_log(error_str); - return; - } - //snprintf(error_str, sizeof(error_str), "NOTICE: Alarm information has already send!\n"); - //record_log(error_str); - close(sock_sd); -} diff --git a/code/branches/sys_nicmonitor-1.0/sys_netcard.h b/code/branches/sys_nicmonitor-1.0/sys_netcard.h deleted file mode 100644 index 0d7746c..0000000 --- a/code/branches/sys_nicmonitor-1.0/sys_netcard.h +++ /dev/null @@ -1,88 +0,0 @@ -#ifndef _SYS_NETCARD_H -#define _SYS_NETCARD_H - -#include - -#ifndef NIC_NAME_LEN -#define NIC_NAME_LEN 64 -#endif - -#define SEM_PROJ_ID 's' -#define MAXNICNUM 32 - -#ifdef __cplusplus -extern "C"{ -#endif - -typedef long KEYID; - -typedef struct _msg_frame // message frame -{ - short len; // message length - short seqno; // send sequence - short serv; // services ID - short event; // event ID - unsigned char domain; // domain ID - unsigned char ctxt; // Context ID - short stid; // SOURCE task id - short dtid; // DESTINATION task ID - unsigned char ver_coding; // 版本号 + 编码 - unsigned char mes_type; // 帧类型 -}MSG_FRAME, *LPMSG_FRAME; - -typedef struct _net_info -{ - KEYID ID; - char charname[NIC_NAME_LEN]; - char descr[128]; - struct sockaddr addr; - struct sockaddr broadaddr; - struct sockaddr netmask; - struct sockaddr hwaddr; - int time_stamp; - short flags; - int mtu; - int tx_queue_len; - unsigned long long average_flow; - unsigned long long rx_packets; - unsigned long long tx_packets; - unsigned long long rx_bytes; - unsigned long long tx_bytes; - unsigned long rx_errors; - unsigned long tx_errors; - unsigned long rx_dropped; - unsigned long tx_dropped; - unsigned long rx_multicast; - unsigned long collisions; - unsigned long rx_fifo_errors; - unsigned long tx_carrier_errors; - unsigned long tx_fifo_errors; -}NETCARD_INFO, *LPNETCARD_INFO; - -#define NETCARD_ALARM_FAULT 1 -#define NETCARD_ALARM_RESUME 2 -#define NETCARD_ALARM_SWITCH 3 -#define NETCARD_ALARM_ABNORMAL 4 -#define NETCARD_ALARM_NORMAL 5 -typedef struct _sys_netcard_alarm -{ - char fault_devname[NIC_NAME_LEN]; /*故障设备名称 */ - char switch_devname[NIC_NAME_LEN]; /*切换设备名称 */ - short flags; /* 状态标记 : 1:网卡故障。2:网卡恢复。3:网卡切换。4:流量异常。*/ - short retrytimes; //0,1,2 重发次数 -}SYS_NETCARD_ALARM, *LPSYS_NETCARD_ALARM; - - -typedef struct _d5000_nic_alarm -{ - MSG_FRAME tMsgFrame ; - SYS_NETCARD_ALARM tSysNetcardAlarm ; -}D5000_NIC_ALARM, *LPD5000_NIC_ALARM; - - -#ifdef __cplusplus -} -#endif - -#endif - diff --git a/code/trunk/sys_nicmonitor/src/build.sh b/code/build.sh similarity index 100% rename from code/trunk/sys_nicmonitor/src/build.sh rename to code/build.sh diff --git a/code/trunk/sys_nicmonitor/src/dep/dotconf#1.0.13-1.pkg.tar.gz b/code/dep/dotconf#1.0.13-1.pkg.tar.gz similarity index 100% rename from code/trunk/sys_nicmonitor/src/dep/dotconf#1.0.13-1.pkg.tar.gz rename to code/dep/dotconf#1.0.13-1.pkg.tar.gz diff --git a/code/tags/libman.so b/code/dep/libman.so similarity index 100% rename from code/tags/libman.so rename to code/dep/libman.so diff --git a/code/trunk/sys_nicmonitor/src/dep/run.sh b/code/dep/run.sh similarity index 100% rename from code/trunk/sys_nicmonitor/src/dep/run.sh rename to code/dep/run.sh diff --git a/code/tags/sys_nicmonitor-1.8/src/post_add.sh b/code/post_add.sh similarity index 100% rename from code/tags/sys_nicmonitor-1.8/src/post_add.sh rename to code/post_add.sh diff --git a/code/tags/sys_nicmonitor-1.8/src/post_mk.sh b/code/post_mk.sh similarity index 100% rename from code/tags/sys_nicmonitor-1.8/src/post_mk.sh rename to code/post_mk.sh diff --git a/code/trunk/sys_nicmonitor/src/sys_nicmonitor/Makefile b/code/sys_nicmonitor/Makefile similarity index 100% rename from code/trunk/sys_nicmonitor/src/sys_nicmonitor/Makefile rename to code/sys_nicmonitor/Makefile diff --git a/branches/sys_nicmonitor-1.4-龙芯-32/Makefile.config b/code/sys_nicmonitor/Makefile.config similarity index 100% rename from branches/sys_nicmonitor-1.4-龙芯-32/Makefile.config rename to code/sys_nicmonitor/Makefile.config diff --git a/code/trunk/sys_nicmonitor/src/sys_nicmonitor/README b/code/sys_nicmonitor/README similarity index 100% rename from code/trunk/sys_nicmonitor/src/sys_nicmonitor/README rename to code/sys_nicmonitor/README diff --git a/code/trunk/sys_nicmonitor/src/sys_nicmonitor/common.c b/code/sys_nicmonitor/common.c similarity index 100% rename from code/trunk/sys_nicmonitor/src/sys_nicmonitor/common.c rename to code/sys_nicmonitor/common.c diff --git a/branches/sys_nicmonitor-1.4-龙芯-32/const.h b/code/sys_nicmonitor/const.h similarity index 100% rename from branches/sys_nicmonitor-1.4-龙芯-32/const.h rename to code/sys_nicmonitor/const.h diff --git a/code/trunk/sys_nicmonitor/src/sys_nicmonitor/mnic.c b/code/sys_nicmonitor/mnic.c similarity index 100% rename from code/trunk/sys_nicmonitor/src/sys_nicmonitor/mnic.c rename to code/sys_nicmonitor/mnic.c diff --git a/branches/sys_nicmonitor-1.4-龙芯-32/mnic.h b/code/sys_nicmonitor/mnic.h similarity index 100% rename from branches/sys_nicmonitor-1.4-龙芯-32/mnic.h rename to code/sys_nicmonitor/mnic.h diff --git a/code/trunk/sys_nicmonitor/src/sys_nicmonitor/nicinfo_shm.c b/code/sys_nicmonitor/nicinfo_shm.c similarity index 100% rename from code/trunk/sys_nicmonitor/src/sys_nicmonitor/nicinfo_shm.c rename to code/sys_nicmonitor/nicinfo_shm.c diff --git a/branches/sys_nicmonitor-1.4-龙芯-32/nicinfo_shm.h b/code/sys_nicmonitor/nicinfo_shm.h similarity index 100% rename from branches/sys_nicmonitor-1.4-龙芯-32/nicinfo_shm.h rename to code/sys_nicmonitor/nicinfo_shm.h diff --git a/code/trunk/sys_nicmonitor/src/sys_nicmonitor/proc_inv.h b/code/sys_nicmonitor/proc_inv.h similarity index 100% rename from code/trunk/sys_nicmonitor/src/sys_nicmonitor/proc_inv.h rename to code/sys_nicmonitor/proc_inv.h diff --git a/code/trunk/sys_nicmonitor/src/sys_nicmonitor/read_netcard.c b/code/sys_nicmonitor/read_netcard.c similarity index 100% rename from code/trunk/sys_nicmonitor/src/sys_nicmonitor/read_netcard.c rename to code/sys_nicmonitor/read_netcard.c diff --git a/branches/sys_nicmonitor-1.4-龙芯-32/send_alarm.c b/code/sys_nicmonitor/send_alarm.c similarity index 100% rename from branches/sys_nicmonitor-1.4-龙芯-32/send_alarm.c rename to code/sys_nicmonitor/send_alarm.c diff --git a/branches/sys_nicmonitor-1.4-龙芯-32/sys_netcard.h b/code/sys_nicmonitor/sys_netcard.h similarity index 100% rename from branches/sys_nicmonitor-1.4-龙芯-32/sys_netcard.h rename to code/sys_nicmonitor/sys_netcard.h diff --git a/code/trunk/sys_nicmonitor/src/sys_nicmonitor/test.c b/code/sys_nicmonitor/test.c similarity index 100% rename from code/trunk/sys_nicmonitor/src/sys_nicmonitor/test.c rename to code/sys_nicmonitor/test.c diff --git a/code/trunk/sys_nicmonitor/src/sys_nicmonitor/testread.c b/code/sys_nicmonitor/testread.c similarity index 100% rename from code/trunk/sys_nicmonitor/src/sys_nicmonitor/testread.c rename to code/sys_nicmonitor/testread.c diff --git a/code/trunk/sys_nicmonitor/src/sys_nicmonitor/version.h b/code/sys_nicmonitor/version.h similarity index 100% rename from code/trunk/sys_nicmonitor/src/sys_nicmonitor/version.h rename to code/sys_nicmonitor/version.h diff --git a/code/tags/README1.5 b/code/tags/README1.5 deleted file mode 100644 index fa1087e..0000000 --- a/code/tags/README1.5 +++ /dev/null @@ -1,32 +0,0 @@ - -lijin:/home/d5000/lijin/sys_nicmonitor # ./sys_nicmonitor -shm_path:/home/d5000/tmp/sys_netcard_shm_path -log_path:/home/d5000/var/log/netcard/20110110_sys_nicmonitor.log -udp    bond0 -nic    bond0 -nic    eth0 -nic    eth1 - -d5000@lijin:~ > cat var/log/netcard/20110110_sys_nicmonitor.log -2011-01-10 11:10:37.761 sys_nicmonitor NOTICE: sys_nicmonitor V1.5 is -started ! -2011-01-10 11:10:37.761 sys_nicmonitor -shm_path:/home/d5000/tmp/sys_netcard_shm_path -2011-01-10 11:10:37.761 sys_nicmonitor -log_path:/home/d5000/var/log/netcard/20110110_sys_nicmonitor.log -2011-01-10 11:10:37.761 sys_nicmonitor udp    bond0 -2011-01-10 11:10:37.761 sys_nicmonitor nic    bond0 -2011-01-10 11:10:37.761 sys_nicmonitor nic    eth0 -2011-01-10 11:10:37.762 sys_nicmonitor nic    eth1 -2011-01-10 11:10:37.762 sys_nicmonitor NOTICE: bond0 is NORMAL! -2011-01-10 11:10:37.763 sys_nicmonitor NOTICE: eth0 is NORMAL! -2011-01-10 11:10:37.763 sys_nicmonitor NOTICE: eth1 is NORMAL! - -与1.4不一样的地方在与增加log文件中的打印语句 -sys_nicmonitor started! version v1.4 -shm_path: /home/d5000/tmp -log_path: /home/d5000/ var/log/netcard -udp bond0 -nic bond0 -nic eth0 -nic eth1 diff --git a/code/tags/README1.6 b/code/tags/README1.6 deleted file mode 100644 index da22610..0000000 --- a/code/tags/README1.6 +++ /dev/null @@ -1,2 +0,0 @@ -V1.5与V1.6差异 -将ping_gw中的system调用改为popen减少系统误报 diff --git a/code/tags/README1.7 b/code/tags/README1.7 deleted file mode 100644 index b2afb8c..0000000 --- a/code/tags/README1.7 +++ /dev/null @@ -1,15 +0,0 @@ -V1.6与V1.7差异 -共享内存路径修改: -由/home/d5000/someone/tmp/sys_netcard_shm_path 改为 /home/d5000/someone/share/sys_netcard_shm_path -信号量路径修改: -由/home/d5000/someone/tmp/sys_netcard_sem_path 改为 /home/d5000/someone/share/sys_netcard_sem_path - -注意:在编译过程中,需要动态看libman.so,它存放在目录 tags/下面 ,在进行编译的时候需要将其拷贝到 -/lib64/目录下,编译的基础环境是 Rocky 4.2 系统。 - -pkgmk命令的使用: -将源代码打包成name-version.tar.gz样式,编写Pkgfile文件。 -使用命令: - pkgmk -um #更新软件包的md5值 - pkgmk -kw #制作pkg安装包 - diff --git a/code/tags/sys_nicmonitor-1.0.0/Makefile b/code/tags/sys_nicmonitor-1.0.0/Makefile deleted file mode 100644 index 5cccc77..0000000 --- a/code/tags/sys_nicmonitor-1.0.0/Makefile +++ /dev/null @@ -1,28 +0,0 @@ -#! /usr/bin/make -f - -DEFAULTS = Makefile.config - -include $(DEFAULTS) - -all:sys_nicmonitor libnic_shm.so - -CFLAGS += -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 $@ $^ -libnic_shm.so:nicinfo_shm.c - $(CC) -fpic -shared -o $@ $^ -clean: - $(RM) *.o - $(RM) libnic_shm.so sys_nicmonitor -install: - $(MKDIR) -p $(LIBDIR) - $(MKDIR) -p $(BINDIR) - $(MKDIR) -p $(INCDIR) - $(CP) libnic_shm.so $(LIBDIR) - $(CP) sys_nicmonitor $(BINDIR) - $(CP) nicinfo_shm.h $(INCDIR) -uninstall: - $(RM) -f $(LIBDIR)/libnic_shm.so - $(RM) -f $(BINDIR)/sys_nicmonitor - $(RM) -f $(INCDIR)/nicinfo_shm.h diff --git a/code/tags/sys_nicmonitor-1.0.0/Makefile.config b/code/tags/sys_nicmonitor-1.0.0/Makefile.config deleted file mode 100644 index 7a2d8e6..0000000 --- a/code/tags/sys_nicmonitor-1.0.0/Makefile.config +++ /dev/null @@ -1,16 +0,0 @@ - -PREFIX = $(DESTDIR) - -BINDIR = $(PREFIX)/usr/bin - -LIBDIR = $(PREFIX)/usr/lib64 - -INCDIR = $(PREFIX)/usr/include - -INSTALL_SH = ./install.sh - -CP = cp -RM = rm -#CC = gcc -CC = g++ -MKDIR = mkdir diff --git a/code/tags/sys_nicmonitor-1.0.0/const.h b/code/tags/sys_nicmonitor-1.0.0/const.h deleted file mode 100644 index e2ebfa5..0000000 --- a/code/tags/sys_nicmonitor-1.0.0/const.h +++ /dev/null @@ -1,51 +0,0 @@ -#ifndef _RTE_CONST_H -#define _RTE_CONST_H - -#include - - -#define MAX_STRING_LEN 24 -#define MAX_EXECMD_LEN 200 -#define MAX_FILENAME_LEN 200 - -#define MAX_CONTEXT 8 -#define MAX_HOSTNAME_LEN 24 -#define MAX_LOCAL_MESPROC 512 -#define MAX_LOCAL_PROCESS MAX_LOCAL_MESPROC -#define MAX_LOCAL_APP 32 -#define MAX_LOCAL_NODE 256 - -#define MAX_SET 256 //max num of event set -#define MAX_EVENT 1300 -#define MAX_REG_PROC 20 - -#define LEN_SHMBLK_BIG 4096 -#define MAX_MSGBUF_LEN 32767 -#define FREE_PAGE_SIZE 65536 -#define MAX_PAGE_NUM 1024 - -#define MAX_QUE MAX_LOCAL_MESPROC -#define MAX_SEMPHORE_SET MAX_LOCAL_MESPROC*2 -#define RTE_HAN 0 // queue number for event handeler0(RTE) -#define EX_EVENT_HAN RTE_HAN // queue number for extrnal event handeler - -#define DOMAIN_I 1 -#define DOMAIN_II 2 -#define DOMAIN_III 3 - -//Add 20090225 -const int PROC_TYPE_RPT =1; -const int PROC_TYPE_UNRPT =0; -//add end - - -//status for proc and app -const int NON_ACTIVE = 0; -const int ACTIVE = 1; -const int HANGUP = 2; -const int FAILURE = 5; -const int START = 6; -const int STOP = 7; - -#endif - diff --git a/code/tags/sys_nicmonitor-1.0.0/mnic.c b/code/tags/sys_nicmonitor-1.0.0/mnic.c deleted file mode 100644 index 8491e73..0000000 --- a/code/tags/sys_nicmonitor-1.0.0/mnic.c +++ /dev/null @@ -1,1430 +0,0 @@ -#include "mnic.h" -#include "dotconf.h" -#include -#include -#include "proc_inv.h" - -#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 "/tmp/sys_netcard_shm_path" -#define SEM_PATH "/tmp/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.2" - -#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. -}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 find_nic_config(char *); - -static pthread_mutex_t mut = PTHREAD_MUTEX_INITIALIZER; -static short seqno = 1; -static char log_flag = 1; -static int sem_id = 0; -static char process_name[64]; -char log_path[1024]; - -int host_name_size = 0; -char host_name[HOST_NAME_SIZE]; -int semid = -1; -SHM *shm_ptr; - -//20100510 -int total_ping_time, elapsed_ping_time; -int get_netcard_status(NETCARD_INFO_ST *); - -/****dotconf****/ -static DOTCONF_CB(cb_str); -static DOTCONF_CB(cb_int); -int nicnum = 0; -CONFIG_FILE_ST conf; - -char myconf[][64] = { - "domain", // 0 - "serv", // 1 - "event", // 2 - "udpport", // 3 - "sys_netcard_shm_path", // 4 - "sys_netcard_sem_path", // 5 - "monitor_interval", // 6 - "write_interval", // 7 - "flow_interval", // 8 - "flow_limit", // 9 - "flow_peak", // 10 - "udp", // 11 - "nic", // 12 - "ping", // 13 - "pingnum", // 14 - "pinglap", // 15 - "" -}; - -static const configoption_t options[] = { - {myconf[0], ARG_INT, cb_int, NULL, CTX_ALL}, - {myconf[1], ARG_INT, cb_int, NULL, CTX_ALL}, - {myconf[2], ARG_INT, cb_int, NULL, CTX_ALL}, - {myconf[3], ARG_INT, cb_int, NULL, CTX_ALL}, - {myconf[4], ARG_STR, cb_str, NULL, CTX_ALL}, - {myconf[5], ARG_STR, cb_str, NULL, CTX_ALL}, - {myconf[6], ARG_INT, cb_int, NULL, CTX_ALL}, - {myconf[7], ARG_INT, cb_int, NULL, CTX_ALL}, - {myconf[8], ARG_INT, cb_int, NULL, CTX_ALL}, - {myconf[9], ARG_INT, cb_int, NULL, CTX_ALL}, - {myconf[10], ARG_INT, cb_int, NULL, CTX_ALL}, - {myconf[11], ARG_STR, cb_str, NULL, CTX_ALL}, - {myconf[12], ARG_STR, cb_str, NULL, CTX_ALL}, - {myconf[13], ARG_STR, cb_str, NULL, CTX_ALL}, - {myconf[14], ARG_INT, cb_int, NULL, CTX_ALL}, - {myconf[15], ARG_INT, cb_int, NULL, CTX_ALL}, - LAST_OPTION -}; -/****dotconf****/ - -/* write error in logfile */ -void record_log(char *str) -{ - time_t tamp; - char str_tm[4]; - char log_str[512]; - FILE *log_fp = NULL; - struct tm tmptr; - struct timeval tv; - struct timezone tz; - - if((log_fp = fopen(log_path, "a")) != NULL){ - tamp = time(NULL); - memset(str_tm, 0, sizeof(str_tm)); - memset(log_str, 0, sizeof(log_str)); - localtime_r(&tamp, &tmptr); - gettimeofday(&tv, &tz); - snprintf(str_tm, sizeof(str_tm), "%d", (int)tv.tv_usec/1000); - if(str_tm[1] == '\0')str_tm[1] = '0'; - if(str_tm[2] == '\0')str_tm[2] = '0'; - strftime(log_str, sizeof(log_str), "%F %T.", &tmptr); - strcat(log_str, str_tm); - strcat(log_str, " "); - strcat(log_str, process_name); - strcat(log_str, " "); - strcat(log_str, str); - if(fwrite(log_str, 1, strlen(log_str), log_fp) < 1){ - } - fclose(log_fp); - } -} - -static void init_sem(int *semid, CONFIG_FILE_ST *ptr) -{ - key_t key; - char error_str[200]; - -/* write error in logfile */ - memset(error_str, 0, sizeof(error_str)); - if ((key = ftok(SEM_PATH, SEM_PROJ_ID)) == -1) { - snprintf(error_str, sizeof(error_str), "EMERG: ftok():%s\n", strerror(errno)); - record_log(error_str); - return; - } - if((*semid = semget(key, 1, IPC_CREAT|0666)) == -1){ - snprintf(error_str, sizeof(error_str), "EMERG: semget():%s\n", strerror(errno)); - record_log(error_str); - return; - } - if(semctl(*semid, 0, SETVAL, 1) == -1){ - snprintf(error_str, sizeof(error_str), "EMERG: semctl: %s !\n", strerror(errno)); - record_log(error_str); - semctl(*semid, 0, IPC_RMID); - return; - } -} - -static void init_shm(SHM **ptr, CONFIG_FILE_ST *p) -{ - int fd = -1; - char error_str[200]; - - mode_t mask_tmp; - mask_tmp = umask(0000); - if((fd = open(SHM_PATH, O_RDWR|O_CREAT|O_TRUNC, 0666)) == -1){ - printf("could not create %s\n", SHM_PATH); - return; - } - umask(mask_tmp); - lseek(fd, sizeof(SHM), SEEK_SET); - write(fd, "\0", 1); - *ptr = (SHM *)mmap(NULL, sizeof(SHM), PROT_READ|PROT_WRITE, MAP_SHARED, fd, 0); - if(*ptr == MAP_FAILED){ - snprintf(error_str, sizeof(error_str), "EMERG: mmap():%s\n", strerror(errno)); - record_log(error_str); - return; - } - close(fd); - -} - -static void get_sem(int semid) -{ - struct sembuf lock; - char error_str[200]; - - lock.sem_num = 0; - lock.sem_op = -1; - lock.sem_flg = SEM_UNDO; - - while(semop(semid, &lock, 1)){ - if(errno == EINTR)continue; - snprintf(error_str, sizeof(error_str), "EMERG: get_sem semop():%s\n", strerror(errno)); - record_log(error_str); - return; - } -} - -static void release_sem(int semid) -{ - int ret; - char error_str[200]; - struct sembuf unlock; - - unlock.sem_num = 0; - unlock.sem_op = 1; - unlock.sem_flg = SEM_UNDO; - - if((ret = semop(semid, &unlock, 1)) == -1){ - snprintf(error_str, sizeof(error_str), "EMERG: rel_sem semop():%s\n", strerror(errno)); - record_log(error_str); - return; - } -} - -static void proc_get_name(NET_NAME_ST *nodep) -{ - char linebuf[LINE_SIZE]; - char error_str[200]; - char *retp = NULL; - int err_val=0; - NET_NAME_ST *node = NULL; - FILE *dev_fp; - - if((dev_fp = fopen("/proc/net/dev", "rb")) == NULL){ - snprintf(error_str, sizeof(error_str), "EMERG: Can't open dev file:%s\n", strerror(errno)); - record_log(error_str); - return; - } - memset(linebuf, 0, sizeof(linebuf)); - fgets(linebuf, sizeof(linebuf), dev_fp); - fgets(linebuf, sizeof(linebuf), dev_fp); - while((retp = fgets(linebuf, sizeof(linebuf), dev_fp)) != NULL){ - if((node = (NET_NAME_ST *)malloc(sizeof(NET_NAME_ST))) == NULL){ - err_val = 1; - break; - } - memset(node->name, 0, NIC_NAME_LEN); - get_name(node->name, retp); - node->next = nodep->next; - nodep->next = node; - } - if(err_val){ - NET_NAME_ST *curr = nodep->next; - while(curr != NULL){ - node = curr->next; - free(curr); - curr = node; - } - } - fclose(dev_fp); -} - -static void ioctl_get_name(NET_NAME_ST *nodep) -{ - int count = 0; - int inuse = 0; - int err_val = 0; - NETCARD_INFO net_name[MAXNICNUM]; - NET_NAME_ST *node = NULL, *curr = NULL; - - memset(net_name, 0, sizeof(net_name)); - if((count = ioc_get_name(net_name)) == 0)return; - if(nodep->next == NULL) - inuse = 1; - while(count > 0){ - curr = nodep->next; - while(curr != NULL){ - if(!strncmp(net_name[count - 1].charname, curr->name, NIC_NAME_LEN)){ - inuse = 0; - break; - } - inuse = 1; - curr = curr->next; - } - if(inuse){ - if((node = (NET_NAME_ST *)malloc(sizeof(NET_NAME_ST))) == NULL){ - err_val = 1; - break; - } - memset(node->name, 0, NIC_NAME_LEN); - strncpy(node->name, net_name[count - 1].charname, NIC_NAME_LEN); - node->next = nodep->next; - nodep->next = node; - } - count--; - } - if(err_val){ - curr = nodep->next; - while(curr != NULL){ - node = curr->next; - free(curr); - curr = node; - } - } -} - -static void get_all_name(NET_NAME_ST *nodep) -{ - proc_get_name(nodep); - ioctl_get_name(nodep); -} - -static void create_monit_list(NET_NAME_ST *nodep, NETCARD_INFO_ST *info_list, char *bond) -{ - int err_val = 0; - NETCARD_INFO_ST *info_node = NULL; - NET_NAME_ST *curr, *node; - - curr = nodep->next; - while(curr != NULL){ - if((info_node = (NETCARD_INFO_ST *)malloc(sizeof(NETCARD_INFO_ST))) == NULL){ - err_val = 1; - break; - } - memset(info_node, 0, sizeof(NETCARD_INFO_ST)); - strncpy(info_node->info.charname, curr->name, NIC_NAME_LEN); - strncpy(info_node->info.descr, bond, strlen(bond)); - info_node->next = info_list->next; - info_list->next = info_node; - curr = curr->next; - } - if(err_val){ - NETCARD_INFO_ST *info_curr = info_list->next; - while(info_curr != NULL){ - info_node = info_curr->next; - free(info_curr); - info_curr = info_node; - } - curr = nodep->next; - while(curr != NULL){ - node = curr->next; - free(curr); - curr = node; - } - } -} - -static int get_inc_info(NETCARD_INFO *net) -{ - return if_fetch(net); -} - -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){ - if((str = strstr(linebuf, net->charname)) != NULL){ - str +=strlen(net->charname) + 2; - get_dev_fields(str, net); - break; - } - } - fclose(dev_fp); -} - -static void send_inc_info_one(NETCARD_INFO *net, int semid, SHM *ptr) -{ - int i; - for(i = 0; ptr->info[i].charname[0] != 0; i++){ - if(!strncmp(ptr->info[i].charname, net->charname, NIC_NAME_LEN)){ - get_sem(semid); - memcpy(&ptr->info[i], net, sizeof(NETCARD_INFO)); - release_sem(semid); - } - } -} - -static void *send_inc_info(void *p) -{ - int i = 0; - int flags = 1; - char error_str[200]; - unsigned long long bytes[MAXNICNUM]; - THMEM *ptr = (THMEM *)p; - NETCARD_INFO_ST *curr; - struct timespec s_time, os_time; - - if((curr = ptr->listp->next) == NULL){ - pthread_mutex_lock(&mut); - snprintf(error_str, sizeof(error_str), "EMERG: Empty Netcard list!\n"); - record_log(error_str); - pthread_mutex_unlock(&mut); - return NULL; - } - s_time.tv_sec = ptr->conf->write_interval; - s_time.tv_nsec = 0; - - /* copy all net_info*/ - while(1){ - i = 0; - get_sem(ptr->semid); - memset(ptr->shm_ptr, 0, sizeof(SHM)); - curr = ptr->listp->next; - while(curr != NULL){ - get_inc_info(&curr->info); - if(curr->status == NIC_UNLINKABLE){ - curr->info.flags &= (~IFF_UP); - curr->info.flags &= (~IFF_RUNNING); - } - get_inc_stats(&curr->info); - if(flags){ - bytes[i] = curr->info.rx_bytes + curr->info.tx_bytes; - } - curr->info.average_flow =(unsigned long long)((curr->info.rx_bytes + curr->info.tx_bytes - bytes[i])/(ptr->conf->write_interval)); - memcpy(&ptr->shm_ptr->info[i], &curr->info, sizeof(NETCARD_INFO)); - bytes[i] = curr->info.rx_bytes + curr->info.tx_bytes; - i++; - curr = curr->next; - } - release_sem(ptr->semid); - flags = 0; - nanosleep(&s_time, &os_time); - } -} - -static int get_netcard_count(char *name) -{ - int count = 0; - FILE *fp = NULL; - char path[64] = "/proc/net/bonding/"; - char buf[LINE_SIZE]; - char *retp = NULL; - char *str = NULL; - - strncat(path, name, strlen(name)); - if((fp = fopen(path, "r")) == NULL)return 1; - - memset(buf, 0, sizeof(buf)); - fseek(fp, 65, SEEK_SET); - while((retp = fgets(buf, sizeof(buf), fp)) != NULL){ - if((str = strstr(buf, "active-backup")) != NULL){ - fclose(fp); - return 1; - } - if(!strncmp(buf, "Slave Interface:", 16))count++; - } - fclose(fp); - return count; -} - -static void *cacu_flow(void *p) -{ - int i = 0, count_t = 1; - int summit = 0; - int flags = 1; - int count_ok[MAXNICNUM]; - int count_no[MAXNICNUM]; - char error_str[200]; - unsigned long long bytes[MAXNICNUM]; - THFLOW *ptr = (THFLOW *)p; - NETCARD_INFO_ST *curr; - struct timespec s_time, os_time; - - if((curr = ptr->mem.listp->next) == NULL){ - pthread_mutex_lock(&mut); - snprintf(error_str, sizeof(error_str), "EMERG: Empty Netcard list!\n"); - record_log(error_str); - pthread_mutex_unlock(&mut); - return NULL; - } - s_time.tv_sec = ptr->mem.conf->flow_interval; - s_time.tv_nsec = 0; - - for(i = 0; i < MAXNICNUM ; i++){ - count_ok[i] = 0; - count_no[i] = 0; - } - while(1){ - i = 0; - curr = ptr->mem.listp->next; - while(curr != NULL){ - get_inc_info(&curr->info); - get_inc_stats(&curr->info); - if(flags){ - bytes[i] = curr->info.rx_bytes + 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); - 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); -#if 0 - printf("%s:\tcount=%d\tsummit=%d\tflow_limit:%d\taverage_flow:%d\n", curr->info.charname, count_t, summit, conf.flow_limit, curr->info.average_flow); -#endif - 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); - strncat(ptr->env.Malarm.tSysNetcardAlarm.fault_devname, "-", 1); - strncat(ptr->env.Malarm.tSysNetcardAlarm.fault_devname, curr->info.charname, (NIC_NAME_LEN - ptr->env.host_name_size - 1)); - ptr->env.Malarm.tSysNetcardAlarm.flags = NETCARD_ALARM_ABNORMAL; - ptr->env.Malarm.tSysNetcardAlarm.retrytimes = 0; - ptr->env.Malarm.tMsgFrame.len = sizeof(ptr->env.Malarm); - ptr->env.Malarm.tMsgFrame.seqno = seqno; - pthread_mutex_lock(&mut); - while(count_no[i] > 0){ - send_alarm(&ptr->env.Malarm, ptr->mem.conf->udpport, ptr->mem.conf->ip); - seqno++; - ptr->env.Malarm.tMsgFrame.seqno = seqno; - ptr->env.Malarm.tSysNetcardAlarm.retrytimes++; - count_no[i]--; - if(count_no[i] == 0){ - send_inc_info_one(&curr->info, ptr->mem.semid, ptr->mem.shm_ptr); - //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); - snprintf(error_str, sizeof(error_str), "NOTICE: %s flow is abnormal !\n", curr->info.charname); - record_log(error_str); - } - } - pthread_mutex_unlock(&mut); - count_ok[i] = 3; - }else{ - 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); - strncat(ptr->env.Malarm.tSysNetcardAlarm.fault_devname, "-", 1); - strncat(ptr->env.Malarm.tSysNetcardAlarm.fault_devname, curr->info.charname, (NIC_NAME_LEN - ptr->env.host_name_size - 1)); - ptr->env.Malarm.tSysNetcardAlarm.flags = NETCARD_ALARM_NORMAL; - ptr->env.Malarm.tSysNetcardAlarm.retrytimes = 0; - ptr->env.Malarm.tMsgFrame.len = sizeof(ptr->env.Malarm); - ptr->env.Malarm.tMsgFrame.seqno = seqno; - pthread_mutex_lock(&mut); - while(count_ok[i] > 0){ - send_alarm(&ptr->env.Malarm, ptr->mem.conf->udpport, ptr->mem.conf->ip); - seqno++; - ptr->env.Malarm.tMsgFrame.seqno = seqno; - ptr->env.Malarm.tSysNetcardAlarm.retrytimes++; - count_ok[i]--; - if(count_ok[i] == 0){ - send_inc_info_one(&curr->info, ptr->mem.semid, ptr->mem.shm_ptr); - snprintf(error_str, sizeof(error_str), "NOTICE: %s flow is normal now !\n", curr->info.charname); - record_log(error_str); - } - } - pthread_mutex_unlock(&mut); - count_no[i] = 3; - }/* end if(summit... */ - bytes[i] = curr->info.rx_bytes + curr->info.tx_bytes; - i++; - curr = curr->next; - }/* end while(curr...) */ - flags = 0; - nanosleep(&s_time, &os_time); - }/* end while(1) */ -} -static char *parse_str(char **s) -{ - char *retp = NULL; - char *p = *s; - - while(isspace(*p))p++; - retp = p; - while(!isspace(*p))p++; - *p = '\0'; - - return retp; -} - -int ping_gw(char *devname) -{ - int i, ret = -1, j, k; - char buf[64]; - - i = find_nic_config(devname); - if( i == -1 ) - return 0; - - if(conf.nic[i].gw_num == 0) - return 0; - - for(k=0; k /dev/null 2>&1", conf.pinglap,conf.nic[i].ping_ip[j]); - ret=system(buf); -#if 0 - if (WIFSIGNALED(ret) && - (WTERMSIG(ret) == SIGINT || WTERMSIG(ret) == SIGQUIT)) - exit(0); -#endif - if(ret == 0) - return 0; - - } - } - - return -1; -} - -static void *check_inc_switch(void *ptr) -{ - THENV *arg = (THENV *)ptr; - FILE *fp = NULL; - char name[NIC_NAME_LEN]; - struct timespec s_time, os_time; - int count = 0; - char linebuf[LINE_SIZE]; - char error_str[200]; - char *str = NULL; - char *retp = NULL; - - if((fp = fopen(arg->bond_file_path, "r")) == NULL){ - snprintf(error_str, sizeof(error_str), "EMERG: fopen():%s\n", strerror(errno)); - record_log(error_str); - free(ptr); - return NULL; - } - fseek(fp, 65, SEEK_SET); - memset(name, 0, sizeof(name)); - memset(linebuf, 0, sizeof(linebuf)); - if((retp = fgets(linebuf, sizeof(linebuf), fp)) != NULL){ - if((retp = strstr(linebuf, "active-backup")) == NULL){ - free(ptr); - fclose(fp); - return NULL; - } - } - - s_time.tv_sec = 0; - s_time.tv_nsec = 100000000; - - while(1){ - while((str = fgets(linebuf, sizeof(linebuf), fp)) != NULL){ - if(!strncmp(linebuf, "Currently Active Slave:", 23)){ - str += 23; - retp = parse_str(&str); - break; - } - } - if(name[0] == 0){ - strncpy(name, retp, strlen(retp)); - }else if(strncmp(name, retp, strlen(retp)) != 0){ - memset(arg->Malarm.tSysNetcardAlarm.switch_devname, 0, NIC_NAME_LEN); - strncpy(arg->Malarm.tSysNetcardAlarm.switch_devname, arg->host_name, arg->host_name_size); - strncat(arg->Malarm.tSysNetcardAlarm.switch_devname, "-", 1); - strncat(arg->Malarm.tSysNetcardAlarm.switch_devname, retp, (NIC_NAME_LEN - arg->host_name_size - 1)); - arg->Malarm.tSysNetcardAlarm.flags = NETCARD_ALARM_SWITCH; - arg->Malarm.tSysNetcardAlarm.retrytimes = 0; - arg->Malarm.tMsgFrame.len = sizeof(arg->Malarm); - arg->Malarm.tMsgFrame.seqno = seqno; - count = 3; - pthread_mutex_lock(&mut); - snprintf(error_str, sizeof(error_str), "NOTICE: Net card checking to %s!\n", retp); - record_log(error_str); - while(count > 0){ - send_alarm(&arg->Malarm, arg->conf->udpport, arg->conf->ip); - seqno++; - arg->Malarm.tMsgFrame.seqno = seqno; - arg->Malarm.tSysNetcardAlarm.retrytimes++; - count--; - } - pthread_mutex_unlock(&mut); - memset(name, 0, NIC_NAME_LEN); - strncpy(name, retp, strlen(retp)); - } - fclose(fp); - nanosleep(&s_time, &os_time); - if((fp = fopen(arg->bond_file_path, "r")) == NULL){ - free(ptr); - return NULL; - } - fseek(fp, 65, SEEK_SET); - } - free(ptr); - fclose(fp); - return NULL; -} - -static void *create_logfile(void *ptr) -{ - char buf[128]; - char del_file[DAYS][1024]; - char *s; - int head, tail, fd; - int flag = 0, del_flag = 0; - int old_mon = 0; - int old_day = 0; - time_t tamp; - struct tm tmptr; - struct timespec s_time, os_time; - - s_time.tv_sec = 1; - s_time.tv_nsec = 0; - head = tail = 0; - while(1){ - tamp = time(NULL); - localtime_r(&tamp, &tmptr); - if(old_mon != (tmptr.tm_mon + 1))flag = 1; - else if(old_day != tmptr.tm_mday)flag = 1; - else flag = 0; - if(flag){ - s = strrchr(log_path, '/'); - s++; - *s = '\0'; - memset(buf, 0, sizeof(buf)); - strftime(buf, sizeof(buf), "%Y%m%d_", &tmptr); - strcat(buf, process_name); - strcat(buf, ".log"); - strcat(log_path, buf); - mode_t mask_tmp; - mask_tmp = umask(0000); - if((fd = open(log_path, O_RDWR|O_CREAT, 0666)) != -1){ - log_flag = 0; - close(fd); - } - umask(mask_tmp); - old_mon = tmptr.tm_mon+1; - old_day = tmptr.tm_mday; - strcpy(del_file[head++], log_path); - if(head == DAYS){ - head = 0; - del_flag = 1; - } - if(del_flag){ - unlink(del_file[tail]); - tail++; - if(tail == DAYS)tail = 0; - } - } - nanosleep(&s_time, &os_time); - } -} - -void write_time(int semid, SHM *ptr) -{ - int i = 0; - int flag_time_stamp; - - flag_time_stamp = time(NULL); - - while(ptr->info[i].charname[0] != 0){ - ptr->info[i].time_stamp = flag_time_stamp; - i++; - } -} -#if 0 -char *parse(char *line) -{ - char *begin = NULL; - char *end = NULL; - - if((begin = strchr(line, '/')) != NULL){ - if((end = strstr(line, "bin")) != NULL){ - *end='\0'; - return begin; - } - } - return NULL; -} -#endif -void release_name_list(NET_NAME_ST *head) -{ - NET_NAME_ST *curr, *save; - - curr = head->next; - while(curr != NULL){ - save = curr->next; - free(curr); - curr = save; - } -} -int get_netcard(char *line, char *nic) -{ - char *begin, *retp; - - while(isspace(*line))line++; - begin = line; - while(!isspace(*line))line++; - *line++ = '\0'; - if(!strncmp(begin, "nic", 4)){ - if((retp = parse_str(&line)) != NULL){ - strncpy(nic, retp, MAXNICNUM); - return 1; - } - } - return 0; -} - -void sig_handler(int sig) -{ -// char err_str[200]; - -#if 0 - if(unlink(SHM_PATH) == -1){ - snprintf(err_str, sizeof(err_str), "NOTICE: error delete %s\n", strerror(errno)); - record_log(err_str); - } - if(unlink(SEM_PATH) == -1){ - snprintf(err_str, sizeof(err_str), "NOTICE: error delete %s\n", strerror(errno)); - record_log(err_str); - } -#endif -// semctl(sem_id, 0, IPC_RMID); - -// snprintf(err_str, sizeof(err_str), "NOTICE: process exit by signal %d\n", sig); -// record_log(err_str); - exit(0); -} - -/* - * 查看数据结构中是否有给定的网卡 - * 输入:网卡名称 - * 返回值:-1 表示没有 - * 非0值表示设备列表中的索引 - * - */ -int find_nic_config(char *dev_name){ - int i, index=-1; - - for(i=0; iname, myconf[13]) == 0) && (nicnum <= MAXNICNUM)){ - i = 1; - if(cmd->data.list[0] && cmd->data.list[1]){ - //查找设备列表中是否已有该网卡 - //如果没有则创建一项 - //如果已有则只要增加ping的地址和地址数量 - //一般都能找到,因为配置文件中一般总是先定义"nic",后定义"ping" - //如果没找到,则表明先定义了"ping",未定义"nic"或后定义"nic",而未定义"nic"应该是错误的 - index = find_nic_config(cmd->data.list[0]); - if(index == -1){ - strncpy(conf.nic[nicnum].dev_name, cmd->data.list[0], strlen(cmd->data.list[0]) >= IPLEN ? IPLEN-1 : strlen(cmd->data.list[0])); - card_info_index = nicnum; - nicnum++; - } - else - card_info_index = index; - - while(cmd->data.list[i]){ - if((int)(inet_addr(cmd->data.list[i])) != -1){ - strncpy(conf.nic[card_info_index].ping_ip[i-1], cmd->data.list[i], strlen(cmd->data.list[i]) >= IPLEN ? IPLEN-1 : strlen(cmd->data.list[i])); - i++; - } - } - conf.nic[card_info_index].gw_num = i-1; - } - } - - //"nic"选项的处理 - if((strcmp(cmd ->name, myconf[12]) == 0) && (nicnum <= MAXNICNUM)){ - //设备列表中没有给定的网卡设备,则添加一项 - //否则不必添加 - //对应配置文件中先指定"ping"后指定"nic"的情况 - index = find_nic_config(cmd->data.str); - if(index == -1){ - strncpy(conf.nic[nicnum].dev_name, cmd->data.str, strlen(cmd->data.str) >= NIC_NAME_LEN ? NIC_NAME_LEN-1 : strlen(cmd->data.str)); - nicnum++; - } - } - - if((strcmp(cmd ->name, myconf[11]) == 0)){ - strncpy(conf.udp, cmd->data.str, strlen(cmd->data.str)); - } - - return NULL; -} - -DOTCONF_CB(cb_int) -{ - if(strcmp(cmd ->name, myconf[0]) == 0){ - conf.domain = cmd->data.value; - } - if(strcmp(cmd ->name, myconf[1]) == 0){ - conf.serv = cmd->data.value; - } - if(strcmp(cmd ->name, myconf[2]) == 0){ - conf.event = cmd->data.value; - } - if(strcmp(cmd ->name, myconf[3]) == 0){ - conf.udpport = cmd->data.value; - } - if(strcmp(cmd ->name, myconf[6]) == 0){ - conf.monitor_interval = cmd->data.value; - } - if(strcmp(cmd ->name, myconf[7]) == 0){ - conf.write_interval = cmd->data.value; - } - if(strcmp(cmd ->name, myconf[8]) == 0){ - conf.flow_interval = cmd->data.value; - } - if(strcmp(cmd ->name, myconf[9]) == 0){ - conf.flow_limit = cmd->data.value; - } - if(strcmp(cmd ->name, myconf[10]) == 0){ - conf.flow_peak = cmd->data.value; - } - if(strcmp(cmd ->name, myconf[14]) == 0){ - conf.pingnum = cmd->data.value; - } - if(strcmp(cmd ->name, myconf[15]) == 0){ - conf.pinglap = cmd->data.value; - } - - return NULL; -} - -DOTCONF_CB(cb_list) -{ - return NULL; -} - -int get_conf() -{ -// int uid; - char buf[1024]; -// int i; - struct passwd *user; - configfile_t *configfile; - - memset(buf, 0, sizeof(buf)); -// uid=getuid(); - user = getpwnam("d5000"); -// user = getpwuid(uid); - - sprintf(buf,"%s", user->pw_dir); - strcat(buf,CONF_FILE); - - configfile = dotconf_create(buf, options, NULL, CASE_INSENSITIVE); - if (!configfile) - { - record_log("Warning can't reading config file\n"); - return -1; - } - - if (dotconf_command_loop(configfile) == 0){ - record_log("Warning can't reading config file\n"); - return -1; - } - dotconf_cleanup(configfile); - - if(conf.domain == 0) - conf.domain = 10; - if(conf.serv == 0) - conf.serv = 1; - if(conf.event == 0) - conf.event = 2; - if(conf.udpport == 0) - conf.udpport = 15000; - if(conf.monitor_interval == 0) - conf.monitor_interval = 100; - if(conf.write_interval == 0) - conf.write_interval = 300; - if(conf.flow_interval == 0) - conf.flow_interval = 60; - if(conf.flow_limit == 0) - conf.flow_limit = 30; - if(conf.flow_peak == 0) - conf.flow_peak = 1000; - if(conf.udp[0] == 0) - strcpy(conf.udp, "bond0"); - if(conf.nic[0].dev_name[0] == 0) - strcpy(conf.nic[0].dev_name, "bond0"); - if(conf.pingnum == 0) - conf.pingnum = 2; - if(conf.pinglap == 0) - conf.pinglap = 1; - - return 0; -} - -int isrun(char *p) -{ - char buf[16]; - char cmdbuf[1024]; - FILE *fp; - - memset(buf, 0, sizeof(buf)); - memset(cmdbuf, 0, sizeof(cmdbuf)); - - snprintf(cmdbuf, sizeof(cmdbuf), "pidof %s", p); - fp=popen(cmdbuf, "r"); - fread(buf, sizeof(char), sizeof(buf), fp); - pclose(fp); - if(strchr(buf, ' ') != NULL) - return 1; - else - return 0; -} - -int get_broad_ip(void) -{ - char ip_p[4][4]; - char error_str[200]; - int skfd; - struct ifreq ifr; - - memset(&ifr, 0, sizeof(ifr)); - memset(ip_p, 0, sizeof(ip_p)); - - if((skfd = socket(PF_INET, SOCK_DGRAM, 0)) == -1)return -1; - strcpy(ifr.ifr_name, conf.udp); - if (!ioctl(skfd, SIOCGIFBRDADDR, &ifr)){ - sprintf(ip_p[0], "%d", (unsigned char)ifr.ifr_broadaddr.sa_data[2]); - sprintf(ip_p[1], "%d", (unsigned char)ifr.ifr_broadaddr.sa_data[3]); - sprintf(ip_p[2], "%d", (unsigned char)ifr.ifr_broadaddr.sa_data[4]); - sprintf(ip_p[3], "%d", (unsigned char)ifr.ifr_broadaddr.sa_data[5]); - }else{ - snprintf(error_str, sizeof(error_str), "EMERG: Invalid broadcast device -- %s\n", conf.udp); - record_log(error_str); - close(skfd); - return -1; - } - close(skfd); - strcat(conf.ip, ip_p[0]); - strcat(conf.ip, "."); - strcat(conf.ip, ip_p[1]); - strcat(conf.ip, "."); - strcat(conf.ip, ip_p[2]); - strcat(conf.ip, "."); - strcat(conf.ip, ip_p[3]); - - return 0; -} - -void create_logdir(void) -{ - int ret = 0; - - strcpy(log_path, LOG_PATH); - mode_t mask_tmp; - mask_tmp = umask(0000); - if(mkdir(log_path, 0777) != 0){ - if(errno != EEXIST){ - strcpy(log_path, "/tmp/"); - } - } - if((ret = open(SEM_PATH, O_RDWR | O_CREAT, 0666)) != -1){ - close(ret); - } - umask(mask_tmp); -} - - /* create netcard name list */ -int create_name_list(NET_NAME_ST *all_name_list, NET_NAME_ST *opt_name_list) -{ - int i, count_t, err_val = 0; - NET_NAME_ST *curr, *node; - char error_str[200]; - - for(i = 0, count_t = 0; conf.nic[i].dev_name[0] != 0 && (i < MAXNICNUM); i++){ - curr = all_name_list->next; - while(curr != NULL){ - if(!strncmp(curr->name, conf.nic[i].dev_name, NIC_NAME_LEN)){ - if((node = (NET_NAME_ST *)malloc(sizeof(NET_NAME_ST))) == NULL){ - err_val = 1; - break; - } - memset(node->name, 0, NIC_NAME_LEN); - strncpy(node->name, conf.nic[i].dev_name, NIC_NAME_LEN); - node->next = opt_name_list->next; - opt_name_list->next = node; - count_t ++; - break; - } - curr = curr->next; - } - if(err_val)break; - } - if(count_t != i || err_val){ - for(curr = opt_name_list->next; curr != NULL; curr = curr->next)free(curr); - snprintf(error_str, sizeof(error_str), "EMERG: Invalid netcard name --\n"); - record_log(error_str); - return -1; - } - err_val = 0; - - return 0; -} - -int get_netcard_status(NETCARD_INFO_ST *listp) -{ - if((listp->info.flags & IFF_UP) != IFF_UP) - return NIC_DOWN; - - if((listp->info.flags & IFF_RUNNING) != IFF_RUNNING) - return NIC_UNRUNNING; - - //20100510 - if(elapsed_ping_time == total_ping_time){ - if(ping_gw(listp->info.charname) != 0) - return NIC_UNLINKABLE; - } - else{ - if(listp->status == NIC_UNLINKABLE) - return NIC_UNLINKABLE; - } - - return NIC_NORMAL; -} - -int do_alarm(NETCARD_INFO_ST *listp, int nic_status, D5000_NIC_ALARM *Malarm) -{ - int i; - char error_str[200]; - - memset(Malarm->tSysNetcardAlarm.fault_devname, 0, NIC_NAME_LEN); - strncpy(Malarm->tSysNetcardAlarm.fault_devname, host_name, host_name_size); - strncat(Malarm->tSysNetcardAlarm.fault_devname, "-", 1); - strncat(Malarm->tSysNetcardAlarm.fault_devname, listp->info.charname, NIC_NAME_LEN - host_name_size - 1); - Malarm->tSysNetcardAlarm.retrytimes = 0; - Malarm->tMsgFrame.len = sizeof(Malarm); - Malarm->tMsgFrame.seqno = seqno; - - switch(nic_status){ - case NIC_DOWN: - Malarm->tSysNetcardAlarm.flags = NETCARD_ALARM_FAULT; - snprintf(error_str, sizeof(error_str), "NOTICE: %s is DOWN!\n", listp->info.charname); - break; - case NIC_UNRUNNING: - Malarm->tSysNetcardAlarm.flags = NETCARD_ALARM_FAULT; - snprintf(error_str, sizeof(error_str), "NOTICE: %s is UP but UNRUNNING!\n", listp->info.charname); - break; - case NIC_UNLINKABLE: - Malarm->tSysNetcardAlarm.flags = NETCARD_ALARM_FAULT; - listp->info.flags &= (~IFF_UP); - listp->info.flags &= (~IFF_RUNNING); - snprintf(error_str, sizeof(error_str), "NOTICE: %s is RUNNING but UNLINKABLE!\n", listp->info.charname); - break; - case NIC_NORMAL: - Malarm->tSysNetcardAlarm.flags = NETCARD_ALARM_RESUME; - snprintf(error_str, sizeof(error_str), "NOTICE: %s is NORMAL!\n", listp->info.charname); - break; - } - pthread_mutex_lock(&mut); - for(i = 0; i < 3; i++){ - send_alarm(Malarm, conf.udpport, conf.ip); - seqno++; - Malarm->tMsgFrame.seqno = seqno; - Malarm->tSysNetcardAlarm.retrytimes++; - } - send_inc_info_one(&listp->info, semid, shm_ptr); - pthread_mutex_unlock(&mut); - record_log(error_str); - - return 0; -} - -int main(int argc, char ** argv) -{ - int ret = 0, proc_stat = 0; - unsigned int i = 0; - int nic_status; - int sys_time_stamp = 0; - char *retp = NULL; - char *str = NULL; - char bond[128]; - char error_str[200]; - char linebuf[LINE_SIZE]; - THENV *arg; - glob_t res; - FILE *bond_fp = NULL; - pthread_t tid[THRNR]; - pthread_t tid_mem; - pthread_t tid_flow; - pthread_t tid_time; - THMEM mem; - THFLOW flow; - struct timespec s_time, os_time, log_time; - NET_NAME_ST all_name_list, opt_name_list; - NETCARD_INFO_ST inc_info_list, *listp = NULL; - D5000_NIC_ALARM Malarm; - struct sigaction sig; - proc_invocation prcm; - - memset(&conf, 0, sizeof(CONFIG_FILE_ST)); - if(isrun(argv[0]) == 1){ - printf("the program is already started!\n"); - exit(0); - } -//process register -#if 1 - if((proc_stat=prcm.proc_init("sys","base_srv","sys_nicmonitor"))==-1){ - perror("proc_init()"); - exit(-1); - } -#endif - - sig.sa_handler = sig_handler; - sigemptyset(&sig.sa_mask); - sigaddset(&sig.sa_mask, SIGINT); - sigaddset(&sig.sa_mask, SIGTERM); - sig.sa_flags = 0; - sigaction(SIGTERM, &sig, NULL); - sigaction(SIGINT, &sig, NULL); - sigaction(SIGSEGV, &sig, NULL); - - /* log file path */ - create_logdir(); - - /* get process name */ - memset(process_name, 0, sizeof(process_name)); - if((retp = strrchr(argv[0], '/')) != NULL){ - retp++; - strcpy(process_name, retp); - }else{ - strcpy(process_name, argv[0]); - } - /*create logfile thread */ - pthread_create(&tid_time, NULL, create_logfile, NULL); - - /* initialize some....*/ - memset(&all_name_list, 0, sizeof(NET_NAME_ST)); - all_name_list.next = NULL; - memset(&opt_name_list, 0, sizeof(NET_NAME_ST)); - opt_name_list.next = NULL; - - memset(&inc_info_list, 0, sizeof(NETCARD_INFO_ST)); - inc_info_list.next = NULL; - - - /* get all netcard by proc/net/dev and ioctl */ - get_all_name(&all_name_list); - - /* wait for create log thread */ - log_time.tv_sec = 0; - log_time.tv_nsec = 1000000; - while(log_flag)nanosleep(&log_time, NULL); - - /* sure the process is started */ - snprintf(error_str, sizeof(error_str), "NOTICE: %s V%s is started !\n", process_name, MNIC_VERSION); - record_log(error_str); - - get_conf(); - - /* get broadcast address*/ - if((ret = get_broad_ip()) != 0) - return -1; - if((ret = create_name_list(&all_name_list, &opt_name_list)) != 0) - return -1; - /* get hostname */ - if((ret = gethostname(host_name, sizeof(host_name))) != -1){ - host_name_size = strlen(host_name); - }else host_name_size = HOST_NAME_SIZE; - /* initialize Malarm */ - Malarm.tMsgFrame.serv = conf.serv; - Malarm.tMsgFrame.event = conf.event; - Malarm.tMsgFrame.domain = conf.domain; - - /* get bonding file */ - memset(bond, 0, sizeof(bond)); - memset(linebuf, 0, sizeof(linebuf)); - if((ret = glob(BOND_PATH, 0, NULL, &res)) != 0){ - snprintf(error_str, sizeof(error_str), "NOTICE: No bonding netcard !\n"); - record_log(error_str); - strcpy(bond, "No bonding netcard!"); - } - for (i = 0; i < res.gl_pathc; ++i) { - if((arg = (THENV *)malloc(sizeof(THENV))) != NULL){ - memset(arg, 0, sizeof(THENV)); - strncpy(arg->bond_file_path, res.gl_pathv[i], strlen(res.gl_pathv[i])); - arg->Malarm = Malarm; - strncpy(arg->host_name, host_name, host_name_size); - arg->host_name_size = host_name_size; - arg->conf = &conf; - if(pthread_create(tid+i, NULL, check_inc_switch, (void*)arg)){ - snprintf(error_str, sizeof(error_str), "EMERG: pthread_create(): %s\n", strerror(errno)); - record_log(error_str); - } - } - } - for(i = 0; i < res.gl_pathc; ++i){ - if((bond_fp = fopen(res.gl_pathv[i], "r")) == NULL){ - snprintf(error_str, sizeof(error_str), "EMERG: fopen(): %s\n", strerror(errno)); - record_log(error_str); - }else{ - retp = strrchr(res.gl_pathv[i], '/'); - retp++; - strcat(bond, retp); - strcat(bond, ":"); - while((str = fgets(linebuf, sizeof(linebuf), bond_fp)) != NULL){ - if(!strncmp(linebuf, "Slave Interface:", 16)){ - str += 16; - retp = parse_str(&str); - strcat(bond, retp); - strcat(bond, " "); - } - } - fclose(bond_fp); - } - } - - /* initialize time*/ - s_time.tv_sec = 0; - s_time.tv_nsec = conf.monitor_interval*1000*1000; - - /* initialize sem*/ - init_sem(&semid, &conf); - init_shm(&shm_ptr, &conf); - /* for IPC_RMID */ - sem_id = semid; - - /* create monitor netcard list */ - create_monit_list(&opt_name_list, &inc_info_list, bond); - release_name_list(&all_name_list); - release_name_list(&opt_name_list); - - mem.semid = semid; - mem.shm_ptr = shm_ptr; - mem.listp = &inc_info_list; - mem.conf = &conf; - if(pthread_create(&tid_mem, NULL, send_inc_info, (void *)&mem)){ - snprintf(error_str, sizeof(error_str), "EMERG: pthread_create(): %s\n", strerror(errno)); - record_log(error_str); - } - - /* create caculate flow thread*/ - flow.env.Malarm = Malarm; - strncpy(flow.env.host_name, host_name, host_name_size); - flow.env.host_name_size = host_name_size; - flow.mem.semid = semid; - flow.mem.shm_ptr = shm_ptr; - flow.mem.listp = &inc_info_list; - flow.mem.conf = &conf; - if(pthread_create(&tid_flow, NULL, cacu_flow, (void *)&flow)){ - snprintf(error_str, sizeof(error_str), "EMERG: pthread_create(): %s\n", strerror(errno)); - record_log(error_str); - } - - total_ping_time = 1000 / conf.monitor_interval; - if(total_ping_time == 0) - total_ping_time = 1; - elapsed_ping_time = 1; - - while(1){ - i = 0; - listp = inc_info_list.next; - for(i = 0; listp != NULL; i++){ - get_inc_info(&listp->info); - get_inc_stats(&listp->info); - nic_status = get_netcard_status(listp); - if(nic_status != listp->status){ - do_alarm(listp, nic_status, &Malarm); - listp->status = nic_status; - } - listp = listp->next; - } - - if(seqno > 32765)seqno = 0; - nanosleep(&s_time, &os_time); - - - //20100510 - ++elapsed_ping_time; - if(elapsed_ping_time > total_ping_time) - elapsed_ping_time = 1; - - sys_time_stamp += conf.monitor_interval; - if(sys_time_stamp >= 1000){ - sys_time_stamp = 0; - write_time(semid, shm_ptr); - } - } - for (i = 0; i < res.gl_pathc; i++) { - pthread_join(tid[i], NULL); - } - pthread_join(tid_mem, NULL); - pthread_join(tid_flow, NULL); - pthread_join(tid_time, NULL); - pthread_mutex_destroy(&mut); - globfree(&res); - munmap(shm_ptr, sizeof(SHM)); - return 0; -} diff --git a/code/tags/sys_nicmonitor-1.0.0/mnic.h b/code/tags/sys_nicmonitor-1.0.0/mnic.h deleted file mode 100644 index d7a6390..0000000 --- a/code/tags/sys_nicmonitor-1.0.0/mnic.h +++ /dev/null @@ -1,44 +0,0 @@ -#ifndef __MNIC_H -#define __MNIC_H - -//#define _GNU_SOURCE -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include - -#include "sys_netcard.h" -#include -#include -#define IPSIZE 16 - -char *get_name(char *, char *); -int if_fetch(NETCARD_INFO *); -int ioc_get_name(NETCARD_INFO *); -int get_dev_fields(char *p, NETCARD_INFO *); -int get_user_home(int , char *); -void send_alarm(D5000_NIC_ALARM *, int , char *); -void record_log(char *); - -#endif diff --git a/code/tags/sys_nicmonitor-1.0.0/nicinfo_shm.c b/code/tags/sys_nicmonitor-1.0.0/nicinfo_shm.c deleted file mode 100644 index 9eb5b45..0000000 --- a/code/tags/sys_nicmonitor-1.0.0/nicinfo_shm.c +++ /dev/null @@ -1,188 +0,0 @@ -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include - -#include "nicinfo_shm.h" - -#define SHM_PATH "/tmp/sys_netcard_shm_path" -#define SEM_PATH "/tmp/sys_netcard_sem_path" -#define LOG_PATH "/var/log/netcard/" - -typedef struct net_info{ - NETCARD_INFO info[MAXNICNUM]; -}SHM; - -static int semid = 0; -static char log_path[1024]; -SHM *ptr = NULL; - -/* write error in logfile */ -void record_log(char *str) -{ - time_t tamp; - char str_tm[4]; - char log_str[512]; - FILE *log_fp = NULL; - struct tm tmptr; - struct timeval tv; - struct timezone tz; - - if((log_fp = fopen(log_path, "a")) != NULL){ - tamp = time(NULL); - memset(str_tm, 0, sizeof(str_tm)); - memset(log_str, 0, sizeof(log_str)); - localtime_r(&tamp, &tmptr); - gettimeofday(&tv, &tz); - snprintf(str_tm, sizeof(str_tm), "%d", (int)tv.tv_usec/1000); - if(str_tm[1] == '\0')str_tm[1] = '0'; - if(str_tm[2] == '\0')str_tm[2] = '0'; - strftime(log_str, sizeof(log_str), "%F %T.", &tmptr); - strcat(log_str, str_tm); - strcat(log_str, " "); - strcat(log_str, "get_nic_info"); - strcat(log_str, " "); - strcat(log_str, str); - if(fwrite(log_str, 1, strlen(log_str), log_fp) < 1){ - } - fclose(log_fp); - } -} - -void get_sem(int semid) -{ - char err_str[200]; - struct sembuf lock; - - lock.sem_num = 0; - lock.sem_op = -1; - lock.sem_flg = SEM_UNDO; - - while(semop(semid, &lock, 1)){ - if (errno == EINTR) { - continue; - } - snprintf(err_str, sizeof(err_str), "EMERG: semop():\n", strerror(errno)); - record_log(err_str); - return; - } -} - -void release_sem(int semid) -{ - int ret; - char err_str[200]; - struct sembuf unlock; - - unlock.sem_num = 0; - unlock.sem_op = 1; - unlock.sem_flg = SEM_UNDO; - - if((ret = semop(semid, &unlock, 1)) == -1){ - snprintf(err_str, sizeof(err_str), "EMERG: semop():\n", strerror(errno)); - record_log(err_str); - return; - } -} - - -int init_nic_info(void) -{ - int fd = -1; - int sem_val; - char err_str[200]; - key_t key; - - - if ((key=ftok(SEM_PATH, SEM_PROJ_ID)) == -1) { - snprintf(err_str, sizeof(err_str), "EMERG: ftok():%s\n", strerror(errno)); - record_log(err_str); - return -1; - } - if((semid = semget(key, 1, IPC_CREAT|0666)) == -1){ - snprintf(err_str, sizeof(err_str), "EMERG: Create semaphore error: %s\n", strerror(errno)); - record_log(err_str); - return -1; - } - if((sem_val = semctl(semid, 0, GETVAL, 0)) == -1){ - snprintf(err_str, sizeof(err_str), "EMERG: semctl: %s !\n", strerror(errno)); - record_log(err_str); - semctl(semid, 0, IPC_RMID); - return -1; - } - if(!sem_val){ - if(semctl(semid, 0, SETVAL, 1) == -1){ - snprintf(err_str, sizeof(err_str), "EMERG: semctl: %s !\n", strerror(errno)); - record_log(err_str); - semctl(semid, 0, IPC_RMID); - return -1; - } - } - if((fd = open(SHM_PATH, O_RDONLY)) == -1){ - snprintf(err_str, sizeof(err_str), "EMERG: Error open %s: %s!\n", SHM_PATH, strerror(errno)); - record_log(err_str); - return -1; - } - ptr = (SHM*)mmap(NULL, sizeof(SHM), PROT_READ, MAP_SHARED, fd, 0); - if(ptr == MAP_FAILED){ - snprintf(err_str, sizeof(err_str), "EMERG: mmap():%s\n", strerror(errno)); - record_log(err_str); - return -1; - } - close(fd); - - return 0; -} -int get_nic_info(char *nic_name, NETCARD_INFO *net_info) -{ - int i,ret; - time_t tamp; - struct tm tmptr; - DIR *dir; - char *s; - char buf[128]; - char err_str[200]; - - tamp = time(NULL); - localtime_r(&tamp, &tmptr); - strcpy(log_path, LOG_PATH); - if((dir = opendir(log_path)) == NULL){ - if(errno == ENOENT){ - strcpy(log_path, "/tmp/"); - } - } - closedir(dir); - s = strrchr(log_path, '/'); - s++; - *s = '\0'; - memset(buf, 0, sizeof(buf)); - strftime(buf, sizeof(buf), "%Y%m%d_", &tmptr); - strcat(buf, "sys_nicmonitor"); - strcat(buf, ".log"); - strcat(log_path, buf); - if((ret = init_nic_info()) == -1){ - return -1; - } - for(i = 0; ptr->info[i].charname[0] != 0; i++){ - if(!strcmp(ptr->info[i].charname, nic_name)){ - get_sem(semid); - memcpy(net_info, &ptr->info[i], sizeof(NETCARD_INFO)); - release_sem(semid); - munmap(ptr, sizeof(SHM)); - return 0; - } - } - snprintf(err_str, sizeof(err_str), "NOTICE: No information of %s !\n", nic_name); - record_log(err_str); - munmap(ptr, sizeof(SHM)); - return -1; -} diff --git a/code/tags/sys_nicmonitor-1.0.0/nicinfo_shm.h b/code/tags/sys_nicmonitor-1.0.0/nicinfo_shm.h deleted file mode 100644 index cd74135..0000000 --- a/code/tags/sys_nicmonitor-1.0.0/nicinfo_shm.h +++ /dev/null @@ -1,52 +0,0 @@ -#ifndef NIC_SHM_H -#define NIC_SHM_H - -#include - -#ifdef __cplusplus -extern "C" { -#endif - -#ifndef NIC_NAME_LEN -#define NIC_NAME_LEN 64 -#endif -#define SEM_PROJ_ID 's' -#define MAXNICNUM 32 - -typedef long KEYID; -typedef struct _net_info -{ - KEYID ID; - char charname[NIC_NAME_LEN]; - char descr[128]; - struct sockaddr addr; - struct sockaddr broadaddr; - struct sockaddr netmask; - struct sockaddr hwaddr; - int time_stamp; - short flags; - int mtu; - int tx_queue_len; - unsigned long long average_flow; - unsigned long long rx_packets; - unsigned long long tx_packets; - unsigned long long rx_bytes; - unsigned long long tx_bytes; - unsigned long rx_errors; - unsigned long tx_errors; - unsigned long rx_dropped; - unsigned long tx_dropped; - unsigned long rx_multicast; - unsigned long collisions; - unsigned long rx_fifo_errors; - unsigned long tx_carrier_errors; - unsigned long tx_fifo_errors; -}NETCARD_INFO; - -int get_nic_info(char *nic_name, NETCARD_INFO *net_info); - - -#ifdef __cplusplus -} -#endif -#endif diff --git a/code/tags/sys_nicmonitor-1.0.0/proc_inv.h b/code/tags/sys_nicmonitor-1.0.0/proc_inv.h deleted file mode 100644 index 945622c..0000000 --- a/code/tags/sys_nicmonitor-1.0.0/proc_inv.h +++ /dev/null @@ -1,198 +0,0 @@ -//////////////////////////////////////////////////////////////////////////////////// -// -// Copyright (c) Comets' Grp. of Kedong Corp 2008. All Rights Reserved. -// -// FileName : procconf.h -// -// Function : this class realize some basic functions for process managerment, -// such as initiate process, report status of process, check status of process, -// update status of process, get information of process -// -// Author : -// -// Date : -// -// Modify by : -// -// Mod Date : -// -//////////////////////////////////////////////////////////////////////////////////// - -#ifndef _PROCCONF_H -#define _PROCCONF_H - -#include -#include -#include -#include -#include -#include -#include -//#include -#include - -#include "const.h" - -//for alarm ................................ -typedef struct MESS_BH -{ - unsigned char mtype; - int length; -}; -typedef struct PROCESS_ALM -{ - char context_name[MAX_STRING_LEN]; - char app_name[MAX_STRING_LEN]; - char proc_name[MAX_STRING_LEN]; - unsigned char status; -}; -//for alarm end ............................. - -//for mmi.................................... -const int MAX_BUFFER_LEN = 500; -typedef struct MESS_BLOCK -{ - unsigned char num; - char buffer[MAX_BUFFER_LEN]; -}; -typedef struct MESS_PROC -{ - char context_name[MAX_STRING_LEN]; - char app_name[MAX_STRING_LEN]; - char proc_name[MAX_STRING_LEN]; - char status; -}; -//for mmi end ................................ - -const int DEFAULT_PERIOD = 3; -const int COUNT_LIMIT = 2; -const int START_DEFAULT_PERIOD = 60; -const int START_COUNT_LIMIT = 5; -const int APP_COUNT_LIMIT = 1; - -//process critical level -//const int WST_CRITICAL = 1; // Shutdown and reboot workstation when failed -//const int SYS_CRITICAL = 2; // Shutdown and reboot the system on the wst when failed -//const int USER_CRITICAL = 3; // Shutdown and reboot the subsystem when failed -//const int GENERAL = 4; // reboot the process -const int CRUCIAL = 1; // crucial process -const int GENERAL = 0; // general process - - -extern int srv_init(char *service,int port); -extern int Tcp_close(int sockfd); -extern int Tcp_read(int fd,char *ptr,int nbytes); -extern int Tcp_write(int fd,char *ptr,int nbytes); -extern int srv_accept(int fd,struct sockaddr *cli_addr,int *clilen); -extern int client_tcp_open(char *host,char *service,int port); - -typedef struct -{ - char context_name[MAX_STRING_LEN]; - char app_name[MAX_STRING_LEN]; - char proc_name[MAX_STRING_LEN]; - pid_t proc_pid; -}PROC_ADM_INFO; - -//*************************************************************** -// structure name : PROC_INFO -// function : store process informatin -// author : -// date : -// modify by : -// modification : -// mod date : -//*************************************************************** -typedef struct -{ - int position; - char node_name[MAX_STRING_LEN]; - char context_name[MAX_STRING_LEN]; - char app_name[MAX_STRING_LEN]; - char proc_name[MAX_STRING_LEN]; - - unsigned char active_flag; - unsigned char master_flag; - - time_t startup_time; - time_t refresh_time; - short refresh_peri; - unsigned char monitor_type; - - pid_t proc_pid; - unsigned char auto_start; - unsigned char act_timer; - unsigned char start_timer; - unsigned char critical_level; - - char exefile_path[MAX_EXECMD_LEN]; - -}PROC_INFO; - -//*************************************************************** -// structure name : APP_INFO -// function : store application informatin -// author : -// date : -// modify by : -// modification : -// mod date : -//*************************************************************** -typedef struct -{ - int position; - char context_name[MAX_STRING_LEN]; - int context_id; - char app_name[MAX_STRING_LEN]; - int app_id; - unsigned char act_timer; - unsigned char active_flag; -}APP_INFO; - - -typedef struct -{ - int no_proc; - int semdes_cfg; - PROC_INFO proc[MAX_LOCAL_PROCESS]; - APP_INFO app[MAX_LOCAL_APP]; -}PROCCFG; - -class proc_invocation -{ - public: - int m_init; - PROCCFG *proccfg_p; - - public: - proc_invocation(); - ~proc_invocation(); - - int conf_create(); - //int check_proc_status(); - //int update_rtdb(); - //int kill_proc(pid_t pid); - //int start_proc(char *cmdline); - //int send_alarm(char *context_name, char *app_name, char *proc_name, unsigned char status); - //int update_proc_status(char *context_name, char *app_name, char *proc_name, char status); - - //int proc_init(char *context_name, char *app_name, char *proc_name, int critical_level);//exefile_path,auto_start - int proc_init(char *context_name, char *app_name, char *proc_name); - int proc_report(int pos, char status, int intertime=3); - int proc_exit(int proc_pos); - - int get_pos(char *context_name, char *app_name, char *proc_name); - int is_proc_exist(char *context_name, char *app_name, char *proc_name); - int conf_map(); - - int get_procinfo(int position, PROC_ADM_INFO *p_info); - int get_active_pid(int &num, int *p_pidlist); - int is_proc_run(pid_t pid); - int is_proc_run(char *context_name, char *app_name, char *proc_name); - -}; - -#endif - - - diff --git a/code/tags/sys_nicmonitor-1.0.0/read_netcard.c b/code/tags/sys_nicmonitor-1.0.0/read_netcard.c deleted file mode 100644 index c745f48..0000000 --- a/code/tags/sys_nicmonitor-1.0.0/read_netcard.c +++ /dev/null @@ -1,133 +0,0 @@ -#include "mnic.h" - -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; -} - - -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 == '\0') - return NULL; - p++; - break; - } - *name++ = *p++; - } - *name++ = '\0'; - return p; -} - -int get_dev_fields(char *str, NETCARD_INFO *ife) -{ - 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]); - return 0; -} - -int if_fetch(NETCARD_INFO *ife) -{ - int skfd; - struct ifreq ifr; - - if((skfd = socket(PF_INET, SOCK_DGRAM, 0)) == -1)return -1; - - strcpy(ifr.ifr_name, ife->charname); - if (!ioctl(skfd, SIOCGIFFLAGS, &ifr)) - ife->flags = ifr.ifr_flags; - - if (!ioctl(skfd, SIOCGIFHWADDR, &ifr)) - ife->hwaddr = ifr.ifr_hwaddr; - - if (!ioctl(skfd, SIOCGIFMTU, &ifr)) - ife->mtu = ifr.ifr_mtu; - - if (!ioctl(skfd, SIOCGIFTXQLEN, &ifr)) - ife->tx_queue_len = ifr.ifr_qlen; - - if (!ioctl(skfd, SIOCGIFADDR, &ifr)) - ife->addr = ifr.ifr_addr; - - if (!ioctl(skfd, SIOCGIFBRDADDR, &ifr)) - ife->broadaddr = ifr.ifr_broadaddr; - - if (!ioctl(skfd, SIOCGIFNETMASK, &ifr)) - ife->netmask = ifr.ifr_netmask; - - ife->time_stamp = time(NULL); - close(skfd); - return 0; -} - - -int ioc_get_name(NETCARD_INFO *name) -{ - int skfd; - int ifrnum; - int i = 0; - struct ifconf ifc; - struct ifreq buf[MAXNICNUM]; - - if((skfd = socket(PF_INET, SOCK_DGRAM, 0)) == -1)return -1; - memset(buf, 0, sizeof(buf)); - ifc.ifc_len = sizeof(buf); - ifc.ifc_buf = (caddr_t) buf; - ioctl(skfd, SIOCGIFCONF, &ifc); - ifrnum = ifc.ifc_len / sizeof(struct ifreq); - while(ifrnum-- > 0){ - strcpy(name[i].charname, buf[ifrnum].ifr_name); - i++; - } - close(skfd); - return i; -} - diff --git a/code/tags/sys_nicmonitor-1.0.0/send_alarm.c b/code/tags/sys_nicmonitor-1.0.0/send_alarm.c deleted file mode 100644 index e88635f..0000000 --- a/code/tags/sys_nicmonitor-1.0.0/send_alarm.c +++ /dev/null @@ -1,37 +0,0 @@ -#include "mnic.h" - -void send_alarm(D5000_NIC_ALARM *mesg, int socket_port, char *ipv4) -{ - int bytes; - int sock_sd; - int val; - char error_str[200]; - struct sockaddr_in recever; - socklen_t sock_len; - - if((sock_sd = socket(PF_INET, SOCK_DGRAM, IPPROTO_UDP)) == -1){ - snprintf(error_str, sizeof(error_str), "EMERG: socket(): %s\n", strerror(errno)); - record_log(error_str); - return; - } - val=1; - if ((setsockopt(sock_sd, SOL_SOCKET, SO_BROADCAST, &val, sizeof(val))) == -1) { - snprintf(error_str, sizeof(error_str), "EMERG: setsockopt(): %s\n", strerror(errno)); - record_log(error_str); - return; - } - /* init socket*/ - recever.sin_family = AF_INET; - recever.sin_port = htons(socket_port); - inet_pton(AF_INET, ipv4, &recever.sin_addr); - sock_len = sizeof(recever); - /* send alarm*/ - if((bytes = sendto(sock_sd, mesg, sizeof(D5000_NIC_ALARM), 0, (const struct sockaddr *)&recever, sock_len)) < 1){ - snprintf(error_str, sizeof(error_str), "EMERG: sendto(): %s\n", strerror(errno)); - record_log(error_str); - return; - } - //snprintf(error_str, sizeof(error_str), "NOTICE: Alarm information has already send!\n"); - //record_log(error_str); - close(sock_sd); -} diff --git a/code/tags/sys_nicmonitor-1.0.0/sys_netcard.h b/code/tags/sys_nicmonitor-1.0.0/sys_netcard.h deleted file mode 100644 index 0d7746c..0000000 --- a/code/tags/sys_nicmonitor-1.0.0/sys_netcard.h +++ /dev/null @@ -1,88 +0,0 @@ -#ifndef _SYS_NETCARD_H -#define _SYS_NETCARD_H - -#include - -#ifndef NIC_NAME_LEN -#define NIC_NAME_LEN 64 -#endif - -#define SEM_PROJ_ID 's' -#define MAXNICNUM 32 - -#ifdef __cplusplus -extern "C"{ -#endif - -typedef long KEYID; - -typedef struct _msg_frame // message frame -{ - short len; // message length - short seqno; // send sequence - short serv; // services ID - short event; // event ID - unsigned char domain; // domain ID - unsigned char ctxt; // Context ID - short stid; // SOURCE task id - short dtid; // DESTINATION task ID - unsigned char ver_coding; // 版本号 + 编码 - unsigned char mes_type; // 帧类型 -}MSG_FRAME, *LPMSG_FRAME; - -typedef struct _net_info -{ - KEYID ID; - char charname[NIC_NAME_LEN]; - char descr[128]; - struct sockaddr addr; - struct sockaddr broadaddr; - struct sockaddr netmask; - struct sockaddr hwaddr; - int time_stamp; - short flags; - int mtu; - int tx_queue_len; - unsigned long long average_flow; - unsigned long long rx_packets; - unsigned long long tx_packets; - unsigned long long rx_bytes; - unsigned long long tx_bytes; - unsigned long rx_errors; - unsigned long tx_errors; - unsigned long rx_dropped; - unsigned long tx_dropped; - unsigned long rx_multicast; - unsigned long collisions; - unsigned long rx_fifo_errors; - unsigned long tx_carrier_errors; - unsigned long tx_fifo_errors; -}NETCARD_INFO, *LPNETCARD_INFO; - -#define NETCARD_ALARM_FAULT 1 -#define NETCARD_ALARM_RESUME 2 -#define NETCARD_ALARM_SWITCH 3 -#define NETCARD_ALARM_ABNORMAL 4 -#define NETCARD_ALARM_NORMAL 5 -typedef struct _sys_netcard_alarm -{ - char fault_devname[NIC_NAME_LEN]; /*故障设备名称 */ - char switch_devname[NIC_NAME_LEN]; /*切换设备名称 */ - short flags; /* 状态标记 : 1:网卡故障。2:网卡恢复。3:网卡切换。4:流量异常。*/ - short retrytimes; //0,1,2 重发次数 -}SYS_NETCARD_ALARM, *LPSYS_NETCARD_ALARM; - - -typedef struct _d5000_nic_alarm -{ - MSG_FRAME tMsgFrame ; - SYS_NETCARD_ALARM tSysNetcardAlarm ; -}D5000_NIC_ALARM, *LPD5000_NIC_ALARM; - - -#ifdef __cplusplus -} -#endif - -#endif - diff --git a/code/tags/sys_nicmonitor-1.5/Makefile b/code/tags/sys_nicmonitor-1.5/Makefile deleted file mode 100644 index 5cccc77..0000000 --- a/code/tags/sys_nicmonitor-1.5/Makefile +++ /dev/null @@ -1,28 +0,0 @@ -#! /usr/bin/make -f - -DEFAULTS = Makefile.config - -include $(DEFAULTS) - -all:sys_nicmonitor libnic_shm.so - -CFLAGS += -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 $@ $^ -libnic_shm.so:nicinfo_shm.c - $(CC) -fpic -shared -o $@ $^ -clean: - $(RM) *.o - $(RM) libnic_shm.so sys_nicmonitor -install: - $(MKDIR) -p $(LIBDIR) - $(MKDIR) -p $(BINDIR) - $(MKDIR) -p $(INCDIR) - $(CP) libnic_shm.so $(LIBDIR) - $(CP) sys_nicmonitor $(BINDIR) - $(CP) nicinfo_shm.h $(INCDIR) -uninstall: - $(RM) -f $(LIBDIR)/libnic_shm.so - $(RM) -f $(BINDIR)/sys_nicmonitor - $(RM) -f $(INCDIR)/nicinfo_shm.h diff --git a/code/tags/sys_nicmonitor-1.5/Makefile.config b/code/tags/sys_nicmonitor-1.5/Makefile.config deleted file mode 100644 index 7a2d8e6..0000000 --- a/code/tags/sys_nicmonitor-1.5/Makefile.config +++ /dev/null @@ -1,16 +0,0 @@ - -PREFIX = $(DESTDIR) - -BINDIR = $(PREFIX)/usr/bin - -LIBDIR = $(PREFIX)/usr/lib64 - -INCDIR = $(PREFIX)/usr/include - -INSTALL_SH = ./install.sh - -CP = cp -RM = rm -#CC = gcc -CC = g++ -MKDIR = mkdir diff --git a/code/tags/sys_nicmonitor-1.5/const.h b/code/tags/sys_nicmonitor-1.5/const.h deleted file mode 100644 index e2ebfa5..0000000 --- a/code/tags/sys_nicmonitor-1.5/const.h +++ /dev/null @@ -1,51 +0,0 @@ -#ifndef _RTE_CONST_H -#define _RTE_CONST_H - -#include - - -#define MAX_STRING_LEN 24 -#define MAX_EXECMD_LEN 200 -#define MAX_FILENAME_LEN 200 - -#define MAX_CONTEXT 8 -#define MAX_HOSTNAME_LEN 24 -#define MAX_LOCAL_MESPROC 512 -#define MAX_LOCAL_PROCESS MAX_LOCAL_MESPROC -#define MAX_LOCAL_APP 32 -#define MAX_LOCAL_NODE 256 - -#define MAX_SET 256 //max num of event set -#define MAX_EVENT 1300 -#define MAX_REG_PROC 20 - -#define LEN_SHMBLK_BIG 4096 -#define MAX_MSGBUF_LEN 32767 -#define FREE_PAGE_SIZE 65536 -#define MAX_PAGE_NUM 1024 - -#define MAX_QUE MAX_LOCAL_MESPROC -#define MAX_SEMPHORE_SET MAX_LOCAL_MESPROC*2 -#define RTE_HAN 0 // queue number for event handeler0(RTE) -#define EX_EVENT_HAN RTE_HAN // queue number for extrnal event handeler - -#define DOMAIN_I 1 -#define DOMAIN_II 2 -#define DOMAIN_III 3 - -//Add 20090225 -const int PROC_TYPE_RPT =1; -const int PROC_TYPE_UNRPT =0; -//add end - - -//status for proc and app -const int NON_ACTIVE = 0; -const int ACTIVE = 1; -const int HANGUP = 2; -const int FAILURE = 5; -const int START = 6; -const int STOP = 7; - -#endif - diff --git a/code/tags/sys_nicmonitor-1.5/mnic.c b/code/tags/sys_nicmonitor-1.5/mnic.c deleted file mode 100644 index c3bcf62..0000000 --- a/code/tags/sys_nicmonitor-1.5/mnic.c +++ /dev/null @@ -1,1541 +0,0 @@ -#include "mnic.h" -#include "dotconf.h" -#include -#include -#include "proc_inv.h" - -#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 "/tmp/sys_netcard_shm_path" -#define SEM_PATH "/tmp/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.5" - -#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. -}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 find_nic_config(char *); - -static pthread_mutex_t mut = PTHREAD_MUTEX_INITIALIZER; -static short seqno = 1; -static char log_flag = 1; -static int sem_id = 0; -static char process_name[64]; -char log_path[1024]; -char shm_path[1024]; -char sem_path[1024]; -char conf_path[1024]; - -int host_name_size = 0; -char host_name[HOST_NAME_SIZE]; -int semid = -1; -SHM *shm_ptr; - -//20100510 -int total_ping_time, elapsed_ping_time; -int get_netcard_status(NETCARD_INFO_ST *); - -/****dotconf****/ -static DOTCONF_CB(cb_str); -static DOTCONF_CB(cb_int); -int nicnum = 0; -CONFIG_FILE_ST conf; - -char myconf[][64] = { - "domain", // 0 - "serv", // 1 - "event", // 2 - "udpport", // 3 - "sys_netcard_shm_path", // 4 - "sys_netcard_sem_path", // 5 - "monitor_interval", // 6 - "write_interval", // 7 - "flow_interval", // 8 - "flow_limit", // 9 - "flow_peak", // 10 - "udp", // 11 - "nic", // 12 - "ping", // 13 - "pingnum", // 14 - "pinglap", // 15 - "" -}; - -static const configoption_t options[] = { - {myconf[0], ARG_INT, cb_int, NULL, CTX_ALL}, - {myconf[1], ARG_INT, cb_int, NULL, CTX_ALL}, - {myconf[2], ARG_INT, cb_int, NULL, CTX_ALL}, - {myconf[3], ARG_INT, cb_int, NULL, CTX_ALL}, - {myconf[4], ARG_STR, cb_str, NULL, CTX_ALL}, - {myconf[5], ARG_STR, cb_str, NULL, CTX_ALL}, - {myconf[6], ARG_INT, cb_int, NULL, CTX_ALL}, - {myconf[7], ARG_INT, cb_int, NULL, CTX_ALL}, - {myconf[8], ARG_INT, cb_int, NULL, CTX_ALL}, - {myconf[9], ARG_INT, cb_int, NULL, CTX_ALL}, - {myconf[10], ARG_INT, cb_int, NULL, CTX_ALL}, - {myconf[11], ARG_STR, cb_str, NULL, CTX_ALL}, - {myconf[12], ARG_STR, cb_str, NULL, CTX_ALL}, - {myconf[13], ARG_STR, cb_str, NULL, CTX_ALL}, - {myconf[14], ARG_INT, cb_int, NULL, CTX_ALL}, - {myconf[15], ARG_INT, cb_int, NULL, CTX_ALL}, - LAST_OPTION -}; -/****dotconf****/ - -/* write error in logfile */ -void record_log(char *str) -{ - time_t tamp; - char str_tm[4]; - char log_str[512]; - FILE *log_fp = NULL; - struct tm tmptr; - struct timeval tv; - struct timezone tz; - - if((log_fp = fopen(log_path, "a")) != NULL){ - tamp = time(NULL); - memset(str_tm, 0, sizeof(str_tm)); - memset(log_str, 0, sizeof(log_str)); - localtime_r(&tamp, &tmptr); - gettimeofday(&tv, &tz); - snprintf(str_tm, sizeof(str_tm), "%d", (int)tv.tv_usec/1000); - if(str_tm[1] == '\0')str_tm[1] = '0'; - if(str_tm[2] == '\0')str_tm[2] = '0'; - strftime(log_str, sizeof(log_str), "%F %T.", &tmptr); - strcat(log_str, str_tm); - strcat(log_str, " "); - strcat(log_str, process_name); - strcat(log_str, " "); - strcat(log_str, str); - if(fwrite(log_str, 1, strlen(log_str), log_fp) < 1){ - } - fclose(log_fp); - } -} - -static void init_sem(int *semid, CONFIG_FILE_ST *ptr) -{ - key_t key; - char error_str[200]; - -/* write error in logfile */ - memset(error_str, 0, sizeof(error_str)); - if ((key = ftok(sem_path, SEM_PROJ_ID)) == -1) { - snprintf(error_str, sizeof(error_str), "EMERG: ftok():%s\n", strerror(errno)); - record_log(error_str); - return; - } - if((*semid = semget(key, 1, IPC_CREAT|0666)) == -1){ - snprintf(error_str, sizeof(error_str), "EMERG: semget():%s\n", strerror(errno)); - record_log(error_str); - return; - } - if(semctl(*semid, 0, SETVAL, 0) == -1){ - snprintf(error_str, sizeof(error_str), "EMERG: semctl: %s !\n", strerror(errno)); - record_log(error_str); - semctl(*semid, 0, IPC_RMID); - return; - } -} - -static void init_shm(SHM **ptr, CONFIG_FILE_ST *p) -{ - int fd = -1; - char error_str[200]; - - mode_t mask_tmp; - mask_tmp = umask(0000); - if((fd = open(shm_path, O_RDWR|O_CREAT|O_TRUNC, 0666)) == -1){ - printf("could not create %s\n", shm_path); - return; - } - umask(mask_tmp); - lseek(fd, sizeof(SHM), SEEK_SET); - write(fd, "\0", 1); - *ptr = (SHM *)mmap(NULL, sizeof(SHM), PROT_READ|PROT_WRITE, MAP_SHARED, fd, 0); - if(*ptr == MAP_FAILED){ - snprintf(error_str, sizeof(error_str), "EMERG: mmap():%s\n", strerror(errno)); - record_log(error_str); - return; - } - close(fd); - -} - -static void get_sem(int semid) -{ - struct sembuf lock[2]; - char error_str[200]; - - lock[0].sem_num = 0; - lock[0].sem_op = 0; - lock[0].sem_flg = SEM_UNDO; - - lock[1].sem_num = 0; - lock[1].sem_op = 1; - lock[1].sem_flg = SEM_UNDO; - - while(semop(semid, lock, 2)){ - if(errno == EINTR)continue; - snprintf(error_str, sizeof(error_str), "EMERG: get_sem semop():%s\n", strerror(errno)); - record_log(error_str); - return; - } -} - -static void release_sem(int semid) -{ - int ret; - char error_str[200]; - struct sembuf unlock; - - unlock.sem_num = 0; - unlock.sem_op = -1; - unlock.sem_flg = SEM_UNDO; - - if((ret = semop(semid, &unlock, 1)) == -1){ - snprintf(error_str, sizeof(error_str), "EMERG: rel_sem semop():%s\n", strerror(errno)); - record_log(error_str); - return; - } -} - -static void proc_get_name(NET_NAME_ST *nodep) -{ - char linebuf[LINE_SIZE]; - char error_str[200]; - char *retp = NULL; - int err_val=0; - NET_NAME_ST *node = NULL; - FILE *dev_fp; - - if((dev_fp = fopen("/proc/net/dev", "rb")) == NULL){ - snprintf(error_str, sizeof(error_str), "EMERG: Can't open dev file:%s\n", strerror(errno)); - record_log(error_str); - return; - } - memset(linebuf, 0, sizeof(linebuf)); - fgets(linebuf, sizeof(linebuf), dev_fp); - fgets(linebuf, sizeof(linebuf), dev_fp); - while((retp = fgets(linebuf, sizeof(linebuf), dev_fp)) != NULL){ - if((node = (NET_NAME_ST *)malloc(sizeof(NET_NAME_ST))) == NULL){ - err_val = 1; - break; - } - memset(node->name, 0, NIC_NAME_LEN); - get_name(node->name, retp); - node->next = nodep->next; - nodep->next = node; - } - if(err_val){ - NET_NAME_ST *curr = nodep->next; - while(curr != NULL){ - node = curr->next; - free(curr); - curr = node; - } - } - fclose(dev_fp); -} - -static void ioctl_get_name(NET_NAME_ST *nodep) -{ - int count = 0; - int inuse = 0; - int err_val = 0; - NETCARD_INFO net_name[MAXNICNUM]; - NET_NAME_ST *node = NULL, *curr = NULL; - - memset(net_name, 0, sizeof(net_name)); - if((count = ioc_get_name(net_name)) == 0)return; - if(nodep->next == NULL) - inuse = 1; - while(count > 0){ - curr = nodep->next; - while(curr != NULL){ - if(!strncmp(net_name[count - 1].charname, curr->name, NIC_NAME_LEN)){ - inuse = 0; - break; - } - inuse = 1; - curr = curr->next; - } - if(inuse){ - if((node = (NET_NAME_ST *)malloc(sizeof(NET_NAME_ST))) == NULL){ - err_val = 1; - break; - } - memset(node->name, 0, NIC_NAME_LEN); - strncpy(node->name, net_name[count - 1].charname, NIC_NAME_LEN); - node->next = nodep->next; - nodep->next = node; - } - count--; - } - if(err_val){ - curr = nodep->next; - while(curr != NULL){ - node = curr->next; - free(curr); - curr = node; - } - } -} - -static void get_all_name(NET_NAME_ST *nodep) -{ - proc_get_name(nodep); - ioctl_get_name(nodep); -} - -static void create_monit_list(NET_NAME_ST *nodep, NETCARD_INFO_ST *info_list, char *bond) -{ - int err_val = 0; - NETCARD_INFO_ST *info_node = NULL; - NET_NAME_ST *curr, *node; - - curr = nodep->next; - while(curr != NULL){ - if((info_node = (NETCARD_INFO_ST *)malloc(sizeof(NETCARD_INFO_ST))) == NULL){ - err_val = 1; - break; - } - memset(info_node, 0, sizeof(NETCARD_INFO_ST)); - strncpy(info_node->info.charname, curr->name, NIC_NAME_LEN); - strncpy(info_node->info.descr, bond, strlen(bond)); - info_node->next = info_list->next; - info_list->next = info_node; - curr = curr->next; - } - if(err_val){ - NETCARD_INFO_ST *info_curr = info_list->next; - while(info_curr != NULL){ - info_node = info_curr->next; - free(info_curr); - info_curr = info_node; - } - curr = nodep->next; - while(curr != NULL){ - node = curr->next; - free(curr); - curr = node; - } - } -} - -static int get_inc_info(NETCARD_INFO *net) -{ - return if_fetch(net); -} - -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){ - if((str = strstr(linebuf, net->charname)) != NULL){ - str +=strlen(net->charname) + 2; - get_dev_fields(str, net); - break; - } - } - fclose(dev_fp); -} - -static void send_inc_info_one(NETCARD_INFO *net, int semid, SHM *ptr) -{ - int i; - for(i = 0; ptr->info[i].charname[0] != 0; i++){ - if(!strncmp(ptr->info[i].charname, net->charname, NIC_NAME_LEN)){ - get_sem(semid); - memcpy(&ptr->info[i], net, sizeof(NETCARD_INFO)); - release_sem(semid); - } - } -} - -static void *send_inc_info(void *p) -{ - int i = 0; - int flags = 1; - char error_str[200]; - unsigned long long bytes[MAXNICNUM]; - THMEM *ptr = (THMEM *)p; - NETCARD_INFO_ST *curr; - struct timespec s_time, os_time; - - if((curr = ptr->listp->next) == NULL){ - pthread_mutex_lock(&mut); - snprintf(error_str, sizeof(error_str), "EMERG: Empty Netcard list!\n"); - record_log(error_str); - pthread_mutex_unlock(&mut); - return NULL; - } - s_time.tv_sec = ptr->conf->write_interval; - s_time.tv_nsec = 0; - - /* copy all net_info*/ - while(1){ - i = 0; - get_sem(ptr->semid); - memset(ptr->shm_ptr, 0, sizeof(SHM)); - curr = ptr->listp->next; - while(curr != NULL){ - get_inc_info(&curr->info); - if(curr->status == NIC_UNLINKABLE){ - curr->info.flags &= (~IFF_UP); - curr->info.flags &= (~IFF_RUNNING); - } - get_inc_stats(&curr->info); - if(flags){ - bytes[i] = curr->info.rx_bytes + curr->info.tx_bytes; - } - curr->info.average_flow =(unsigned long long)((curr->info.rx_bytes + curr->info.tx_bytes - bytes[i])/(ptr->conf->write_interval)); - memcpy(&ptr->shm_ptr->info[i], &curr->info, sizeof(NETCARD_INFO)); - bytes[i] = curr->info.rx_bytes + curr->info.tx_bytes; - i++; - curr = curr->next; - } - release_sem(ptr->semid); - flags = 0; - nanosleep(&s_time, &os_time); - } -} - -static int get_netcard_count(char *name) -{ - int count = 0; - FILE *fp = NULL; - char path[64] = "/proc/net/bonding/"; - char buf[LINE_SIZE]; - char *retp = NULL; - char *str = NULL; - - strncat(path, name, strlen(name)); - if((fp = fopen(path, "r")) == NULL)return 1; - - memset(buf, 0, sizeof(buf)); - fseek(fp, 65, SEEK_SET); - while((retp = fgets(buf, sizeof(buf), fp)) != NULL){ - if((str = strstr(buf, "active-backup")) != NULL){ - fclose(fp); - return 1; - } - if(!strncmp(buf, "Slave Interface:", 16))count++; - } - fclose(fp); - return count; -} - -static void *cacu_flow(void *p) -{ - int i = 0, count_t = 1; - int summit = 0; - int flags = 1; - int count_ok[MAXNICNUM]; - int count_no[MAXNICNUM]; - char error_str[200]; - unsigned long long bytes[MAXNICNUM]; - THFLOW *ptr = (THFLOW *)p; - NETCARD_INFO_ST *curr; - struct timespec s_time, os_time; - - if((curr = ptr->mem.listp->next) == NULL){ - pthread_mutex_lock(&mut); - snprintf(error_str, sizeof(error_str), "EMERG: Empty Netcard list!\n"); - record_log(error_str); - pthread_mutex_unlock(&mut); - return NULL; - } - s_time.tv_sec = ptr->mem.conf->flow_interval; - s_time.tv_nsec = 0; - - for(i = 0; i < MAXNICNUM ; i++){ - count_ok[i] = 0; - count_no[i] = 0; - } - while(1){ - i = 0; - curr = ptr->mem.listp->next; - while(curr != NULL){ - get_inc_info(&curr->info); - get_inc_stats(&curr->info); - if(flags){ - bytes[i] = curr->info.rx_bytes + 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); - 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); -#if 0 - printf("%s:\tcount=%d\tsummit=%d\tflow_limit:%d\taverage_flow:%d\n", curr->info.charname, count_t, summit, conf.flow_limit, curr->info.average_flow); -#endif - 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); - strncat(ptr->env.Malarm.tSysNetcardAlarm.fault_devname, "-", 1); - strncat(ptr->env.Malarm.tSysNetcardAlarm.fault_devname, curr->info.charname, (NIC_NAME_LEN - ptr->env.host_name_size - 1)); - ptr->env.Malarm.tSysNetcardAlarm.flags = NETCARD_ALARM_ABNORMAL; - ptr->env.Malarm.tSysNetcardAlarm.retrytimes = 0; - ptr->env.Malarm.tMsgFrame.len = sizeof(ptr->env.Malarm); - ptr->env.Malarm.tMsgFrame.seqno = seqno; - pthread_mutex_lock(&mut); - while(count_no[i] > 0){ - send_alarm(&ptr->env.Malarm, ptr->mem.conf->udpport, ptr->mem.conf->ip); - seqno++; - ptr->env.Malarm.tMsgFrame.seqno = seqno; - ptr->env.Malarm.tSysNetcardAlarm.retrytimes++; - count_no[i]--; - if(count_no[i] == 0){ - send_inc_info_one(&curr->info, ptr->mem.semid, ptr->mem.shm_ptr); - //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); - snprintf(error_str, sizeof(error_str), "NOTICE: %s flow is abnormal !\n", curr->info.charname); - record_log(error_str); - } - } - pthread_mutex_unlock(&mut); - count_ok[i] = 3; - }else{ - 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); - strncat(ptr->env.Malarm.tSysNetcardAlarm.fault_devname, "-", 1); - strncat(ptr->env.Malarm.tSysNetcardAlarm.fault_devname, curr->info.charname, (NIC_NAME_LEN - ptr->env.host_name_size - 1)); - ptr->env.Malarm.tSysNetcardAlarm.flags = NETCARD_ALARM_NORMAL; - ptr->env.Malarm.tSysNetcardAlarm.retrytimes = 0; - ptr->env.Malarm.tMsgFrame.len = sizeof(ptr->env.Malarm); - ptr->env.Malarm.tMsgFrame.seqno = seqno; - pthread_mutex_lock(&mut); - while(count_ok[i] > 0){ - send_alarm(&ptr->env.Malarm, ptr->mem.conf->udpport, ptr->mem.conf->ip); - seqno++; - ptr->env.Malarm.tMsgFrame.seqno = seqno; - ptr->env.Malarm.tSysNetcardAlarm.retrytimes++; - count_ok[i]--; - if(count_ok[i] == 0){ - send_inc_info_one(&curr->info, ptr->mem.semid, ptr->mem.shm_ptr); - snprintf(error_str, sizeof(error_str), "NOTICE: %s flow is normal now !\n", curr->info.charname); - record_log(error_str); - } - } - pthread_mutex_unlock(&mut); - count_no[i] = 3; - }/* end if(summit... */ - bytes[i] = curr->info.rx_bytes + curr->info.tx_bytes; - i++; - curr = curr->next; - }/* end while(curr...) */ - flags = 0; - nanosleep(&s_time, &os_time); - }/* end while(1) */ -} -static char *parse_str(char **s) -{ - char *retp = NULL; - char *p = *s; - - while(isspace(*p))p++; - retp = p; - while(!isspace(*p))p++; - *p = '\0'; - - return retp; -} - -int ping_gw(char *devname) -{ - int i, ret = -1, j, k; - char buf[64]; - - i = find_nic_config(devname); - if( i == -1 ) - return 0; - - if(conf.nic[i].gw_num == 0) - return 0; - - for(k=0; k /dev/null 2>&1", conf.pinglap,conf.nic[i].ping_ip[j]); - ret=system(buf); -#if 0 - if (WIFSIGNALED(ret) && - (WTERMSIG(ret) == SIGINT || WTERMSIG(ret) == SIGQUIT)) - exit(0); -#endif - if(ret == 0) - return 0; - - } - } - - return -1; -} - -static void *check_inc_switch(void *ptr) -{ - THENV *arg = (THENV *)ptr; - FILE *fp = NULL; - char name[NIC_NAME_LEN]; - struct timespec s_time, os_time; - int count = 0; - char linebuf[LINE_SIZE]; - char error_str[200]; - char *str = NULL; - char *retp = NULL; - - if((fp = fopen(arg->bond_file_path, "r")) == NULL){ - snprintf(error_str, sizeof(error_str), "EMERG: fopen():%s\n", strerror(errno)); - record_log(error_str); - free(ptr); - return NULL; - } - fseek(fp, 65, SEEK_SET); - memset(name, 0, sizeof(name)); - memset(linebuf, 0, sizeof(linebuf)); - if((retp = fgets(linebuf, sizeof(linebuf), fp)) != NULL){ - if((retp = strstr(linebuf, "active-backup")) == NULL){ - free(ptr); - fclose(fp); - return NULL; - } - } - - s_time.tv_sec = 0; - s_time.tv_nsec = 100000000; - - while(1){ - while((str = fgets(linebuf, sizeof(linebuf), fp)) != NULL){ - if(!strncmp(linebuf, "Currently Active Slave:", 23)){ - str += 23; - retp = parse_str(&str); - break; - } - } - if(name[0] == 0){ - strncpy(name, retp, strlen(retp)); - }else if(strncmp(name, retp, strlen(retp)) != 0){ - memset(arg->Malarm.tSysNetcardAlarm.switch_devname, 0, NIC_NAME_LEN); - strncpy(arg->Malarm.tSysNetcardAlarm.switch_devname, arg->host_name, arg->host_name_size); - strncat(arg->Malarm.tSysNetcardAlarm.switch_devname, "-", 1); - strncat(arg->Malarm.tSysNetcardAlarm.switch_devname, retp, (NIC_NAME_LEN - arg->host_name_size - 1)); - arg->Malarm.tSysNetcardAlarm.flags = NETCARD_ALARM_SWITCH; - arg->Malarm.tSysNetcardAlarm.retrytimes = 0; - arg->Malarm.tMsgFrame.len = sizeof(arg->Malarm); - arg->Malarm.tMsgFrame.seqno = seqno; - count = 3; - pthread_mutex_lock(&mut); - snprintf(error_str, sizeof(error_str), "NOTICE: Net card checking to %s!\n", retp); - record_log(error_str); - while(count > 0){ - send_alarm(&arg->Malarm, arg->conf->udpport, arg->conf->ip); - seqno++; - arg->Malarm.tMsgFrame.seqno = seqno; - arg->Malarm.tSysNetcardAlarm.retrytimes++; - count--; - } - pthread_mutex_unlock(&mut); - memset(name, 0, NIC_NAME_LEN); - strncpy(name, retp, strlen(retp)); - } - fclose(fp); - nanosleep(&s_time, &os_time); - if((fp = fopen(arg->bond_file_path, "r")) == NULL){ - free(ptr); - return NULL; - } - fseek(fp, 65, SEEK_SET); - } - free(ptr); - fclose(fp); - return NULL; -} - -static void *create_logfile(void *ptr) -{ - char buf[128]; - char del_file[DAYS][1024]; - char *s; - int head, tail, fd; - int flag = 0, del_flag = 0; - int old_mon = 0; - int old_day = 0; - time_t tamp; - struct tm tmptr; - struct timespec s_time, os_time; - - s_time.tv_sec = 1; - s_time.tv_nsec = 0; - head = tail = 0; - while(1){ - tamp = time(NULL); - localtime_r(&tamp, &tmptr); - if(old_mon != (tmptr.tm_mon + 1))flag = 1; - else if(old_day != tmptr.tm_mday)flag = 1; - else flag = 0; - if(flag){ - s = strrchr(log_path, '/'); - s++; - *s = '\0'; - memset(buf, 0, sizeof(buf)); - strftime(buf, sizeof(buf), "%Y%m%d_", &tmptr); - strcat(buf, process_name); - strcat(buf, ".log"); - strcat(log_path, buf); - mode_t mask_tmp; - mask_tmp = umask(0000); - if((fd = open(log_path, O_RDWR|O_CREAT, 0666)) != -1){ - log_flag = 0; - close(fd); - } - umask(mask_tmp); - old_mon = tmptr.tm_mon+1; - old_day = tmptr.tm_mday; - strcpy(del_file[head++], log_path); - if(head == DAYS){ - head = 0; - del_flag = 1; - } - if(del_flag){ - unlink(del_file[tail]); - tail++; - if(tail == DAYS)tail = 0; - } - } - nanosleep(&s_time, &os_time); - } -} - -void write_time(int semid, SHM *ptr) -{ - int i = 0; - int flag_time_stamp; - - flag_time_stamp = time(NULL); - - while(ptr->info[i].charname[0] != 0){ - ptr->info[i].time_stamp = flag_time_stamp; - i++; - } -} -#if 0 -char *parse(char *line) -{ - char *begin = NULL; - char *end = NULL; - - if((begin = strchr(line, '/')) != NULL){ - if((end = strstr(line, "bin")) != NULL){ - *end='\0'; - return begin; - } - } - return NULL; -} -#endif -void release_name_list(NET_NAME_ST *head) -{ - NET_NAME_ST *curr, *save; - - curr = head->next; - while(curr != NULL){ - save = curr->next; - free(curr); - curr = save; - } -} -int get_netcard(char *line, char *nic) -{ - char *begin, *retp; - - while(isspace(*line))line++; - begin = line; - while(!isspace(*line))line++; - *line++ = '\0'; - if(!strncmp(begin, "nic", 4)){ - if((retp = parse_str(&line)) != NULL){ - strncpy(nic, retp, MAXNICNUM); - return 1; - } - } - return 0; -} - -void sig_handler(int sig) -{ -// char err_str[200]; - -#if 0 - if(unlink(SHM_PATH) == -1){ - snprintf(err_str, sizeof(err_str), "NOTICE: error delete %s\n", strerror(errno)); - record_log(err_str); - } - if(unlink(SEM_PATH) == -1){ - snprintf(err_str, sizeof(err_str), "NOTICE: error delete %s\n", strerror(errno)); - record_log(err_str); - } -#endif -// semctl(sem_id, 0, IPC_RMID); - -// snprintf(err_str, sizeof(err_str), "NOTICE: process exit by signal %d\n", sig); -// record_log(err_str); - exit(0); -} - -/* - * 查看数据结构中是否有给定的网卡 - * 输入:网卡名称 - * 返回值:-1 表示没有 - * 非0值表示设备列表中的索引 - * - */ -int find_nic_config(char *dev_name){ - int i, index=-1; - - for(i=0; iname, myconf[13]) == 0) && (nicnum <= MAXNICNUM)){ - i = 1; - if(cmd->data.list[0] && cmd->data.list[1]){ - //查找设备列表中是否已有该网卡 - //如果没有则创建一项 - //如果已有则只要增加ping的地址和地址数量 - //一般都能找到,因为配置文件中一般总是先定义"nic",后定义"ping" - //如果没找到,则表明先定义了"ping",未定义"nic"或后定义"nic",而未定义"nic"应该是错误的 - index = find_nic_config(cmd->data.list[0]); - if(index == -1){ - strncpy(conf.nic[nicnum].dev_name, cmd->data.list[0], strlen(cmd->data.list[0]) >= IPLEN ? IPLEN-1 : strlen(cmd->data.list[0])); - card_info_index = nicnum; - nicnum++; - } - else - card_info_index = index; - - while(cmd->data.list[i]){ - if((int)(inet_addr(cmd->data.list[i])) == -1){ - sprintf(wrong_ip,"The wrong IP address:'%s'",cmd->data.list[i]); - strcat(wrong_ip,"\n"); - record_log(wrong_ip); - } else{ - strncpy(conf.nic[card_info_index].ping_ip[conf.nic[card_info_index].gw_num++], cmd->data.list[i], strlen(cmd->data.list[i]) >= IPLEN ? IPLEN-1 : strlen(cmd->data.list[i])); - } - i++; - } - } - } - - //"nic"选项的处理 - if((strcmp(cmd ->name, myconf[12]) == 0) && (nicnum <= MAXNICNUM)){ - //设备列表中没有给定的网卡设备,则添加一项 - //否则不必添加 - //对应配置文件中先指定"ping"后指定"nic"的情况 - index = find_nic_config(cmd->data.str); - if(index == -1){ - strncpy(conf.nic[nicnum].dev_name, cmd->data.str, strlen(cmd->data.str) >= NIC_NAME_LEN ? NIC_NAME_LEN-1 : strlen(cmd->data.str)); - nicnum++; - } - } - - if((strcmp(cmd ->name, myconf[11]) == 0)){ - strncpy(conf.udp, cmd->data.str, strlen(cmd->data.str)); - } - - return NULL; -} - -DOTCONF_CB(cb_int) -{ - if(strcmp(cmd ->name, myconf[0]) == 0){ - conf.domain = cmd->data.value; - } - if(strcmp(cmd ->name, myconf[1]) == 0){ - conf.serv = cmd->data.value; - } - if(strcmp(cmd ->name, myconf[2]) == 0){ - conf.event = cmd->data.value; - } - if(strcmp(cmd ->name, myconf[3]) == 0){ - conf.udpport = cmd->data.value; - } - if(strcmp(cmd ->name, myconf[6]) == 0){ - conf.monitor_interval = cmd->data.value; - } - if(strcmp(cmd ->name, myconf[7]) == 0){ - conf.write_interval = cmd->data.value; - } - if(strcmp(cmd ->name, myconf[8]) == 0){ - conf.flow_interval = cmd->data.value; - } - if(strcmp(cmd ->name, myconf[9]) == 0){ - conf.flow_limit = cmd->data.value; - } - if(strcmp(cmd ->name, myconf[10]) == 0){ - conf.flow_peak = cmd->data.value; - } - if(strcmp(cmd ->name, myconf[14]) == 0){ - conf.pingnum = cmd->data.value; - } - if(strcmp(cmd ->name, myconf[15]) == 0){ - conf.pinglap = cmd->data.value; - } - - return NULL; -} - -DOTCONF_CB(cb_list) -{ - return NULL; -} - -int get_conf() -{ -// int uid; -// char buf[1024]; -// int i; -// struct passwd *user; - configfile_t *configfile; - -// memset(buf, 0, sizeof(buf)); -// uid=getuid(); -// user = getpwnam("d5000"); -// user = getpwuid(uid); - -// sprintf(buf,"%s", user->pw_dir); -// strcat(buf,conf_path); - - configfile = dotconf_create(conf_path, options, NULL, CASE_INSENSITIVE); - if (!configfile) - { - record_log("Warning can't reading config file\n"); - return -1; - } - - if (dotconf_command_loop(configfile) == 0){ - record_log("Warning can't reading config file\n"); - return -1; - } - dotconf_cleanup(configfile); - - if(conf.domain == 0) - conf.domain = 10; - if(conf.serv == 0) - conf.serv = 1; - if(conf.event == 0) - conf.event = 2; - if(conf.udpport == 0) - conf.udpport = 15000; - if(conf.monitor_interval == 0) - conf.monitor_interval = 100; - if(conf.write_interval == 0) - conf.write_interval = 300; - if(conf.flow_interval == 0) - conf.flow_interval = 60; - if(conf.flow_limit == 0) - conf.flow_limit = 30; - if(conf.flow_peak == 0) - conf.flow_peak = 1000; - if(conf.udp[0] == 0) - strcpy(conf.udp, "bond0"); - if(conf.nic[0].dev_name[0] == 0) - strcpy(conf.nic[0].dev_name, "bond0"); - if(conf.pingnum == 0) - conf.pingnum = 2; - if(conf.pinglap == 0) - conf.pinglap = 1; - - return 0; -} - -int isrun(char *p) -{ - char buf[16]; - char cmdbuf[1024]; - FILE *fp; - - memset(buf, 0, sizeof(buf)); - memset(cmdbuf, 0, sizeof(cmdbuf)); - - snprintf(cmdbuf, sizeof(cmdbuf), "pidof %s", p); - fp=popen(cmdbuf, "r"); - fread(buf, sizeof(char), sizeof(buf), fp); - pclose(fp); - if(strchr(buf, ' ') != NULL) - return 1; - else - return 0; -} - -int get_broad_ip(void) -{ - char ip_p[4][4]; - char error_str[200]; - int skfd; - struct ifreq ifr; - - memset(&ifr, 0, sizeof(ifr)); - memset(ip_p, 0, sizeof(ip_p)); - - if((skfd = socket(PF_INET, SOCK_DGRAM, 0)) == -1)return -1; - strcpy(ifr.ifr_name, conf.udp); - if (!ioctl(skfd, SIOCGIFBRDADDR, &ifr)){ - sprintf(ip_p[0], "%d", (unsigned char)ifr.ifr_broadaddr.sa_data[2]); - sprintf(ip_p[1], "%d", (unsigned char)ifr.ifr_broadaddr.sa_data[3]); - sprintf(ip_p[2], "%d", (unsigned char)ifr.ifr_broadaddr.sa_data[4]); - sprintf(ip_p[3], "%d", (unsigned char)ifr.ifr_broadaddr.sa_data[5]); - }else{ - snprintf(error_str, sizeof(error_str), "EMERG: Invalid broadcast device -- %s\n", conf.udp); - record_log(error_str); - close(skfd); - return -1; - } - close(skfd); - strcat(conf.ip, ip_p[0]); - strcat(conf.ip, "."); - strcat(conf.ip, ip_p[1]); - strcat(conf.ip, "."); - strcat(conf.ip, ip_p[2]); - strcat(conf.ip, "."); - strcat(conf.ip, ip_p[3]); - - return 0; -} - -void create_dir(char *create_path) -{ - int i,len = strlen(create_path); - - len = strlen(create_path); - - for(i=1; inext; - while(curr != NULL){ - if(!strncmp(curr->name, conf.nic[i].dev_name, NIC_NAME_LEN)){ - if((node = (NET_NAME_ST *)malloc(sizeof(NET_NAME_ST))) == NULL){ - err_val = 1; - break; - } - memset(node->name, 0, NIC_NAME_LEN); - strncpy(node->name, conf.nic[i].dev_name, NIC_NAME_LEN); - node->next = opt_name_list->next; - opt_name_list->next = node; - count_t ++; - break; - } - curr = curr->next; - } - if(err_val)break; - } - if(count_t != i || err_val){ - for(curr = opt_name_list->next; curr != NULL; curr = curr->next)free(curr); - snprintf(error_str, sizeof(error_str), "EMERG: Invalid netcard name --\n"); - record_log(error_str); - return -1; - } - err_val = 0; - - return 0; -} - -int get_netcard_status(NETCARD_INFO_ST *listp) -{ - if((listp->info.flags & IFF_UP) != IFF_UP) - return NIC_DOWN; - - if((listp->info.flags & IFF_RUNNING) != IFF_RUNNING) - return NIC_UNRUNNING; - - //20100510 - if(elapsed_ping_time == total_ping_time){ - if(ping_gw(listp->info.charname) != 0) - return NIC_UNLINKABLE; - } - else{ - if(listp->status == NIC_UNLINKABLE) - return NIC_UNLINKABLE; - } - - return NIC_NORMAL; -} - -int do_alarm(NETCARD_INFO_ST *listp, int nic_status, D5000_NIC_ALARM *Malarm) -{ - int i; - char error_str[200]; - - memset(Malarm->tSysNetcardAlarm.fault_devname, 0, NIC_NAME_LEN); - strncpy(Malarm->tSysNetcardAlarm.fault_devname, host_name, host_name_size); - strncat(Malarm->tSysNetcardAlarm.fault_devname, "-", 1); - strncat(Malarm->tSysNetcardAlarm.fault_devname, listp->info.charname, NIC_NAME_LEN - host_name_size - 1); - Malarm->tSysNetcardAlarm.retrytimes = 0; - Malarm->tMsgFrame.len = sizeof(Malarm); - Malarm->tMsgFrame.seqno = seqno; - - switch(nic_status){ - case NIC_DOWN: - Malarm->tSysNetcardAlarm.flags = NETCARD_ALARM_FAULT; - snprintf(error_str, sizeof(error_str), "NOTICE: %s is DOWN!\n", listp->info.charname); - break; - case NIC_UNRUNNING: - Malarm->tSysNetcardAlarm.flags = NETCARD_ALARM_FAULT; - snprintf(error_str, sizeof(error_str), "NOTICE: %s is UP but UNRUNNING!\n", listp->info.charname); - break; - case NIC_UNLINKABLE: - Malarm->tSysNetcardAlarm.flags = NETCARD_ALARM_FAULT; - listp->info.flags &= (~IFF_UP); - listp->info.flags &= (~IFF_RUNNING); - snprintf(error_str, sizeof(error_str), "NOTICE: %s is RUNNING but UNLINKABLE!\n", listp->info.charname); - break; - case NIC_NORMAL: - Malarm->tSysNetcardAlarm.flags = NETCARD_ALARM_RESUME; - snprintf(error_str, sizeof(error_str), "NOTICE: %s is NORMAL!\n", listp->info.charname); - break; - } - pthread_mutex_lock(&mut); - for(i = 0; i < 3; i++){ - send_alarm(Malarm, conf.udpport, conf.ip); - seqno++; - Malarm->tMsgFrame.seqno = seqno; - Malarm->tSysNetcardAlarm.retrytimes++; - } - send_inc_info_one(&listp->info, semid, shm_ptr); - pthread_mutex_unlock(&mut); - record_log(error_str); - - return 0; -} - -int main(int argc, char ** argv) -{ - int ret = 0, proc_stat = 0; - unsigned int i = 0; - int nic_status; - int sys_time_stamp = 0; - char *retp = NULL; - char *str = NULL; - char bond[128]; - char error_str[200]; - char linebuf[LINE_SIZE]; - THENV *arg; - glob_t res; - FILE *bond_fp = NULL; - pthread_t tid[THRNR]; - pthread_t tid_mem; - pthread_t tid_flow; - pthread_t tid_time; - THMEM mem; - THFLOW flow; - struct timespec s_time, os_time, log_time; - NET_NAME_ST all_name_list, opt_name_list; - NETCARD_INFO_ST inc_info_list, *listp = NULL; - D5000_NIC_ALARM Malarm; - struct sigaction sig; - struct passwd *user; - proc_invocation prcm; - - char log_record[125]={0}; - - memset(&conf, 0, sizeof(CONFIG_FILE_ST)); - if(isrun(argv[0]) == 1){ - printf("the program is already started!\n"); - exit(0); - } -//process register -#if 1 - if((proc_stat=prcm.proc_init("sys","base_srv","sys_nicmonitor"))==-1){ - perror("proc_init()"); - exit(-1); - } -#endif - - sig.sa_handler = sig_handler; - sigemptyset(&sig.sa_mask); - sigaddset(&sig.sa_mask, SIGINT); - sigaddset(&sig.sa_mask, SIGTERM); - sig.sa_flags = 0; - sigaction(SIGTERM, &sig, NULL); - sigaction(SIGINT, &sig, NULL); - sigaction(SIGSEGV, &sig, NULL); - - /* create dir file path */ - if((user = getpwnam("d5000")) != NULL){ - sprintf(log_path,"%s",user->pw_dir); - sprintf(shm_path,"%s",user->pw_dir); - sprintf(sem_path,"%s",user->pw_dir); - sprintf(conf_path,"%s",user->pw_dir); - } - - strcat(log_path,LOG_PATH); - strcat(shm_path,SHM_PATH); - strcat(sem_path,SEM_PATH); - strcat(conf_path,CONF_FILE); - - create_semdir(); - create_shmdir(); - create_logdir(); - create_confdir(); - - /* get process name */ - memset(process_name, 0, sizeof(process_name)); - if((retp = strrchr(argv[0], '/')) != NULL){ - retp++; - strcpy(process_name, retp); - }else{ - strcpy(process_name, argv[0]); - } - /*create logfile thread */ - pthread_create(&tid_time, NULL, create_logfile, NULL); - - /* initialize some....*/ - memset(&all_name_list, 0, sizeof(NET_NAME_ST)); - all_name_list.next = NULL; - memset(&opt_name_list, 0, sizeof(NET_NAME_ST)); - opt_name_list.next = NULL; - - memset(&inc_info_list, 0, sizeof(NETCARD_INFO_ST)); - inc_info_list.next = NULL; - - - /* get all netcard by proc/net/dev and ioctl */ - get_all_name(&all_name_list); - - /* wait for create log thread */ - log_time.tv_sec = 0; - log_time.tv_nsec = 1000000; - while(log_flag)nanosleep(&log_time, NULL); - - /* sure the process is started */ - snprintf(error_str, sizeof(error_str), "NOTICE: %s V%s is started !\n", process_name, MNIC_VERSION); - record_log(error_str); - - get_conf(); - - if(shm_path){ - printf("shm_path:%s\n",shm_path); - sprintf(log_record,"shm_path:%s\n",shm_path); - record_log(log_record); - } - - if(log_path){ - printf("log_path:%s\n",log_path); - sprintf(log_record,"log_path:%s\n",log_path); - record_log(log_record); - } - - - if(conf.udp){ - printf("udp %s\n",conf.udp); - sprintf(log_record,"udp %s\n",conf.udp); - record_log(log_record); - } - - for(i=0;conf.nic[i].dev_name[0]!=0;i++){ - printf("nic %s\n",conf.nic[i].dev_name); - sprintf(log_record,"nic %s\n",conf.nic[i].dev_name); - record_log(log_record); - } - - /* get broadcast address*/ - if((ret = get_broad_ip()) != 0) - return -1; - if((ret = create_name_list(&all_name_list, &opt_name_list)) != 0) - return -1; - /* get hostname */ - if((ret = gethostname(host_name, sizeof(host_name))) != -1){ - host_name_size = strlen(host_name); - }else host_name_size = HOST_NAME_SIZE; - /* initialize Malarm */ - Malarm.tMsgFrame.serv = conf.serv; - Malarm.tMsgFrame.event = conf.event; - Malarm.tMsgFrame.domain = conf.domain; - - /* get bonding file */ - memset(bond, 0, sizeof(bond)); - memset(linebuf, 0, sizeof(linebuf)); - if((ret = glob(BOND_PATH, 0, NULL, &res)) != 0){ - snprintf(error_str, sizeof(error_str), "NOTICE: No bonding netcard !\n"); - record_log(error_str); - strcpy(bond, "No bonding netcard!"); - } - for (i = 0; i < res.gl_pathc; ++i) { - if((arg = (THENV *)malloc(sizeof(THENV))) != NULL){ - memset(arg, 0, sizeof(THENV)); - strncpy(arg->bond_file_path, res.gl_pathv[i], strlen(res.gl_pathv[i])); - arg->Malarm = Malarm; - strncpy(arg->host_name, host_name, host_name_size); - arg->host_name_size = host_name_size; - arg->conf = &conf; - if(pthread_create(tid+i, NULL, check_inc_switch, (void*)arg)){ - snprintf(error_str, sizeof(error_str), "EMERG: pthread_create(): %s\n", strerror(errno)); - record_log(error_str); - } - } - } - for(i = 0; i < res.gl_pathc; ++i){ - if((bond_fp = fopen(res.gl_pathv[i], "r")) == NULL){ - snprintf(error_str, sizeof(error_str), "EMERG: fopen(): %s\n", strerror(errno)); - record_log(error_str); - }else{ - retp = strrchr(res.gl_pathv[i], '/'); - retp++; - strcat(bond, retp); - strcat(bond, ":"); - while((str = fgets(linebuf, sizeof(linebuf), bond_fp)) != NULL){ - if(!strncmp(linebuf, "Slave Interface:", 16)){ - str += 16; - retp = parse_str(&str); - strcat(bond, retp); - strcat(bond, " "); - } - } - fclose(bond_fp); - } - } - - /* initialize time*/ - s_time.tv_sec = 0; - s_time.tv_nsec = conf.monitor_interval*1000*1000; - - /* initialize sem*/ - init_sem(&semid, &conf); - init_shm(&shm_ptr, &conf); - /* for IPC_RMID */ - sem_id = semid; - - /* create monitor netcard list */ - create_monit_list(&opt_name_list, &inc_info_list, bond); - release_name_list(&all_name_list); - release_name_list(&opt_name_list); - - mem.semid = semid; - mem.shm_ptr = shm_ptr; - mem.listp = &inc_info_list; - mem.conf = &conf; - if(pthread_create(&tid_mem, NULL, send_inc_info, (void *)&mem)){ - snprintf(error_str, sizeof(error_str), "EMERG: pthread_create(): %s\n", strerror(errno)); - record_log(error_str); - } - - /* create caculate flow thread*/ - flow.env.Malarm = Malarm; - strncpy(flow.env.host_name, host_name, host_name_size); - flow.env.host_name_size = host_name_size; - flow.mem.semid = semid; - flow.mem.shm_ptr = shm_ptr; - flow.mem.listp = &inc_info_list; - flow.mem.conf = &conf; - if(pthread_create(&tid_flow, NULL, cacu_flow, (void *)&flow)){ - snprintf(error_str, sizeof(error_str), "EMERG: pthread_create(): %s\n", strerror(errno)); - record_log(error_str); - } - - total_ping_time = 1000 / conf.monitor_interval; - if(total_ping_time == 0) - total_ping_time = 1; - elapsed_ping_time = 1; - - while(1){ - i = 0; - listp = inc_info_list.next; - for(i = 0; listp != NULL; i++){ - get_inc_info(&listp->info); - get_inc_stats(&listp->info); - nic_status = get_netcard_status(listp); - if(nic_status != listp->status){ - do_alarm(listp, nic_status, &Malarm); - listp->status = nic_status; - } - listp = listp->next; - } - - if(seqno > 32765)seqno = 0; - nanosleep(&s_time, &os_time); - - - //20100510 - ++elapsed_ping_time; - if(elapsed_ping_time > total_ping_time) - elapsed_ping_time = 1; - - sys_time_stamp += conf.monitor_interval; - if(sys_time_stamp >= 1000){ - sys_time_stamp = 0; - write_time(semid, shm_ptr); - } - } - for (i = 0; i < res.gl_pathc; i++) { - pthread_join(tid[i], NULL); - } - pthread_join(tid_mem, NULL); - pthread_join(tid_flow, NULL); - pthread_join(tid_time, NULL); - pthread_mutex_destroy(&mut); - globfree(&res); - munmap(shm_ptr, sizeof(SHM)); - return 0; -} diff --git a/code/tags/sys_nicmonitor-1.5/mnic.h b/code/tags/sys_nicmonitor-1.5/mnic.h deleted file mode 100644 index d7a6390..0000000 --- a/code/tags/sys_nicmonitor-1.5/mnic.h +++ /dev/null @@ -1,44 +0,0 @@ -#ifndef __MNIC_H -#define __MNIC_H - -//#define _GNU_SOURCE -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include - -#include "sys_netcard.h" -#include -#include -#define IPSIZE 16 - -char *get_name(char *, char *); -int if_fetch(NETCARD_INFO *); -int ioc_get_name(NETCARD_INFO *); -int get_dev_fields(char *p, NETCARD_INFO *); -int get_user_home(int , char *); -void send_alarm(D5000_NIC_ALARM *, int , char *); -void record_log(char *); - -#endif diff --git a/code/tags/sys_nicmonitor-1.5/nicinfo_shm.c b/code/tags/sys_nicmonitor-1.5/nicinfo_shm.c deleted file mode 100644 index c8a8bf2..0000000 --- a/code/tags/sys_nicmonitor-1.5/nicinfo_shm.c +++ /dev/null @@ -1,209 +0,0 @@ -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include - -#include "nicinfo_shm.h" - -#define SHM_PATH "/tmp/sys_netcard_shm_path" -#define SEM_PATH "/tmp/sys_netcard_sem_path" -#define LOG_PATH "/var/log/netcard/" - -typedef struct net_info{ - NETCARD_INFO info[MAXNICNUM]; -}SHM; - -static int semid = 0; -static char log_path[1024]; -static char shm_path[1024]; -static char sem_path[1024]; -SHM *ptr = NULL; - -/* write error in logfile */ -void record_log(char *str) -{ - time_t tamp; - char str_tm[4]; - char log_str[512]; - FILE *log_fp = NULL; - struct tm tmptr; - struct timeval tv; - struct timezone tz; - - if((log_fp = fopen(log_path, "a")) != NULL){ - tamp = time(NULL); - memset(str_tm, 0, sizeof(str_tm)); - memset(log_str, 0, sizeof(log_str)); - localtime_r(&tamp, &tmptr); - gettimeofday(&tv, &tz); - snprintf(str_tm, sizeof(str_tm), "%d", (int)tv.tv_usec/1000); - if(str_tm[1] == '\0')str_tm[1] = '0'; - if(str_tm[2] == '\0')str_tm[2] = '0'; - strftime(log_str, sizeof(log_str), "%F %T.", &tmptr); - strcat(log_str, str_tm); - strcat(log_str, " "); - strcat(log_str, "get_nic_info"); - strcat(log_str, " "); - strcat(log_str, str); - if(fwrite(log_str, 1, strlen(log_str), log_fp) < 1){ - } - fclose(log_fp); - } -} - -void get_sem(int semid) -{ - char err_str[200]; - struct sembuf lock[2]; - - lock[0].sem_num = 0; - lock[0].sem_op = 0; - lock[0].sem_flg = SEM_UNDO; - - lock[1].sem_num = 0; - lock[1].sem_op = 1; - lock[1].sem_flg = SEM_UNDO; - - while(semop(semid, lock, 2)){ - if (errno == EINTR) { - continue; - } - snprintf(err_str, sizeof(err_str), "EMERG: semop():\n", strerror(errno)); - record_log(err_str); - return; - } -} - -void release_sem(int semid) -{ - int ret; - char err_str[200]; - struct sembuf unlock; - - unlock.sem_num = 0; - unlock.sem_op = -1; - unlock.sem_flg = SEM_UNDO; - - if((ret = semop(semid, &unlock, 1)) == -1){ - snprintf(err_str, sizeof(err_str), "EMERG: semop():\n", strerror(errno)); - record_log(err_str); - return; - } -} - - -int init_nic_info(void) -{ - int fd = -1; - int sem_val; - char err_str[200]; - key_t key; - - - if ((key=ftok(sem_path, SEM_PROJ_ID)) == -1) { - snprintf(err_str, sizeof(err_str), "EMERG: ftok():%s\n", strerror(errno)); - record_log(err_str); - return -1; - } - if((semid = semget(key, 1, IPC_CREAT|0666)) == -1){ - snprintf(err_str, sizeof(err_str), "EMERG: Create semaphore error: %s\n", strerror(errno)); - record_log(err_str); - return -1; - } - /* - if((sem_val = semctl(semid, 0, GETVAL, 0)) == -1){ - snprintf(err_str, sizeof(err_str), "EMERG: semctl: %s !\n", strerror(errno)); - record_log(err_str); - semctl(semid, 0, IPC_RMID); - return -1; - } - if(!sem_val){ - if(semctl(semid, 0, SETVAL, 1) == -1){ - snprintf(err_str, sizeof(err_str), "EMERG: semctl: %s !\n", strerror(errno)); - record_log(err_str); - semctl(semid, 0, IPC_RMID); - return -1; - } - } - */ - if((fd = open(shm_path, O_RDONLY)) == -1){ - snprintf(err_str, sizeof(err_str), "EMERG: Error open %s: %s!\n",shm_path, strerror(errno)); - record_log(err_str); - return -1; - } - ptr = (SHM*)mmap(NULL, sizeof(SHM), PROT_READ, MAP_SHARED, fd, 0); - if(ptr == MAP_FAILED){ - snprintf(err_str, sizeof(err_str), "EMERG: mmap():%s\n", strerror(errno)); - record_log(err_str); - return -1; - } - close(fd); - - return 0; -} -int get_nic_info(char *nic_name, NETCARD_INFO *net_info) -{ - int i,ret; - time_t tamp; - struct tm tmptr; - DIR *dir; - char *s; - char buf[128]; - char err_str[200]; - struct passwd *user; - - tamp = time(NULL); - localtime_r(&tamp, &tmptr); - - if((user = getpwnam("d5000"))!= NULL){ - sprintf(log_path,"%s",user->pw_dir); - sprintf(shm_path,"%s",user->pw_dir); - sprintf(sem_path,"%s",user->pw_dir); - } - strcat(log_path,LOG_PATH); - strcat(shm_path,SHM_PATH); - strcat(sem_path,SEM_PATH); - - if((dir = opendir(log_path)) == NULL){ - if(errno == ENOENT){ - strcpy(log_path, "/tmp/"); - } - } - closedir(dir); - s = strrchr(log_path, '/'); - s++; - *s = '\0'; - memset(buf, 0, sizeof(buf)); - strftime(buf, sizeof(buf), "%Y%m%d_", &tmptr); - strcat(buf, "sys_nicmonitor"); - strcat(buf, ".log"); - strcat(log_path, buf); - if((ret = init_nic_info()) == -1){ - return -1; - } - get_sem(semid); - for(i = 0; ptr->info[i].charname[0] != 0; i++){ - if(!strcmp(ptr->info[i].charname, nic_name)){ - // get_sem(semid); - memcpy(net_info, &ptr->info[i], sizeof(NETCARD_INFO)); - release_sem(semid); - munmap(ptr, sizeof(SHM)); - return 0; - } - } - release_sem(semid); - snprintf(err_str, sizeof(err_str), "NOTICE: No information of %s !\n", nic_name); - record_log(err_str); - munmap(ptr, sizeof(SHM)); - return -1; -} diff --git a/code/tags/sys_nicmonitor-1.5/nicinfo_shm.h b/code/tags/sys_nicmonitor-1.5/nicinfo_shm.h deleted file mode 100644 index cd74135..0000000 --- a/code/tags/sys_nicmonitor-1.5/nicinfo_shm.h +++ /dev/null @@ -1,52 +0,0 @@ -#ifndef NIC_SHM_H -#define NIC_SHM_H - -#include - -#ifdef __cplusplus -extern "C" { -#endif - -#ifndef NIC_NAME_LEN -#define NIC_NAME_LEN 64 -#endif -#define SEM_PROJ_ID 's' -#define MAXNICNUM 32 - -typedef long KEYID; -typedef struct _net_info -{ - KEYID ID; - char charname[NIC_NAME_LEN]; - char descr[128]; - struct sockaddr addr; - struct sockaddr broadaddr; - struct sockaddr netmask; - struct sockaddr hwaddr; - int time_stamp; - short flags; - int mtu; - int tx_queue_len; - unsigned long long average_flow; - unsigned long long rx_packets; - unsigned long long tx_packets; - unsigned long long rx_bytes; - unsigned long long tx_bytes; - unsigned long rx_errors; - unsigned long tx_errors; - unsigned long rx_dropped; - unsigned long tx_dropped; - unsigned long rx_multicast; - unsigned long collisions; - unsigned long rx_fifo_errors; - unsigned long tx_carrier_errors; - unsigned long tx_fifo_errors; -}NETCARD_INFO; - -int get_nic_info(char *nic_name, NETCARD_INFO *net_info); - - -#ifdef __cplusplus -} -#endif -#endif diff --git a/code/tags/sys_nicmonitor-1.5/proc_inv.h b/code/tags/sys_nicmonitor-1.5/proc_inv.h deleted file mode 100644 index 945622c..0000000 --- a/code/tags/sys_nicmonitor-1.5/proc_inv.h +++ /dev/null @@ -1,198 +0,0 @@ -//////////////////////////////////////////////////////////////////////////////////// -// -// Copyright (c) Comets' Grp. of Kedong Corp 2008. All Rights Reserved. -// -// FileName : procconf.h -// -// Function : this class realize some basic functions for process managerment, -// such as initiate process, report status of process, check status of process, -// update status of process, get information of process -// -// Author : -// -// Date : -// -// Modify by : -// -// Mod Date : -// -//////////////////////////////////////////////////////////////////////////////////// - -#ifndef _PROCCONF_H -#define _PROCCONF_H - -#include -#include -#include -#include -#include -#include -#include -//#include -#include - -#include "const.h" - -//for alarm ................................ -typedef struct MESS_BH -{ - unsigned char mtype; - int length; -}; -typedef struct PROCESS_ALM -{ - char context_name[MAX_STRING_LEN]; - char app_name[MAX_STRING_LEN]; - char proc_name[MAX_STRING_LEN]; - unsigned char status; -}; -//for alarm end ............................. - -//for mmi.................................... -const int MAX_BUFFER_LEN = 500; -typedef struct MESS_BLOCK -{ - unsigned char num; - char buffer[MAX_BUFFER_LEN]; -}; -typedef struct MESS_PROC -{ - char context_name[MAX_STRING_LEN]; - char app_name[MAX_STRING_LEN]; - char proc_name[MAX_STRING_LEN]; - char status; -}; -//for mmi end ................................ - -const int DEFAULT_PERIOD = 3; -const int COUNT_LIMIT = 2; -const int START_DEFAULT_PERIOD = 60; -const int START_COUNT_LIMIT = 5; -const int APP_COUNT_LIMIT = 1; - -//process critical level -//const int WST_CRITICAL = 1; // Shutdown and reboot workstation when failed -//const int SYS_CRITICAL = 2; // Shutdown and reboot the system on the wst when failed -//const int USER_CRITICAL = 3; // Shutdown and reboot the subsystem when failed -//const int GENERAL = 4; // reboot the process -const int CRUCIAL = 1; // crucial process -const int GENERAL = 0; // general process - - -extern int srv_init(char *service,int port); -extern int Tcp_close(int sockfd); -extern int Tcp_read(int fd,char *ptr,int nbytes); -extern int Tcp_write(int fd,char *ptr,int nbytes); -extern int srv_accept(int fd,struct sockaddr *cli_addr,int *clilen); -extern int client_tcp_open(char *host,char *service,int port); - -typedef struct -{ - char context_name[MAX_STRING_LEN]; - char app_name[MAX_STRING_LEN]; - char proc_name[MAX_STRING_LEN]; - pid_t proc_pid; -}PROC_ADM_INFO; - -//*************************************************************** -// structure name : PROC_INFO -// function : store process informatin -// author : -// date : -// modify by : -// modification : -// mod date : -//*************************************************************** -typedef struct -{ - int position; - char node_name[MAX_STRING_LEN]; - char context_name[MAX_STRING_LEN]; - char app_name[MAX_STRING_LEN]; - char proc_name[MAX_STRING_LEN]; - - unsigned char active_flag; - unsigned char master_flag; - - time_t startup_time; - time_t refresh_time; - short refresh_peri; - unsigned char monitor_type; - - pid_t proc_pid; - unsigned char auto_start; - unsigned char act_timer; - unsigned char start_timer; - unsigned char critical_level; - - char exefile_path[MAX_EXECMD_LEN]; - -}PROC_INFO; - -//*************************************************************** -// structure name : APP_INFO -// function : store application informatin -// author : -// date : -// modify by : -// modification : -// mod date : -//*************************************************************** -typedef struct -{ - int position; - char context_name[MAX_STRING_LEN]; - int context_id; - char app_name[MAX_STRING_LEN]; - int app_id; - unsigned char act_timer; - unsigned char active_flag; -}APP_INFO; - - -typedef struct -{ - int no_proc; - int semdes_cfg; - PROC_INFO proc[MAX_LOCAL_PROCESS]; - APP_INFO app[MAX_LOCAL_APP]; -}PROCCFG; - -class proc_invocation -{ - public: - int m_init; - PROCCFG *proccfg_p; - - public: - proc_invocation(); - ~proc_invocation(); - - int conf_create(); - //int check_proc_status(); - //int update_rtdb(); - //int kill_proc(pid_t pid); - //int start_proc(char *cmdline); - //int send_alarm(char *context_name, char *app_name, char *proc_name, unsigned char status); - //int update_proc_status(char *context_name, char *app_name, char *proc_name, char status); - - //int proc_init(char *context_name, char *app_name, char *proc_name, int critical_level);//exefile_path,auto_start - int proc_init(char *context_name, char *app_name, char *proc_name); - int proc_report(int pos, char status, int intertime=3); - int proc_exit(int proc_pos); - - int get_pos(char *context_name, char *app_name, char *proc_name); - int is_proc_exist(char *context_name, char *app_name, char *proc_name); - int conf_map(); - - int get_procinfo(int position, PROC_ADM_INFO *p_info); - int get_active_pid(int &num, int *p_pidlist); - int is_proc_run(pid_t pid); - int is_proc_run(char *context_name, char *app_name, char *proc_name); - -}; - -#endif - - - diff --git a/code/tags/sys_nicmonitor-1.5/read_netcard.c b/code/tags/sys_nicmonitor-1.5/read_netcard.c deleted file mode 100644 index c745f48..0000000 --- a/code/tags/sys_nicmonitor-1.5/read_netcard.c +++ /dev/null @@ -1,133 +0,0 @@ -#include "mnic.h" - -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; -} - - -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 == '\0') - return NULL; - p++; - break; - } - *name++ = *p++; - } - *name++ = '\0'; - return p; -} - -int get_dev_fields(char *str, NETCARD_INFO *ife) -{ - 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]); - return 0; -} - -int if_fetch(NETCARD_INFO *ife) -{ - int skfd; - struct ifreq ifr; - - if((skfd = socket(PF_INET, SOCK_DGRAM, 0)) == -1)return -1; - - strcpy(ifr.ifr_name, ife->charname); - if (!ioctl(skfd, SIOCGIFFLAGS, &ifr)) - ife->flags = ifr.ifr_flags; - - if (!ioctl(skfd, SIOCGIFHWADDR, &ifr)) - ife->hwaddr = ifr.ifr_hwaddr; - - if (!ioctl(skfd, SIOCGIFMTU, &ifr)) - ife->mtu = ifr.ifr_mtu; - - if (!ioctl(skfd, SIOCGIFTXQLEN, &ifr)) - ife->tx_queue_len = ifr.ifr_qlen; - - if (!ioctl(skfd, SIOCGIFADDR, &ifr)) - ife->addr = ifr.ifr_addr; - - if (!ioctl(skfd, SIOCGIFBRDADDR, &ifr)) - ife->broadaddr = ifr.ifr_broadaddr; - - if (!ioctl(skfd, SIOCGIFNETMASK, &ifr)) - ife->netmask = ifr.ifr_netmask; - - ife->time_stamp = time(NULL); - close(skfd); - return 0; -} - - -int ioc_get_name(NETCARD_INFO *name) -{ - int skfd; - int ifrnum; - int i = 0; - struct ifconf ifc; - struct ifreq buf[MAXNICNUM]; - - if((skfd = socket(PF_INET, SOCK_DGRAM, 0)) == -1)return -1; - memset(buf, 0, sizeof(buf)); - ifc.ifc_len = sizeof(buf); - ifc.ifc_buf = (caddr_t) buf; - ioctl(skfd, SIOCGIFCONF, &ifc); - ifrnum = ifc.ifc_len / sizeof(struct ifreq); - while(ifrnum-- > 0){ - strcpy(name[i].charname, buf[ifrnum].ifr_name); - i++; - } - close(skfd); - return i; -} - diff --git a/code/tags/sys_nicmonitor-1.5/send_alarm.c b/code/tags/sys_nicmonitor-1.5/send_alarm.c deleted file mode 100644 index e88635f..0000000 --- a/code/tags/sys_nicmonitor-1.5/send_alarm.c +++ /dev/null @@ -1,37 +0,0 @@ -#include "mnic.h" - -void send_alarm(D5000_NIC_ALARM *mesg, int socket_port, char *ipv4) -{ - int bytes; - int sock_sd; - int val; - char error_str[200]; - struct sockaddr_in recever; - socklen_t sock_len; - - if((sock_sd = socket(PF_INET, SOCK_DGRAM, IPPROTO_UDP)) == -1){ - snprintf(error_str, sizeof(error_str), "EMERG: socket(): %s\n", strerror(errno)); - record_log(error_str); - return; - } - val=1; - if ((setsockopt(sock_sd, SOL_SOCKET, SO_BROADCAST, &val, sizeof(val))) == -1) { - snprintf(error_str, sizeof(error_str), "EMERG: setsockopt(): %s\n", strerror(errno)); - record_log(error_str); - return; - } - /* init socket*/ - recever.sin_family = AF_INET; - recever.sin_port = htons(socket_port); - inet_pton(AF_INET, ipv4, &recever.sin_addr); - sock_len = sizeof(recever); - /* send alarm*/ - if((bytes = sendto(sock_sd, mesg, sizeof(D5000_NIC_ALARM), 0, (const struct sockaddr *)&recever, sock_len)) < 1){ - snprintf(error_str, sizeof(error_str), "EMERG: sendto(): %s\n", strerror(errno)); - record_log(error_str); - return; - } - //snprintf(error_str, sizeof(error_str), "NOTICE: Alarm information has already send!\n"); - //record_log(error_str); - close(sock_sd); -} diff --git a/code/tags/sys_nicmonitor-1.5/sys_netcard.h b/code/tags/sys_nicmonitor-1.5/sys_netcard.h deleted file mode 100644 index 0d7746c..0000000 --- a/code/tags/sys_nicmonitor-1.5/sys_netcard.h +++ /dev/null @@ -1,88 +0,0 @@ -#ifndef _SYS_NETCARD_H -#define _SYS_NETCARD_H - -#include - -#ifndef NIC_NAME_LEN -#define NIC_NAME_LEN 64 -#endif - -#define SEM_PROJ_ID 's' -#define MAXNICNUM 32 - -#ifdef __cplusplus -extern "C"{ -#endif - -typedef long KEYID; - -typedef struct _msg_frame // message frame -{ - short len; // message length - short seqno; // send sequence - short serv; // services ID - short event; // event ID - unsigned char domain; // domain ID - unsigned char ctxt; // Context ID - short stid; // SOURCE task id - short dtid; // DESTINATION task ID - unsigned char ver_coding; // 版本号 + 编码 - unsigned char mes_type; // 帧类型 -}MSG_FRAME, *LPMSG_FRAME; - -typedef struct _net_info -{ - KEYID ID; - char charname[NIC_NAME_LEN]; - char descr[128]; - struct sockaddr addr; - struct sockaddr broadaddr; - struct sockaddr netmask; - struct sockaddr hwaddr; - int time_stamp; - short flags; - int mtu; - int tx_queue_len; - unsigned long long average_flow; - unsigned long long rx_packets; - unsigned long long tx_packets; - unsigned long long rx_bytes; - unsigned long long tx_bytes; - unsigned long rx_errors; - unsigned long tx_errors; - unsigned long rx_dropped; - unsigned long tx_dropped; - unsigned long rx_multicast; - unsigned long collisions; - unsigned long rx_fifo_errors; - unsigned long tx_carrier_errors; - unsigned long tx_fifo_errors; -}NETCARD_INFO, *LPNETCARD_INFO; - -#define NETCARD_ALARM_FAULT 1 -#define NETCARD_ALARM_RESUME 2 -#define NETCARD_ALARM_SWITCH 3 -#define NETCARD_ALARM_ABNORMAL 4 -#define NETCARD_ALARM_NORMAL 5 -typedef struct _sys_netcard_alarm -{ - char fault_devname[NIC_NAME_LEN]; /*故障设备名称 */ - char switch_devname[NIC_NAME_LEN]; /*切换设备名称 */ - short flags; /* 状态标记 : 1:网卡故障。2:网卡恢复。3:网卡切换。4:流量异常。*/ - short retrytimes; //0,1,2 重发次数 -}SYS_NETCARD_ALARM, *LPSYS_NETCARD_ALARM; - - -typedef struct _d5000_nic_alarm -{ - MSG_FRAME tMsgFrame ; - SYS_NETCARD_ALARM tSysNetcardAlarm ; -}D5000_NIC_ALARM, *LPD5000_NIC_ALARM; - - -#ifdef __cplusplus -} -#endif - -#endif - diff --git a/code/tags/sys_nicmonitor-1.6/Makefile b/code/tags/sys_nicmonitor-1.6/Makefile deleted file mode 100644 index 5cccc77..0000000 --- a/code/tags/sys_nicmonitor-1.6/Makefile +++ /dev/null @@ -1,28 +0,0 @@ -#! /usr/bin/make -f - -DEFAULTS = Makefile.config - -include $(DEFAULTS) - -all:sys_nicmonitor libnic_shm.so - -CFLAGS += -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 $@ $^ -libnic_shm.so:nicinfo_shm.c - $(CC) -fpic -shared -o $@ $^ -clean: - $(RM) *.o - $(RM) libnic_shm.so sys_nicmonitor -install: - $(MKDIR) -p $(LIBDIR) - $(MKDIR) -p $(BINDIR) - $(MKDIR) -p $(INCDIR) - $(CP) libnic_shm.so $(LIBDIR) - $(CP) sys_nicmonitor $(BINDIR) - $(CP) nicinfo_shm.h $(INCDIR) -uninstall: - $(RM) -f $(LIBDIR)/libnic_shm.so - $(RM) -f $(BINDIR)/sys_nicmonitor - $(RM) -f $(INCDIR)/nicinfo_shm.h diff --git a/code/tags/sys_nicmonitor-1.6/Makefile.config b/code/tags/sys_nicmonitor-1.6/Makefile.config deleted file mode 100644 index 7a2d8e6..0000000 --- a/code/tags/sys_nicmonitor-1.6/Makefile.config +++ /dev/null @@ -1,16 +0,0 @@ - -PREFIX = $(DESTDIR) - -BINDIR = $(PREFIX)/usr/bin - -LIBDIR = $(PREFIX)/usr/lib64 - -INCDIR = $(PREFIX)/usr/include - -INSTALL_SH = ./install.sh - -CP = cp -RM = rm -#CC = gcc -CC = g++ -MKDIR = mkdir diff --git a/code/tags/sys_nicmonitor-1.6/const.h b/code/tags/sys_nicmonitor-1.6/const.h deleted file mode 100644 index e2ebfa5..0000000 --- a/code/tags/sys_nicmonitor-1.6/const.h +++ /dev/null @@ -1,51 +0,0 @@ -#ifndef _RTE_CONST_H -#define _RTE_CONST_H - -#include - - -#define MAX_STRING_LEN 24 -#define MAX_EXECMD_LEN 200 -#define MAX_FILENAME_LEN 200 - -#define MAX_CONTEXT 8 -#define MAX_HOSTNAME_LEN 24 -#define MAX_LOCAL_MESPROC 512 -#define MAX_LOCAL_PROCESS MAX_LOCAL_MESPROC -#define MAX_LOCAL_APP 32 -#define MAX_LOCAL_NODE 256 - -#define MAX_SET 256 //max num of event set -#define MAX_EVENT 1300 -#define MAX_REG_PROC 20 - -#define LEN_SHMBLK_BIG 4096 -#define MAX_MSGBUF_LEN 32767 -#define FREE_PAGE_SIZE 65536 -#define MAX_PAGE_NUM 1024 - -#define MAX_QUE MAX_LOCAL_MESPROC -#define MAX_SEMPHORE_SET MAX_LOCAL_MESPROC*2 -#define RTE_HAN 0 // queue number for event handeler0(RTE) -#define EX_EVENT_HAN RTE_HAN // queue number for extrnal event handeler - -#define DOMAIN_I 1 -#define DOMAIN_II 2 -#define DOMAIN_III 3 - -//Add 20090225 -const int PROC_TYPE_RPT =1; -const int PROC_TYPE_UNRPT =0; -//add end - - -//status for proc and app -const int NON_ACTIVE = 0; -const int ACTIVE = 1; -const int HANGUP = 2; -const int FAILURE = 5; -const int START = 6; -const int STOP = 7; - -#endif - diff --git a/code/tags/sys_nicmonitor-1.6/mnic.c b/code/tags/sys_nicmonitor-1.6/mnic.c deleted file mode 100755 index 51a27a5..0000000 --- a/code/tags/sys_nicmonitor-1.6/mnic.c +++ /dev/null @@ -1,1552 +0,0 @@ -#include "mnic.h" -#include "dotconf.h" -#include -#include -#include "proc_inv.h" - -#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 "/tmp/sys_netcard_shm_path" -#define SEM_PATH "/tmp/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.6" - -#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. -}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 find_nic_config(char *); - -static pthread_mutex_t mut = PTHREAD_MUTEX_INITIALIZER; -static short seqno = 1; -static char log_flag = 1; -static int sem_id = 0; -static char process_name[64]; -char log_path[1024]; -char shm_path[1024]; -char sem_path[1024]; -char conf_path[1024]; - -int host_name_size = 0; -char host_name[HOST_NAME_SIZE]; -int semid = -1; -SHM *shm_ptr; - -//20100510 -int total_ping_time, elapsed_ping_time; -int get_netcard_status(NETCARD_INFO_ST *); - -/****dotconf****/ -static DOTCONF_CB(cb_str); -static DOTCONF_CB(cb_int); -int nicnum = 0; -CONFIG_FILE_ST conf; - -char myconf[][64] = { - "domain", // 0 - "serv", // 1 - "event", // 2 - "udpport", // 3 - "sys_netcard_shm_path", // 4 - "sys_netcard_sem_path", // 5 - "monitor_interval", // 6 - "write_interval", // 7 - "flow_interval", // 8 - "flow_limit", // 9 - "flow_peak", // 10 - "udp", // 11 - "nic", // 12 - "ping", // 13 - "pingnum", // 14 - "pinglap", // 15 - "" -}; - -static const configoption_t options[] = { - {myconf[0], ARG_INT, cb_int, NULL, CTX_ALL}, - {myconf[1], ARG_INT, cb_int, NULL, CTX_ALL}, - {myconf[2], ARG_INT, cb_int, NULL, CTX_ALL}, - {myconf[3], ARG_INT, cb_int, NULL, CTX_ALL}, - {myconf[4], ARG_STR, cb_str, NULL, CTX_ALL}, - {myconf[5], ARG_STR, cb_str, NULL, CTX_ALL}, - {myconf[6], ARG_INT, cb_int, NULL, CTX_ALL}, - {myconf[7], ARG_INT, cb_int, NULL, CTX_ALL}, - {myconf[8], ARG_INT, cb_int, NULL, CTX_ALL}, - {myconf[9], ARG_INT, cb_int, NULL, CTX_ALL}, - {myconf[10], ARG_INT, cb_int, NULL, CTX_ALL}, - {myconf[11], ARG_STR, cb_str, NULL, CTX_ALL}, - {myconf[12], ARG_STR, cb_str, NULL, CTX_ALL}, - {myconf[13], ARG_STR, cb_str, NULL, CTX_ALL}, - {myconf[14], ARG_INT, cb_int, NULL, CTX_ALL}, - {myconf[15], ARG_INT, cb_int, NULL, CTX_ALL}, - LAST_OPTION -}; -/****dotconf****/ - -/* write error in logfile */ -void record_log(char *str) -{ - time_t tamp; - char str_tm[4]; - char log_str[512]; - FILE *log_fp = NULL; - struct tm tmptr; - struct timeval tv; - struct timezone tz; - - if((log_fp = fopen(log_path, "a")) != NULL){ - tamp = time(NULL); - memset(str_tm, 0, sizeof(str_tm)); - memset(log_str, 0, sizeof(log_str)); - localtime_r(&tamp, &tmptr); - gettimeofday(&tv, &tz); - snprintf(str_tm, sizeof(str_tm), "%d", (int)tv.tv_usec/1000); - if(str_tm[1] == '\0')str_tm[1] = '0'; - if(str_tm[2] == '\0')str_tm[2] = '0'; - strftime(log_str, sizeof(log_str), "%F %T.", &tmptr); - strcat(log_str, str_tm); - strcat(log_str, " "); - strcat(log_str, process_name); - strcat(log_str, " "); - strcat(log_str, str); - if(fwrite(log_str, 1, strlen(log_str), log_fp) < 1){ - } - fclose(log_fp); - } -} - -static void init_sem(int *semid, CONFIG_FILE_ST *ptr) -{ - key_t key; - char error_str[200]; - -/* write error in logfile */ - memset(error_str, 0, sizeof(error_str)); - if ((key = ftok(sem_path, SEM_PROJ_ID)) == -1) { - snprintf(error_str, sizeof(error_str), "EMERG: ftok():%s\n", strerror(errno)); - record_log(error_str); - return; - } - if((*semid = semget(key, 1, IPC_CREAT|0666)) == -1){ - snprintf(error_str, sizeof(error_str), "EMERG: semget():%s\n", strerror(errno)); - record_log(error_str); - return; - } - if(semctl(*semid, 0, SETVAL, 0) == -1){ - snprintf(error_str, sizeof(error_str), "EMERG: semctl: %s !\n", strerror(errno)); - record_log(error_str); - semctl(*semid, 0, IPC_RMID); - return; - } -} - -static void init_shm(SHM **ptr, CONFIG_FILE_ST *p) -{ - int fd = -1; - char error_str[200]; - - mode_t mask_tmp; - mask_tmp = umask(0000); - if((fd = open(shm_path, O_RDWR|O_CREAT|O_TRUNC, 0666)) == -1){ - printf("could not create %s\n", shm_path); - return; - } - umask(mask_tmp); - lseek(fd, sizeof(SHM), SEEK_SET); - write(fd, "\0", 1); - *ptr = (SHM *)mmap(NULL, sizeof(SHM), PROT_READ|PROT_WRITE, MAP_SHARED, fd, 0); - if(*ptr == MAP_FAILED){ - snprintf(error_str, sizeof(error_str), "EMERG: mmap():%s\n", strerror(errno)); - record_log(error_str); - return; - } - close(fd); - -} - -static void get_sem(int semid) -{ - struct sembuf lock[2]; - char error_str[200]; - - lock[0].sem_num = 0; - lock[0].sem_op = 0; - lock[0].sem_flg = SEM_UNDO; - - lock[1].sem_num = 0; - lock[1].sem_op = 1; - lock[1].sem_flg = SEM_UNDO; - - while(semop(semid, lock, 2)){ - if(errno == EINTR)continue; - snprintf(error_str, sizeof(error_str), "EMERG: get_sem semop():%s\n", strerror(errno)); - record_log(error_str); - return; - } -} - -static void release_sem(int semid) -{ - int ret; - char error_str[200]; - struct sembuf unlock; - - unlock.sem_num = 0; - unlock.sem_op = -1; - unlock.sem_flg = SEM_UNDO; - - if((ret = semop(semid, &unlock, 1)) == -1){ - snprintf(error_str, sizeof(error_str), "EMERG: rel_sem semop():%s\n", strerror(errno)); - record_log(error_str); - return; - } -} - -static void proc_get_name(NET_NAME_ST *nodep) -{ - char linebuf[LINE_SIZE]; - char error_str[200]; - char *retp = NULL; - int err_val=0; - NET_NAME_ST *node = NULL; - FILE *dev_fp; - - if((dev_fp = fopen("/proc/net/dev", "rb")) == NULL){ - snprintf(error_str, sizeof(error_str), "EMERG: Can't open dev file:%s\n", strerror(errno)); - record_log(error_str); - return; - } - memset(linebuf, 0, sizeof(linebuf)); - fgets(linebuf, sizeof(linebuf), dev_fp); - fgets(linebuf, sizeof(linebuf), dev_fp); - while((retp = fgets(linebuf, sizeof(linebuf), dev_fp)) != NULL){ - if((node = (NET_NAME_ST *)malloc(sizeof(NET_NAME_ST))) == NULL){ - err_val = 1; - break; - } - memset(node->name, 0, NIC_NAME_LEN); - get_name(node->name, retp); - node->next = nodep->next; - nodep->next = node; - } - if(err_val){ - NET_NAME_ST *curr = nodep->next; - while(curr != NULL){ - node = curr->next; - free(curr); - curr = node; - } - } - fclose(dev_fp); -} - -static void ioctl_get_name(NET_NAME_ST *nodep) -{ - int count = 0; - int inuse = 0; - int err_val = 0; - NETCARD_INFO net_name[MAXNICNUM]; - NET_NAME_ST *node = NULL, *curr = NULL; - - memset(net_name, 0, sizeof(net_name)); - if((count = ioc_get_name(net_name)) == 0)return; - if(nodep->next == NULL) - inuse = 1; - while(count > 0){ - curr = nodep->next; - while(curr != NULL){ - if(!strncmp(net_name[count - 1].charname, curr->name, NIC_NAME_LEN)){ - inuse = 0; - break; - } - inuse = 1; - curr = curr->next; - } - if(inuse){ - if((node = (NET_NAME_ST *)malloc(sizeof(NET_NAME_ST))) == NULL){ - err_val = 1; - break; - } - memset(node->name, 0, NIC_NAME_LEN); - strncpy(node->name, net_name[count - 1].charname, NIC_NAME_LEN); - node->next = nodep->next; - nodep->next = node; - } - count--; - } - if(err_val){ - curr = nodep->next; - while(curr != NULL){ - node = curr->next; - free(curr); - curr = node; - } - } -} - -static void get_all_name(NET_NAME_ST *nodep) -{ - proc_get_name(nodep); - ioctl_get_name(nodep); -} - -static void create_monit_list(NET_NAME_ST *nodep, NETCARD_INFO_ST *info_list, char *bond) -{ - int err_val = 0; - NETCARD_INFO_ST *info_node = NULL; - NET_NAME_ST *curr, *node; - - curr = nodep->next; - while(curr != NULL){ - if((info_node = (NETCARD_INFO_ST *)malloc(sizeof(NETCARD_INFO_ST))) == NULL){ - err_val = 1; - break; - } - memset(info_node, 0, sizeof(NETCARD_INFO_ST)); - strncpy(info_node->info.charname, curr->name, NIC_NAME_LEN); - strncpy(info_node->info.descr, bond, strlen(bond)); - info_node->next = info_list->next; - info_list->next = info_node; - curr = curr->next; - } - if(err_val){ - NETCARD_INFO_ST *info_curr = info_list->next; - while(info_curr != NULL){ - info_node = info_curr->next; - free(info_curr); - info_curr = info_node; - } - curr = nodep->next; - while(curr != NULL){ - node = curr->next; - free(curr); - curr = node; - } - } -} - -static int get_inc_info(NETCARD_INFO *net) -{ - return if_fetch(net); -} - -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){ - if((str = strstr(linebuf, net->charname)) != NULL){ - str +=strlen(net->charname) + 2; - get_dev_fields(str, net); - break; - } - } - fclose(dev_fp); -} - -static void send_inc_info_one(NETCARD_INFO *net, int semid, SHM *ptr) -{ - int i; - for(i = 0; ptr->info[i].charname[0] != 0; i++){ - if(!strncmp(ptr->info[i].charname, net->charname, NIC_NAME_LEN)){ - get_sem(semid); - memcpy(&ptr->info[i], net, sizeof(NETCARD_INFO)); - release_sem(semid); - } - } -} - -static void *send_inc_info(void *p) -{ - int i = 0; - int flags = 1; - char error_str[200]; - unsigned long long bytes[MAXNICNUM]; - THMEM *ptr = (THMEM *)p; - NETCARD_INFO_ST *curr; - struct timespec s_time, os_time; - - if((curr = ptr->listp->next) == NULL){ - pthread_mutex_lock(&mut); - snprintf(error_str, sizeof(error_str), "EMERG: Empty Netcard list!\n"); - record_log(error_str); - pthread_mutex_unlock(&mut); - return NULL; - } - s_time.tv_sec = ptr->conf->write_interval; - s_time.tv_nsec = 0; - - /* copy all net_info*/ - while(1){ - i = 0; - get_sem(ptr->semid); - //memset(ptr->shm_ptr, 0, sizeof(SHM)); - curr = ptr->listp->next; - while(curr != NULL){ - get_inc_info(&curr->info); - if(curr->status == NIC_UNLINKABLE){ - curr->info.flags &= (~IFF_UP); - curr->info.flags &= (~IFF_RUNNING); - } - get_inc_stats(&curr->info); - if(flags){ - bytes[i] = curr->info.rx_bytes + curr->info.tx_bytes; - } - curr->info.average_flow =(unsigned long long)((curr->info.rx_bytes + curr->info.tx_bytes - bytes[i])/(ptr->conf->write_interval)); - memcpy(&ptr->shm_ptr->info[i], &curr->info, sizeof(NETCARD_INFO)); - bytes[i] = curr->info.rx_bytes + curr->info.tx_bytes; - i++; - curr = curr->next; - } - release_sem(ptr->semid); - flags = 0; - nanosleep(&s_time, &os_time); - } -} - -static int get_netcard_count(char *name) -{ - int count = 0; - FILE *fp = NULL; - char path[64] = "/proc/net/bonding/"; - char buf[LINE_SIZE]; - char *retp = NULL; - char *str = NULL; - - strncat(path, name, strlen(name)); - if((fp = fopen(path, "r")) == NULL)return 1; - - memset(buf, 0, sizeof(buf)); - fseek(fp, 65, SEEK_SET); - while((retp = fgets(buf, sizeof(buf), fp)) != NULL){ - if((str = strstr(buf, "active-backup")) != NULL){ - fclose(fp); - return 1; - } - if(!strncmp(buf, "Slave Interface:", 16))count++; - } - fclose(fp); - return count; -} - -static void *cacu_flow(void *p) -{ - int i = 0, count_t = 1; - int summit = 0; - int flags = 1; - int count_ok[MAXNICNUM]; - int count_no[MAXNICNUM]; - char error_str[200]; - unsigned long long bytes[MAXNICNUM]; - THFLOW *ptr = (THFLOW *)p; - NETCARD_INFO_ST *curr; - struct timespec s_time, os_time; - - if((curr = ptr->mem.listp->next) == NULL){ - pthread_mutex_lock(&mut); - snprintf(error_str, sizeof(error_str), "EMERG: Empty Netcard list!\n"); - record_log(error_str); - pthread_mutex_unlock(&mut); - return NULL; - } - s_time.tv_sec = ptr->mem.conf->flow_interval; - s_time.tv_nsec = 0; - - for(i = 0; i < MAXNICNUM ; i++){ - count_ok[i] = 0; - count_no[i] = 0; - } - while(1){ - i = 0; - curr = ptr->mem.listp->next; - while(curr != NULL){ - get_inc_info(&curr->info); - get_inc_stats(&curr->info); - if(flags){ - bytes[i] = curr->info.rx_bytes + 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); - 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); -#if 0 - printf("%s:\tcount=%d\tsummit=%d\tflow_limit:%d\taverage_flow:%d\n", curr->info.charname, count_t, summit, conf.flow_limit, curr->info.average_flow); -#endif - 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); - strncat(ptr->env.Malarm.tSysNetcardAlarm.fault_devname, "-", 1); - strncat(ptr->env.Malarm.tSysNetcardAlarm.fault_devname, curr->info.charname, (NIC_NAME_LEN - ptr->env.host_name_size - 1)); - ptr->env.Malarm.tSysNetcardAlarm.flags = NETCARD_ALARM_ABNORMAL; - ptr->env.Malarm.tSysNetcardAlarm.retrytimes = 0; - ptr->env.Malarm.tMsgFrame.len = sizeof(ptr->env.Malarm); - ptr->env.Malarm.tMsgFrame.seqno = seqno; - pthread_mutex_lock(&mut); - while(count_no[i] > 0){ - send_alarm(&ptr->env.Malarm, ptr->mem.conf->udpport, ptr->mem.conf->ip); - seqno++; - ptr->env.Malarm.tMsgFrame.seqno = seqno; - ptr->env.Malarm.tSysNetcardAlarm.retrytimes++; - count_no[i]--; - if(count_no[i] == 0){ - send_inc_info_one(&curr->info, ptr->mem.semid, ptr->mem.shm_ptr); - //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); - snprintf(error_str, sizeof(error_str), "NOTICE: %s flow is abnormal !\n", curr->info.charname); - record_log(error_str); - } - } - pthread_mutex_unlock(&mut); - count_ok[i] = 3; - }else{ - 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); - strncat(ptr->env.Malarm.tSysNetcardAlarm.fault_devname, "-", 1); - strncat(ptr->env.Malarm.tSysNetcardAlarm.fault_devname, curr->info.charname, (NIC_NAME_LEN - ptr->env.host_name_size - 1)); - ptr->env.Malarm.tSysNetcardAlarm.flags = NETCARD_ALARM_NORMAL; - ptr->env.Malarm.tSysNetcardAlarm.retrytimes = 0; - ptr->env.Malarm.tMsgFrame.len = sizeof(ptr->env.Malarm); - ptr->env.Malarm.tMsgFrame.seqno = seqno; - pthread_mutex_lock(&mut); - while(count_ok[i] > 0){ - send_alarm(&ptr->env.Malarm, ptr->mem.conf->udpport, ptr->mem.conf->ip); - seqno++; - ptr->env.Malarm.tMsgFrame.seqno = seqno; - ptr->env.Malarm.tSysNetcardAlarm.retrytimes++; - count_ok[i]--; - if(count_ok[i] == 0){ - send_inc_info_one(&curr->info, ptr->mem.semid, ptr->mem.shm_ptr); - snprintf(error_str, sizeof(error_str), "NOTICE: %s flow is normal now !\n", curr->info.charname); - record_log(error_str); - } - } - pthread_mutex_unlock(&mut); - count_no[i] = 3; - }/* end if(summit... */ - bytes[i] = curr->info.rx_bytes + curr->info.tx_bytes; - i++; - curr = curr->next; - }/* end while(curr...) */ - flags = 0; - nanosleep(&s_time, &os_time); - }/* end while(1) */ -} -static char *parse_str(char **s) -{ - char *retp = NULL; - char *p = *s; - - while(isspace(*p))p++; - retp = p; - while(!isspace(*p))p++; - *p = '\0'; - - return retp; -} - -int ping_gw(char *devname) -{ - FILE *fp; - int i, j, k; - char buf[128]; - char getbuf[64]; - - i = find_nic_config(devname); - if( i == -1 ) - return 0; - - if(conf.nic[i].gw_num == 0) - return 0; - - for(k=0; kbond_file_path, "r")) == NULL){ - snprintf(error_str, sizeof(error_str), "EMERG: fopen():%s\n", strerror(errno)); - record_log(error_str); - free(ptr); - return NULL; - } - fseek(fp, 65, SEEK_SET); - memset(name, 0, sizeof(name)); - memset(linebuf, 0, sizeof(linebuf)); - if((retp = fgets(linebuf, sizeof(linebuf), fp)) != NULL){ - if((retp = strstr(linebuf, "active-backup")) == NULL){ - free(ptr); - fclose(fp); - return NULL; - } - } - - s_time.tv_sec = 0; - s_time.tv_nsec = 100000000; - - while(1){ - while((str = fgets(linebuf, sizeof(linebuf), fp)) != NULL){ - if(!strncmp(linebuf, "Currently Active Slave:", 23)){ - str += 23; - retp = parse_str(&str); - break; - } - } - if(name[0] == 0){ - strncpy(name, retp, strlen(retp)); - }else if(strncmp(name, retp, strlen(retp)) != 0){ - memset(arg->Malarm.tSysNetcardAlarm.switch_devname, 0, NIC_NAME_LEN); - strncpy(arg->Malarm.tSysNetcardAlarm.switch_devname, arg->host_name, arg->host_name_size); - strncat(arg->Malarm.tSysNetcardAlarm.switch_devname, "-", 1); - strncat(arg->Malarm.tSysNetcardAlarm.switch_devname, retp, (NIC_NAME_LEN - arg->host_name_size - 1)); - arg->Malarm.tSysNetcardAlarm.flags = NETCARD_ALARM_SWITCH; - arg->Malarm.tSysNetcardAlarm.retrytimes = 0; - arg->Malarm.tMsgFrame.len = sizeof(arg->Malarm); - arg->Malarm.tMsgFrame.seqno = seqno; - count = 3; - pthread_mutex_lock(&mut); - snprintf(error_str, sizeof(error_str), "NOTICE: Net card checking to %s!\n", retp); - record_log(error_str); - while(count > 0){ - send_alarm(&arg->Malarm, arg->conf->udpport, arg->conf->ip); - seqno++; - arg->Malarm.tMsgFrame.seqno = seqno; - arg->Malarm.tSysNetcardAlarm.retrytimes++; - count--; - } - pthread_mutex_unlock(&mut); - memset(name, 0, NIC_NAME_LEN); - strncpy(name, retp, strlen(retp)); - } - fclose(fp); - nanosleep(&s_time, &os_time); - if((fp = fopen(arg->bond_file_path, "r")) == NULL){ - free(ptr); - return NULL; - } - fseek(fp, 65, SEEK_SET); - } - free(ptr); - fclose(fp); - return NULL; -} - -static void *create_logfile(void *ptr) -{ - char buf[128]; - char del_file[DAYS][1024]; - char *s; - int head, tail, fd; - int flag = 0, del_flag = 0; - int old_mon = 0; - int old_day = 0; - time_t tamp; - struct tm tmptr; - struct timespec s_time, os_time; - - s_time.tv_sec = 1; - s_time.tv_nsec = 0; - head = tail = 0; - while(1){ - tamp = time(NULL); - localtime_r(&tamp, &tmptr); - if(old_mon != (tmptr.tm_mon + 1))flag = 1; - else if(old_day != tmptr.tm_mday)flag = 1; - else flag = 0; - if(flag){ - s = strrchr(log_path, '/'); - s++; - *s = '\0'; - memset(buf, 0, sizeof(buf)); - strftime(buf, sizeof(buf), "%Y%m%d_", &tmptr); - strcat(buf, process_name); - strcat(buf, ".log"); - strcat(log_path, buf); - mode_t mask_tmp; - mask_tmp = umask(0000); - if((fd = open(log_path, O_RDWR|O_CREAT, 0666)) != -1){ - log_flag = 0; - close(fd); - } - umask(mask_tmp); - old_mon = tmptr.tm_mon+1; - old_day = tmptr.tm_mday; - strcpy(del_file[head++], log_path); - if(head == DAYS){ - head = 0; - del_flag = 1; - } - if(del_flag){ - unlink(del_file[tail]); - tail++; - if(tail == DAYS)tail = 0; - } - } - nanosleep(&s_time, &os_time); - } -} - -void write_time(int semid, SHM *ptr) -{ - int i = 0; - int flag_time_stamp; - - flag_time_stamp = time(NULL); - - while(ptr->info[i].charname[0] != 0){ - ptr->info[i].time_stamp = flag_time_stamp; - i++; - } -} -#if 0 -char *parse(char *line) -{ - char *begin = NULL; - char *end = NULL; - - if((begin = strchr(line, '/')) != NULL){ - if((end = strstr(line, "bin")) != NULL){ - *end='\0'; - return begin; - } - } - return NULL; -} -#endif -void release_name_list(NET_NAME_ST *head) -{ - NET_NAME_ST *curr, *save; - - curr = head->next; - while(curr != NULL){ - save = curr->next; - free(curr); - curr = save; - } -} -int get_netcard(char *line, char *nic) -{ - char *begin, *retp; - - while(isspace(*line))line++; - begin = line; - while(!isspace(*line))line++; - *line++ = '\0'; - if(!strncmp(begin, "nic", 4)){ - if((retp = parse_str(&line)) != NULL){ - strncpy(nic, retp, MAXNICNUM); - return 1; - } - } - return 0; -} - -void sig_handler(int sig) -{ -// char err_str[200]; - -#if 0 - if(unlink(SHM_PATH) == -1){ - snprintf(err_str, sizeof(err_str), "NOTICE: error delete %s\n", strerror(errno)); - record_log(err_str); - } - if(unlink(SEM_PATH) == -1){ - snprintf(err_str, sizeof(err_str), "NOTICE: error delete %s\n", strerror(errno)); - record_log(err_str); - } -#endif -// semctl(sem_id, 0, IPC_RMID); - -// snprintf(err_str, sizeof(err_str), "NOTICE: process exit by signal %d\n", sig); -// record_log(err_str); - exit(0); -} - -/* - * 查看数据结构中是否有给定的网卡 - * 输入:网卡名称 - * 返回值:-1 表示没有 - * 非0值表示设备列表中的索引 - * - */ -int find_nic_config(char *dev_name){ - int i, index=-1; - - for(i=0; iname, myconf[13]) == 0) && (nicnum <= MAXNICNUM)){ - i = 1; - if(cmd->data.list[0] && cmd->data.list[1]){ - //查找设备列表中是否已有该网卡 - //如果没有则创建一项 - //如果已有则只要增加ping的地址和地址数量 - //一般都能找到,因为配置文件中一般总是先定义"nic",后定义"ping" - //如果没找到,则表明先定义了"ping",未定义"nic"或后定义"nic",而未定义"nic"应该是错误的 - index = find_nic_config(cmd->data.list[0]); - if(index == -1){ - strncpy(conf.nic[nicnum].dev_name, cmd->data.list[0], strlen(cmd->data.list[0]) >= IPLEN ? IPLEN-1 : strlen(cmd->data.list[0])); - card_info_index = nicnum; - nicnum++; - } - else - card_info_index = index; - - while(cmd->data.list[i]){ - if((int)(inet_addr(cmd->data.list[i])) == -1){ - sprintf(wrong_ip,"The wrong IP address:'%s'",cmd->data.list[i]); - strcat(wrong_ip,"\n"); - record_log(wrong_ip); - } else{ - strncpy(conf.nic[card_info_index].ping_ip[conf.nic[card_info_index].gw_num++], cmd->data.list[i], strlen(cmd->data.list[i]) >= IPLEN ? IPLEN-1 : strlen(cmd->data.list[i])); - } - i++; - } - } - } - - //"nic"选项的处理 - if((strcmp(cmd ->name, myconf[12]) == 0) && (nicnum <= MAXNICNUM)){ - //设备列表中没有给定的网卡设备,则添加一项 - //否则不必添加 - //对应配置文件中先指定"ping"后指定"nic"的情况 - index = find_nic_config(cmd->data.str); - if(index == -1){ - strncpy(conf.nic[nicnum].dev_name, cmd->data.str, strlen(cmd->data.str) >= NIC_NAME_LEN ? NIC_NAME_LEN-1 : strlen(cmd->data.str)); - nicnum++; - } - } - - if((strcmp(cmd ->name, myconf[11]) == 0)){ - strncpy(conf.udp, cmd->data.str, strlen(cmd->data.str)); - } - - return NULL; -} - -DOTCONF_CB(cb_int) -{ - if(strcmp(cmd ->name, myconf[0]) == 0){ - conf.domain = cmd->data.value; - } - if(strcmp(cmd ->name, myconf[1]) == 0){ - conf.serv = cmd->data.value; - } - if(strcmp(cmd ->name, myconf[2]) == 0){ - conf.event = cmd->data.value; - } - if(strcmp(cmd ->name, myconf[3]) == 0){ - conf.udpport = cmd->data.value; - } - if(strcmp(cmd ->name, myconf[6]) == 0){ - conf.monitor_interval = cmd->data.value; - } - if(strcmp(cmd ->name, myconf[7]) == 0){ - conf.write_interval = cmd->data.value; - } - if(strcmp(cmd ->name, myconf[8]) == 0){ - conf.flow_interval = cmd->data.value; - } - if(strcmp(cmd ->name, myconf[9]) == 0){ - conf.flow_limit = cmd->data.value; - } - if(strcmp(cmd ->name, myconf[10]) == 0){ - conf.flow_peak = cmd->data.value; - } - if(strcmp(cmd ->name, myconf[14]) == 0){ - conf.pingnum = cmd->data.value; - } - if(strcmp(cmd ->name, myconf[15]) == 0){ - conf.pinglap = cmd->data.value; - } - - return NULL; -} - -DOTCONF_CB(cb_list) -{ - return NULL; -} - -int get_conf() -{ -// int uid; -// char buf[1024]; -// int i; -// struct passwd *user; - configfile_t *configfile; - -// memset(buf, 0, sizeof(buf)); -// uid=getuid(); -// user = getpwnam("d5000"); -// user = getpwuid(uid); - -// sprintf(buf,"%s", user->pw_dir); -// strcat(buf,conf_path); - - configfile = dotconf_create(conf_path, options, NULL, CASE_INSENSITIVE); - if (!configfile) - { - record_log("Warning can't reading config file\n"); - return -1; - } - - if (dotconf_command_loop(configfile) == 0){ - record_log("Warning can't reading config file\n"); - return -1; - } - dotconf_cleanup(configfile); - - if(conf.domain == 0) - conf.domain = 10; - if(conf.serv == 0) - conf.serv = 1; - if(conf.event == 0) - conf.event = 2; - if(conf.udpport == 0) - conf.udpport = 15000; - if(conf.monitor_interval == 0) - conf.monitor_interval = 100; - if(conf.write_interval == 0) - conf.write_interval = 300; - if(conf.flow_interval == 0) - conf.flow_interval = 60; - if(conf.flow_limit == 0) - conf.flow_limit = 30; - if(conf.flow_peak == 0) - conf.flow_peak = 1000; - if(conf.udp[0] == 0) - strcpy(conf.udp, "bond0"); - if(conf.nic[0].dev_name[0] == 0) - strcpy(conf.nic[0].dev_name, "bond0"); - if(conf.pingnum == 0) - conf.pingnum = 2; - if(conf.pinglap == 0) - conf.pinglap = 1; - - return 0; -} - -int isrun(char *p) -{ - char buf[16]; - char cmdbuf[1024]; - FILE *fp; - - memset(buf, 0, sizeof(buf)); - memset(cmdbuf, 0, sizeof(cmdbuf)); - - snprintf(cmdbuf, sizeof(cmdbuf), "pidof %s", p); - fp=popen(cmdbuf, "r"); - fread(buf, sizeof(char), sizeof(buf), fp); - pclose(fp); - if(strchr(buf, ' ') != NULL) - return 1; - else - return 0; -} - -int get_broad_ip(void) -{ - char ip_p[4][4]; - char error_str[200]; - int skfd; - struct ifreq ifr; - - memset(&ifr, 0, sizeof(ifr)); - memset(ip_p, 0, sizeof(ip_p)); - - if((skfd = socket(PF_INET, SOCK_DGRAM, 0)) == -1)return -1; - strcpy(ifr.ifr_name, conf.udp); - if (!ioctl(skfd, SIOCGIFBRDADDR, &ifr)){ - sprintf(ip_p[0], "%d", (unsigned char)ifr.ifr_broadaddr.sa_data[2]); - sprintf(ip_p[1], "%d", (unsigned char)ifr.ifr_broadaddr.sa_data[3]); - sprintf(ip_p[2], "%d", (unsigned char)ifr.ifr_broadaddr.sa_data[4]); - sprintf(ip_p[3], "%d", (unsigned char)ifr.ifr_broadaddr.sa_data[5]); - }else{ - snprintf(error_str, sizeof(error_str), "EMERG: Invalid broadcast device -- %s\n", conf.udp); - record_log(error_str); - close(skfd); - return -1; - } - close(skfd); - strcat(conf.ip, ip_p[0]); - strcat(conf.ip, "."); - strcat(conf.ip, ip_p[1]); - strcat(conf.ip, "."); - strcat(conf.ip, ip_p[2]); - strcat(conf.ip, "."); - strcat(conf.ip, ip_p[3]); - - return 0; -} - -void create_dir(char *create_path) -{ - int i,len = strlen(create_path); - - len = strlen(create_path); - - for(i=1; inext; - while(curr != NULL){ - if(!strncmp(curr->name, conf.nic[i].dev_name, NIC_NAME_LEN)){ - if((node = (NET_NAME_ST *)malloc(sizeof(NET_NAME_ST))) == NULL){ - err_val = 1; - break; - } - memset(node->name, 0, NIC_NAME_LEN); - strncpy(node->name, conf.nic[i].dev_name, NIC_NAME_LEN); - node->next = opt_name_list->next; - opt_name_list->next = node; - count_t ++; - break; - } - curr = curr->next; - } - if(err_val)break; - } - if(count_t != i || err_val){ - for(curr = opt_name_list->next; curr != NULL; curr = curr->next)free(curr); - snprintf(error_str, sizeof(error_str), "EMERG: Invalid netcard name --\n"); - record_log(error_str); - return -1; - } - err_val = 0; - - return 0; -} - -int get_netcard_status(NETCARD_INFO_ST *listp) -{ - if((listp->info.flags & IFF_UP) != IFF_UP) - return NIC_DOWN; - - if((listp->info.flags & IFF_RUNNING) != IFF_RUNNING) - return NIC_UNRUNNING; - - //20100510 - if(elapsed_ping_time == total_ping_time){ - if(ping_gw(listp->info.charname) != 0) - return NIC_UNLINKABLE; - } - else{ - if(listp->status == NIC_UNLINKABLE) - return NIC_UNLINKABLE; - } - - return NIC_NORMAL; -} - -int do_alarm(NETCARD_INFO_ST *listp, int nic_status, D5000_NIC_ALARM *Malarm) -{ - int i; - char error_str[200]; - - memset(Malarm->tSysNetcardAlarm.fault_devname, 0, NIC_NAME_LEN); - strncpy(Malarm->tSysNetcardAlarm.fault_devname, host_name, host_name_size); - strncat(Malarm->tSysNetcardAlarm.fault_devname, "-", 1); - strncat(Malarm->tSysNetcardAlarm.fault_devname, listp->info.charname, NIC_NAME_LEN - host_name_size - 1); - Malarm->tSysNetcardAlarm.retrytimes = 0; - Malarm->tMsgFrame.len = sizeof(Malarm); - Malarm->tMsgFrame.seqno = seqno; - - switch(nic_status){ - case NIC_DOWN: - Malarm->tSysNetcardAlarm.flags = NETCARD_ALARM_FAULT; - snprintf(error_str, sizeof(error_str), "NOTICE: %s is DOWN!\n", listp->info.charname); - break; - case NIC_UNRUNNING: - Malarm->tSysNetcardAlarm.flags = NETCARD_ALARM_FAULT; - snprintf(error_str, sizeof(error_str), "NOTICE: %s is UP but UNRUNNING!\n", listp->info.charname); - break; - case NIC_UNLINKABLE: - Malarm->tSysNetcardAlarm.flags = NETCARD_ALARM_FAULT; - listp->info.flags &= (~IFF_UP); - listp->info.flags &= (~IFF_RUNNING); - snprintf(error_str, sizeof(error_str), "NOTICE: %s is RUNNING but UNLINKABLE!\n", listp->info.charname); - break; - case NIC_NORMAL: - Malarm->tSysNetcardAlarm.flags = NETCARD_ALARM_RESUME; - snprintf(error_str, sizeof(error_str), "NOTICE: %s is NORMAL!\n", listp->info.charname); - break; - } - pthread_mutex_lock(&mut); - for(i = 0; i < 3; i++){ - send_alarm(Malarm, conf.udpport, conf.ip); - seqno++; - Malarm->tMsgFrame.seqno = seqno; - Malarm->tSysNetcardAlarm.retrytimes++; - } - send_inc_info_one(&listp->info, semid, shm_ptr); - pthread_mutex_unlock(&mut); - record_log(error_str); - - return 0; -} - -int main(int argc, char ** argv) -{ - int ret = 0, proc_stat = 0; - unsigned int i = 0; - int nic_status; - int sys_time_stamp = 0; - char *retp = NULL; - char *str = NULL; - char bond[128]; - char error_str[200]; - char linebuf[LINE_SIZE]; - THENV *arg; - glob_t res; - FILE *bond_fp = NULL; - pthread_t tid[THRNR]; - pthread_t tid_mem; - pthread_t tid_flow; - pthread_t tid_time; - THMEM mem; - THFLOW flow; - struct timespec s_time, os_time, log_time; - NET_NAME_ST all_name_list, opt_name_list; - NETCARD_INFO_ST inc_info_list, *listp = NULL; - D5000_NIC_ALARM Malarm; - struct sigaction sig; - struct passwd *user; - proc_invocation prcm; - - char log_record[125]={0}; - - memset(&conf, 0, sizeof(CONFIG_FILE_ST)); - if(isrun(argv[0]) == 1){ - printf("the program is already started!\n"); - exit(0); - } -//process register -#if 1 - if((proc_stat=prcm.proc_init("sys","base_srv","sys_nicmonitor"))==-1){ - perror("proc_init()"); - exit(-1); - } -#endif - - sig.sa_handler = sig_handler; - sigemptyset(&sig.sa_mask); - sigaddset(&sig.sa_mask, SIGINT); - sigaddset(&sig.sa_mask, SIGTERM); - sig.sa_flags = 0; - sigaction(SIGTERM, &sig, NULL); - sigaction(SIGINT, &sig, NULL); - sigaction(SIGSEGV, &sig, NULL); - - /* create dir file path */ - if((user = getpwnam("d5000")) != NULL){ - sprintf(log_path,"%s",user->pw_dir); - sprintf(shm_path,"%s",user->pw_dir); - sprintf(sem_path,"%s",user->pw_dir); - sprintf(conf_path,"%s",user->pw_dir); - } - - strcat(log_path,LOG_PATH); - strcat(shm_path,SHM_PATH); - strcat(sem_path,SEM_PATH); - strcat(conf_path,CONF_FILE); - - create_semdir(); - create_shmdir(); - create_logdir(); - create_confdir(); - - /* get process name */ - memset(process_name, 0, sizeof(process_name)); - if((retp = strrchr(argv[0], '/')) != NULL){ - retp++; - strcpy(process_name, retp); - }else{ - strcpy(process_name, argv[0]); - } - /*create logfile thread */ - pthread_create(&tid_time, NULL, create_logfile, NULL); - - /* initialize some....*/ - memset(&all_name_list, 0, sizeof(NET_NAME_ST)); - all_name_list.next = NULL; - memset(&opt_name_list, 0, sizeof(NET_NAME_ST)); - opt_name_list.next = NULL; - - memset(&inc_info_list, 0, sizeof(NETCARD_INFO_ST)); - inc_info_list.next = NULL; - - - /* get all netcard by proc/net/dev and ioctl */ - get_all_name(&all_name_list); - - /* wait for create log thread */ - log_time.tv_sec = 0; - log_time.tv_nsec = 1000000; - while(log_flag)nanosleep(&log_time, NULL); - - /* sure the process is started */ - snprintf(error_str, sizeof(error_str), "NOTICE: %s V%s is started !\n", process_name, MNIC_VERSION); - record_log(error_str); - - get_conf(); - - if(shm_path){ - printf("shm_path:%s\n",shm_path); - sprintf(log_record,"shm_path:%s\n",shm_path); - record_log(log_record); - } - - if(log_path){ - printf("log_path:%s\n",log_path); - sprintf(log_record,"log_path:%s\n",log_path); - record_log(log_record); - } - - - if(conf.udp){ - printf("udp %s\n",conf.udp); - sprintf(log_record,"udp %s\n",conf.udp); - record_log(log_record); - } - - for(i=0;conf.nic[i].dev_name[0]!=0;i++){ - printf("nic %s\n",conf.nic[i].dev_name); - sprintf(log_record,"nic %s\n",conf.nic[i].dev_name); - record_log(log_record); - } - - /* get broadcast address*/ - if((ret = get_broad_ip()) != 0) - return -1; - if((ret = create_name_list(&all_name_list, &opt_name_list)) != 0) - return -1; - /* get hostname */ - if((ret = gethostname(host_name, sizeof(host_name))) != -1){ - host_name_size = strlen(host_name); - }else host_name_size = HOST_NAME_SIZE; - /* initialize Malarm */ - Malarm.tMsgFrame.serv = conf.serv; - Malarm.tMsgFrame.event = conf.event; - Malarm.tMsgFrame.domain = conf.domain; - - /* get bonding file */ - memset(bond, 0, sizeof(bond)); - memset(linebuf, 0, sizeof(linebuf)); - if((ret = glob(BOND_PATH, 0, NULL, &res)) != 0){ - snprintf(error_str, sizeof(error_str), "NOTICE: No bonding netcard !\n"); - record_log(error_str); - strcpy(bond, "No bonding netcard!"); - } - for (i = 0; i < res.gl_pathc; ++i) { - if((arg = (THENV *)malloc(sizeof(THENV))) != NULL){ - memset(arg, 0, sizeof(THENV)); - strncpy(arg->bond_file_path, res.gl_pathv[i], strlen(res.gl_pathv[i])); - arg->Malarm = Malarm; - strncpy(arg->host_name, host_name, host_name_size); - arg->host_name_size = host_name_size; - arg->conf = &conf; - if(pthread_create(tid+i, NULL, check_inc_switch, (void*)arg)){ - snprintf(error_str, sizeof(error_str), "EMERG: pthread_create(): %s\n", strerror(errno)); - record_log(error_str); - } - } - } - for(i = 0; i < res.gl_pathc; ++i){ - if((bond_fp = fopen(res.gl_pathv[i], "r")) == NULL){ - snprintf(error_str, sizeof(error_str), "EMERG: fopen(): %s\n", strerror(errno)); - record_log(error_str); - }else{ - retp = strrchr(res.gl_pathv[i], '/'); - retp++; - strcat(bond, retp); - strcat(bond, ":"); - while((str = fgets(linebuf, sizeof(linebuf), bond_fp)) != NULL){ - if(!strncmp(linebuf, "Slave Interface:", 16)){ - str += 16; - retp = parse_str(&str); - strcat(bond, retp); - strcat(bond, " "); - } - } - fclose(bond_fp); - } - } - - /* initialize time*/ - s_time.tv_sec = 0; - s_time.tv_nsec = conf.monitor_interval*1000*1000; - - /* initialize sem*/ - init_sem(&semid, &conf); - init_shm(&shm_ptr, &conf); - /* for IPC_RMID */ - sem_id = semid; - - /* create monitor netcard list */ - create_monit_list(&opt_name_list, &inc_info_list, bond); - release_name_list(&all_name_list); - release_name_list(&opt_name_list); - - mem.semid = semid; - mem.shm_ptr = shm_ptr; - mem.listp = &inc_info_list; - mem.conf = &conf; - if(pthread_create(&tid_mem, NULL, send_inc_info, (void *)&mem)){ - snprintf(error_str, sizeof(error_str), "EMERG: pthread_create(): %s\n", strerror(errno)); - record_log(error_str); - } - - /* create caculate flow thread*/ - flow.env.Malarm = Malarm; - strncpy(flow.env.host_name, host_name, host_name_size); - flow.env.host_name_size = host_name_size; - flow.mem.semid = semid; - flow.mem.shm_ptr = shm_ptr; - flow.mem.listp = &inc_info_list; - flow.mem.conf = &conf; - if(pthread_create(&tid_flow, NULL, cacu_flow, (void *)&flow)){ - snprintf(error_str, sizeof(error_str), "EMERG: pthread_create(): %s\n", strerror(errno)); - record_log(error_str); - } - - total_ping_time = 1000 / conf.monitor_interval; - if(total_ping_time == 0) - total_ping_time = 1; - elapsed_ping_time = 1; - - while(1){ - i = 0; - listp = inc_info_list.next; - for(i = 0; listp != NULL; i++){ - get_inc_info(&listp->info); - get_inc_stats(&listp->info); - nic_status = get_netcard_status(listp); - if(nic_status != listp->status){ - do_alarm(listp, nic_status, &Malarm); - listp->status = nic_status; - } - listp = listp->next; - } - - if(seqno > 32765)seqno = 0; - nanosleep(&s_time, &os_time); - - - //20100510 - ++elapsed_ping_time; - if(elapsed_ping_time > total_ping_time) - elapsed_ping_time = 1; - - sys_time_stamp += conf.monitor_interval; - if(sys_time_stamp >= 1000){ - sys_time_stamp = 0; - write_time(semid, shm_ptr); - } - } - for (i = 0; i < res.gl_pathc; i++) { - pthread_join(tid[i], NULL); - } - pthread_join(tid_mem, NULL); - pthread_join(tid_flow, NULL); - pthread_join(tid_time, NULL); - pthread_mutex_destroy(&mut); - globfree(&res); - munmap(shm_ptr, sizeof(SHM)); - return 0; -} diff --git a/code/tags/sys_nicmonitor-1.6/mnic.h b/code/tags/sys_nicmonitor-1.6/mnic.h deleted file mode 100644 index d7a6390..0000000 --- a/code/tags/sys_nicmonitor-1.6/mnic.h +++ /dev/null @@ -1,44 +0,0 @@ -#ifndef __MNIC_H -#define __MNIC_H - -//#define _GNU_SOURCE -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include - -#include "sys_netcard.h" -#include -#include -#define IPSIZE 16 - -char *get_name(char *, char *); -int if_fetch(NETCARD_INFO *); -int ioc_get_name(NETCARD_INFO *); -int get_dev_fields(char *p, NETCARD_INFO *); -int get_user_home(int , char *); -void send_alarm(D5000_NIC_ALARM *, int , char *); -void record_log(char *); - -#endif diff --git a/code/tags/sys_nicmonitor-1.6/nicinfo_shm.c b/code/tags/sys_nicmonitor-1.6/nicinfo_shm.c deleted file mode 100644 index c8a8bf2..0000000 --- a/code/tags/sys_nicmonitor-1.6/nicinfo_shm.c +++ /dev/null @@ -1,209 +0,0 @@ -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include - -#include "nicinfo_shm.h" - -#define SHM_PATH "/tmp/sys_netcard_shm_path" -#define SEM_PATH "/tmp/sys_netcard_sem_path" -#define LOG_PATH "/var/log/netcard/" - -typedef struct net_info{ - NETCARD_INFO info[MAXNICNUM]; -}SHM; - -static int semid = 0; -static char log_path[1024]; -static char shm_path[1024]; -static char sem_path[1024]; -SHM *ptr = NULL; - -/* write error in logfile */ -void record_log(char *str) -{ - time_t tamp; - char str_tm[4]; - char log_str[512]; - FILE *log_fp = NULL; - struct tm tmptr; - struct timeval tv; - struct timezone tz; - - if((log_fp = fopen(log_path, "a")) != NULL){ - tamp = time(NULL); - memset(str_tm, 0, sizeof(str_tm)); - memset(log_str, 0, sizeof(log_str)); - localtime_r(&tamp, &tmptr); - gettimeofday(&tv, &tz); - snprintf(str_tm, sizeof(str_tm), "%d", (int)tv.tv_usec/1000); - if(str_tm[1] == '\0')str_tm[1] = '0'; - if(str_tm[2] == '\0')str_tm[2] = '0'; - strftime(log_str, sizeof(log_str), "%F %T.", &tmptr); - strcat(log_str, str_tm); - strcat(log_str, " "); - strcat(log_str, "get_nic_info"); - strcat(log_str, " "); - strcat(log_str, str); - if(fwrite(log_str, 1, strlen(log_str), log_fp) < 1){ - } - fclose(log_fp); - } -} - -void get_sem(int semid) -{ - char err_str[200]; - struct sembuf lock[2]; - - lock[0].sem_num = 0; - lock[0].sem_op = 0; - lock[0].sem_flg = SEM_UNDO; - - lock[1].sem_num = 0; - lock[1].sem_op = 1; - lock[1].sem_flg = SEM_UNDO; - - while(semop(semid, lock, 2)){ - if (errno == EINTR) { - continue; - } - snprintf(err_str, sizeof(err_str), "EMERG: semop():\n", strerror(errno)); - record_log(err_str); - return; - } -} - -void release_sem(int semid) -{ - int ret; - char err_str[200]; - struct sembuf unlock; - - unlock.sem_num = 0; - unlock.sem_op = -1; - unlock.sem_flg = SEM_UNDO; - - if((ret = semop(semid, &unlock, 1)) == -1){ - snprintf(err_str, sizeof(err_str), "EMERG: semop():\n", strerror(errno)); - record_log(err_str); - return; - } -} - - -int init_nic_info(void) -{ - int fd = -1; - int sem_val; - char err_str[200]; - key_t key; - - - if ((key=ftok(sem_path, SEM_PROJ_ID)) == -1) { - snprintf(err_str, sizeof(err_str), "EMERG: ftok():%s\n", strerror(errno)); - record_log(err_str); - return -1; - } - if((semid = semget(key, 1, IPC_CREAT|0666)) == -1){ - snprintf(err_str, sizeof(err_str), "EMERG: Create semaphore error: %s\n", strerror(errno)); - record_log(err_str); - return -1; - } - /* - if((sem_val = semctl(semid, 0, GETVAL, 0)) == -1){ - snprintf(err_str, sizeof(err_str), "EMERG: semctl: %s !\n", strerror(errno)); - record_log(err_str); - semctl(semid, 0, IPC_RMID); - return -1; - } - if(!sem_val){ - if(semctl(semid, 0, SETVAL, 1) == -1){ - snprintf(err_str, sizeof(err_str), "EMERG: semctl: %s !\n", strerror(errno)); - record_log(err_str); - semctl(semid, 0, IPC_RMID); - return -1; - } - } - */ - if((fd = open(shm_path, O_RDONLY)) == -1){ - snprintf(err_str, sizeof(err_str), "EMERG: Error open %s: %s!\n",shm_path, strerror(errno)); - record_log(err_str); - return -1; - } - ptr = (SHM*)mmap(NULL, sizeof(SHM), PROT_READ, MAP_SHARED, fd, 0); - if(ptr == MAP_FAILED){ - snprintf(err_str, sizeof(err_str), "EMERG: mmap():%s\n", strerror(errno)); - record_log(err_str); - return -1; - } - close(fd); - - return 0; -} -int get_nic_info(char *nic_name, NETCARD_INFO *net_info) -{ - int i,ret; - time_t tamp; - struct tm tmptr; - DIR *dir; - char *s; - char buf[128]; - char err_str[200]; - struct passwd *user; - - tamp = time(NULL); - localtime_r(&tamp, &tmptr); - - if((user = getpwnam("d5000"))!= NULL){ - sprintf(log_path,"%s",user->pw_dir); - sprintf(shm_path,"%s",user->pw_dir); - sprintf(sem_path,"%s",user->pw_dir); - } - strcat(log_path,LOG_PATH); - strcat(shm_path,SHM_PATH); - strcat(sem_path,SEM_PATH); - - if((dir = opendir(log_path)) == NULL){ - if(errno == ENOENT){ - strcpy(log_path, "/tmp/"); - } - } - closedir(dir); - s = strrchr(log_path, '/'); - s++; - *s = '\0'; - memset(buf, 0, sizeof(buf)); - strftime(buf, sizeof(buf), "%Y%m%d_", &tmptr); - strcat(buf, "sys_nicmonitor"); - strcat(buf, ".log"); - strcat(log_path, buf); - if((ret = init_nic_info()) == -1){ - return -1; - } - get_sem(semid); - for(i = 0; ptr->info[i].charname[0] != 0; i++){ - if(!strcmp(ptr->info[i].charname, nic_name)){ - // get_sem(semid); - memcpy(net_info, &ptr->info[i], sizeof(NETCARD_INFO)); - release_sem(semid); - munmap(ptr, sizeof(SHM)); - return 0; - } - } - release_sem(semid); - snprintf(err_str, sizeof(err_str), "NOTICE: No information of %s !\n", nic_name); - record_log(err_str); - munmap(ptr, sizeof(SHM)); - return -1; -} diff --git a/code/tags/sys_nicmonitor-1.6/nicinfo_shm.h b/code/tags/sys_nicmonitor-1.6/nicinfo_shm.h deleted file mode 100644 index cd74135..0000000 --- a/code/tags/sys_nicmonitor-1.6/nicinfo_shm.h +++ /dev/null @@ -1,52 +0,0 @@ -#ifndef NIC_SHM_H -#define NIC_SHM_H - -#include - -#ifdef __cplusplus -extern "C" { -#endif - -#ifndef NIC_NAME_LEN -#define NIC_NAME_LEN 64 -#endif -#define SEM_PROJ_ID 's' -#define MAXNICNUM 32 - -typedef long KEYID; -typedef struct _net_info -{ - KEYID ID; - char charname[NIC_NAME_LEN]; - char descr[128]; - struct sockaddr addr; - struct sockaddr broadaddr; - struct sockaddr netmask; - struct sockaddr hwaddr; - int time_stamp; - short flags; - int mtu; - int tx_queue_len; - unsigned long long average_flow; - unsigned long long rx_packets; - unsigned long long tx_packets; - unsigned long long rx_bytes; - unsigned long long tx_bytes; - unsigned long rx_errors; - unsigned long tx_errors; - unsigned long rx_dropped; - unsigned long tx_dropped; - unsigned long rx_multicast; - unsigned long collisions; - unsigned long rx_fifo_errors; - unsigned long tx_carrier_errors; - unsigned long tx_fifo_errors; -}NETCARD_INFO; - -int get_nic_info(char *nic_name, NETCARD_INFO *net_info); - - -#ifdef __cplusplus -} -#endif -#endif diff --git a/code/tags/sys_nicmonitor-1.6/proc_inv.h b/code/tags/sys_nicmonitor-1.6/proc_inv.h deleted file mode 100644 index 945622c..0000000 --- a/code/tags/sys_nicmonitor-1.6/proc_inv.h +++ /dev/null @@ -1,198 +0,0 @@ -//////////////////////////////////////////////////////////////////////////////////// -// -// Copyright (c) Comets' Grp. of Kedong Corp 2008. All Rights Reserved. -// -// FileName : procconf.h -// -// Function : this class realize some basic functions for process managerment, -// such as initiate process, report status of process, check status of process, -// update status of process, get information of process -// -// Author : -// -// Date : -// -// Modify by : -// -// Mod Date : -// -//////////////////////////////////////////////////////////////////////////////////// - -#ifndef _PROCCONF_H -#define _PROCCONF_H - -#include -#include -#include -#include -#include -#include -#include -//#include -#include - -#include "const.h" - -//for alarm ................................ -typedef struct MESS_BH -{ - unsigned char mtype; - int length; -}; -typedef struct PROCESS_ALM -{ - char context_name[MAX_STRING_LEN]; - char app_name[MAX_STRING_LEN]; - char proc_name[MAX_STRING_LEN]; - unsigned char status; -}; -//for alarm end ............................. - -//for mmi.................................... -const int MAX_BUFFER_LEN = 500; -typedef struct MESS_BLOCK -{ - unsigned char num; - char buffer[MAX_BUFFER_LEN]; -}; -typedef struct MESS_PROC -{ - char context_name[MAX_STRING_LEN]; - char app_name[MAX_STRING_LEN]; - char proc_name[MAX_STRING_LEN]; - char status; -}; -//for mmi end ................................ - -const int DEFAULT_PERIOD = 3; -const int COUNT_LIMIT = 2; -const int START_DEFAULT_PERIOD = 60; -const int START_COUNT_LIMIT = 5; -const int APP_COUNT_LIMIT = 1; - -//process critical level -//const int WST_CRITICAL = 1; // Shutdown and reboot workstation when failed -//const int SYS_CRITICAL = 2; // Shutdown and reboot the system on the wst when failed -//const int USER_CRITICAL = 3; // Shutdown and reboot the subsystem when failed -//const int GENERAL = 4; // reboot the process -const int CRUCIAL = 1; // crucial process -const int GENERAL = 0; // general process - - -extern int srv_init(char *service,int port); -extern int Tcp_close(int sockfd); -extern int Tcp_read(int fd,char *ptr,int nbytes); -extern int Tcp_write(int fd,char *ptr,int nbytes); -extern int srv_accept(int fd,struct sockaddr *cli_addr,int *clilen); -extern int client_tcp_open(char *host,char *service,int port); - -typedef struct -{ - char context_name[MAX_STRING_LEN]; - char app_name[MAX_STRING_LEN]; - char proc_name[MAX_STRING_LEN]; - pid_t proc_pid; -}PROC_ADM_INFO; - -//*************************************************************** -// structure name : PROC_INFO -// function : store process informatin -// author : -// date : -// modify by : -// modification : -// mod date : -//*************************************************************** -typedef struct -{ - int position; - char node_name[MAX_STRING_LEN]; - char context_name[MAX_STRING_LEN]; - char app_name[MAX_STRING_LEN]; - char proc_name[MAX_STRING_LEN]; - - unsigned char active_flag; - unsigned char master_flag; - - time_t startup_time; - time_t refresh_time; - short refresh_peri; - unsigned char monitor_type; - - pid_t proc_pid; - unsigned char auto_start; - unsigned char act_timer; - unsigned char start_timer; - unsigned char critical_level; - - char exefile_path[MAX_EXECMD_LEN]; - -}PROC_INFO; - -//*************************************************************** -// structure name : APP_INFO -// function : store application informatin -// author : -// date : -// modify by : -// modification : -// mod date : -//*************************************************************** -typedef struct -{ - int position; - char context_name[MAX_STRING_LEN]; - int context_id; - char app_name[MAX_STRING_LEN]; - int app_id; - unsigned char act_timer; - unsigned char active_flag; -}APP_INFO; - - -typedef struct -{ - int no_proc; - int semdes_cfg; - PROC_INFO proc[MAX_LOCAL_PROCESS]; - APP_INFO app[MAX_LOCAL_APP]; -}PROCCFG; - -class proc_invocation -{ - public: - int m_init; - PROCCFG *proccfg_p; - - public: - proc_invocation(); - ~proc_invocation(); - - int conf_create(); - //int check_proc_status(); - //int update_rtdb(); - //int kill_proc(pid_t pid); - //int start_proc(char *cmdline); - //int send_alarm(char *context_name, char *app_name, char *proc_name, unsigned char status); - //int update_proc_status(char *context_name, char *app_name, char *proc_name, char status); - - //int proc_init(char *context_name, char *app_name, char *proc_name, int critical_level);//exefile_path,auto_start - int proc_init(char *context_name, char *app_name, char *proc_name); - int proc_report(int pos, char status, int intertime=3); - int proc_exit(int proc_pos); - - int get_pos(char *context_name, char *app_name, char *proc_name); - int is_proc_exist(char *context_name, char *app_name, char *proc_name); - int conf_map(); - - int get_procinfo(int position, PROC_ADM_INFO *p_info); - int get_active_pid(int &num, int *p_pidlist); - int is_proc_run(pid_t pid); - int is_proc_run(char *context_name, char *app_name, char *proc_name); - -}; - -#endif - - - diff --git a/code/tags/sys_nicmonitor-1.6/read_netcard.c b/code/tags/sys_nicmonitor-1.6/read_netcard.c deleted file mode 100644 index c745f48..0000000 --- a/code/tags/sys_nicmonitor-1.6/read_netcard.c +++ /dev/null @@ -1,133 +0,0 @@ -#include "mnic.h" - -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; -} - - -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 == '\0') - return NULL; - p++; - break; - } - *name++ = *p++; - } - *name++ = '\0'; - return p; -} - -int get_dev_fields(char *str, NETCARD_INFO *ife) -{ - 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]); - return 0; -} - -int if_fetch(NETCARD_INFO *ife) -{ - int skfd; - struct ifreq ifr; - - if((skfd = socket(PF_INET, SOCK_DGRAM, 0)) == -1)return -1; - - strcpy(ifr.ifr_name, ife->charname); - if (!ioctl(skfd, SIOCGIFFLAGS, &ifr)) - ife->flags = ifr.ifr_flags; - - if (!ioctl(skfd, SIOCGIFHWADDR, &ifr)) - ife->hwaddr = ifr.ifr_hwaddr; - - if (!ioctl(skfd, SIOCGIFMTU, &ifr)) - ife->mtu = ifr.ifr_mtu; - - if (!ioctl(skfd, SIOCGIFTXQLEN, &ifr)) - ife->tx_queue_len = ifr.ifr_qlen; - - if (!ioctl(skfd, SIOCGIFADDR, &ifr)) - ife->addr = ifr.ifr_addr; - - if (!ioctl(skfd, SIOCGIFBRDADDR, &ifr)) - ife->broadaddr = ifr.ifr_broadaddr; - - if (!ioctl(skfd, SIOCGIFNETMASK, &ifr)) - ife->netmask = ifr.ifr_netmask; - - ife->time_stamp = time(NULL); - close(skfd); - return 0; -} - - -int ioc_get_name(NETCARD_INFO *name) -{ - int skfd; - int ifrnum; - int i = 0; - struct ifconf ifc; - struct ifreq buf[MAXNICNUM]; - - if((skfd = socket(PF_INET, SOCK_DGRAM, 0)) == -1)return -1; - memset(buf, 0, sizeof(buf)); - ifc.ifc_len = sizeof(buf); - ifc.ifc_buf = (caddr_t) buf; - ioctl(skfd, SIOCGIFCONF, &ifc); - ifrnum = ifc.ifc_len / sizeof(struct ifreq); - while(ifrnum-- > 0){ - strcpy(name[i].charname, buf[ifrnum].ifr_name); - i++; - } - close(skfd); - return i; -} - diff --git a/code/tags/sys_nicmonitor-1.6/send_alarm.c b/code/tags/sys_nicmonitor-1.6/send_alarm.c deleted file mode 100644 index e88635f..0000000 --- a/code/tags/sys_nicmonitor-1.6/send_alarm.c +++ /dev/null @@ -1,37 +0,0 @@ -#include "mnic.h" - -void send_alarm(D5000_NIC_ALARM *mesg, int socket_port, char *ipv4) -{ - int bytes; - int sock_sd; - int val; - char error_str[200]; - struct sockaddr_in recever; - socklen_t sock_len; - - if((sock_sd = socket(PF_INET, SOCK_DGRAM, IPPROTO_UDP)) == -1){ - snprintf(error_str, sizeof(error_str), "EMERG: socket(): %s\n", strerror(errno)); - record_log(error_str); - return; - } - val=1; - if ((setsockopt(sock_sd, SOL_SOCKET, SO_BROADCAST, &val, sizeof(val))) == -1) { - snprintf(error_str, sizeof(error_str), "EMERG: setsockopt(): %s\n", strerror(errno)); - record_log(error_str); - return; - } - /* init socket*/ - recever.sin_family = AF_INET; - recever.sin_port = htons(socket_port); - inet_pton(AF_INET, ipv4, &recever.sin_addr); - sock_len = sizeof(recever); - /* send alarm*/ - if((bytes = sendto(sock_sd, mesg, sizeof(D5000_NIC_ALARM), 0, (const struct sockaddr *)&recever, sock_len)) < 1){ - snprintf(error_str, sizeof(error_str), "EMERG: sendto(): %s\n", strerror(errno)); - record_log(error_str); - return; - } - //snprintf(error_str, sizeof(error_str), "NOTICE: Alarm information has already send!\n"); - //record_log(error_str); - close(sock_sd); -} diff --git a/code/tags/sys_nicmonitor-1.6/sys_netcard.h b/code/tags/sys_nicmonitor-1.6/sys_netcard.h deleted file mode 100644 index 0d7746c..0000000 --- a/code/tags/sys_nicmonitor-1.6/sys_netcard.h +++ /dev/null @@ -1,88 +0,0 @@ -#ifndef _SYS_NETCARD_H -#define _SYS_NETCARD_H - -#include - -#ifndef NIC_NAME_LEN -#define NIC_NAME_LEN 64 -#endif - -#define SEM_PROJ_ID 's' -#define MAXNICNUM 32 - -#ifdef __cplusplus -extern "C"{ -#endif - -typedef long KEYID; - -typedef struct _msg_frame // message frame -{ - short len; // message length - short seqno; // send sequence - short serv; // services ID - short event; // event ID - unsigned char domain; // domain ID - unsigned char ctxt; // Context ID - short stid; // SOURCE task id - short dtid; // DESTINATION task ID - unsigned char ver_coding; // 版本号 + 编码 - unsigned char mes_type; // 帧类型 -}MSG_FRAME, *LPMSG_FRAME; - -typedef struct _net_info -{ - KEYID ID; - char charname[NIC_NAME_LEN]; - char descr[128]; - struct sockaddr addr; - struct sockaddr broadaddr; - struct sockaddr netmask; - struct sockaddr hwaddr; - int time_stamp; - short flags; - int mtu; - int tx_queue_len; - unsigned long long average_flow; - unsigned long long rx_packets; - unsigned long long tx_packets; - unsigned long long rx_bytes; - unsigned long long tx_bytes; - unsigned long rx_errors; - unsigned long tx_errors; - unsigned long rx_dropped; - unsigned long tx_dropped; - unsigned long rx_multicast; - unsigned long collisions; - unsigned long rx_fifo_errors; - unsigned long tx_carrier_errors; - unsigned long tx_fifo_errors; -}NETCARD_INFO, *LPNETCARD_INFO; - -#define NETCARD_ALARM_FAULT 1 -#define NETCARD_ALARM_RESUME 2 -#define NETCARD_ALARM_SWITCH 3 -#define NETCARD_ALARM_ABNORMAL 4 -#define NETCARD_ALARM_NORMAL 5 -typedef struct _sys_netcard_alarm -{ - char fault_devname[NIC_NAME_LEN]; /*故障设备名称 */ - char switch_devname[NIC_NAME_LEN]; /*切换设备名称 */ - short flags; /* 状态标记 : 1:网卡故障。2:网卡恢复。3:网卡切换。4:流量异常。*/ - short retrytimes; //0,1,2 重发次数 -}SYS_NETCARD_ALARM, *LPSYS_NETCARD_ALARM; - - -typedef struct _d5000_nic_alarm -{ - MSG_FRAME tMsgFrame ; - SYS_NETCARD_ALARM tSysNetcardAlarm ; -}D5000_NIC_ALARM, *LPD5000_NIC_ALARM; - - -#ifdef __cplusplus -} -#endif - -#endif - diff --git a/code/tags/sys_nicmonitor-1.7/Makefile b/code/tags/sys_nicmonitor-1.7/Makefile deleted file mode 100644 index 5cccc77..0000000 --- a/code/tags/sys_nicmonitor-1.7/Makefile +++ /dev/null @@ -1,28 +0,0 @@ -#! /usr/bin/make -f - -DEFAULTS = Makefile.config - -include $(DEFAULTS) - -all:sys_nicmonitor libnic_shm.so - -CFLAGS += -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 $@ $^ -libnic_shm.so:nicinfo_shm.c - $(CC) -fpic -shared -o $@ $^ -clean: - $(RM) *.o - $(RM) libnic_shm.so sys_nicmonitor -install: - $(MKDIR) -p $(LIBDIR) - $(MKDIR) -p $(BINDIR) - $(MKDIR) -p $(INCDIR) - $(CP) libnic_shm.so $(LIBDIR) - $(CP) sys_nicmonitor $(BINDIR) - $(CP) nicinfo_shm.h $(INCDIR) -uninstall: - $(RM) -f $(LIBDIR)/libnic_shm.so - $(RM) -f $(BINDIR)/sys_nicmonitor - $(RM) -f $(INCDIR)/nicinfo_shm.h diff --git a/code/tags/sys_nicmonitor-1.7/Makefile.config b/code/tags/sys_nicmonitor-1.7/Makefile.config deleted file mode 100644 index 7a2d8e6..0000000 --- a/code/tags/sys_nicmonitor-1.7/Makefile.config +++ /dev/null @@ -1,16 +0,0 @@ - -PREFIX = $(DESTDIR) - -BINDIR = $(PREFIX)/usr/bin - -LIBDIR = $(PREFIX)/usr/lib64 - -INCDIR = $(PREFIX)/usr/include - -INSTALL_SH = ./install.sh - -CP = cp -RM = rm -#CC = gcc -CC = g++ -MKDIR = mkdir diff --git a/code/tags/sys_nicmonitor-1.7/const.h b/code/tags/sys_nicmonitor-1.7/const.h deleted file mode 100644 index e2ebfa5..0000000 --- a/code/tags/sys_nicmonitor-1.7/const.h +++ /dev/null @@ -1,51 +0,0 @@ -#ifndef _RTE_CONST_H -#define _RTE_CONST_H - -#include - - -#define MAX_STRING_LEN 24 -#define MAX_EXECMD_LEN 200 -#define MAX_FILENAME_LEN 200 - -#define MAX_CONTEXT 8 -#define MAX_HOSTNAME_LEN 24 -#define MAX_LOCAL_MESPROC 512 -#define MAX_LOCAL_PROCESS MAX_LOCAL_MESPROC -#define MAX_LOCAL_APP 32 -#define MAX_LOCAL_NODE 256 - -#define MAX_SET 256 //max num of event set -#define MAX_EVENT 1300 -#define MAX_REG_PROC 20 - -#define LEN_SHMBLK_BIG 4096 -#define MAX_MSGBUF_LEN 32767 -#define FREE_PAGE_SIZE 65536 -#define MAX_PAGE_NUM 1024 - -#define MAX_QUE MAX_LOCAL_MESPROC -#define MAX_SEMPHORE_SET MAX_LOCAL_MESPROC*2 -#define RTE_HAN 0 // queue number for event handeler0(RTE) -#define EX_EVENT_HAN RTE_HAN // queue number for extrnal event handeler - -#define DOMAIN_I 1 -#define DOMAIN_II 2 -#define DOMAIN_III 3 - -//Add 20090225 -const int PROC_TYPE_RPT =1; -const int PROC_TYPE_UNRPT =0; -//add end - - -//status for proc and app -const int NON_ACTIVE = 0; -const int ACTIVE = 1; -const int HANGUP = 2; -const int FAILURE = 5; -const int START = 6; -const int STOP = 7; - -#endif - diff --git a/code/tags/sys_nicmonitor-1.7/mnic.c b/code/tags/sys_nicmonitor-1.7/mnic.c deleted file mode 100644 index a538209..0000000 --- a/code/tags/sys_nicmonitor-1.7/mnic.c +++ /dev/null @@ -1,1552 +0,0 @@ -#include "mnic.h" -#include "dotconf.h" -#include -#include -#include "proc_inv.h" - -#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.7" - -#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. -}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 find_nic_config(char *); - -static pthread_mutex_t mut = PTHREAD_MUTEX_INITIALIZER; -static short seqno = 1; -static char log_flag = 1; -static int sem_id = 0; -static char process_name[64]; -char log_path[1024]; -char shm_path[1024]; -char sem_path[1024]; -char conf_path[1024]; - -int host_name_size = 0; -char host_name[HOST_NAME_SIZE]; -int semid = -1; -SHM *shm_ptr; - -//20100510 -int total_ping_time, elapsed_ping_time; -int get_netcard_status(NETCARD_INFO_ST *); - -/****dotconf****/ -static DOTCONF_CB(cb_str); -static DOTCONF_CB(cb_int); -int nicnum = 0; -CONFIG_FILE_ST conf; - -char myconf[][64] = { - "domain", // 0 - "serv", // 1 - "event", // 2 - "udpport", // 3 - "sys_netcard_shm_path", // 4 - "sys_netcard_sem_path", // 5 - "monitor_interval", // 6 - "write_interval", // 7 - "flow_interval", // 8 - "flow_limit", // 9 - "flow_peak", // 10 - "udp", // 11 - "nic", // 12 - "ping", // 13 - "pingnum", // 14 - "pinglap", // 15 - "" -}; - -static const configoption_t options[] = { - {myconf[0], ARG_INT, cb_int, NULL, CTX_ALL}, - {myconf[1], ARG_INT, cb_int, NULL, CTX_ALL}, - {myconf[2], ARG_INT, cb_int, NULL, CTX_ALL}, - {myconf[3], ARG_INT, cb_int, NULL, CTX_ALL}, - {myconf[4], ARG_STR, cb_str, NULL, CTX_ALL}, - {myconf[5], ARG_STR, cb_str, NULL, CTX_ALL}, - {myconf[6], ARG_INT, cb_int, NULL, CTX_ALL}, - {myconf[7], ARG_INT, cb_int, NULL, CTX_ALL}, - {myconf[8], ARG_INT, cb_int, NULL, CTX_ALL}, - {myconf[9], ARG_INT, cb_int, NULL, CTX_ALL}, - {myconf[10], ARG_INT, cb_int, NULL, CTX_ALL}, - {myconf[11], ARG_STR, cb_str, NULL, CTX_ALL}, - {myconf[12], ARG_STR, cb_str, NULL, CTX_ALL}, - {myconf[13], ARG_STR, cb_str, NULL, CTX_ALL}, - {myconf[14], ARG_INT, cb_int, NULL, CTX_ALL}, - {myconf[15], ARG_INT, cb_int, NULL, CTX_ALL}, - LAST_OPTION -}; -/****dotconf****/ - -/* write error in logfile */ -void record_log(char *str) -{ - time_t tamp; - char str_tm[4]; - char log_str[512]; - FILE *log_fp = NULL; - struct tm tmptr; - struct timeval tv; - struct timezone tz; - - if((log_fp = fopen(log_path, "a")) != NULL){ - tamp = time(NULL); - memset(str_tm, 0, sizeof(str_tm)); - memset(log_str, 0, sizeof(log_str)); - localtime_r(&tamp, &tmptr); - gettimeofday(&tv, &tz); - snprintf(str_tm, sizeof(str_tm), "%d", (int)tv.tv_usec/1000); - if(str_tm[1] == '\0')str_tm[1] = '0'; - if(str_tm[2] == '\0')str_tm[2] = '0'; - strftime(log_str, sizeof(log_str), "%F %T.", &tmptr); - strcat(log_str, str_tm); - strcat(log_str, " "); - strcat(log_str, process_name); - strcat(log_str, " "); - strcat(log_str, str); - if(fwrite(log_str, 1, strlen(log_str), log_fp) < 1){ - } - fclose(log_fp); - } -} - -static void init_sem(int *semid, CONFIG_FILE_ST *ptr) -{ - key_t key; - char error_str[200]; - - /* write error in logfile */ - memset(error_str, 0, sizeof(error_str)); - if ((key = ftok(sem_path, SEM_PROJ_ID)) == -1) { - snprintf(error_str, sizeof(error_str), "EMERG: ftok():%s\n", strerror(errno)); - record_log(error_str); - return; - } - if((*semid = semget(key, 1, IPC_CREAT|0666)) == -1){ - snprintf(error_str, sizeof(error_str), "EMERG: semget():%s\n", strerror(errno)); - record_log(error_str); - return; - } - if(semctl(*semid, 0, SETVAL, 0) == -1){ - snprintf(error_str, sizeof(error_str), "EMERG: semctl: %s !\n", strerror(errno)); - record_log(error_str); - semctl(*semid, 0, IPC_RMID); - return; - } -} - -static void init_shm(SHM **ptr, CONFIG_FILE_ST *p) -{ - int fd = -1; - char error_str[200]; - - mode_t mask_tmp; - mask_tmp = umask(0000); - if((fd = open(shm_path, O_RDWR|O_CREAT|O_TRUNC, 0666)) == -1){ - printf("could not create %s\n", shm_path); - return; - } - umask(mask_tmp); - lseek(fd, sizeof(SHM), SEEK_SET); - write(fd, "\0", 1); - *ptr = (SHM *)mmap(NULL, sizeof(SHM), PROT_READ|PROT_WRITE, MAP_SHARED, fd, 0); - if(*ptr == MAP_FAILED){ - snprintf(error_str, sizeof(error_str), "EMERG: mmap():%s\n", strerror(errno)); - record_log(error_str); - return; - } - close(fd); - -} - -static void get_sem(int semid) -{ - struct sembuf lock[2]; - char error_str[200]; - - lock[0].sem_num = 0; - lock[0].sem_op = 0; - lock[0].sem_flg = SEM_UNDO; - - lock[1].sem_num = 0; - lock[1].sem_op = 1; - lock[1].sem_flg = SEM_UNDO; - - while(semop(semid, lock, 2)){ - if(errno == EINTR)continue; - snprintf(error_str, sizeof(error_str), "EMERG: get_sem semop():%s\n", strerror(errno)); - record_log(error_str); - return; - } -} - -static void release_sem(int semid) -{ - int ret; - char error_str[200]; - struct sembuf unlock; - - unlock.sem_num = 0; - unlock.sem_op = -1; - unlock.sem_flg = SEM_UNDO; - - if((ret = semop(semid, &unlock, 1)) == -1){ - snprintf(error_str, sizeof(error_str), "EMERG: rel_sem semop():%s\n", strerror(errno)); - record_log(error_str); - return; - } -} - -static void proc_get_name(NET_NAME_ST *nodep) -{ - char linebuf[LINE_SIZE]; - char error_str[200]; - char *retp = NULL; - int err_val=0; - NET_NAME_ST *node = NULL; - FILE *dev_fp; - - if((dev_fp = fopen("/proc/net/dev", "rb")) == NULL){ - snprintf(error_str, sizeof(error_str), "EMERG: Can't open dev file:%s\n", strerror(errno)); - record_log(error_str); - return; - } - memset(linebuf, 0, sizeof(linebuf)); - fgets(linebuf, sizeof(linebuf), dev_fp); - fgets(linebuf, sizeof(linebuf), dev_fp); - while((retp = fgets(linebuf, sizeof(linebuf), dev_fp)) != NULL){ - if((node = (NET_NAME_ST *)malloc(sizeof(NET_NAME_ST))) == NULL){ - err_val = 1; - break; - } - memset(node->name, 0, NIC_NAME_LEN); - get_name(node->name, retp); - node->next = nodep->next; - nodep->next = node; - } - if(err_val){ - NET_NAME_ST *curr = nodep->next; - while(curr != NULL){ - node = curr->next; - free(curr); - curr = node; - } - } - fclose(dev_fp); -} - -static void ioctl_get_name(NET_NAME_ST *nodep) -{ - int count = 0; - int inuse = 0; - int err_val = 0; - NETCARD_INFO net_name[MAXNICNUM]; - NET_NAME_ST *node = NULL, *curr = NULL; - - memset(net_name, 0, sizeof(net_name)); - if((count = ioc_get_name(net_name)) == 0)return; - if(nodep->next == NULL) - inuse = 1; - while(count > 0){ - curr = nodep->next; - while(curr != NULL){ - if(!strncmp(net_name[count - 1].charname, curr->name, NIC_NAME_LEN)){ - inuse = 0; - break; - } - inuse = 1; - curr = curr->next; - } - if(inuse){ - if((node = (NET_NAME_ST *)malloc(sizeof(NET_NAME_ST))) == NULL){ - err_val = 1; - break; - } - memset(node->name, 0, NIC_NAME_LEN); - strncpy(node->name, net_name[count - 1].charname, NIC_NAME_LEN); - node->next = nodep->next; - nodep->next = node; - } - count--; - } - if(err_val){ - curr = nodep->next; - while(curr != NULL){ - node = curr->next; - free(curr); - curr = node; - } - } -} - -static void get_all_name(NET_NAME_ST *nodep) -{ - proc_get_name(nodep); - ioctl_get_name(nodep); -} - -static void create_monit_list(NET_NAME_ST *nodep, NETCARD_INFO_ST *info_list, char *bond) -{ - int err_val = 0; - NETCARD_INFO_ST *info_node = NULL; - NET_NAME_ST *curr, *node; - - curr = nodep->next; - while(curr != NULL){ - if((info_node = (NETCARD_INFO_ST *)malloc(sizeof(NETCARD_INFO_ST))) == NULL){ - err_val = 1; - break; - } - memset(info_node, 0, sizeof(NETCARD_INFO_ST)); - strncpy(info_node->info.charname, curr->name, NIC_NAME_LEN); - strncpy(info_node->info.descr, bond, strlen(bond)); - info_node->next = info_list->next; - info_list->next = info_node; - curr = curr->next; - } - if(err_val){ - NETCARD_INFO_ST *info_curr = info_list->next; - while(info_curr != NULL){ - info_node = info_curr->next; - free(info_curr); - info_curr = info_node; - } - curr = nodep->next; - while(curr != NULL){ - node = curr->next; - free(curr); - curr = node; - } - } -} - -static int get_inc_info(NETCARD_INFO *net) -{ - return if_fetch(net); -} - -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){ - if((str = strstr(linebuf, net->charname)) != NULL){ - str +=strlen(net->charname) + 2; - get_dev_fields(str, net); - break; - } - } - fclose(dev_fp); -} - -static void send_inc_info_one(NETCARD_INFO *net, int semid, SHM *ptr) -{ - int i; - for(i = 0; ptr->info[i].charname[0] != 0; i++){ - if(!strncmp(ptr->info[i].charname, net->charname, NIC_NAME_LEN)){ - get_sem(semid); - memcpy(&ptr->info[i], net, sizeof(NETCARD_INFO)); - release_sem(semid); - } - } -} - -static void *send_inc_info(void *p) -{ - int i = 0; - int flags = 1; - char error_str[200]; - unsigned long long bytes[MAXNICNUM]; - THMEM *ptr = (THMEM *)p; - NETCARD_INFO_ST *curr; - struct timespec s_time, os_time; - - if((curr = ptr->listp->next) == NULL){ - pthread_mutex_lock(&mut); - snprintf(error_str, sizeof(error_str), "EMERG: Empty Netcard list!\n"); - record_log(error_str); - pthread_mutex_unlock(&mut); - return NULL; - } - s_time.tv_sec = ptr->conf->write_interval; - s_time.tv_nsec = 0; - - /* copy all net_info*/ - while(1){ - i = 0; - get_sem(ptr->semid); - //memset(ptr->shm_ptr, 0, sizeof(SHM)); - curr = ptr->listp->next; - while(curr != NULL){ - get_inc_info(&curr->info); - if(curr->status == NIC_UNLINKABLE){ - curr->info.flags &= (~IFF_UP); - curr->info.flags &= (~IFF_RUNNING); - } - get_inc_stats(&curr->info); - if(flags){ - bytes[i] = curr->info.rx_bytes + curr->info.tx_bytes; - } - curr->info.average_flow =(unsigned long long)((curr->info.rx_bytes + curr->info.tx_bytes - bytes[i])/(ptr->conf->write_interval)); - memcpy(&ptr->shm_ptr->info[i], &curr->info, sizeof(NETCARD_INFO)); - bytes[i] = curr->info.rx_bytes + curr->info.tx_bytes; - i++; - curr = curr->next; - } - release_sem(ptr->semid); - flags = 0; - nanosleep(&s_time, &os_time); - } -} - -static int get_netcard_count(char *name) -{ - int count = 0; - FILE *fp = NULL; - char path[64] = "/proc/net/bonding/"; - char buf[LINE_SIZE]; - char *retp = NULL; - char *str = NULL; - - strncat(path, name, strlen(name)); - if((fp = fopen(path, "r")) == NULL)return 1; - - memset(buf, 0, sizeof(buf)); - fseek(fp, 65, SEEK_SET); - while((retp = fgets(buf, sizeof(buf), fp)) != NULL){ - if((str = strstr(buf, "active-backup")) != NULL){ - fclose(fp); - return 1; - } - if(!strncmp(buf, "Slave Interface:", 16))count++; - } - fclose(fp); - return count; -} - -static void *cacu_flow(void *p) -{ - int i = 0, count_t = 1; - int summit = 0; - int flags = 1; - int count_ok[MAXNICNUM]; - int count_no[MAXNICNUM]; - char error_str[200]; - unsigned long long bytes[MAXNICNUM]; - THFLOW *ptr = (THFLOW *)p; - NETCARD_INFO_ST *curr; - struct timespec s_time, os_time; - - if((curr = ptr->mem.listp->next) == NULL){ - pthread_mutex_lock(&mut); - snprintf(error_str, sizeof(error_str), "EMERG: Empty Netcard list!\n"); - record_log(error_str); - pthread_mutex_unlock(&mut); - return NULL; - } - s_time.tv_sec = ptr->mem.conf->flow_interval; - s_time.tv_nsec = 0; - - for(i = 0; i < MAXNICNUM ; i++){ - count_ok[i] = 0; - count_no[i] = 0; - } - while(1){ - i = 0; - curr = ptr->mem.listp->next; - while(curr != NULL){ - get_inc_info(&curr->info); - get_inc_stats(&curr->info); - if(flags){ - bytes[i] = curr->info.rx_bytes + 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); - 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); -#if 0 - printf("%s:\tcount=%d\tsummit=%d\tflow_limit:%d\taverage_flow:%d\n", curr->info.charname, count_t, summit, conf.flow_limit, curr->info.average_flow); -#endif - 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); - strncat(ptr->env.Malarm.tSysNetcardAlarm.fault_devname, "-", 1); - strncat(ptr->env.Malarm.tSysNetcardAlarm.fault_devname, curr->info.charname, (NIC_NAME_LEN - ptr->env.host_name_size - 1)); - ptr->env.Malarm.tSysNetcardAlarm.flags = NETCARD_ALARM_ABNORMAL; - ptr->env.Malarm.tSysNetcardAlarm.retrytimes = 0; - ptr->env.Malarm.tMsgFrame.len = sizeof(ptr->env.Malarm); - ptr->env.Malarm.tMsgFrame.seqno = seqno; - pthread_mutex_lock(&mut); - while(count_no[i] > 0){ - send_alarm(&ptr->env.Malarm, ptr->mem.conf->udpport, ptr->mem.conf->ip); - seqno++; - ptr->env.Malarm.tMsgFrame.seqno = seqno; - ptr->env.Malarm.tSysNetcardAlarm.retrytimes++; - count_no[i]--; - if(count_no[i] == 0){ - send_inc_info_one(&curr->info, ptr->mem.semid, ptr->mem.shm_ptr); - //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); - snprintf(error_str, sizeof(error_str), "NOTICE: %s flow is abnormal !\n", curr->info.charname); - record_log(error_str); - } - } - pthread_mutex_unlock(&mut); - count_ok[i] = 3; - }else{ - 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); - strncat(ptr->env.Malarm.tSysNetcardAlarm.fault_devname, "-", 1); - strncat(ptr->env.Malarm.tSysNetcardAlarm.fault_devname, curr->info.charname, (NIC_NAME_LEN - ptr->env.host_name_size - 1)); - ptr->env.Malarm.tSysNetcardAlarm.flags = NETCARD_ALARM_NORMAL; - ptr->env.Malarm.tSysNetcardAlarm.retrytimes = 0; - ptr->env.Malarm.tMsgFrame.len = sizeof(ptr->env.Malarm); - ptr->env.Malarm.tMsgFrame.seqno = seqno; - pthread_mutex_lock(&mut); - while(count_ok[i] > 0){ - send_alarm(&ptr->env.Malarm, ptr->mem.conf->udpport, ptr->mem.conf->ip); - seqno++; - ptr->env.Malarm.tMsgFrame.seqno = seqno; - ptr->env.Malarm.tSysNetcardAlarm.retrytimes++; - count_ok[i]--; - if(count_ok[i] == 0){ - send_inc_info_one(&curr->info, ptr->mem.semid, ptr->mem.shm_ptr); - snprintf(error_str, sizeof(error_str), "NOTICE: %s flow is normal now !\n", curr->info.charname); - record_log(error_str); - } - } - pthread_mutex_unlock(&mut); - count_no[i] = 3; - }/* end if(summit... */ - bytes[i] = curr->info.rx_bytes + curr->info.tx_bytes; - i++; - curr = curr->next; - }/* end while(curr...) */ - flags = 0; - nanosleep(&s_time, &os_time); - }/* end while(1) */ -} -static char *parse_str(char **s) -{ - char *retp = NULL; - char *p = *s; - - while(isspace(*p))p++; - retp = p; - while(!isspace(*p))p++; - *p = '\0'; - - return retp; -} - -int ping_gw(char *devname) -{ - FILE *fp; - int i, j, k; - char buf[128]; - char getbuf[64]; - - i = find_nic_config(devname); - if( i == -1 ) - return 0; - - if(conf.nic[i].gw_num == 0) - return 0; - - for(k=0; kbond_file_path, "r")) == NULL){ - snprintf(error_str, sizeof(error_str), "EMERG: fopen():%s\n", strerror(errno)); - record_log(error_str); - free(ptr); - return NULL; - } - fseek(fp, 65, SEEK_SET); - memset(name, 0, sizeof(name)); - memset(linebuf, 0, sizeof(linebuf)); - if((retp = fgets(linebuf, sizeof(linebuf), fp)) != NULL){ - if((retp = strstr(linebuf, "active-backup")) == NULL){ - free(ptr); - fclose(fp); - return NULL; - } - } - - s_time.tv_sec = 0; - s_time.tv_nsec = 100000000; - - while(1){ - while((str = fgets(linebuf, sizeof(linebuf), fp)) != NULL){ - if(!strncmp(linebuf, "Currently Active Slave:", 23)){ - str += 23; - retp = parse_str(&str); - break; - } - } - if(name[0] == 0){ - strncpy(name, retp, strlen(retp)); - }else if(strncmp(name, retp, strlen(retp)) != 0){ - memset(arg->Malarm.tSysNetcardAlarm.switch_devname, 0, NIC_NAME_LEN); - strncpy(arg->Malarm.tSysNetcardAlarm.switch_devname, arg->host_name, arg->host_name_size); - strncat(arg->Malarm.tSysNetcardAlarm.switch_devname, "-", 1); - strncat(arg->Malarm.tSysNetcardAlarm.switch_devname, retp, (NIC_NAME_LEN - arg->host_name_size - 1)); - arg->Malarm.tSysNetcardAlarm.flags = NETCARD_ALARM_SWITCH; - arg->Malarm.tSysNetcardAlarm.retrytimes = 0; - arg->Malarm.tMsgFrame.len = sizeof(arg->Malarm); - arg->Malarm.tMsgFrame.seqno = seqno; - count = 3; - pthread_mutex_lock(&mut); - snprintf(error_str, sizeof(error_str), "NOTICE: Net card checking to %s!\n", retp); - record_log(error_str); - while(count > 0){ - send_alarm(&arg->Malarm, arg->conf->udpport, arg->conf->ip); - seqno++; - arg->Malarm.tMsgFrame.seqno = seqno; - arg->Malarm.tSysNetcardAlarm.retrytimes++; - count--; - } - pthread_mutex_unlock(&mut); - memset(name, 0, NIC_NAME_LEN); - strncpy(name, retp, strlen(retp)); - } - fclose(fp); - nanosleep(&s_time, &os_time); - if((fp = fopen(arg->bond_file_path, "r")) == NULL){ - free(ptr); - return NULL; - } - fseek(fp, 65, SEEK_SET); - } - free(ptr); - fclose(fp); - return NULL; -} - -static void *create_logfile(void *ptr) -{ - char buf[128]; - char del_file[DAYS][1024]; - char *s; - int head, tail, fd; - int flag = 0, del_flag = 0; - int old_mon = 0; - int old_day = 0; - time_t tamp; - struct tm tmptr; - struct timespec s_time, os_time; - - s_time.tv_sec = 1; - s_time.tv_nsec = 0; - head = tail = 0; - while(1){ - tamp = time(NULL); - localtime_r(&tamp, &tmptr); - if(old_mon != (tmptr.tm_mon + 1))flag = 1; - else if(old_day != tmptr.tm_mday)flag = 1; - else flag = 0; - if(flag){ - s = strrchr(log_path, '/'); - s++; - *s = '\0'; - memset(buf, 0, sizeof(buf)); - strftime(buf, sizeof(buf), "%Y%m%d_", &tmptr); - strcat(buf, process_name); - strcat(buf, ".log"); - strcat(log_path, buf); - mode_t mask_tmp; - mask_tmp = umask(0000); - if((fd = open(log_path, O_RDWR|O_CREAT, 0666)) != -1){ - log_flag = 0; - close(fd); - } - umask(mask_tmp); - old_mon = tmptr.tm_mon+1; - old_day = tmptr.tm_mday; - strcpy(del_file[head++], log_path); - if(head == DAYS){ - head = 0; - del_flag = 1; - } - if(del_flag){ - unlink(del_file[tail]); - tail++; - if(tail == DAYS)tail = 0; - } - } - nanosleep(&s_time, &os_time); - } -} - -void write_time(int semid, SHM *ptr) -{ - int i = 0; - int flag_time_stamp; - - flag_time_stamp = time(NULL); - - while(ptr->info[i].charname[0] != 0){ - ptr->info[i].time_stamp = flag_time_stamp; - i++; - } -} -#if 0 -char *parse(char *line) -{ - char *begin = NULL; - char *end = NULL; - - if((begin = strchr(line, '/')) != NULL){ - if((end = strstr(line, "bin")) != NULL){ - *end='\0'; - return begin; - } - } - return NULL; -} -#endif -void release_name_list(NET_NAME_ST *head) -{ - NET_NAME_ST *curr, *save; - - curr = head->next; - while(curr != NULL){ - save = curr->next; - free(curr); - curr = save; - } -} -int get_netcard(char *line, char *nic) -{ - char *begin, *retp; - - while(isspace(*line))line++; - begin = line; - while(!isspace(*line))line++; - *line++ = '\0'; - if(!strncmp(begin, "nic", 4)){ - if((retp = parse_str(&line)) != NULL){ - strncpy(nic, retp, MAXNICNUM); - return 1; - } - } - return 0; -} - -void sig_handler(int sig) -{ - // char err_str[200]; - -#if 0 - if(unlink(SHM_PATH) == -1){ - snprintf(err_str, sizeof(err_str), "NOTICE: error delete %s\n", strerror(errno)); - record_log(err_str); - } - if(unlink(SEM_PATH) == -1){ - snprintf(err_str, sizeof(err_str), "NOTICE: error delete %s\n", strerror(errno)); - record_log(err_str); - } -#endif - // semctl(sem_id, 0, IPC_RMID); - - // snprintf(err_str, sizeof(err_str), "NOTICE: process exit by signal %d\n", sig); - // record_log(err_str); - exit(0); -} - -/* - * 查看数据结构中是否有给定的网卡 - * 输入:网卡名称 - * 返回值:-1 表示没有 - * 非0值表示设备列表中的索引 - * - */ -int find_nic_config(char *dev_name){ - int i, index=-1; - - for(i=0; iname, myconf[13]) == 0) && (nicnum <= MAXNICNUM)){ - i = 1; - if(cmd->data.list[0] && cmd->data.list[1]){ - //查找设备列表中是否已有该网卡 - //如果没有则创建一项 - //如果已有则只要增加ping的地址和地址数量 - //一般都能找到,因为配置文件中一般总是先定义"nic",后定义"ping" - //如果没找到,则表明先定义了"ping",未定义"nic"或后定义"nic",而未定义"nic"应该是错误的 - index = find_nic_config(cmd->data.list[0]); - if(index == -1){ - strncpy(conf.nic[nicnum].dev_name, cmd->data.list[0], strlen(cmd->data.list[0]) >= IPLEN ? IPLEN-1 : strlen(cmd->data.list[0])); - card_info_index = nicnum; - nicnum++; - } - else - card_info_index = index; - - while(cmd->data.list[i]){ - if((int)(inet_addr(cmd->data.list[i])) == -1){ - sprintf(wrong_ip,"The wrong IP address:'%s'",cmd->data.list[i]); - strcat(wrong_ip,"\n"); - record_log(wrong_ip); - } else{ - strncpy(conf.nic[card_info_index].ping_ip[conf.nic[card_info_index].gw_num++], cmd->data.list[i], strlen(cmd->data.list[i]) >= IPLEN ? IPLEN-1 : strlen(cmd->data.list[i])); - } - i++; - } - } - } - - //"nic"选项的处理 - if((strcmp(cmd ->name, myconf[12]) == 0) && (nicnum <= MAXNICNUM)){ - //设备列表中没有给定的网卡设备,则添加一项 - //否则不必添加 - //对应配置文件中先指定"ping"后指定"nic"的情况 - index = find_nic_config(cmd->data.str); - if(index == -1){ - strncpy(conf.nic[nicnum].dev_name, cmd->data.str, strlen(cmd->data.str) >= NIC_NAME_LEN ? NIC_NAME_LEN-1 : strlen(cmd->data.str)); - nicnum++; - } - } - - if((strcmp(cmd ->name, myconf[11]) == 0)){ - strncpy(conf.udp, cmd->data.str, strlen(cmd->data.str)); - } - - return NULL; -} - -DOTCONF_CB(cb_int) -{ - if(strcmp(cmd ->name, myconf[0]) == 0){ - conf.domain = cmd->data.value; - } - if(strcmp(cmd ->name, myconf[1]) == 0){ - conf.serv = cmd->data.value; - } - if(strcmp(cmd ->name, myconf[2]) == 0){ - conf.event = cmd->data.value; - } - if(strcmp(cmd ->name, myconf[3]) == 0){ - conf.udpport = cmd->data.value; - } - if(strcmp(cmd ->name, myconf[6]) == 0){ - conf.monitor_interval = cmd->data.value; - } - if(strcmp(cmd ->name, myconf[7]) == 0){ - conf.write_interval = cmd->data.value; - } - if(strcmp(cmd ->name, myconf[8]) == 0){ - conf.flow_interval = cmd->data.value; - } - if(strcmp(cmd ->name, myconf[9]) == 0){ - conf.flow_limit = cmd->data.value; - } - if(strcmp(cmd ->name, myconf[10]) == 0){ - conf.flow_peak = cmd->data.value; - } - if(strcmp(cmd ->name, myconf[14]) == 0){ - conf.pingnum = cmd->data.value; - } - if(strcmp(cmd ->name, myconf[15]) == 0){ - conf.pinglap = cmd->data.value; - } - - return NULL; -} - -DOTCONF_CB(cb_list) -{ - return NULL; -} - -int get_conf() -{ - // int uid; - // char buf[1024]; - // int i; - // struct passwd *user; - configfile_t *configfile; - - // memset(buf, 0, sizeof(buf)); - // uid=getuid(); - // user = getpwnam("d5000"); - // user = getpwuid(uid); - - // sprintf(buf,"%s", user->pw_dir); - // strcat(buf,conf_path); - - configfile = dotconf_create(conf_path, options, NULL, CASE_INSENSITIVE); - if (!configfile) - { - record_log("Warning can't reading config file\n"); - return -1; - } - - if (dotconf_command_loop(configfile) == 0){ - record_log("Warning can't reading config file\n"); - return -1; - } - dotconf_cleanup(configfile); - - if(conf.domain == 0) - conf.domain = 10; - if(conf.serv == 0) - conf.serv = 1; - if(conf.event == 0) - conf.event = 2; - if(conf.udpport == 0) - conf.udpport = 15000; - if(conf.monitor_interval == 0) - conf.monitor_interval = 100; - if(conf.write_interval == 0) - conf.write_interval = 300; - if(conf.flow_interval == 0) - conf.flow_interval = 60; - if(conf.flow_limit == 0) - conf.flow_limit = 30; - if(conf.flow_peak == 0) - conf.flow_peak = 1000; - if(conf.udp[0] == 0) - strcpy(conf.udp, "bond0"); - if(conf.nic[0].dev_name[0] == 0) - strcpy(conf.nic[0].dev_name, "bond0"); - if(conf.pingnum == 0) - conf.pingnum = 2; - if(conf.pinglap == 0) - conf.pinglap = 1; - - return 0; -} - -int isrun(char *p) -{ - char buf[16]; - char cmdbuf[1024]; - FILE *fp; - - memset(buf, 0, sizeof(buf)); - memset(cmdbuf, 0, sizeof(cmdbuf)); - - snprintf(cmdbuf, sizeof(cmdbuf), "pidof %s", p); - fp=popen(cmdbuf, "r"); - fread(buf, sizeof(char), sizeof(buf), fp); - pclose(fp); - if(strchr(buf, ' ') != NULL) - return 1; - else - return 0; -} - -int get_broad_ip(void) -{ - char ip_p[4][4]; - char error_str[200]; - int skfd; - struct ifreq ifr; - - memset(&ifr, 0, sizeof(ifr)); - memset(ip_p, 0, sizeof(ip_p)); - - if((skfd = socket(PF_INET, SOCK_DGRAM, 0)) == -1)return -1; - strcpy(ifr.ifr_name, conf.udp); - if (!ioctl(skfd, SIOCGIFBRDADDR, &ifr)){ - sprintf(ip_p[0], "%d", (unsigned char)ifr.ifr_broadaddr.sa_data[2]); - sprintf(ip_p[1], "%d", (unsigned char)ifr.ifr_broadaddr.sa_data[3]); - sprintf(ip_p[2], "%d", (unsigned char)ifr.ifr_broadaddr.sa_data[4]); - sprintf(ip_p[3], "%d", (unsigned char)ifr.ifr_broadaddr.sa_data[5]); - }else{ - snprintf(error_str, sizeof(error_str), "EMERG: Invalid broadcast device -- %s\n", conf.udp); - record_log(error_str); - close(skfd); - return -1; - } - close(skfd); - strcat(conf.ip, ip_p[0]); - strcat(conf.ip, "."); - strcat(conf.ip, ip_p[1]); - strcat(conf.ip, "."); - strcat(conf.ip, ip_p[2]); - strcat(conf.ip, "."); - strcat(conf.ip, ip_p[3]); - - return 0; -} - -void create_dir(char *create_path) -{ - int i,len = strlen(create_path); - - len = strlen(create_path); - - for(i=1; inext; - while(curr != NULL){ - if(!strncmp(curr->name, conf.nic[i].dev_name, NIC_NAME_LEN)){ - if((node = (NET_NAME_ST *)malloc(sizeof(NET_NAME_ST))) == NULL){ - err_val = 1; - break; - } - memset(node->name, 0, NIC_NAME_LEN); - strncpy(node->name, conf.nic[i].dev_name, NIC_NAME_LEN); - node->next = opt_name_list->next; - opt_name_list->next = node; - count_t ++; - break; - } - curr = curr->next; - } - if(err_val)break; - } - if(count_t != i || err_val){ - for(curr = opt_name_list->next; curr != NULL; curr = curr->next)free(curr); - snprintf(error_str, sizeof(error_str), "EMERG: Invalid netcard name --\n"); - record_log(error_str); - return -1; - } - err_val = 0; - - return 0; -} - -int get_netcard_status(NETCARD_INFO_ST *listp) -{ - if((listp->info.flags & IFF_UP) != IFF_UP) - return NIC_DOWN; - - if((listp->info.flags & IFF_RUNNING) != IFF_RUNNING) - return NIC_UNRUNNING; - - //20100510 - if(elapsed_ping_time == total_ping_time){ - if(ping_gw(listp->info.charname) != 0) - return NIC_UNLINKABLE; - } - else{ - if(listp->status == NIC_UNLINKABLE) - return NIC_UNLINKABLE; - } - - return NIC_NORMAL; -} - -int do_alarm(NETCARD_INFO_ST *listp, int nic_status, D5000_NIC_ALARM *Malarm) -{ - int i; - char error_str[200]; - - memset(Malarm->tSysNetcardAlarm.fault_devname, 0, NIC_NAME_LEN); - strncpy(Malarm->tSysNetcardAlarm.fault_devname, host_name, host_name_size); - strncat(Malarm->tSysNetcardAlarm.fault_devname, "-", 1); - strncat(Malarm->tSysNetcardAlarm.fault_devname, listp->info.charname, NIC_NAME_LEN - host_name_size - 1); - Malarm->tSysNetcardAlarm.retrytimes = 0; - Malarm->tMsgFrame.len = sizeof(Malarm); - Malarm->tMsgFrame.seqno = seqno; - - switch(nic_status){ - case NIC_DOWN: - Malarm->tSysNetcardAlarm.flags = NETCARD_ALARM_FAULT; - snprintf(error_str, sizeof(error_str), "NOTICE: %s is DOWN!\n", listp->info.charname); - break; - case NIC_UNRUNNING: - Malarm->tSysNetcardAlarm.flags = NETCARD_ALARM_FAULT; - snprintf(error_str, sizeof(error_str), "NOTICE: %s is UP but UNRUNNING!\n", listp->info.charname); - break; - case NIC_UNLINKABLE: - Malarm->tSysNetcardAlarm.flags = NETCARD_ALARM_FAULT; - listp->info.flags &= (~IFF_UP); - listp->info.flags &= (~IFF_RUNNING); - snprintf(error_str, sizeof(error_str), "NOTICE: %s is RUNNING but UNLINKABLE!\n", listp->info.charname); - break; - case NIC_NORMAL: - Malarm->tSysNetcardAlarm.flags = NETCARD_ALARM_RESUME; - snprintf(error_str, sizeof(error_str), "NOTICE: %s is NORMAL!\n", listp->info.charname); - break; - } - pthread_mutex_lock(&mut); - for(i = 0; i < 3; i++){ - send_alarm(Malarm, conf.udpport, conf.ip); - seqno++; - Malarm->tMsgFrame.seqno = seqno; - Malarm->tSysNetcardAlarm.retrytimes++; - } - send_inc_info_one(&listp->info, semid, shm_ptr); - pthread_mutex_unlock(&mut); - record_log(error_str); - - return 0; -} - -int main(int argc, char ** argv) -{ - int ret = 0, proc_stat = 0; - unsigned int i = 0; - int nic_status; - int sys_time_stamp = 0; - char *retp = NULL; - char *str = NULL; - char bond[128]; - char error_str[200]; - char linebuf[LINE_SIZE]; - THENV *arg; - glob_t res; - FILE *bond_fp = NULL; - pthread_t tid[THRNR]; - pthread_t tid_mem; - pthread_t tid_flow; - pthread_t tid_time; - THMEM mem; - THFLOW flow; - struct timespec s_time, os_time, log_time; - NET_NAME_ST all_name_list, opt_name_list; - NETCARD_INFO_ST inc_info_list, *listp = NULL; - D5000_NIC_ALARM Malarm; - struct sigaction sig; - struct passwd *user; - proc_invocation prcm; - - char log_record[125]={0}; - - memset(&conf, 0, sizeof(CONFIG_FILE_ST)); - if(isrun(argv[0]) == 1){ - printf("the program is already started!\n"); - exit(0); - } - //process register -#if 1 - if((proc_stat=prcm.proc_init("sys","base_srv","sys_nicmonitor"))==-1){ - perror("proc_init()"); - exit(-1); - } -#endif - - sig.sa_handler = sig_handler; - sigemptyset(&sig.sa_mask); - sigaddset(&sig.sa_mask, SIGINT); - sigaddset(&sig.sa_mask, SIGTERM); - sig.sa_flags = 0; - sigaction(SIGTERM, &sig, NULL); - sigaction(SIGINT, &sig, NULL); - sigaction(SIGSEGV, &sig, NULL); - - /* create dir file path */ - if((user = getpwnam("d5000")) != NULL){ - sprintf(log_path,"%s",user->pw_dir); - sprintf(shm_path,"%s",user->pw_dir); - sprintf(sem_path,"%s",user->pw_dir); - sprintf(conf_path,"%s",user->pw_dir); - } - - strcat(log_path,LOG_PATH); - strcat(shm_path,SHM_PATH); - strcat(sem_path,SEM_PATH); - strcat(conf_path,CONF_FILE); - - create_semdir(); - create_shmdir(); - create_logdir(); - create_confdir(); - - /* get process name */ - memset(process_name, 0, sizeof(process_name)); - if((retp = strrchr(argv[0], '/')) != NULL){ - retp++; - strcpy(process_name, retp); - }else{ - strcpy(process_name, argv[0]); - } - /*create logfile thread */ - pthread_create(&tid_time, NULL, create_logfile, NULL); - - /* initialize some....*/ - memset(&all_name_list, 0, sizeof(NET_NAME_ST)); - all_name_list.next = NULL; - memset(&opt_name_list, 0, sizeof(NET_NAME_ST)); - opt_name_list.next = NULL; - - memset(&inc_info_list, 0, sizeof(NETCARD_INFO_ST)); - inc_info_list.next = NULL; - - - /* get all netcard by proc/net/dev and ioctl */ - get_all_name(&all_name_list); - - /* wait for create log thread */ - log_time.tv_sec = 0; - log_time.tv_nsec = 1000000; - while(log_flag)nanosleep(&log_time, NULL); - - /* sure the process is started */ - snprintf(error_str, sizeof(error_str), "NOTICE: %s V%s is started !\n", process_name, MNIC_VERSION); - record_log(error_str); - - get_conf(); - - if(shm_path){ - printf("shm_path:%s\n",shm_path); - sprintf(log_record,"shm_path:%s\n",shm_path); - record_log(log_record); - } - - if(log_path){ - printf("log_path:%s\n",log_path); - sprintf(log_record,"log_path:%s\n",log_path); - record_log(log_record); - } - - - if(conf.udp){ - printf("udp %s\n",conf.udp); - sprintf(log_record,"udp %s\n",conf.udp); - record_log(log_record); - } - - for(i=0;conf.nic[i].dev_name[0]!=0;i++){ - printf("nic %s\n",conf.nic[i].dev_name); - sprintf(log_record,"nic %s\n",conf.nic[i].dev_name); - record_log(log_record); - } - - /* get broadcast address*/ - if((ret = get_broad_ip()) != 0) - return -1; - if((ret = create_name_list(&all_name_list, &opt_name_list)) != 0) - return -1; - /* get hostname */ - if((ret = gethostname(host_name, sizeof(host_name))) != -1){ - host_name_size = strlen(host_name); - }else host_name_size = HOST_NAME_SIZE; - /* initialize Malarm */ - Malarm.tMsgFrame.serv = conf.serv; - Malarm.tMsgFrame.event = conf.event; - Malarm.tMsgFrame.domain = conf.domain; - - /* get bonding file */ - memset(bond, 0, sizeof(bond)); - memset(linebuf, 0, sizeof(linebuf)); - if((ret = glob(BOND_PATH, 0, NULL, &res)) != 0){ - snprintf(error_str, sizeof(error_str), "NOTICE: No bonding netcard !\n"); - record_log(error_str); - strcpy(bond, "No bonding netcard!"); - } - for (i = 0; i < res.gl_pathc; ++i) { - if((arg = (THENV *)malloc(sizeof(THENV))) != NULL){ - memset(arg, 0, sizeof(THENV)); - strncpy(arg->bond_file_path, res.gl_pathv[i], strlen(res.gl_pathv[i])); - arg->Malarm = Malarm; - strncpy(arg->host_name, host_name, host_name_size); - arg->host_name_size = host_name_size; - arg->conf = &conf; - if(pthread_create(tid+i, NULL, check_inc_switch, (void*)arg)){ - snprintf(error_str, sizeof(error_str), "EMERG: pthread_create(): %s\n", strerror(errno)); - record_log(error_str); - } - } - } - for(i = 0; i < res.gl_pathc; ++i){ - if((bond_fp = fopen(res.gl_pathv[i], "r")) == NULL){ - snprintf(error_str, sizeof(error_str), "EMERG: fopen(): %s\n", strerror(errno)); - record_log(error_str); - }else{ - retp = strrchr(res.gl_pathv[i], '/'); - retp++; - strcat(bond, retp); - strcat(bond, ":"); - while((str = fgets(linebuf, sizeof(linebuf), bond_fp)) != NULL){ - if(!strncmp(linebuf, "Slave Interface:", 16)){ - str += 16; - retp = parse_str(&str); - strcat(bond, retp); - strcat(bond, " "); - } - } - fclose(bond_fp); - } - } - - /* initialize time*/ - s_time.tv_sec = 0; - s_time.tv_nsec = conf.monitor_interval*1000*1000; - - /* initialize sem*/ - init_sem(&semid, &conf); - init_shm(&shm_ptr, &conf); - /* for IPC_RMID */ - sem_id = semid; - - /* create monitor netcard list */ - create_monit_list(&opt_name_list, &inc_info_list, bond); - release_name_list(&all_name_list); - release_name_list(&opt_name_list); - - mem.semid = semid; - mem.shm_ptr = shm_ptr; - mem.listp = &inc_info_list; - mem.conf = &conf; - if(pthread_create(&tid_mem, NULL, send_inc_info, (void *)&mem)){ - snprintf(error_str, sizeof(error_str), "EMERG: pthread_create(): %s\n", strerror(errno)); - record_log(error_str); - } - - /* create caculate flow thread*/ - flow.env.Malarm = Malarm; - strncpy(flow.env.host_name, host_name, host_name_size); - flow.env.host_name_size = host_name_size; - flow.mem.semid = semid; - flow.mem.shm_ptr = shm_ptr; - flow.mem.listp = &inc_info_list; - flow.mem.conf = &conf; - if(pthread_create(&tid_flow, NULL, cacu_flow, (void *)&flow)){ - snprintf(error_str, sizeof(error_str), "EMERG: pthread_create(): %s\n", strerror(errno)); - record_log(error_str); - } - - total_ping_time = 1000 / conf.monitor_interval; - if(total_ping_time == 0) - total_ping_time = 1; - elapsed_ping_time = 1; - - while(1){ - i = 0; - listp = inc_info_list.next; - for(i = 0; listp != NULL; i++){ - get_inc_info(&listp->info); - get_inc_stats(&listp->info); - nic_status = get_netcard_status(listp); - if(nic_status != listp->status){ - do_alarm(listp, nic_status, &Malarm); - listp->status = nic_status; - } - listp = listp->next; - } - - if(seqno > 32765)seqno = 0; - nanosleep(&s_time, &os_time); - - - //20100510 - ++elapsed_ping_time; - if(elapsed_ping_time > total_ping_time) - elapsed_ping_time = 1; - - sys_time_stamp += conf.monitor_interval; - if(sys_time_stamp >= 1000){ - sys_time_stamp = 0; - write_time(semid, shm_ptr); - } - } - for (i = 0; i < res.gl_pathc; i++) { - pthread_join(tid[i], NULL); - } - pthread_join(tid_mem, NULL); - pthread_join(tid_flow, NULL); - pthread_join(tid_time, NULL); - pthread_mutex_destroy(&mut); - globfree(&res); - munmap(shm_ptr, sizeof(SHM)); - return 0; -} diff --git a/code/tags/sys_nicmonitor-1.7/mnic.h b/code/tags/sys_nicmonitor-1.7/mnic.h deleted file mode 100644 index d7a6390..0000000 --- a/code/tags/sys_nicmonitor-1.7/mnic.h +++ /dev/null @@ -1,44 +0,0 @@ -#ifndef __MNIC_H -#define __MNIC_H - -//#define _GNU_SOURCE -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include - -#include "sys_netcard.h" -#include -#include -#define IPSIZE 16 - -char *get_name(char *, char *); -int if_fetch(NETCARD_INFO *); -int ioc_get_name(NETCARD_INFO *); -int get_dev_fields(char *p, NETCARD_INFO *); -int get_user_home(int , char *); -void send_alarm(D5000_NIC_ALARM *, int , char *); -void record_log(char *); - -#endif diff --git a/code/tags/sys_nicmonitor-1.7/nicinfo_shm.c b/code/tags/sys_nicmonitor-1.7/nicinfo_shm.c deleted file mode 100644 index 7363568..0000000 --- a/code/tags/sys_nicmonitor-1.7/nicinfo_shm.c +++ /dev/null @@ -1,209 +0,0 @@ -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include - -#include "nicinfo_shm.h" - -#define SHM_PATH "/share/sys_netcard_shm_path" -#define SEM_PATH "/share/sys_netcard_sem_path" -#define LOG_PATH "/var/log/netcard/" - -typedef struct net_info{ - NETCARD_INFO info[MAXNICNUM]; -}SHM; - -static int semid = 0; -static char log_path[1024]; -static char shm_path[1024]; -static char sem_path[1024]; -SHM *ptr = NULL; - -/* write error in logfile */ -void record_log(char *str) -{ - time_t tamp; - char str_tm[4]; - char log_str[512]; - FILE *log_fp = NULL; - struct tm tmptr; - struct timeval tv; - struct timezone tz; - - if((log_fp = fopen(log_path, "a")) != NULL){ - tamp = time(NULL); - memset(str_tm, 0, sizeof(str_tm)); - memset(log_str, 0, sizeof(log_str)); - localtime_r(&tamp, &tmptr); - gettimeofday(&tv, &tz); - snprintf(str_tm, sizeof(str_tm), "%d", (int)tv.tv_usec/1000); - if(str_tm[1] == '\0')str_tm[1] = '0'; - if(str_tm[2] == '\0')str_tm[2] = '0'; - strftime(log_str, sizeof(log_str), "%F %T.", &tmptr); - strcat(log_str, str_tm); - strcat(log_str, " "); - strcat(log_str, "get_nic_info"); - strcat(log_str, " "); - strcat(log_str, str); - if(fwrite(log_str, 1, strlen(log_str), log_fp) < 1){ - } - fclose(log_fp); - } -} - -void get_sem(int semid) -{ - char err_str[200]; - struct sembuf lock[2]; - - lock[0].sem_num = 0; - lock[0].sem_op = 0; - lock[0].sem_flg = SEM_UNDO; - - lock[1].sem_num = 0; - lock[1].sem_op = 1; - lock[1].sem_flg = SEM_UNDO; - - while(semop(semid, lock, 2)){ - if (errno == EINTR) { - continue; - } - snprintf(err_str, sizeof(err_str), "EMERG: semop():\n", strerror(errno)); - record_log(err_str); - return; - } -} - -void release_sem(int semid) -{ - int ret; - char err_str[200]; - struct sembuf unlock; - - unlock.sem_num = 0; - unlock.sem_op = -1; - unlock.sem_flg = SEM_UNDO; - - if((ret = semop(semid, &unlock, 1)) == -1){ - snprintf(err_str, sizeof(err_str), "EMERG: semop():\n", strerror(errno)); - record_log(err_str); - return; - } -} - - -int init_nic_info(void) -{ - int fd = -1; - int sem_val; - char err_str[200]; - key_t key; - - - if ((key=ftok(sem_path, SEM_PROJ_ID)) == -1) { - snprintf(err_str, sizeof(err_str), "EMERG: ftok():%s\n", strerror(errno)); - record_log(err_str); - return -1; - } - if((semid = semget(key, 1, IPC_CREAT|0666)) == -1){ - snprintf(err_str, sizeof(err_str), "EMERG: Create semaphore error: %s\n", strerror(errno)); - record_log(err_str); - return -1; - } - /* - if((sem_val = semctl(semid, 0, GETVAL, 0)) == -1){ - snprintf(err_str, sizeof(err_str), "EMERG: semctl: %s !\n", strerror(errno)); - record_log(err_str); - semctl(semid, 0, IPC_RMID); - return -1; - } - if(!sem_val){ - if(semctl(semid, 0, SETVAL, 1) == -1){ - snprintf(err_str, sizeof(err_str), "EMERG: semctl: %s !\n", strerror(errno)); - record_log(err_str); - semctl(semid, 0, IPC_RMID); - return -1; - } - } - */ - if((fd = open(shm_path, O_RDONLY)) == -1){ - snprintf(err_str, sizeof(err_str), "EMERG: Error open %s: %s!\n",shm_path, strerror(errno)); - record_log(err_str); - return -1; - } - ptr = (SHM*)mmap(NULL, sizeof(SHM), PROT_READ, MAP_SHARED, fd, 0); - if(ptr == MAP_FAILED){ - snprintf(err_str, sizeof(err_str), "EMERG: mmap():%s\n", strerror(errno)); - record_log(err_str); - return -1; - } - close(fd); - - return 0; -} -int get_nic_info(char *nic_name, NETCARD_INFO *net_info) -{ - int i,ret; - time_t tamp; - struct tm tmptr; - DIR *dir; - char *s; - char buf[128]; - char err_str[200]; - struct passwd *user; - - tamp = time(NULL); - localtime_r(&tamp, &tmptr); - - if((user = getpwnam("d5000"))!= NULL){ - sprintf(log_path,"%s",user->pw_dir); - sprintf(shm_path,"%s",user->pw_dir); - sprintf(sem_path,"%s",user->pw_dir); - } - strcat(log_path,LOG_PATH); - strcat(shm_path,SHM_PATH); - strcat(sem_path,SEM_PATH); - - if((dir = opendir(log_path)) == NULL){ - if(errno == ENOENT){ - strcpy(log_path, "/tmp/"); - } - } - closedir(dir); - s = strrchr(log_path, '/'); - s++; - *s = '\0'; - memset(buf, 0, sizeof(buf)); - strftime(buf, sizeof(buf), "%Y%m%d_", &tmptr); - strcat(buf, "sys_nicmonitor"); - strcat(buf, ".log"); - strcat(log_path, buf); - if((ret = init_nic_info()) == -1){ - return -1; - } - get_sem(semid); - for(i = 0; ptr->info[i].charname[0] != 0; i++){ - if(!strcmp(ptr->info[i].charname, nic_name)){ - // get_sem(semid); - memcpy(net_info, &ptr->info[i], sizeof(NETCARD_INFO)); - release_sem(semid); - munmap(ptr, sizeof(SHM)); - return 0; - } - } - release_sem(semid); - snprintf(err_str, sizeof(err_str), "NOTICE: No information of %s !\n", nic_name); - record_log(err_str); - munmap(ptr, sizeof(SHM)); - return -1; -} diff --git a/code/tags/sys_nicmonitor-1.7/nicinfo_shm.h b/code/tags/sys_nicmonitor-1.7/nicinfo_shm.h deleted file mode 100644 index cd74135..0000000 --- a/code/tags/sys_nicmonitor-1.7/nicinfo_shm.h +++ /dev/null @@ -1,52 +0,0 @@ -#ifndef NIC_SHM_H -#define NIC_SHM_H - -#include - -#ifdef __cplusplus -extern "C" { -#endif - -#ifndef NIC_NAME_LEN -#define NIC_NAME_LEN 64 -#endif -#define SEM_PROJ_ID 's' -#define MAXNICNUM 32 - -typedef long KEYID; -typedef struct _net_info -{ - KEYID ID; - char charname[NIC_NAME_LEN]; - char descr[128]; - struct sockaddr addr; - struct sockaddr broadaddr; - struct sockaddr netmask; - struct sockaddr hwaddr; - int time_stamp; - short flags; - int mtu; - int tx_queue_len; - unsigned long long average_flow; - unsigned long long rx_packets; - unsigned long long tx_packets; - unsigned long long rx_bytes; - unsigned long long tx_bytes; - unsigned long rx_errors; - unsigned long tx_errors; - unsigned long rx_dropped; - unsigned long tx_dropped; - unsigned long rx_multicast; - unsigned long collisions; - unsigned long rx_fifo_errors; - unsigned long tx_carrier_errors; - unsigned long tx_fifo_errors; -}NETCARD_INFO; - -int get_nic_info(char *nic_name, NETCARD_INFO *net_info); - - -#ifdef __cplusplus -} -#endif -#endif diff --git a/code/tags/sys_nicmonitor-1.7/proc_inv.h b/code/tags/sys_nicmonitor-1.7/proc_inv.h deleted file mode 100644 index 945622c..0000000 --- a/code/tags/sys_nicmonitor-1.7/proc_inv.h +++ /dev/null @@ -1,198 +0,0 @@ -//////////////////////////////////////////////////////////////////////////////////// -// -// Copyright (c) Comets' Grp. of Kedong Corp 2008. All Rights Reserved. -// -// FileName : procconf.h -// -// Function : this class realize some basic functions for process managerment, -// such as initiate process, report status of process, check status of process, -// update status of process, get information of process -// -// Author : -// -// Date : -// -// Modify by : -// -// Mod Date : -// -//////////////////////////////////////////////////////////////////////////////////// - -#ifndef _PROCCONF_H -#define _PROCCONF_H - -#include -#include -#include -#include -#include -#include -#include -//#include -#include - -#include "const.h" - -//for alarm ................................ -typedef struct MESS_BH -{ - unsigned char mtype; - int length; -}; -typedef struct PROCESS_ALM -{ - char context_name[MAX_STRING_LEN]; - char app_name[MAX_STRING_LEN]; - char proc_name[MAX_STRING_LEN]; - unsigned char status; -}; -//for alarm end ............................. - -//for mmi.................................... -const int MAX_BUFFER_LEN = 500; -typedef struct MESS_BLOCK -{ - unsigned char num; - char buffer[MAX_BUFFER_LEN]; -}; -typedef struct MESS_PROC -{ - char context_name[MAX_STRING_LEN]; - char app_name[MAX_STRING_LEN]; - char proc_name[MAX_STRING_LEN]; - char status; -}; -//for mmi end ................................ - -const int DEFAULT_PERIOD = 3; -const int COUNT_LIMIT = 2; -const int START_DEFAULT_PERIOD = 60; -const int START_COUNT_LIMIT = 5; -const int APP_COUNT_LIMIT = 1; - -//process critical level -//const int WST_CRITICAL = 1; // Shutdown and reboot workstation when failed -//const int SYS_CRITICAL = 2; // Shutdown and reboot the system on the wst when failed -//const int USER_CRITICAL = 3; // Shutdown and reboot the subsystem when failed -//const int GENERAL = 4; // reboot the process -const int CRUCIAL = 1; // crucial process -const int GENERAL = 0; // general process - - -extern int srv_init(char *service,int port); -extern int Tcp_close(int sockfd); -extern int Tcp_read(int fd,char *ptr,int nbytes); -extern int Tcp_write(int fd,char *ptr,int nbytes); -extern int srv_accept(int fd,struct sockaddr *cli_addr,int *clilen); -extern int client_tcp_open(char *host,char *service,int port); - -typedef struct -{ - char context_name[MAX_STRING_LEN]; - char app_name[MAX_STRING_LEN]; - char proc_name[MAX_STRING_LEN]; - pid_t proc_pid; -}PROC_ADM_INFO; - -//*************************************************************** -// structure name : PROC_INFO -// function : store process informatin -// author : -// date : -// modify by : -// modification : -// mod date : -//*************************************************************** -typedef struct -{ - int position; - char node_name[MAX_STRING_LEN]; - char context_name[MAX_STRING_LEN]; - char app_name[MAX_STRING_LEN]; - char proc_name[MAX_STRING_LEN]; - - unsigned char active_flag; - unsigned char master_flag; - - time_t startup_time; - time_t refresh_time; - short refresh_peri; - unsigned char monitor_type; - - pid_t proc_pid; - unsigned char auto_start; - unsigned char act_timer; - unsigned char start_timer; - unsigned char critical_level; - - char exefile_path[MAX_EXECMD_LEN]; - -}PROC_INFO; - -//*************************************************************** -// structure name : APP_INFO -// function : store application informatin -// author : -// date : -// modify by : -// modification : -// mod date : -//*************************************************************** -typedef struct -{ - int position; - char context_name[MAX_STRING_LEN]; - int context_id; - char app_name[MAX_STRING_LEN]; - int app_id; - unsigned char act_timer; - unsigned char active_flag; -}APP_INFO; - - -typedef struct -{ - int no_proc; - int semdes_cfg; - PROC_INFO proc[MAX_LOCAL_PROCESS]; - APP_INFO app[MAX_LOCAL_APP]; -}PROCCFG; - -class proc_invocation -{ - public: - int m_init; - PROCCFG *proccfg_p; - - public: - proc_invocation(); - ~proc_invocation(); - - int conf_create(); - //int check_proc_status(); - //int update_rtdb(); - //int kill_proc(pid_t pid); - //int start_proc(char *cmdline); - //int send_alarm(char *context_name, char *app_name, char *proc_name, unsigned char status); - //int update_proc_status(char *context_name, char *app_name, char *proc_name, char status); - - //int proc_init(char *context_name, char *app_name, char *proc_name, int critical_level);//exefile_path,auto_start - int proc_init(char *context_name, char *app_name, char *proc_name); - int proc_report(int pos, char status, int intertime=3); - int proc_exit(int proc_pos); - - int get_pos(char *context_name, char *app_name, char *proc_name); - int is_proc_exist(char *context_name, char *app_name, char *proc_name); - int conf_map(); - - int get_procinfo(int position, PROC_ADM_INFO *p_info); - int get_active_pid(int &num, int *p_pidlist); - int is_proc_run(pid_t pid); - int is_proc_run(char *context_name, char *app_name, char *proc_name); - -}; - -#endif - - - diff --git a/code/tags/sys_nicmonitor-1.7/read_netcard.c b/code/tags/sys_nicmonitor-1.7/read_netcard.c deleted file mode 100644 index c745f48..0000000 --- a/code/tags/sys_nicmonitor-1.7/read_netcard.c +++ /dev/null @@ -1,133 +0,0 @@ -#include "mnic.h" - -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; -} - - -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 == '\0') - return NULL; - p++; - break; - } - *name++ = *p++; - } - *name++ = '\0'; - return p; -} - -int get_dev_fields(char *str, NETCARD_INFO *ife) -{ - 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]); - return 0; -} - -int if_fetch(NETCARD_INFO *ife) -{ - int skfd; - struct ifreq ifr; - - if((skfd = socket(PF_INET, SOCK_DGRAM, 0)) == -1)return -1; - - strcpy(ifr.ifr_name, ife->charname); - if (!ioctl(skfd, SIOCGIFFLAGS, &ifr)) - ife->flags = ifr.ifr_flags; - - if (!ioctl(skfd, SIOCGIFHWADDR, &ifr)) - ife->hwaddr = ifr.ifr_hwaddr; - - if (!ioctl(skfd, SIOCGIFMTU, &ifr)) - ife->mtu = ifr.ifr_mtu; - - if (!ioctl(skfd, SIOCGIFTXQLEN, &ifr)) - ife->tx_queue_len = ifr.ifr_qlen; - - if (!ioctl(skfd, SIOCGIFADDR, &ifr)) - ife->addr = ifr.ifr_addr; - - if (!ioctl(skfd, SIOCGIFBRDADDR, &ifr)) - ife->broadaddr = ifr.ifr_broadaddr; - - if (!ioctl(skfd, SIOCGIFNETMASK, &ifr)) - ife->netmask = ifr.ifr_netmask; - - ife->time_stamp = time(NULL); - close(skfd); - return 0; -} - - -int ioc_get_name(NETCARD_INFO *name) -{ - int skfd; - int ifrnum; - int i = 0; - struct ifconf ifc; - struct ifreq buf[MAXNICNUM]; - - if((skfd = socket(PF_INET, SOCK_DGRAM, 0)) == -1)return -1; - memset(buf, 0, sizeof(buf)); - ifc.ifc_len = sizeof(buf); - ifc.ifc_buf = (caddr_t) buf; - ioctl(skfd, SIOCGIFCONF, &ifc); - ifrnum = ifc.ifc_len / sizeof(struct ifreq); - while(ifrnum-- > 0){ - strcpy(name[i].charname, buf[ifrnum].ifr_name); - i++; - } - close(skfd); - return i; -} - diff --git a/code/tags/sys_nicmonitor-1.7/send_alarm.c b/code/tags/sys_nicmonitor-1.7/send_alarm.c deleted file mode 100644 index e88635f..0000000 --- a/code/tags/sys_nicmonitor-1.7/send_alarm.c +++ /dev/null @@ -1,37 +0,0 @@ -#include "mnic.h" - -void send_alarm(D5000_NIC_ALARM *mesg, int socket_port, char *ipv4) -{ - int bytes; - int sock_sd; - int val; - char error_str[200]; - struct sockaddr_in recever; - socklen_t sock_len; - - if((sock_sd = socket(PF_INET, SOCK_DGRAM, IPPROTO_UDP)) == -1){ - snprintf(error_str, sizeof(error_str), "EMERG: socket(): %s\n", strerror(errno)); - record_log(error_str); - return; - } - val=1; - if ((setsockopt(sock_sd, SOL_SOCKET, SO_BROADCAST, &val, sizeof(val))) == -1) { - snprintf(error_str, sizeof(error_str), "EMERG: setsockopt(): %s\n", strerror(errno)); - record_log(error_str); - return; - } - /* init socket*/ - recever.sin_family = AF_INET; - recever.sin_port = htons(socket_port); - inet_pton(AF_INET, ipv4, &recever.sin_addr); - sock_len = sizeof(recever); - /* send alarm*/ - if((bytes = sendto(sock_sd, mesg, sizeof(D5000_NIC_ALARM), 0, (const struct sockaddr *)&recever, sock_len)) < 1){ - snprintf(error_str, sizeof(error_str), "EMERG: sendto(): %s\n", strerror(errno)); - record_log(error_str); - return; - } - //snprintf(error_str, sizeof(error_str), "NOTICE: Alarm information has already send!\n"); - //record_log(error_str); - close(sock_sd); -} diff --git a/code/tags/sys_nicmonitor-1.7/sys_netcard.h b/code/tags/sys_nicmonitor-1.7/sys_netcard.h deleted file mode 100644 index 0d7746c..0000000 --- a/code/tags/sys_nicmonitor-1.7/sys_netcard.h +++ /dev/null @@ -1,88 +0,0 @@ -#ifndef _SYS_NETCARD_H -#define _SYS_NETCARD_H - -#include - -#ifndef NIC_NAME_LEN -#define NIC_NAME_LEN 64 -#endif - -#define SEM_PROJ_ID 's' -#define MAXNICNUM 32 - -#ifdef __cplusplus -extern "C"{ -#endif - -typedef long KEYID; - -typedef struct _msg_frame // message frame -{ - short len; // message length - short seqno; // send sequence - short serv; // services ID - short event; // event ID - unsigned char domain; // domain ID - unsigned char ctxt; // Context ID - short stid; // SOURCE task id - short dtid; // DESTINATION task ID - unsigned char ver_coding; // 版本号 + 编码 - unsigned char mes_type; // 帧类型 -}MSG_FRAME, *LPMSG_FRAME; - -typedef struct _net_info -{ - KEYID ID; - char charname[NIC_NAME_LEN]; - char descr[128]; - struct sockaddr addr; - struct sockaddr broadaddr; - struct sockaddr netmask; - struct sockaddr hwaddr; - int time_stamp; - short flags; - int mtu; - int tx_queue_len; - unsigned long long average_flow; - unsigned long long rx_packets; - unsigned long long tx_packets; - unsigned long long rx_bytes; - unsigned long long tx_bytes; - unsigned long rx_errors; - unsigned long tx_errors; - unsigned long rx_dropped; - unsigned long tx_dropped; - unsigned long rx_multicast; - unsigned long collisions; - unsigned long rx_fifo_errors; - unsigned long tx_carrier_errors; - unsigned long tx_fifo_errors; -}NETCARD_INFO, *LPNETCARD_INFO; - -#define NETCARD_ALARM_FAULT 1 -#define NETCARD_ALARM_RESUME 2 -#define NETCARD_ALARM_SWITCH 3 -#define NETCARD_ALARM_ABNORMAL 4 -#define NETCARD_ALARM_NORMAL 5 -typedef struct _sys_netcard_alarm -{ - char fault_devname[NIC_NAME_LEN]; /*故障设备名称 */ - char switch_devname[NIC_NAME_LEN]; /*切换设备名称 */ - short flags; /* 状态标记 : 1:网卡故障。2:网卡恢复。3:网卡切换。4:流量异常。*/ - short retrytimes; //0,1,2 重发次数 -}SYS_NETCARD_ALARM, *LPSYS_NETCARD_ALARM; - - -typedef struct _d5000_nic_alarm -{ - MSG_FRAME tMsgFrame ; - SYS_NETCARD_ALARM tSysNetcardAlarm ; -}D5000_NIC_ALARM, *LPD5000_NIC_ALARM; - - -#ifdef __cplusplus -} -#endif - -#endif - diff --git a/code/tags/sys_nicmonitor-1.8/bin/README b/code/tags/sys_nicmonitor-1.8/bin/README deleted file mode 100644 index 56665f3..0000000 --- a/code/tags/sys_nicmonitor-1.8/bin/README +++ /dev/null @@ -1,18 +0,0 @@ -======== -安装说明: -直接运行命令: -sh ./install.sh - -或者采用如下步骤: - - 1)安装,以root的用户执行安装命令: - pkgadd sys_nicmonitor#1.8-x86_64-Linx-Rocky4.2.pkg.tar.gz - 如果之前已经安装,则可先卸载旧的然后再安装,或者通过更新方式安装 - 卸载命令: pkgrm sys_nicmonitor - 更新方式安装命令: - pkgadd -u sys_nicmonitor#1.8-x86_64-Linx-Rocky4.2.pkg.tar.gz - - 2)查看安装是否成功 - pkginfo -i|grep sys_nicmonitor - 或者 - 查看d5000用户的目录下lib/libnic_shm.so和bin/sys_nicmonitor的日期是否 diff --git a/code/tags/sys_nicmonitor-1.8/bin/install.sh b/code/tags/sys_nicmonitor-1.8/bin/install.sh deleted file mode 100755 index 7d2c2ac..0000000 --- a/code/tags/sys_nicmonitor-1.8/bin/install.sh +++ /dev/null @@ -1,4 +0,0 @@ -#!/bin/bash -pkgrm sys_nicmonitor -pkgadd sys_nicmonitor#1.8-x86_64-Linx-Rocky4.2.pkg.tar.gz - diff --git a/code/tags/sys_nicmonitor-1.8/bin/sys_nicmonitor#1.8-x86_64-Linx-Rocky4.2.pkg.tar.gz b/code/tags/sys_nicmonitor-1.8/bin/sys_nicmonitor#1.8-x86_64-Linx-Rocky4.2.pkg.tar.gz deleted file mode 100644 index d956807..0000000 Binary files a/code/tags/sys_nicmonitor-1.8/bin/sys_nicmonitor#1.8-x86_64-Linx-Rocky4.2.pkg.tar.gz and /dev/null differ diff --git a/code/tags/sys_nicmonitor-1.8/doc/网卡监视程序动态库使用手册【新】.doc b/code/tags/sys_nicmonitor-1.8/doc/网卡监视程序动态库使用手册【新】.doc deleted file mode 100644 index 2faa068..0000000 Binary files a/code/tags/sys_nicmonitor-1.8/doc/网卡监视程序动态库使用手册【新】.doc and /dev/null differ diff --git a/code/tags/sys_nicmonitor-1.8/doc/网卡监视程序安装说明以及使用手册【新】.doc b/code/tags/sys_nicmonitor-1.8/doc/网卡监视程序安装说明以及使用手册【新】.doc deleted file mode 100644 index 181b641..0000000 Binary files a/code/tags/sys_nicmonitor-1.8/doc/网卡监视程序安装说明以及使用手册【新】.doc and /dev/null differ diff --git a/code/tags/sys_nicmonitor-1.8/src/Pkgfile b/code/tags/sys_nicmonitor-1.8/src/Pkgfile deleted file mode 100644 index b01c071..0000000 --- a/code/tags/sys_nicmonitor-1.8/src/Pkgfile +++ /dev/null @@ -1,20 +0,0 @@ -# Description: A system to store and display time-series data. -# URL: http://oss.oetiker.ch/rrdtool/index.en.html -# Maintainer: Danny Rawlins, monster dot romster at gmail dot com -# Packager: Younes Hafri, ycrux at club-internet dot fr -# Depends on: libart_lgpl libcgi libgd pango python tcl intltool - -name=sys_nicmonitor -version=1.8 -release=x86_64-Linx-Rocky4.2 -source=/home/d5000/d5000/linx/1.8/$name-$version.tar.gz - -build() { - cd $name-$version - - make - make DESTDIR=$PKG install -# mkdir -p $PKG/var/log/netcard -# chmod 777 $PKG/var/log/netcard -} - diff --git a/code/tags/sys_nicmonitor-1.8/src/README1.8 b/code/tags/sys_nicmonitor-1.8/src/README1.8 deleted file mode 100644 index 356afb1..0000000 --- a/code/tags/sys_nicmonitor-1.8/src/README1.8 +++ /dev/null @@ -1,65 +0,0 @@ -========= -编译说明: - 在Rocky 4.2上编译 - 1)修改Pkgfile中的source指向的源文件位置 - 2)然后执行: sh build.sh - 将在当前的目录下生成安装包: - sys_nicmonitor#1.8-x86_64-Linx-Rocky4.2.pkg.tar.gz - -======== -安装说明: - 1)安装,以root的用户执行安装命令: - pkgadd sys_nicmonitor#1.8-x86_64-Linx-Rocky4.2.pkg.tar.gz - 如果之前已经安装,则可先卸载旧的然后再安装,或者通过更新方式安装 - 卸载命令: pkgrm sys_nicmonitor - 更新方式安装命令: - pkgadd -u sys_nicmonitor#1.8-x86_64-Linx-Rocky4.2.pkg.tar.gz - - 2)查看安装是否成功 - pkginfo -i|grep sys_nicmonitor - 或者 - 查看d5000用户的目录下lib/libnic_shm.so和bin/sys_nicmonitor的日期是否 - -======== -版本说明: ---V1.8 -- - 2013-05-27 -对系统高磁盘IO情况下,应用如果进行IO将可能会被阻塞。本版本为此作出调整。 -cs-fes1上编译测试OK。 - -1) 去掉shm和sem的实体文件 -#define SHM_PATH "$D5000_HOME/share/sys_netcard_shm_path" -#define SEM_PATH "$D5000_HOME/share/sys_netcard_sem_path" - -用key=0x1d5200 代替 sem -用key=0x1d5010 代替 shm - - -2)去掉动态库中get_nic_info接口中,判断路径的opendir调用,采用实际路径 - 上层应用每秒可能调用3次这个接口,每判断一个网卡都调用一次,判断都个网卡的状态时就调用多次。 - - 可改进:一次调用get_nic_info接口,可输入多个网卡,一次返回所有网卡状态信息。 - -3) record_log函数为日志函数,每次写一次,都重新打开一次文件。 - 修改为线程模式,当记录日志时,不会被磁盘IO阻塞。 - -4) 安装目录为d5000用户的HOME目录下的lib和bin目录 - ----- - - -V1.6与V1.7差异 -共享内存路径修改: -由/home/d5000/someone/tmp/sys_netcard_shm_path 改为 /home/d5000/someone/share/sys_netcard_shm_path -信号量路径修改: -由/home/d5000/someone/tmp/sys_netcard_sem_path 改为 /home/d5000/someone/share/sys_netcard_sem_path - -注意:在编译过程中,需要动态看libman.so,它存放在目录 tags/下面 ,在进行编译的时候需要将其拷贝到 -/lib64/目录下,编译的基础环境是 Rocky 4.2 系统。 - -pkgmk命令的使用: -将源代码打包成name-version.tar.gz样式,编写Pkgfile文件。 -使用命令: - pkgmk -um #更新软件包的md5值 - pkgmk -kw #制作pkg安装包 - diff --git a/code/tags/sys_nicmonitor-1.8/src/V1.8 b/code/tags/sys_nicmonitor-1.8/src/V1.8 deleted file mode 100644 index e69de29..0000000 diff --git a/code/tags/sys_nicmonitor-1.8/src/build.sh b/code/tags/sys_nicmonitor-1.8/src/build.sh deleted file mode 100644 index ff47a6d..0000000 --- a/code/tags/sys_nicmonitor-1.8/src/build.sh +++ /dev/null @@ -1,7 +0,0 @@ -pkgmk -um - -#For debug -#pkgmk -kw -pa post_add.sh - -#For release -pkgmk -pa post_add.sh diff --git a/code/tags/sys_nicmonitor-1.8/src/sys_nicmonitor-1.8.tar.gz b/code/tags/sys_nicmonitor-1.8/src/sys_nicmonitor-1.8.tar.gz deleted file mode 100644 index 0215da9..0000000 Binary files a/code/tags/sys_nicmonitor-1.8/src/sys_nicmonitor-1.8.tar.gz and /dev/null differ diff --git a/code/tags/sys_nicmonitor-1.8/test/dotconf.h b/code/tags/sys_nicmonitor-1.8/test/dotconf.h deleted file mode 100644 index e802508..0000000 --- a/code/tags/sys_nicmonitor-1.8/test/dotconf.h +++ /dev/null @@ -1,264 +0,0 @@ -#ifndef DOTCONF_H -#define DOTCONF_H - -#ifdef __cplusplus -extern "C" { -#endif - -/* stdio.h should be included by the application - as the manual page says */ -#ifndef _STDIO_H -#include /* needed for FILE* */ -#endif - -#ifdef WIN32 -# ifndef R_OK -#define R_OK 0 -# endif -#endif - -/* some buffersize definitions */ -#define CFG_BUFSIZE 4096 /* max length of one line */ -#define CFG_MAX_OPTION 32 /* max length of any option name */ -#define CFG_MAX_VALUE 4064 /* max length of any options value */ -#define CFG_MAX_FILENAME 256 /* max length of a filename */ -#define CFG_VALUES 16 /* max # of arguments an option takes */ - -#define CFG_INCLUDEPATH_ENV "DC_INCLUDEPATH" -#define WILDCARDS "*?" /* list of supported wild-card characters */ - -/* constants for type of option */ -#define ARG_TOGGLE 0 /* TOGGLE on,off; yes,no; 1, 0; */ -#define ARG_INT 1 /* callback wants an integer */ -#define ARG_STR 2 /* callback expects a \0 terminated str */ -#define ARG_LIST 3 /* wants list of strings */ -#define ARG_NAME 4 /* wants option name plus ARG_LIST stuff */ -#define ARG_RAW 5 /* wants raw argument data */ -#define ARG_NONE 6 /* does not expect ANY args */ - -#define CTX_ALL 0 /* context: option can be used anywhere */ - -/* for convenience of terminating the dotconf_options list */ -#define LAST_OPTION { "", 0, NULL, NULL } -#define LAST_CONTEXT_OPTION { "", 0, NULL, NULL, 0 } - -#define DOTCONF_CB(__name) const char *__name(command_t *cmd, \ - context_t *ctx) -#define FUNC_ERRORHANDLER(_name) int _name(configfile_t * configfile, \ - int type, long dc_errno, const char *msg) - - -/* some flags that change the runtime behaviour of dotconf */ -#define NONE 0 -#define CASE_INSENSITIVE 1<<0 /* match option names case insensitive */ -#define DONT_SUBSTITUTE 1<<1 /* do not call substitute_env after read_arg */ -#define NO_INLINE_COMMENTS 1<<2 /* do not allow inline comments */ -#define DUPLICATE_OPTION_NAMES 1<<3 /* allow for duplicate option names */ - -/* syslog style errors as suggested by Sander Steffann */ -#ifdef HAVE_SYSLOG -#include - -#define DCLOG_EMERG LOG_EMERG /* system is unusable */ -#define DCLOG_ALERT LOG_ALERT /* action must be taken immediately */ -#define DCLOG_CRIT LOG_CRIT /* critical conditions */ -#define DCLOG_ERR LOG_ERR /* error conditions */ -#define DCLOG_WARNING LOG_WARNING /* warning conditions */ -#define DCLOG_NOTICE LOG_NOTICE /* normal but significant condition */ -#define DCLOG_INFO LOG_INFO /* informational */ -#define DCLOG_DEBUG LOG_DEBUG /* debug-level messages */ - -#define DCLOG_LEVELMASK LOG_PRIMASK /* mask off the level value */ - -#else /* HAVE_SYSLOG */ - -#define DCLOG_EMERG 0 /* system is unusable */ -#define DCLOG_ALERT 1 /* action must be taken immediately */ -#define DCLOG_CRIT 2 /* critical conditions */ -#define DCLOG_ERR 3 /* error conditions */ -#define DCLOG_WARNING 4 /* warning conditions */ -#define DCLOG_NOTICE 5 /* normal but significant condition */ -#define DCLOG_INFO 6 /* informational */ -#define DCLOG_DEBUG 7 /* debug-level messages */ - -#define DCLOG_LEVELMASK 7 /* mask off the level value */ - -#endif /* HAVE_SYSLOG */ - -/* callback types for dotconf_callback */ - -/* error constants */ -#define ERR_NOERROR 0x0000 -#define ERR_PARSE_ERROR 0x0001 -#define ERR_UNKNOWN_OPTION 0x0002 -#define ERR_WRONG_ARG_COUNT 0x0003 -#define ERR_INCLUDE_ERROR 0x0004 -#define ERR_NOACCESS 0x0005 -#define ERR_USER 0x1000 /* base for userdefined errno's */ - -/* i needed this to check an ARG_LIST entry if it's toggled in one of my apps; maybe you do too */ -#define CFG_TOGGLED(_val) ( (_val[0] == 'Y' \ - || _val[0] == 'y') \ - || (_val[0] == '1') \ - || ((_val[0] == 'o' \ - || _val[0] == 'O') \ - && (_val[1] == 'n' \ - || _val[1] == 'N'))) - -enum callback_types -{ - ERROR_HANDLER = 1, - CONTEXT_CHECKER -}; - -typedef enum callback_types callback_types; -typedef struct configfile_t configfile_t; -typedef struct configoption_t configoption_t; -typedef struct configoption_t ConfigOption; -typedef struct command_t command_t; -typedef void context_t; -typedef void info_t; - -typedef const char *(*dotconf_callback_t)(command_t *, context_t *); -typedef int (*dotconf_errorhandler_t)(configfile_t *, int, unsigned long, const char *); -typedef const char *(*dotconf_contextchecker_t)(command_t *, unsigned long); - -struct configfile_t -{ - /* ------ the fields in configfile_t are provided to the app via command_t's ; READ ONLY! --- */ - - FILE *stream; - char eof; /* end of file reached ? */ - size_t size; /* file size; cached on-demand for here-documents */ - - context_t *context; - - configoption_t const **config_options; - int config_option_count; - - /* ------ misc read-only fields ------------------------------------------------------------- */ - char *filename; /* name of file this option was found in */ - unsigned long line; /* line number we're currently at */ - unsigned long flags; /* runtime flags given to dotconf_open */ - - char *includepath; - - /* ------ some callbacks for interactivity -------------------------------------------------- */ - dotconf_errorhandler_t errorhandler; - dotconf_contextchecker_t contextchecker; - - int (*cmp_func)(const char *, const char *, size_t); -}; - -struct configoption_t -{ - const char *name; /* name of configuration option */ - int type; /* for possible values, see above */ - dotconf_callback_t callback; /* callback function */ - info_t *info; /* additional info for multi-option callbacks */ - unsigned long context; /* context sensitivity flags */ -}; - -struct command_t -{ - const char *name; /* name of the command */ - configoption_t *option; /* the option as given in the app; READ ONLY */ - - /* ------ argument data filled in for each line / command ----------------------------------- */ - struct { - long value; /* ARG_INT, ARG_TOGGLE */ - char *str; /* ARG_STR */ - char **list; /* ARG_LIST */ - } data; - int arg_count; /* number of arguments (in data.list) */ - - /* ------ misc context information ---------------------------------------------------------- */ - configfile_t *configfile; - context_t *context; -}; - -/* ------ dotconf_create() - create the configfile_t needed for further dot.conf fun ------------ */ -configfile_t *dotconf_create(char *, const configoption_t *, context_t *, unsigned long); - -/* ------ dotconf_cleanup() - tidy up behind dotconf_create and the parser dust ----------------- */ -void dotconf_cleanup(configfile_t *configfile); - -/* ------ dotconf_command_loop() - iterate through each line of file and handle the commands ---- */ -int dotconf_command_loop(configfile_t *configfile); - -/* ------ dotconf_command_loop_until_error() - like continue_line but return on the first error - */ -const char *dotconf_command_loop_until_error(configfile_t *configfile); - -/* ------ dotconf_continue_line() - check if line continuation is to be handled ----------------- */ -int dotconf_continue_line(char *buffer, size_t length); - -/* ------ dotconf_get_next_line() - read in the next line of the configfile_t ------------------- */ -int dotconf_get_next_line(char *buffer, size_t bufsize, configfile_t *configfile); - -/* ------ dotconf_get_here_document() - read the here document until delimit is found ----------- */ -char *dotconf_get_here_document(configfile_t *configfile, const char *delimit); - -/* ------ dotconf_invoke_command() - call the callback for command_t ---------------------------- */ -const char *dotconf_invoke_command(configfile_t *configfile, command_t *cmd); - -/* ------ dotconf_find_command() - iterate through all registered options trying to match ------- */ -configoption_t *dotconf_find_command(configfile_t *configfile, const char *command); - -/* ------ dotconf_read_arg() - read one argument from the line handling quoting and escaping ---- */ -/* - side effects: the char* returned by dotconf_read_arg is malloc() before, hence that pointer - will have to be free()ed later. -*/ -char *dotconf_read_arg(configfile_t *configfile, signed char **line); - -/* ------ dotconf_handle_command() - parse, substitute, find, invoke the command found in buffer */ -const char *dotconf_handle_command(configfile_t *configfile, char *buffer); - -/* ------ dotconf_register_option() - add a new option table to the list of commands ------------ */ -void dotconf_register_options(configfile_t *configfile, const configoption_t *options); - -/* ------ dotconf_warning() - handle the dispatch of error messages of various levels ----------- */ -int dotconf_warning(configfile_t *configfile, int level, unsigned long errnum, const char *, ...); - -/* ------ dotconf_callback() - register a special callback -------------------------------------- */ -void dotconf_callback(configfile_t *configfile, callback_types type, dotconf_callback_t); - -/* ------ dotconf_substitute_env() - handle the substitution on environment variables ----------- */ -char *dotconf_substitute_env(configfile_t *, char *); - -/* ------ internal utility function that verifies if a character is in the WILDCARDS list -- */ -int dotconf_is_wild_card(char value); - -/* ------ internal utility function that calls the appropriate routine for the wildcard passed in -- */ -int dotconf_handle_wild_card(command_t* cmd, char wild_card, char* path, char* pre, char* ext); - -/* ------ internal utility function that frees allocated memory from dotcont_find_wild_card -- */ -void dotconf_wild_card_cleanup(char* path, char* pre); - -/* ------ internal utility function to check for wild cards in file path -- */ -/* ------ path and pre must be freed by the developer ( dotconf_wild_card_cleanup) -- */ -int dotconf_find_wild_card(char* filename, char* wildcard, char** path, char** pre, char** ext); - -/* ------ internal utility function that compares two stings from back to front -- */ -int dotconf_strcmp_from_back(const char* s1, const char* s2); - -/* ------ internal utility function that determins if a string matches the '?' criteria -- */ -int dotconf_question_mark_match(char* dir_name, char* pre, char* ext); - -/* ------ internal utility function that determins if a string matches the '*' criteria -- */ -int dotconf_star_match(char* dir_name, char* pre, char* ext); - -/* ------ internal utility function that determins matches for filenames with -- */ -/* ------ a '?' in name and calls the Internal Include function on that filename -- */ -int dotconf_handle_question_mark(command_t* cmd, char* path, char* pre, char* ext); - -/* ------ internal utility function that determins matches for filenames with -- */ -/* ------ a '*' in name and calls the Internal Include function on that filename -- */ -int dotconf_handle_star(command_t* cmd, char* path, char* pre, char* ext); - - - -#ifdef __cplusplus -} /* extern "C" */ -#endif - -#endif /* DOTCONF_H */ diff --git a/code/tags/sys_nicmonitor-1.8/test/getmem b/code/tags/sys_nicmonitor-1.8/test/getmem deleted file mode 100755 index af039a9..0000000 Binary files a/code/tags/sys_nicmonitor-1.8/test/getmem and /dev/null differ diff --git a/code/tags/sys_nicmonitor-1.8/test/read_shm b/code/tags/sys_nicmonitor-1.8/test/read_shm deleted file mode 100755 index f7702fd..0000000 Binary files a/code/tags/sys_nicmonitor-1.8/test/read_shm and /dev/null differ diff --git a/code/tags/sys_nicmonitor-1.8/test/recv_msg b/code/tags/sys_nicmonitor-1.8/test/recv_msg deleted file mode 100755 index 1814051..0000000 Binary files a/code/tags/sys_nicmonitor-1.8/test/recv_msg and /dev/null differ diff --git a/code/tags/sys_nicmonitor-1.8/test/Makefile b/code/test/Makefile similarity index 100% rename from code/tags/sys_nicmonitor-1.8/test/Makefile rename to code/test/Makefile diff --git a/branches/sys_nicmonitor-1.4-龙芯-32/dotconf.h b/code/test/dotconf.h similarity index 100% rename from branches/sys_nicmonitor-1.4-龙芯-32/dotconf.h rename to code/test/dotconf.h diff --git a/code/tags/sys_nicmonitor-1.8/test/getmem.c b/code/test/getmem.c similarity index 100% rename from code/tags/sys_nicmonitor-1.8/test/getmem.c rename to code/test/getmem.c diff --git a/code/tags/sys_nicmonitor-1.8/test/mnic.h b/code/test/mnic.h similarity index 100% rename from code/tags/sys_nicmonitor-1.8/test/mnic.h rename to code/test/mnic.h diff --git a/code/tags/sys_nicmonitor-1.8/test/nicinfo_shm.h b/code/test/nicinfo_shm.h similarity index 100% rename from code/tags/sys_nicmonitor-1.8/test/nicinfo_shm.h rename to code/test/nicinfo_shm.h diff --git a/code/tags/sys_nicmonitor-1.8/test/read_shm.c b/code/test/read_shm.c similarity index 100% rename from code/tags/sys_nicmonitor-1.8/test/read_shm.c rename to code/test/read_shm.c diff --git a/code/tags/sys_nicmonitor-1.8/test/recv_msg.c b/code/test/recv_msg.c similarity index 100% rename from code/tags/sys_nicmonitor-1.8/test/recv_msg.c rename to code/test/recv_msg.c diff --git a/code/tags/sys_nicmonitor-1.8/test/sys_netcard.h b/code/test/sys_netcard.h similarity index 100% rename from code/tags/sys_nicmonitor-1.8/test/sys_netcard.h rename to code/test/sys_netcard.h diff --git a/code/trunk/LATEST b/code/trunk/LATEST deleted file mode 100644 index 2e0e38c..0000000 --- a/code/trunk/LATEST +++ /dev/null @@ -1 +0,0 @@ -1.9 diff --git a/code/trunk/bin/README b/code/trunk/bin/README deleted file mode 100644 index c5e16e7..0000000 --- a/code/trunk/bin/README +++ /dev/null @@ -1,18 +0,0 @@ -======== -安装说明: -直接运行命令: -sh ./install.sh - -或者采用如下步骤: - - 1)安装,以root的用户执行安装命令: - pkgadd sys_nicmonitor#1.8.1-x86_64-Linx-Rocky4.2.pkg.tar.gz - 如果之前已经安装,则可先卸载旧的然后再安装,或者通过更新方式安装 - 卸载命令: pkgrm sys_nicmonitor - 更新方式安装命令: - pkgadd -u sys_nicmonitor#1.8.1-x86_64-Linx-Rocky4.2.pkg.tar.gz - - 2)查看安装是否成功 - pkginfo -i|grep sys_nicmonitor - 或者 - 查看d5000用户的目录下lib/libnic_shm.so和bin/sys_nicmonitor的日期是否 diff --git a/code/trunk/bin/install.sh b/code/trunk/bin/install.sh deleted file mode 100755 index a787488..0000000 --- a/code/trunk/bin/install.sh +++ /dev/null @@ -1,5 +0,0 @@ -#!/bin/bash -pkgrm sys_nicmonitor -pkgadd sys_nicmonitor#1.8.1-x86_64-Linx-Rocky4.2.pkg.tar.gz -pkginfo -i | grep nic - diff --git a/code/trunk/bin/sys_nicmonitor#1.8.1-x86_64-Linx-Rocky4.2.pkg.tar.gz b/code/trunk/bin/sys_nicmonitor#1.8.1-x86_64-Linx-Rocky4.2.pkg.tar.gz deleted file mode 100644 index 2afbb15..0000000 Binary files a/code/trunk/bin/sys_nicmonitor#1.8.1-x86_64-Linx-Rocky4.2.pkg.tar.gz and /dev/null differ diff --git a/code/trunk/doc/网卡监视程序动态库使用手册【新】.doc b/code/trunk/doc/网卡监视程序动态库使用手册【新】.doc deleted file mode 100644 index 2faa068..0000000 Binary files a/code/trunk/doc/网卡监视程序动态库使用手册【新】.doc and /dev/null differ diff --git a/code/trunk/doc/网卡监视程序安装说明以及使用手册【新】.doc b/code/trunk/doc/网卡监视程序安装说明以及使用手册【新】.doc deleted file mode 100644 index 181b641..0000000 Binary files a/code/trunk/doc/网卡监视程序安装说明以及使用手册【新】.doc and /dev/null differ diff --git a/code/trunk/src/Pkgfile b/code/trunk/src/Pkgfile deleted file mode 100644 index afa9a28..0000000 --- a/code/trunk/src/Pkgfile +++ /dev/null @@ -1,24 +0,0 @@ -# Description: A system to store and display time-series data. -# URL: http://oss.oetiker.ch/rrdtool/index.en.html -# Maintainer: Danny Rawlins, monster dot romster at gmail dot com -# Packager: Younes Hafri, ycrux at club-internet dot fr -# Depends on: libart_lgpl libcgi libgd pango python tcl intltool - -name=sys_nicmonitor -version=1.9 -release=x86_64-Linx-Rocky4.2 -workdir=`pwd` -#source=/home/d5000/d5000/linx/1.8/$name-$version.tar.gz -#source=/mnt/rbqiu/nanri/sys_nicmonitor/0605/1.8/src/$name-$version.tar.gz -echo $workdir -source=$workdir/$name-$version.tar.gz - -build() { - cd $name-$version - - make - make DESTDIR=$PKG install -# mkdir -p $PKG/var/log/netcard -# chmod 777 $PKG/var/log/netcard -} - diff --git a/code/trunk/src/README b/code/trunk/src/README deleted file mode 100644 index 726685a..0000000 --- a/code/trunk/src/README +++ /dev/null @@ -1,90 +0,0 @@ -========= -编译前准备: - 编译前先安装dotconf软件包。 - libman.so拷贝到/usr/lib64/下 - pkgadd dotconf#1.0.13-1.pkg.tar.gz - -编译说明: - 在Rocky 4.2上编译 - - 执行: sh build.sh - 将在当前的目录下生成安装包: - sys_nicmonitor#1.9-x86_64-Linx-Rocky4.2.pkg.tar.gz - -======== -安装说明: - 1)安装,以root的用户执行安装命令: - pkgadd sys_nicmonitor#1.9-x86_64-Linx-Rocky4.2.pkg.tar.gz - 如果之前已经安装,则可先卸载旧的然后再安装,或者通过更新方式安装 - 卸载命令: pkgrm sys_nicmonitor - 更新方式安装命令: - pkgadd -u sys_nicmonitor#1.9-x86_64-Linx-Rocky4.2.pkg.tar.gz - - 2)查看安装是否成功 - pkginfo -i|grep sys_nicmonitor - 或者 - 查看d5000用户的目录下lib/libnic_shm.so和bin/sys_nicmonitor的日期是否匹配 - -======== - -版本说明: ---V1.9 -- - 2013-08-12 - 1)修复问题:占用内存快速累加,导致内存占用很大 - 问题描述:日志线程资源退出时未回收,导致占用内存快速累加 - 解决办法:修改为一个日志线程,并且线程退出时设置为自动回收。 - 将日志写文件的地方放到common.c中,以便动态库和daemon程序使用一个。 - - 2)修复问题:日志文件名在家目录或者var目录下,未在规定位置 - 问题描述:日志文件命名在一个单独的线程进行,导致日志线程和该线程对log_path存在同时访问的情况。 - 导致在换天或者换月时,日志文件名变更一半时,日志写线程此时写日志,用未更新完成的 - log_path创建日志,导致日志文件不再规定的位置。 - 解决办法:增加线程互斥锁pthread_mutex对log_path进行保护,修复该问题。 - - ---V1.8.1-- - 2013-06-05 - 1)修改BUG:ping_gw函数当PING失败时没有尝试对余下的GW进行PING操作 - 2)增加环境变量SYS_NIC_DEBUG用于调试流量抖动问题。 - ---V1.8 -- - 2013-05-27 -对系统高磁盘IO情况下,应用如果进行IO将可能会被阻塞。本版本为此作出调整。 -cs-fes1上编译测试OK。 - -1) 去掉shm和sem的实体文件 -#define SHM_PATH "$D5000_HOME/share/sys_netcard_shm_path" -#define SEM_PATH "$D5000_HOME/share/sys_netcard_sem_path" - -用key=0x1d5200 代替 sem -用key=0x1d5010 代替 shm - - -2)去掉动态库中get_nic_info接口中,判断路径的opendir调用,采用实际路径 - 上层应用每秒可能调用3次这个接口,每判断一个网卡都调用一次,判断都个网卡的状态时就调用多次。 - - 可改进:一次调用get_nic_info接口,可输入多个网卡,一次返回所有网卡状态信息。 - -3) record_log函数为日志函数,每次写一次,都重新打开一次文件。 - 修改为线程模式,当记录日志时,不会被磁盘IO阻塞。 - -4) 安装目录为d5000用户的HOME目录下的lib和bin目录 - ----- - - -V1.6与V1.7差异 -共享内存路径修改: -由/home/d5000/someone/tmp/sys_netcard_shm_path 改为 /home/d5000/someone/share/sys_netcard_shm_path -信号量路径修改: -由/home/d5000/someone/tmp/sys_netcard_sem_path 改为 /home/d5000/someone/share/sys_netcard_sem_path - -注意:在编译过程中,需要动态看libman.so,它存放在目录 tags/下面 ,在进行编译的时候需要将其拷贝到 -/lib64/目录下,编译的基础环境是 Rocky 4.2 系统。 - -pkgmk命令的使用: -将源代码打包成name-version.tar.gz样式,编写Pkgfile文件。 -使用命令: - pkgmk -um #更新软件包的md5值 - pkgmk -kw #制作pkg安装包 - diff --git a/code/trunk/src/build.sh b/code/trunk/src/build.sh deleted file mode 100644 index ff47a6d..0000000 --- a/code/trunk/src/build.sh +++ /dev/null @@ -1,7 +0,0 @@ -pkgmk -um - -#For debug -#pkgmk -kw -pa post_add.sh - -#For release -pkgmk -pa post_add.sh diff --git a/code/trunk/src/post_add.sh b/code/trunk/src/post_add.sh deleted file mode 100644 index 1225839..0000000 --- a/code/trunk/src/post_add.sh +++ /dev/null @@ -1,16 +0,0 @@ -#!/bin/bash -#echo $HOME -DFDIR=`cat /etc/passwd | grep d5000 | cut -d ":" -f 6 ` -#LOGDIR=$DFDIR/var/log/netcard -#mkdir -p $LOGDIR -echo $DFDIR -#echo $LOGDIR - -echo "$DFDIR/lib/libnic_shm.so" -echo "$DFDIR/bin/sys_nicmonitor" - -mv /usr/lib64/libnic_shm.so $DFDIR/lib -mv /usr/bin/sys_nicmonitor $DFDIR/bin - -chown d5000.d5000 $DFDIR/lib/libnic_shm.so -chown d5000.d5000 $DFDIR/bin/sys_nicmonitor diff --git a/code/trunk/src/post_mk.sh b/code/trunk/src/post_mk.sh deleted file mode 100644 index 642b9a9..0000000 --- a/code/trunk/src/post_mk.sh +++ /dev/null @@ -1,10 +0,0 @@ -#!/bin/bash - -PWD=`pwd` -#pkg=`basename $PWD` -pkg=sys_nicmonitor - -# install the script which is run just after pkgadd or -# when booting from disk first time -mkdir -p $PKG/$PI_DIR -install -m 755 post_add.sh $PKG/$PI_DIR/${pkg}.post_add diff --git a/code/trunk/src/sys_nicmonitor-1.8.tar.gz b/code/trunk/src/sys_nicmonitor-1.8.tar.gz deleted file mode 100644 index 0215da9..0000000 Binary files a/code/trunk/src/sys_nicmonitor-1.8.tar.gz and /dev/null differ diff --git a/code/trunk/src/sys_nicmonitor-1.9.tar.gz b/code/trunk/src/sys_nicmonitor-1.9.tar.gz deleted file mode 100644 index 75a49d3..0000000 Binary files a/code/trunk/src/sys_nicmonitor-1.9.tar.gz and /dev/null differ diff --git a/code/trunk/sys_nicmonitor/README.md b/code/trunk/sys_nicmonitor/README.md deleted file mode 100644 index e69de29..0000000 diff --git a/code/trunk/sys_nicmonitor/src/dep/libman.so b/code/trunk/sys_nicmonitor/src/dep/libman.so deleted file mode 100644 index c78c12a..0000000 Binary files a/code/trunk/sys_nicmonitor/src/dep/libman.so and /dev/null differ diff --git a/code/trunk/sys_nicmonitor/src/post_add.sh b/code/trunk/sys_nicmonitor/src/post_add.sh deleted file mode 100644 index 1225839..0000000 --- a/code/trunk/sys_nicmonitor/src/post_add.sh +++ /dev/null @@ -1,16 +0,0 @@ -#!/bin/bash -#echo $HOME -DFDIR=`cat /etc/passwd | grep d5000 | cut -d ":" -f 6 ` -#LOGDIR=$DFDIR/var/log/netcard -#mkdir -p $LOGDIR -echo $DFDIR -#echo $LOGDIR - -echo "$DFDIR/lib/libnic_shm.so" -echo "$DFDIR/bin/sys_nicmonitor" - -mv /usr/lib64/libnic_shm.so $DFDIR/lib -mv /usr/bin/sys_nicmonitor $DFDIR/bin - -chown d5000.d5000 $DFDIR/lib/libnic_shm.so -chown d5000.d5000 $DFDIR/bin/sys_nicmonitor diff --git a/code/trunk/sys_nicmonitor/src/post_mk.sh b/code/trunk/sys_nicmonitor/src/post_mk.sh deleted file mode 100644 index 642b9a9..0000000 --- a/code/trunk/sys_nicmonitor/src/post_mk.sh +++ /dev/null @@ -1,10 +0,0 @@ -#!/bin/bash - -PWD=`pwd` -#pkg=`basename $PWD` -pkg=sys_nicmonitor - -# install the script which is run just after pkgadd or -# when booting from disk first time -mkdir -p $PKG/$PI_DIR -install -m 755 post_add.sh $PKG/$PI_DIR/${pkg}.post_add diff --git a/code/trunk/sys_nicmonitor/src/sys_nicmonitor/Makefile.config b/code/trunk/sys_nicmonitor/src/sys_nicmonitor/Makefile.config deleted file mode 100644 index 7a2d8e6..0000000 --- a/code/trunk/sys_nicmonitor/src/sys_nicmonitor/Makefile.config +++ /dev/null @@ -1,16 +0,0 @@ - -PREFIX = $(DESTDIR) - -BINDIR = $(PREFIX)/usr/bin - -LIBDIR = $(PREFIX)/usr/lib64 - -INCDIR = $(PREFIX)/usr/include - -INSTALL_SH = ./install.sh - -CP = cp -RM = rm -#CC = gcc -CC = g++ -MKDIR = mkdir diff --git a/code/trunk/sys_nicmonitor/src/sys_nicmonitor/const.h b/code/trunk/sys_nicmonitor/src/sys_nicmonitor/const.h deleted file mode 100644 index e2ebfa5..0000000 --- a/code/trunk/sys_nicmonitor/src/sys_nicmonitor/const.h +++ /dev/null @@ -1,51 +0,0 @@ -#ifndef _RTE_CONST_H -#define _RTE_CONST_H - -#include - - -#define MAX_STRING_LEN 24 -#define MAX_EXECMD_LEN 200 -#define MAX_FILENAME_LEN 200 - -#define MAX_CONTEXT 8 -#define MAX_HOSTNAME_LEN 24 -#define MAX_LOCAL_MESPROC 512 -#define MAX_LOCAL_PROCESS MAX_LOCAL_MESPROC -#define MAX_LOCAL_APP 32 -#define MAX_LOCAL_NODE 256 - -#define MAX_SET 256 //max num of event set -#define MAX_EVENT 1300 -#define MAX_REG_PROC 20 - -#define LEN_SHMBLK_BIG 4096 -#define MAX_MSGBUF_LEN 32767 -#define FREE_PAGE_SIZE 65536 -#define MAX_PAGE_NUM 1024 - -#define MAX_QUE MAX_LOCAL_MESPROC -#define MAX_SEMPHORE_SET MAX_LOCAL_MESPROC*2 -#define RTE_HAN 0 // queue number for event handeler0(RTE) -#define EX_EVENT_HAN RTE_HAN // queue number for extrnal event handeler - -#define DOMAIN_I 1 -#define DOMAIN_II 2 -#define DOMAIN_III 3 - -//Add 20090225 -const int PROC_TYPE_RPT =1; -const int PROC_TYPE_UNRPT =0; -//add end - - -//status for proc and app -const int NON_ACTIVE = 0; -const int ACTIVE = 1; -const int HANGUP = 2; -const int FAILURE = 5; -const int START = 6; -const int STOP = 7; - -#endif - diff --git a/code/trunk/sys_nicmonitor/src/sys_nicmonitor/mnic.h b/code/trunk/sys_nicmonitor/src/sys_nicmonitor/mnic.h deleted file mode 100644 index d7a6390..0000000 --- a/code/trunk/sys_nicmonitor/src/sys_nicmonitor/mnic.h +++ /dev/null @@ -1,44 +0,0 @@ -#ifndef __MNIC_H -#define __MNIC_H - -//#define _GNU_SOURCE -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include - -#include "sys_netcard.h" -#include -#include -#define IPSIZE 16 - -char *get_name(char *, char *); -int if_fetch(NETCARD_INFO *); -int ioc_get_name(NETCARD_INFO *); -int get_dev_fields(char *p, NETCARD_INFO *); -int get_user_home(int , char *); -void send_alarm(D5000_NIC_ALARM *, int , char *); -void record_log(char *); - -#endif diff --git a/code/trunk/sys_nicmonitor/src/sys_nicmonitor/nicinfo_shm.h b/code/trunk/sys_nicmonitor/src/sys_nicmonitor/nicinfo_shm.h deleted file mode 100644 index cd74135..0000000 --- a/code/trunk/sys_nicmonitor/src/sys_nicmonitor/nicinfo_shm.h +++ /dev/null @@ -1,52 +0,0 @@ -#ifndef NIC_SHM_H -#define NIC_SHM_H - -#include - -#ifdef __cplusplus -extern "C" { -#endif - -#ifndef NIC_NAME_LEN -#define NIC_NAME_LEN 64 -#endif -#define SEM_PROJ_ID 's' -#define MAXNICNUM 32 - -typedef long KEYID; -typedef struct _net_info -{ - KEYID ID; - char charname[NIC_NAME_LEN]; - char descr[128]; - struct sockaddr addr; - struct sockaddr broadaddr; - struct sockaddr netmask; - struct sockaddr hwaddr; - int time_stamp; - short flags; - int mtu; - int tx_queue_len; - unsigned long long average_flow; - unsigned long long rx_packets; - unsigned long long tx_packets; - unsigned long long rx_bytes; - unsigned long long tx_bytes; - unsigned long rx_errors; - unsigned long tx_errors; - unsigned long rx_dropped; - unsigned long tx_dropped; - unsigned long rx_multicast; - unsigned long collisions; - unsigned long rx_fifo_errors; - unsigned long tx_carrier_errors; - unsigned long tx_fifo_errors; -}NETCARD_INFO; - -int get_nic_info(char *nic_name, NETCARD_INFO *net_info); - - -#ifdef __cplusplus -} -#endif -#endif diff --git a/code/trunk/sys_nicmonitor/src/sys_nicmonitor/send_alarm.c b/code/trunk/sys_nicmonitor/src/sys_nicmonitor/send_alarm.c deleted file mode 100644 index e88635f..0000000 --- a/code/trunk/sys_nicmonitor/src/sys_nicmonitor/send_alarm.c +++ /dev/null @@ -1,37 +0,0 @@ -#include "mnic.h" - -void send_alarm(D5000_NIC_ALARM *mesg, int socket_port, char *ipv4) -{ - int bytes; - int sock_sd; - int val; - char error_str[200]; - struct sockaddr_in recever; - socklen_t sock_len; - - if((sock_sd = socket(PF_INET, SOCK_DGRAM, IPPROTO_UDP)) == -1){ - snprintf(error_str, sizeof(error_str), "EMERG: socket(): %s\n", strerror(errno)); - record_log(error_str); - return; - } - val=1; - if ((setsockopt(sock_sd, SOL_SOCKET, SO_BROADCAST, &val, sizeof(val))) == -1) { - snprintf(error_str, sizeof(error_str), "EMERG: setsockopt(): %s\n", strerror(errno)); - record_log(error_str); - return; - } - /* init socket*/ - recever.sin_family = AF_INET; - recever.sin_port = htons(socket_port); - inet_pton(AF_INET, ipv4, &recever.sin_addr); - sock_len = sizeof(recever); - /* send alarm*/ - if((bytes = sendto(sock_sd, mesg, sizeof(D5000_NIC_ALARM), 0, (const struct sockaddr *)&recever, sock_len)) < 1){ - snprintf(error_str, sizeof(error_str), "EMERG: sendto(): %s\n", strerror(errno)); - record_log(error_str); - return; - } - //snprintf(error_str, sizeof(error_str), "NOTICE: Alarm information has already send!\n"); - //record_log(error_str); - close(sock_sd); -} diff --git a/code/trunk/sys_nicmonitor/src/sys_nicmonitor/sys_netcard.h b/code/trunk/sys_nicmonitor/src/sys_nicmonitor/sys_netcard.h deleted file mode 100644 index 0d7746c..0000000 --- a/code/trunk/sys_nicmonitor/src/sys_nicmonitor/sys_netcard.h +++ /dev/null @@ -1,88 +0,0 @@ -#ifndef _SYS_NETCARD_H -#define _SYS_NETCARD_H - -#include - -#ifndef NIC_NAME_LEN -#define NIC_NAME_LEN 64 -#endif - -#define SEM_PROJ_ID 's' -#define MAXNICNUM 32 - -#ifdef __cplusplus -extern "C"{ -#endif - -typedef long KEYID; - -typedef struct _msg_frame // message frame -{ - short len; // message length - short seqno; // send sequence - short serv; // services ID - short event; // event ID - unsigned char domain; // domain ID - unsigned char ctxt; // Context ID - short stid; // SOURCE task id - short dtid; // DESTINATION task ID - unsigned char ver_coding; // 版本号 + 编码 - unsigned char mes_type; // 帧类型 -}MSG_FRAME, *LPMSG_FRAME; - -typedef struct _net_info -{ - KEYID ID; - char charname[NIC_NAME_LEN]; - char descr[128]; - struct sockaddr addr; - struct sockaddr broadaddr; - struct sockaddr netmask; - struct sockaddr hwaddr; - int time_stamp; - short flags; - int mtu; - int tx_queue_len; - unsigned long long average_flow; - unsigned long long rx_packets; - unsigned long long tx_packets; - unsigned long long rx_bytes; - unsigned long long tx_bytes; - unsigned long rx_errors; - unsigned long tx_errors; - unsigned long rx_dropped; - unsigned long tx_dropped; - unsigned long rx_multicast; - unsigned long collisions; - unsigned long rx_fifo_errors; - unsigned long tx_carrier_errors; - unsigned long tx_fifo_errors; -}NETCARD_INFO, *LPNETCARD_INFO; - -#define NETCARD_ALARM_FAULT 1 -#define NETCARD_ALARM_RESUME 2 -#define NETCARD_ALARM_SWITCH 3 -#define NETCARD_ALARM_ABNORMAL 4 -#define NETCARD_ALARM_NORMAL 5 -typedef struct _sys_netcard_alarm -{ - char fault_devname[NIC_NAME_LEN]; /*故障设备名称 */ - char switch_devname[NIC_NAME_LEN]; /*切换设备名称 */ - short flags; /* 状态标记 : 1:网卡故障。2:网卡恢复。3:网卡切换。4:流量异常。*/ - short retrytimes; //0,1,2 重发次数 -}SYS_NETCARD_ALARM, *LPSYS_NETCARD_ALARM; - - -typedef struct _d5000_nic_alarm -{ - MSG_FRAME tMsgFrame ; - SYS_NETCARD_ALARM tSysNetcardAlarm ; -}D5000_NIC_ALARM, *LPD5000_NIC_ALARM; - - -#ifdef __cplusplus -} -#endif - -#endif - diff --git a/code/trunk/sys_nicmonitor/sys_nicmonitor-from-svn.tar.bz2 b/code/trunk/sys_nicmonitor/sys_nicmonitor-from-svn.tar.bz2 deleted file mode 100644 index b49956e..0000000 Binary files a/code/trunk/sys_nicmonitor/sys_nicmonitor-from-svn.tar.bz2 and /dev/null differ diff --git a/code/trunk/test/Makefile b/code/trunk/test/Makefile deleted file mode 100644 index e60b92f..0000000 --- a/code/trunk/test/Makefile +++ /dev/null @@ -1,8 +0,0 @@ - -all:read_shm recv_msg getmem - -getmem:getmem.c -# g++ -g -L ../dynamic_lib -lnic_shm -o $@ $^ - g++ -g -L ${HOME}/lib -lpthread -lnic_shm -o $@ $^ -clean: - rm getmem read_shm recv_msg diff --git a/code/trunk/test/dotconf.h b/code/trunk/test/dotconf.h deleted file mode 100644 index e802508..0000000 --- a/code/trunk/test/dotconf.h +++ /dev/null @@ -1,264 +0,0 @@ -#ifndef DOTCONF_H -#define DOTCONF_H - -#ifdef __cplusplus -extern "C" { -#endif - -/* stdio.h should be included by the application - as the manual page says */ -#ifndef _STDIO_H -#include /* needed for FILE* */ -#endif - -#ifdef WIN32 -# ifndef R_OK -#define R_OK 0 -# endif -#endif - -/* some buffersize definitions */ -#define CFG_BUFSIZE 4096 /* max length of one line */ -#define CFG_MAX_OPTION 32 /* max length of any option name */ -#define CFG_MAX_VALUE 4064 /* max length of any options value */ -#define CFG_MAX_FILENAME 256 /* max length of a filename */ -#define CFG_VALUES 16 /* max # of arguments an option takes */ - -#define CFG_INCLUDEPATH_ENV "DC_INCLUDEPATH" -#define WILDCARDS "*?" /* list of supported wild-card characters */ - -/* constants for type of option */ -#define ARG_TOGGLE 0 /* TOGGLE on,off; yes,no; 1, 0; */ -#define ARG_INT 1 /* callback wants an integer */ -#define ARG_STR 2 /* callback expects a \0 terminated str */ -#define ARG_LIST 3 /* wants list of strings */ -#define ARG_NAME 4 /* wants option name plus ARG_LIST stuff */ -#define ARG_RAW 5 /* wants raw argument data */ -#define ARG_NONE 6 /* does not expect ANY args */ - -#define CTX_ALL 0 /* context: option can be used anywhere */ - -/* for convenience of terminating the dotconf_options list */ -#define LAST_OPTION { "", 0, NULL, NULL } -#define LAST_CONTEXT_OPTION { "", 0, NULL, NULL, 0 } - -#define DOTCONF_CB(__name) const char *__name(command_t *cmd, \ - context_t *ctx) -#define FUNC_ERRORHANDLER(_name) int _name(configfile_t * configfile, \ - int type, long dc_errno, const char *msg) - - -/* some flags that change the runtime behaviour of dotconf */ -#define NONE 0 -#define CASE_INSENSITIVE 1<<0 /* match option names case insensitive */ -#define DONT_SUBSTITUTE 1<<1 /* do not call substitute_env after read_arg */ -#define NO_INLINE_COMMENTS 1<<2 /* do not allow inline comments */ -#define DUPLICATE_OPTION_NAMES 1<<3 /* allow for duplicate option names */ - -/* syslog style errors as suggested by Sander Steffann */ -#ifdef HAVE_SYSLOG -#include - -#define DCLOG_EMERG LOG_EMERG /* system is unusable */ -#define DCLOG_ALERT LOG_ALERT /* action must be taken immediately */ -#define DCLOG_CRIT LOG_CRIT /* critical conditions */ -#define DCLOG_ERR LOG_ERR /* error conditions */ -#define DCLOG_WARNING LOG_WARNING /* warning conditions */ -#define DCLOG_NOTICE LOG_NOTICE /* normal but significant condition */ -#define DCLOG_INFO LOG_INFO /* informational */ -#define DCLOG_DEBUG LOG_DEBUG /* debug-level messages */ - -#define DCLOG_LEVELMASK LOG_PRIMASK /* mask off the level value */ - -#else /* HAVE_SYSLOG */ - -#define DCLOG_EMERG 0 /* system is unusable */ -#define DCLOG_ALERT 1 /* action must be taken immediately */ -#define DCLOG_CRIT 2 /* critical conditions */ -#define DCLOG_ERR 3 /* error conditions */ -#define DCLOG_WARNING 4 /* warning conditions */ -#define DCLOG_NOTICE 5 /* normal but significant condition */ -#define DCLOG_INFO 6 /* informational */ -#define DCLOG_DEBUG 7 /* debug-level messages */ - -#define DCLOG_LEVELMASK 7 /* mask off the level value */ - -#endif /* HAVE_SYSLOG */ - -/* callback types for dotconf_callback */ - -/* error constants */ -#define ERR_NOERROR 0x0000 -#define ERR_PARSE_ERROR 0x0001 -#define ERR_UNKNOWN_OPTION 0x0002 -#define ERR_WRONG_ARG_COUNT 0x0003 -#define ERR_INCLUDE_ERROR 0x0004 -#define ERR_NOACCESS 0x0005 -#define ERR_USER 0x1000 /* base for userdefined errno's */ - -/* i needed this to check an ARG_LIST entry if it's toggled in one of my apps; maybe you do too */ -#define CFG_TOGGLED(_val) ( (_val[0] == 'Y' \ - || _val[0] == 'y') \ - || (_val[0] == '1') \ - || ((_val[0] == 'o' \ - || _val[0] == 'O') \ - && (_val[1] == 'n' \ - || _val[1] == 'N'))) - -enum callback_types -{ - ERROR_HANDLER = 1, - CONTEXT_CHECKER -}; - -typedef enum callback_types callback_types; -typedef struct configfile_t configfile_t; -typedef struct configoption_t configoption_t; -typedef struct configoption_t ConfigOption; -typedef struct command_t command_t; -typedef void context_t; -typedef void info_t; - -typedef const char *(*dotconf_callback_t)(command_t *, context_t *); -typedef int (*dotconf_errorhandler_t)(configfile_t *, int, unsigned long, const char *); -typedef const char *(*dotconf_contextchecker_t)(command_t *, unsigned long); - -struct configfile_t -{ - /* ------ the fields in configfile_t are provided to the app via command_t's ; READ ONLY! --- */ - - FILE *stream; - char eof; /* end of file reached ? */ - size_t size; /* file size; cached on-demand for here-documents */ - - context_t *context; - - configoption_t const **config_options; - int config_option_count; - - /* ------ misc read-only fields ------------------------------------------------------------- */ - char *filename; /* name of file this option was found in */ - unsigned long line; /* line number we're currently at */ - unsigned long flags; /* runtime flags given to dotconf_open */ - - char *includepath; - - /* ------ some callbacks for interactivity -------------------------------------------------- */ - dotconf_errorhandler_t errorhandler; - dotconf_contextchecker_t contextchecker; - - int (*cmp_func)(const char *, const char *, size_t); -}; - -struct configoption_t -{ - const char *name; /* name of configuration option */ - int type; /* for possible values, see above */ - dotconf_callback_t callback; /* callback function */ - info_t *info; /* additional info for multi-option callbacks */ - unsigned long context; /* context sensitivity flags */ -}; - -struct command_t -{ - const char *name; /* name of the command */ - configoption_t *option; /* the option as given in the app; READ ONLY */ - - /* ------ argument data filled in for each line / command ----------------------------------- */ - struct { - long value; /* ARG_INT, ARG_TOGGLE */ - char *str; /* ARG_STR */ - char **list; /* ARG_LIST */ - } data; - int arg_count; /* number of arguments (in data.list) */ - - /* ------ misc context information ---------------------------------------------------------- */ - configfile_t *configfile; - context_t *context; -}; - -/* ------ dotconf_create() - create the configfile_t needed for further dot.conf fun ------------ */ -configfile_t *dotconf_create(char *, const configoption_t *, context_t *, unsigned long); - -/* ------ dotconf_cleanup() - tidy up behind dotconf_create and the parser dust ----------------- */ -void dotconf_cleanup(configfile_t *configfile); - -/* ------ dotconf_command_loop() - iterate through each line of file and handle the commands ---- */ -int dotconf_command_loop(configfile_t *configfile); - -/* ------ dotconf_command_loop_until_error() - like continue_line but return on the first error - */ -const char *dotconf_command_loop_until_error(configfile_t *configfile); - -/* ------ dotconf_continue_line() - check if line continuation is to be handled ----------------- */ -int dotconf_continue_line(char *buffer, size_t length); - -/* ------ dotconf_get_next_line() - read in the next line of the configfile_t ------------------- */ -int dotconf_get_next_line(char *buffer, size_t bufsize, configfile_t *configfile); - -/* ------ dotconf_get_here_document() - read the here document until delimit is found ----------- */ -char *dotconf_get_here_document(configfile_t *configfile, const char *delimit); - -/* ------ dotconf_invoke_command() - call the callback for command_t ---------------------------- */ -const char *dotconf_invoke_command(configfile_t *configfile, command_t *cmd); - -/* ------ dotconf_find_command() - iterate through all registered options trying to match ------- */ -configoption_t *dotconf_find_command(configfile_t *configfile, const char *command); - -/* ------ dotconf_read_arg() - read one argument from the line handling quoting and escaping ---- */ -/* - side effects: the char* returned by dotconf_read_arg is malloc() before, hence that pointer - will have to be free()ed later. -*/ -char *dotconf_read_arg(configfile_t *configfile, signed char **line); - -/* ------ dotconf_handle_command() - parse, substitute, find, invoke the command found in buffer */ -const char *dotconf_handle_command(configfile_t *configfile, char *buffer); - -/* ------ dotconf_register_option() - add a new option table to the list of commands ------------ */ -void dotconf_register_options(configfile_t *configfile, const configoption_t *options); - -/* ------ dotconf_warning() - handle the dispatch of error messages of various levels ----------- */ -int dotconf_warning(configfile_t *configfile, int level, unsigned long errnum, const char *, ...); - -/* ------ dotconf_callback() - register a special callback -------------------------------------- */ -void dotconf_callback(configfile_t *configfile, callback_types type, dotconf_callback_t); - -/* ------ dotconf_substitute_env() - handle the substitution on environment variables ----------- */ -char *dotconf_substitute_env(configfile_t *, char *); - -/* ------ internal utility function that verifies if a character is in the WILDCARDS list -- */ -int dotconf_is_wild_card(char value); - -/* ------ internal utility function that calls the appropriate routine for the wildcard passed in -- */ -int dotconf_handle_wild_card(command_t* cmd, char wild_card, char* path, char* pre, char* ext); - -/* ------ internal utility function that frees allocated memory from dotcont_find_wild_card -- */ -void dotconf_wild_card_cleanup(char* path, char* pre); - -/* ------ internal utility function to check for wild cards in file path -- */ -/* ------ path and pre must be freed by the developer ( dotconf_wild_card_cleanup) -- */ -int dotconf_find_wild_card(char* filename, char* wildcard, char** path, char** pre, char** ext); - -/* ------ internal utility function that compares two stings from back to front -- */ -int dotconf_strcmp_from_back(const char* s1, const char* s2); - -/* ------ internal utility function that determins if a string matches the '?' criteria -- */ -int dotconf_question_mark_match(char* dir_name, char* pre, char* ext); - -/* ------ internal utility function that determins if a string matches the '*' criteria -- */ -int dotconf_star_match(char* dir_name, char* pre, char* ext); - -/* ------ internal utility function that determins matches for filenames with -- */ -/* ------ a '?' in name and calls the Internal Include function on that filename -- */ -int dotconf_handle_question_mark(command_t* cmd, char* path, char* pre, char* ext); - -/* ------ internal utility function that determins matches for filenames with -- */ -/* ------ a '*' in name and calls the Internal Include function on that filename -- */ -int dotconf_handle_star(command_t* cmd, char* path, char* pre, char* ext); - - - -#ifdef __cplusplus -} /* extern "C" */ -#endif - -#endif /* DOTCONF_H */ diff --git a/code/trunk/test/getmem.c b/code/trunk/test/getmem.c deleted file mode 100644 index e9c31b3..0000000 --- a/code/trunk/test/getmem.c +++ /dev/null @@ -1,82 +0,0 @@ -#include -#include -#include - -#include "nicinfo_shm.h" - -typedef struct net_info{ - NETCARD_INFO info[MAXNICNUM]; -}SHM; - -int main(int argc, char **argv) -{ - int ret; - NETCARD_INFO net; -#if 0 - char shm_path[1024]; - char sem_path[1024]; - - memset(shm_path, 0, sizeof(shm_path)); - memset(sem_path, 0, sizeof(sem_path)); - - strcpy(shm_path, getenv("D5000_HOME")); - strcat(shm_path, "/conf/nic/shm_sys_netcard_info"); - strcpy(sem_path, getenv("D5000_HOME")); - strcat(sem_path, "/conf/nic/sem_sys_netcard_info"); - if((ret = init_nic_info(shm_path, sem_path)) == -1){ - fprintf(stderr, "shm or sem path error!\n"); - return -1; - } -#endif - memset(&net, 0, sizeof(NETCARD_INFO)); - if(argc != 2){ - fprintf(stderr, "Usage:%s netcard\n", argv[0]); - return -1; - } - while((ret = get_nic_info(argv[1], &net)) != -1){ - fprintf(stdout, "==========================网卡信息==============================\n"); - fprintf(stdout,"DEVICE NAME : %s\nIPADDR : %d.%d.%d.%d\nBROADCAST : %d.%d.%d.%d\nNETMASK : %d.%d.%d.%d\nHWADDR : %02x:%02x:%02x:%02x:%02x:%02x\nFLAGS = %d\nMTU = %d\nTx_queue_len = %d\nTime_stamp = %d\n\nReceive bytes:%llu\nReceive packets:%llu\nReceive errors:%lu\nReceive dropped:%lu\nReceive multicast:%lu\nReceive fifo:%lu\n\nTransmit bytes:%llu\nTransmit packets:%llu\nTransmit errors:%lu\nTransmit dropped:%lu\nTransmit fifo:%lu\nTransmit collisions:%lu\nTransmit carrier:%lu\n\nAverage flow:%d\n", - net.charname, - (unsigned char)net.addr.sa_data[2], - (unsigned char)net.addr.sa_data[3], - (unsigned char)net.addr.sa_data[4], - (unsigned char)net.addr.sa_data[5], - (unsigned char)net.broadaddr.sa_data[2], - (unsigned char)net.broadaddr.sa_data[3], - (unsigned char)net.broadaddr.sa_data[4], - (unsigned char)net.broadaddr.sa_data[5], - (unsigned char)net.netmask.sa_data[2], - (unsigned char)net.netmask.sa_data[3], - (unsigned char)net.netmask.sa_data[4], - (unsigned char)net.netmask.sa_data[5], - (unsigned char)net.hwaddr.sa_data[0], - (unsigned char)net.hwaddr.sa_data[1], - (unsigned char)net.hwaddr.sa_data[2], - (unsigned char)net.hwaddr.sa_data[3], - (unsigned char)net.hwaddr.sa_data[4], - (unsigned char)net.hwaddr.sa_data[5], - net.flags, - net.mtu, - net.tx_queue_len, - net.time_stamp, - net.rx_bytes, - net.rx_packets, - net.rx_errors, - net.rx_dropped, - net.rx_multicast, - net.rx_fifo_errors, - net.tx_bytes, - net.tx_packets, - net.tx_errors, - net.tx_dropped, - net.tx_fifo_errors, - net.collisions, - net.tx_carrier_errors, - net.average_flow); - getchar(); - } - fprintf(stderr, "No information of %s !\n", argv[1]); - -// rls_nic_info(); - return 0; -} diff --git a/code/trunk/test/mnic.h b/code/trunk/test/mnic.h deleted file mode 100644 index d6255d5..0000000 --- a/code/trunk/test/mnic.h +++ /dev/null @@ -1,66 +0,0 @@ -#ifndef __MNIC_H -#define __MNIC_H - -#define _GNU_SOURCE -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include - -#include "sys_netcard.h" -#include "dotconf.h" - -#define IPSIZE 16 -#define PATH_SIZE 256 - -DOTCONF_CB(cb_str); -DOTCONF_CB(cb_int); - -typedef struct __config_file_st{ - unsigned char domain; - short serv; - short event; - int udpport; - char sys_netcard_shm_path[PATH_SIZE]; - char sys_netcard_sem_path[PATH_SIZE]; - int monitor_interval; - int write_interval; - int flow_interval; - int flow_limit; - int flow_peak; - char udp[NIC_NAME_LEN]; - char nic[MAXNICNUM][NIC_NAME_LEN]; - char ip[IPSIZE] ; -}CONFIG_FILE_ST; - - -char *get_name(char *, char *); -int if_fetch(NETCARD_INFO *); -int ioc_get_name(NETCARD_INFO *); -int get_dev_fields(char *p, NETCARD_INFO *); -void send_alarm(D5000_NIC_ALARM *, int , char *, char *); -void record_log(void); - -#endif diff --git a/code/trunk/test/nicinfo_shm.h b/code/trunk/test/nicinfo_shm.h deleted file mode 100644 index 14c1aec..0000000 --- a/code/trunk/test/nicinfo_shm.h +++ /dev/null @@ -1,18 +0,0 @@ -#ifndef NIC_SHM_H -#define NIC_SHM_H - -#include "sys_netcard.h" - -#ifdef __cplusplus -extern "C" { -#endif - -int init_nic_info(char *share_path, char *sem_path); -int get_nic_info(char *nic_name, NETCARD_INFO *net_info); -void rls_nic_info(void); - - -#ifdef __cplusplus -} -#endif -#endif diff --git a/code/trunk/test/read_shm.c b/code/trunk/test/read_shm.c deleted file mode 100644 index d2aec3b..0000000 --- a/code/trunk/test/read_shm.c +++ /dev/null @@ -1,127 +0,0 @@ -#include "mnic.h" - -typedef struct net_info{ - NETCARD_INFO info[MAXNICNUM]; -}SHM; - -void get_sem(int semid) -{ - struct sembuf lock; - - lock.sem_num = 0; - lock.sem_op = -1; - lock.sem_flg = SEM_UNDO; - - while(semop(semid, &lock, 1)){ - if (errno == EINTR) { - continue; - } - perror("semop()"); - } -} - -void release_sem(int semid) -{ - int ret; - struct sembuf unlock; - - unlock.sem_num = 0; - unlock.sem_op = 1; - unlock.sem_flg = SEM_UNDO; - - if((ret = semop(semid, &unlock, 1)) == -1){ - perror("semop()"); - } -} - - -int main(void) -{ - int fd = -1; - int i; - int semid; - key_t key; - SHM *p; - struct timespec s_time, os_time; - char shm_path[1024]; - char sem_path[1024]; - - memset(shm_path, 0, sizeof(shm_path)); - memset(sem_path, 0, sizeof(sem_path)); - - strcpy(shm_path, "/tmp/sys_netcard_shm_path"); - strcpy(sem_path, "/tmp/sys_netcard_sem_path"); - - s_time.tv_sec = 1; - s_time.tv_nsec = 0; - - if ((key=ftok(sem_path, SEM_PROJ_ID)) == -1) { - perror("ftok()"); - exit(1); - } - if((semid = semget(key, 1, IPC_CREAT)) == -1){ - fprintf(stderr, "Create semaphore error\n"); - exit(1); - } - - if((fd = open(shm_path, O_RDWR)) == -1){ - fprintf(stderr, "open():%s\n", strerror(errno)); - return -1; - } - p = mmap(NULL, sizeof(SHM), PROT_READ, MAP_SHARED, fd, 0); - if(p == MAP_FAILED){ - perror("mmap"); - return -1; - } - perror("mmap"); - while(1){ - get_sem(semid); - for(i = 0; p->info[i].charname[0] != 0; i++){ - fprintf(stdout, "==========================网卡信息 [%d]==============================\n", (i + 1)); - fprintf(stdout,"KEYID = %ld\nBONDING:%s\nDEVICE NAME : %s\nIPADDR : %d.%d.%d.%d\nBROADCAST : %d.%d.%d.%d\nNETMASK : %d.%d.%d.%d\nHWADDR : %02x:%02x:%02x:%02x:%02x:%02x\nFLAGS = %d\nMTU = %d\nTx_queue_len = %d\nTime_stamp = %d\n\nReceive bytes:%llu\nReceive packets:%llu\nReceive errors:%lu\nReceive dropped:%lu\nReceive multicast:%lu\nReceive fifo:%lu\n\nTransmit bytes:%llu\nTransmit packets:%llu\nTransmit errors:%lu\nTransmit dropped:%lu\nTransmit fifo:%lu\nTransmit collisions:%lu\nTransmit carrier:%lu\nAverage flow : %lld\n", - p->info[i].ID, - p->info[i].descr, - p->info[i].charname, - (unsigned char)p->info[i].addr.sa_data[2], - (unsigned char)p->info[i].addr.sa_data[3], - (unsigned char)p->info[i].addr.sa_data[4], - (unsigned char)p->info[i].addr.sa_data[5], - (unsigned char)p->info[i].broadaddr.sa_data[2], - (unsigned char)p->info[i].broadaddr.sa_data[3], - (unsigned char)p->info[i].broadaddr.sa_data[4], - (unsigned char)p->info[i].broadaddr.sa_data[5], - (unsigned char)p->info[i].netmask.sa_data[2], - (unsigned char)p->info[i].netmask.sa_data[3], - (unsigned char)p->info[i].netmask.sa_data[4], - (unsigned char)p->info[i].netmask.sa_data[5], - (unsigned char)p->info[i].hwaddr.sa_data[0], - (unsigned char)p->info[i].hwaddr.sa_data[1], - (unsigned char)p->info[i].hwaddr.sa_data[2], - (unsigned char)p->info[i].hwaddr.sa_data[3], - (unsigned char)p->info[i].hwaddr.sa_data[4], - (unsigned char)p->info[i].hwaddr.sa_data[5], - p->info[i].flags, - p->info[i].mtu, - p->info[i].tx_queue_len, - p->info[i].time_stamp, - p->info[i].rx_bytes, - p->info[i].rx_packets, - p->info[i].rx_errors, - p->info[i].rx_dropped, - p->info[i].rx_multicast, - p->info[i].rx_fifo_errors, - p->info[i].tx_bytes, - p->info[i].tx_packets, - p->info[i].tx_errors, - p->info[i].tx_dropped, - p->info[i].tx_fifo_errors, - p->info[i].collisions, - p->info[i].tx_carrier_errors, - p->info[i].average_flow); - } - nanosleep(&s_time, &os_time); - release_sem(semid); - } - close(fd); - return 0; -} diff --git a/code/trunk/test/recv_msg.c b/code/trunk/test/recv_msg.c deleted file mode 100644 index a9326bf..0000000 --- a/code/trunk/test/recv_msg.c +++ /dev/null @@ -1,72 +0,0 @@ -#include "mnic.h" -#define IP4STRSIZE 16 - -int -main(int argc, char **argv) -{ - int ret, value; - int sock_sd, sock_port = 15000; - D5000_NIC_ALARM mesg; - struct sockaddr_in myend, hisend; - socklen_t hisend_len; - fd_set rset; - char ip4str[IP4STRSIZE]; - - if(argc > 1){ - sock_port = atoi(argv[1]); - if(sock_port <= 1024 || sock_port > 65535){ - fprintf(stderr, "Invalid socket port!\n"); - exit(1); - } - } - - sock_sd = socket(PF_INET, SOCK_DGRAM, IPPROTO_UDP); - if (sock_sd==-1) { - perror("socket()"); - exit(1); - } - - value=1; - ret = setsockopt(sock_sd, SOL_SOCKET, SO_BROADCAST, &value, sizeof(value)); - if (ret==-1) { - perror("setsockopt()"); - exit(1); - } - - myend.sin_family = AF_INET; - myend.sin_port = htons(sock_port); - inet_pton(AF_INET, "0.0.0.0", &myend.sin_addr); - - ret = bind(sock_sd, (struct sockaddr*)&myend, sizeof(myend)); - if (ret==-1) { - perror("bind()"); - exit(1); - } - - hisend_len = sizeof(hisend); - while (1) { - FD_ZERO(&rset); - FD_SET(sock_sd, &rset); - ret = select(sock_sd+1, &rset, NULL, NULL, NULL); - if (ret==-1) { - perror("select()"); - continue; - } - ret = recvfrom(sock_sd, &mesg, sizeof(mesg), 0, (struct sockaddr*)&hisend, &hisend_len); - inet_ntop(AF_INET, &hisend.sin_addr, ip4str, IP4STRSIZE); - fprintf(stdout, "===========Recieved from %s============\n", ip4str); - fprintf(stdout, "message length : %d\nsend sequence : %d\nservices ID : %d\nenevt ID : %d\ndomain ID : %d\n", mesg.tMsgFrame.len, mesg.tMsgFrame.seqno, mesg.tMsgFrame.serv, mesg.tMsgFrame.event, mesg.tMsgFrame.domain); - if(mesg.tSysNetcardAlarm.flags == NETCARD_ALARM_SWITCH) - fprintf(stdout, "切换网卡名: %s\n", mesg.tSysNetcardAlarm.switch_devname); - else if(mesg.tSysNetcardAlarm.flags == NETCARD_ALARM_RESUME) - fprintf(stdout, "恢复网卡名: %s\n", mesg.tSysNetcardAlarm.fault_devname); - else - fprintf(stdout, "故障网卡名: %s\n", mesg.tSysNetcardAlarm.fault_devname); - fprintf(stdout, "状态标记: %d\n", mesg.tSysNetcardAlarm.flags); - fprintf(stdout, "重发次数: %d\n", mesg.tSysNetcardAlarm.retrytimes); - } - close(sock_sd); - - exit(0); -} - diff --git a/code/trunk/test/sys_netcard.h b/code/trunk/test/sys_netcard.h deleted file mode 100644 index 9d97950..0000000 --- a/code/trunk/test/sys_netcard.h +++ /dev/null @@ -1,87 +0,0 @@ -#ifndef _SYS_NETCARD_H -#define _SYS_NETCARD_H - -#include - -#ifndef NIC_NAME_LEN -#define NIC_NAME_LEN 64 -#endif - -#define SEM_PROJ_ID 's' -#define MAXNICNUM 32 - -#ifdef __cplusplus -extern "C"{ -#endif - -typedef long KEYID; - -typedef struct _msg_frame // message frame -{ - short len; // message length - short seqno; // send sequence - short serv; // services ID - short event; // event ID - unsigned char domain; // domain ID - unsigned char ctxt; // Context ID - short stid; // SOURCE task id - short dtid; // DESTINATION task ID - unsigned char ver_coding; // 版本号 + 编码 - unsigned char mes_type; // 帧类型 -}MSG_FRAME, *LPMSG_FRAME; - -typedef struct _net_info -{ - KEYID ID; - char charname[NIC_NAME_LEN]; - char descr[128]; - struct sockaddr addr; - struct sockaddr broadaddr; - struct sockaddr netmask; - struct sockaddr hwaddr; - int time_stamp; - short flags; - int mtu; - int tx_queue_len; - int average_flow; - unsigned long long rx_packets; - unsigned long long tx_packets; - unsigned long long rx_bytes; - unsigned long long tx_bytes; - unsigned long rx_errors; - unsigned long tx_errors; - unsigned long rx_dropped; - unsigned long tx_dropped; - unsigned long rx_multicast; - unsigned long collisions; - unsigned long rx_fifo_errors; - unsigned long tx_carrier_errors; - unsigned long tx_fifo_errors; -}NETCARD_INFO, *LPNETCARD_INFO; - -#define NETCARD_ALARM_FAULT 1 -#define NETCARD_ALARM_RESUME 2 -#define NETCARD_ALARM_SWITCH 3 -#define NETCARD_ALARM_ABNORMAL 4 -typedef struct _sys_netcard_alarm -{ - char fault_devname[NIC_NAME_LEN]; /*故障设备名称 */ - char switch_devname[NIC_NAME_LEN]; /*切换设备名称 */ - short flags; /* 状态标记 : 1:网卡故障。2:网卡恢复。3:网卡切换。4:流量异常。*/ - short retrytimes; //0,1,2 重发次数 -}SYS_NETCARD_ALARM, *LPSYS_NETCARD_ALARM; - - -typedef struct _d5000_nic_alarm -{ - MSG_FRAME tMsgFrame ; - SYS_NETCARD_ALARM tSysNetcardAlarm ; -}D5000_NIC_ALARM, *LPD5000_NIC_ALARM; - - -#ifdef __cplusplus -} -#endif - -#endif -