From 31e680b4cd843bb55825900aae7404fcbb383ca2 Mon Sep 17 00:00:00 2001 From: yzc Date: Thu, 2 Jan 2020 14:06:12 +0800 Subject: [PATCH] =?UTF-8?q?=E5=8E=BB=E6=8E=89=E6=97=A0=E7=94=A8=E4=BB=A3?= =?UTF-8?q?=E7=A0=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- http_check/src/http_check.c | 33 ++++++++++++++++++++------------- 1 file changed, 20 insertions(+), 13 deletions(-) diff --git a/http_check/src/http_check.c b/http_check/src/http_check.c index e80ab56..3cdf5bc 100644 --- a/http_check/src/http_check.c +++ b/http_check/src/http_check.c @@ -32,9 +32,11 @@ #define MAX_NAME_LEN 32 #define CPU_START_POS 14//stat文件的有效起始行数 #define HTTP_TIMEOUT 100 +#define RET_OK 0 +#define RET_ERROR -1 uint16_t g_http_port; -char g_http_address[MAX_STR_LEN]=""; const char *http_check_conf_file = "./plug/business/http_check/http_check.conf"; +char g_http_address[MAX_STR_LEN]=""; char http_check_log_path[MAX_STR_LEN] = ""; char http_check_error_log_path[MAX_STR_LEN] = ""; char module_name[MAX_STR_LEN] = ""; @@ -157,7 +159,7 @@ float get_cpu_stat(unsigned int pid) } //获取默认网关地址 -int readNlSock(int sockFd, char *bufPtr, int seqNum, int pId) +int readNlSock(int sockFd, char *bufPtr) { struct nlmsghdr *nlHdr = NULL; int readLen = 0, msgLen = 0; @@ -171,7 +173,7 @@ int readNlSock(int sockFd, char *bufPtr, int seqNum, int pId) continue; } #endif - printf("sock recv return for readLen < 0!"); + //printf("sock recv return for readLen < 0!"); return -1; } @@ -179,7 +181,7 @@ int readNlSock(int sockFd, char *bufPtr, int seqNum, int pId) //检查header是否有效 if((NLMSG_OK(nlHdr, readLen) == 0) || (nlHdr->nlmsg_type == NLMSG_ERROR)) { - printf("sock recv return for header error!\n"); + //printf("sock recv return for header error!\n"); return -1; } @@ -202,14 +204,15 @@ int readNlSock(int sockFd, char *bufPtr, int seqNum, int pId) } //分析返回的路由信息 -void parseRoutes(struct nlmsghdr *nlHdr,char *gateway,unsigned int len) +int parseRoutes(struct nlmsghdr *nlHdr,char *gateway,unsigned int len) { struct rtmsg *rtMsg = NULL; struct rtattr *rtAttr = NULL; int rtLen; char dst_address[MAX_STR_LEN] = ""; struct route_info rtInfo; - + int ret = RET_ERROR; + memset(&rtInfo, 0, sizeof(struct route_info)); rtMsg = (struct rtmsg *)NLMSG_DATA(nlHdr); // If the route is not for AF_INET or does not belong to main routing table @@ -218,7 +221,7 @@ void parseRoutes(struct nlmsghdr *nlHdr,char *gateway,unsigned int len) //RT_TABLE_MAIN存储到其它主机的路由表项; if((rtMsg->rtm_family != AF_INET) || (rtMsg->rtm_table != RT_TABLE_MAIN)) { - return; + return ret; } rtAttr = (struct rtattr *)RTM_RTA(rtMsg); //路由信息 @@ -245,11 +248,14 @@ void parseRoutes(struct nlmsghdr *nlHdr,char *gateway,unsigned int len) inet_ntop(AF_INET, &rtInfo.dstAddr, dst_address, MAX_STR_LEN); if (strstr(dst_address, "0.0.0.0")) { - inet_ntop(AF_INET, &rtInfo.gateWay, gateway, len); - //printf("%s\n",gateway); + if(rtInfo.gateWay) + inet_ntop(AF_INET, &rtInfo.gateWay, gateway, len); + else if(rtInfo.ifName) + strncpy(gateway, rtInfo.ifName, IF_NAMESIZE); + ret = RET_OK; } - return; + return ret; } int get_gateway(char *gateway, unsigned int length) @@ -280,7 +286,7 @@ int get_gateway(char *gateway, unsigned int length) goto OUT; } - if((len = readNlSock(sock, msgBuf, msgSeq, getpid())) < 0) + if((len = readNlSock(sock, msgBuf)) < 0) { MESA_handle_runtime_log(error_log_handler, RLOG_LV_FATAL, module_name, "read from socket failed."); printf("====line:%d,%s\n",__LINE__,"read from socket failed."); @@ -289,7 +295,8 @@ int get_gateway(char *gateway, unsigned int length) for(;NLMSG_OK(nlMsg,len);nlMsg = NLMSG_NEXT(nlMsg,len)) { - parseRoutes(nlMsg, gateway, length); + if(!parseRoutes(nlMsg, gateway, length)) + break; } OUT: @@ -468,7 +475,7 @@ int http_check_init() MESA_load_profile_string_nodef(http_check_conf_file, "HTTP", "HTTP_CHECK_ERR_LOG_PATH", http_check_error_log_path, MAX_STR_LEN); MESA_load_profile_string_nodef(http_check_conf_file, "HTTP", "MODULE_NAME", module_name, MAX_STR_LEN); MESA_load_profile_int_def(http_check_conf_file, "HTTP", "LOG_LEVEL", &log_level, 30); - printf("*v6*******read_ip:%s,read_port:%d\n",g_http_address, g_http_port); + printf("*v12*******read_ip:%s,read_port:%d\n",g_http_address, g_http_port); http_check_log_handler = MESA_create_runtime_log_handle(http_check_log_path, RLOG_LV_INFO); error_log_handler = MESA_create_runtime_log_handle(http_check_error_log_path, log_level);