This repository has been archived on 2025-09-14. You can view files and clone it, but cannot push or open issues or pull requests.
Files
yzc-http-count/src/http_count.c

383 lines
10 KiB
C
Raw Normal View History

2020-01-06 14:27:40 +08:00
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include "../../../opt/MESA/include/MESA/stream.h"
#include "../../../opt/MESA/include/MESA/MESA_prof_load.h"
#include "../../../opt/MESA/include/MESA/MESA_handle_logger.h"
#include "../../../opt/MESA/include/MESA/http.h"
2020-01-06 17:31:51 +08:00
#define MAX_STR_LEN 256
#define PERSIST_HASH_SIZE 512
#define DOUBLE_DIRECTION 0x03
#define HTTP_CHECK_CONFIG "./plug/business/http_count/http_count.conf"
#define BASIC_DIR "./plug/business/http_count/"
#define HTTP_COUNT_PLUGNAME "http_count.so"
#define JHASH_GOLDEN_RATIO 0x9e3779b9
2020-01-06 14:27:40 +08:00
#define __jhash_mix(a, b, c)\
{\
2020-01-06 17:48:06 +08:00
a -= b; a -= c; a ^= (c>>13);\
b -= c; b -= a; b ^= (a<<8);\
c -= a; c -= b; c ^= (b>>13);\
a -= b; a -= c; a ^= (c>>12);\
b -= c; b -= a; b ^= (a<<16);\
c -= a; c -= b; c ^= (b>>5);\
a -= b; a -= c; a ^= (c>>3);\
b -= c; b -= a; b ^= (a<<10);\
c -= a; c -= b; c ^= (b>>15);\
2020-01-06 14:27:40 +08:00
}
2020-01-06 16:14:59 +08:00
int g_topn = 0, g_log_level = 0, g_type = 0;
2020-01-06 14:27:40 +08:00
char g_statistics_type[MAX_STR_LEN]="";
char g_http_count_log_path[MAX_STR_LEN]="";
void *g_http_count_log_handler = NULL;
struct http_data_htable *g_http_data_htable = NULL;
enum statistics_type
{
HTTP_TYPE,
USER_AGENT_TYPE,
TYPE_ERROR,
};
struct http_count_node
{
struct http_count_node *next;
2020-01-06 17:31:51 +08:00
//char data[MAX_STR_LEN];
2020-01-06 17:48:06 +08:00
uint64_t count;//统计次数
char *data;//存储host/useragent字段
2020-01-06 14:27:40 +08:00
};
struct http_data_htable
{
struct http_count_node *hlist_head;
uint64_t sum;//所有hash链总节点数统计
};
int add_hlist_node(uint32_t key, char *buf, uint32_t buflen)
{
struct http_count_node *node = NULL;
node = malloc(sizeof(struct http_count_node));
if(!node)
{
printf("http_check.so: malloc node failed\n");
MESA_handle_runtime_log(g_http_count_log_handler, RLOG_LV_FATAL, HTTP_COUNT_PLUGNAME, "malloc node failed!");
return -1;
}
2020-01-06 17:31:51 +08:00
node->data = malloc(buflen+1);
if(!node->data)
{
printf("http_check.so: malloc node->data failed\n");
MESA_handle_runtime_log(g_http_count_log_handler, RLOG_LV_FATAL, HTTP_COUNT_PLUGNAME, "malloc node->data failed!");
return -1;
}
2020-01-06 14:27:40 +08:00
strncpy(node->data, buf, buflen);
2020-01-06 17:31:51 +08:00
node->data[buflen] = 0;
node->count = 1;//命中次数
2020-01-06 14:27:40 +08:00
node->next = g_http_data_htable->hlist_head[key].next;
g_http_data_htable->hlist_head[key].next = node;
g_http_data_htable->hlist_head[key].count++;//哈希链节点数
g_http_data_htable->sum++;//所有hash链总节点数
return 0;
}
struct http_count_node * find_hlist_node(uint32_t key, char *buf, uint32_t buflen)
{
struct http_count_node *node = NULL;
node = g_http_data_htable->hlist_head[key].next;
for(; node != NULL; node = node->next)
{
if(!strncmp(node->data, buf, buflen))
{
return node;
}
}
return NULL;
}
uint32_t jhash (void *key, uint32_t length, uint32_t initval)
{
uint32_t a, b, c, len;
uint8_t *k = key;
len = length;
a = b = JHASH_GOLDEN_RATIO;
c = initval;
while (len >= 12)
{
a += (k[0] + ((uint32_t) k[1] << 8) + ((uint32_t) k[2] << 16) + ((uint32_t) k[3] << 24));
b += (k[4] + ((uint32_t) k[5] << 8) + ((uint32_t) k[6] << 16) + ((uint32_t) k[7] << 24));
c += (k[8] + ((uint32_t) k[9] << 8) + ((uint32_t) k[10] << 16) + ((uint32_t) k[11] << 24));
__jhash_mix (a, b, c);
k += 12;
len -= 12;
}
c += length;
switch (len)
{
case 11:
c += ((uint32_t) k[10] << 24);
case 10:
c += ((uint32_t) k[9] << 16);
case 9:
c += ((uint32_t) k[8] << 8);
case 8:
b += ((uint32_t) k[7] << 24);
case 7:
b += ((uint32_t) k[6] << 16);
case 6:
b += ((uint32_t) k[5] << 8);
case 5:
b += k[4];
case 4:
a += ((uint32_t) k[3] << 24);
case 3:
a += ((uint32_t) k[2] << 16);
case 2:
a += ((uint32_t) k[1] << 8);
case 1:
a += k[0];
};
__jhash_mix (a, b, c);
return c;
}
uint32_t get_hash_key(void *buf, uint32_t buflen)
{
return (jhash(buf, buflen, 0) % PERSIST_HASH_SIZE);
}
2020-01-06 17:31:51 +08:00
int http_count_statistics(char *buf, uint32_t buflen)
2020-01-06 14:27:40 +08:00
{
uint32_t key = 0;
struct http_count_node *node = NULL;
key = get_hash_key(buf, buflen);
node = find_hlist_node(key, buf, buflen);
if(node)
{
node->count++;
}
else
{
2020-01-06 16:14:59 +08:00
add_hlist_node(key, buf, buflen);
2020-01-06 14:27:40 +08:00
}
2020-01-06 16:14:59 +08:00
return 0;
2020-01-06 14:27:40 +08:00
}
char http_count_entry(stSessionInfo* session_info, void **param, int thread_seq, struct streaminfo *a_tcp, void *a_packet)
2020-01-06 16:14:59 +08:00
{
2020-01-06 14:27:40 +08:00
if(a_tcp->dir != DOUBLE_DIRECTION)
return 0;
switch(session_info->prot_flag)
{
case HTTP_HOST:
if(g_type == HTTP_TYPE)
2020-01-06 16:14:59 +08:00
http_count_statistics(session_info->buf, session_info->buflen);
2020-01-06 14:27:40 +08:00
break;
case HTTP_USER_AGENT:
if(g_type == USER_AGENT_TYPE)
2020-01-06 16:14:59 +08:00
http_count_statistics(session_info->buf, session_info->buflen);
2020-01-06 14:27:40 +08:00
break;
default:
break;
}
2020-01-06 16:14:59 +08:00
return PROT_STATE_GIVEME;
2020-01-06 14:27:40 +08:00
}
int http_check_readconf(const char* filename)
{
2020-01-06 16:14:59 +08:00
MESA_load_profile_int_def(filename, "HTTP", "TOPN", (int *)(&g_topn), 5);
2020-01-06 14:27:40 +08:00
MESA_load_profile_string_nodef(filename, "HTTP", "TYPE", g_statistics_type, MAX_STR_LEN);
2020-01-06 16:14:59 +08:00
MESA_load_profile_int_def(filename, "HTTP", "LOG_LEVEL", (int *)(&g_log_level), 30);
2020-01-06 14:27:40 +08:00
MESA_load_profile_string_nodef(filename, "HTTP", "HTTP_COUNT_LOG_PATH", g_http_count_log_path, MAX_STR_LEN);
g_http_count_log_handler = MESA_create_runtime_log_handle(g_http_count_log_path, g_log_level);
if(!g_http_count_log_handler)
{
printf("http_check.so: get log handle error!\n");
MESA_handle_runtime_log(g_http_count_log_handler, RLOG_LV_FATAL, HTTP_COUNT_PLUGNAME, "get log handle error!");
return -1;
}
2020-01-06 16:14:59 +08:00
if(!strncmp(g_statistics_type, "Host", 4))
2020-01-06 14:27:40 +08:00
g_type = HTTP_TYPE;
2020-01-06 16:14:59 +08:00
else if(!strncmp(g_statistics_type, "User-Agent", 10))
2020-01-06 14:27:40 +08:00
g_type = USER_AGENT_TYPE;
else
{
g_type = TYPE_ERROR;
2020-01-06 16:14:59 +08:00
printf("http_check.so: get statistics type error!\n");
MESA_handle_runtime_log(g_http_count_log_handler, RLOG_LV_FATAL, HTTP_COUNT_PLUGNAME, "get statistics type error!");
2020-01-06 14:27:40 +08:00
return -1;
}
//printf("***v1***conf:topn:%d, key:%s(%d), log_level:%d, log_path:%s\n",g_topn, g_statistics_type, g_type, g_log_level, g_http_count_log_path);
MESA_handle_runtime_log(g_http_count_log_handler, RLOG_LV_DEBUG, HTTP_COUNT_PLUGNAME,
"read_conf:topn:%d, key:%s(%d), log_level:%d, log_path:%s\n", g_topn, g_statistics_type, g_type, g_log_level, g_http_count_log_path);
return 0;
}
int http_htable_init(void)
{
uint32_t i = 0;
g_http_data_htable = malloc(sizeof(struct http_data_htable));
if(!g_http_data_htable)
{
printf("http_check.so: malloc g_http_data_htable failed!\n");
MESA_handle_runtime_log(g_http_count_log_handler, RLOG_LV_FATAL, HTTP_COUNT_PLUGNAME, "malloc g_http_data_htable failed!");
return -1;
}
memset(g_http_data_htable, 0, sizeof(struct http_data_htable));
g_http_data_htable->hlist_head = malloc(sizeof(struct http_count_node) * PERSIST_HASH_SIZE);
if(!g_http_data_htable->hlist_head)
{
printf("http_check.so: malloc g_http_data_htable->hlist_head failed!\n");
MESA_handle_runtime_log(g_http_count_log_handler, RLOG_LV_FATAL, HTTP_COUNT_PLUGNAME, "malloc g_http_data_htable->hlist_head failed!");
return -1;
}
for(i=0; i < PERSIST_HASH_SIZE; i++)
memset(&g_http_data_htable->hlist_head[i], 0, sizeof(struct http_count_node));
return 0;
}
int http_count_init(void)
{
int ret = 0;
ret = http_check_readconf(HTTP_CHECK_CONFIG);
if(ret)
{
goto OUT;
}
ret = http_htable_init();
if(ret)
{
goto OUT;
}
OUT:
return ret;
}
void free_all(void)
{
uint32_t i = 0;
struct http_count_node *free_node = NULL;
struct http_count_node *head = NULL;
for(i = 0; i < PERSIST_HASH_SIZE; i++)
{
head = &g_http_data_htable->hlist_head[i];
free_node = head->next;
for(; free_node != NULL; free_node = head->next)
{
head->next = free_node->next;
free_node->next = NULL;
2020-01-06 17:31:51 +08:00
free(free_node->data);
free_node->data = NULL;
2020-01-06 14:27:40 +08:00
free(free_node);
free_node = NULL;
}
}
free(g_http_data_htable->hlist_head);
g_http_data_htable->hlist_head = NULL;
free(g_http_data_htable);
g_http_data_htable = NULL;
return;
}
void http_count_destory(void)
{
uint32_t i = 0;
uint32_t j = 0;
uint64_t max_count;
uint8_t flag = 0;
struct http_count_node *node = NULL;
struct http_count_node *max_node = NULL;
char buff[MAX_STR_LEN] = "";
FILE *fp = NULL;
sprintf(buff, "%s%s.txt", BASIC_DIR, g_statistics_type);
fp = fopen(buff, "w+");
if(!fp)
{
printf("http_check.so: open file error!\n");
MESA_handle_runtime_log(g_http_count_log_handler, RLOG_LV_FATAL, HTTP_COUNT_PLUGNAME, "open file %s error!", buff);
goto OUT;
}
if(g_type == HTTP_TYPE)
2020-01-06 16:14:59 +08:00
fprintf(fp, "%-100s %s\n","Host", "Count");
2020-01-06 14:27:40 +08:00
else if(g_type == USER_AGENT_TYPE)
2020-01-06 16:14:59 +08:00
fprintf(fp, "%-180s %s\n","User-Agent", "Count");
2020-01-06 17:48:06 +08:00
#if 0
2020-01-06 14:27:40 +08:00
for(i = 0; i < PERSIST_HASH_SIZE; i++)//print_test
{
node = g_http_data_htable->hlist_head[i].next;
for(; node != NULL; node = node->next)
{
2020-01-06 16:14:59 +08:00
printf("hash_print:key:%d, data:%s, count:%"PRIu64", hash_node:%"PRIu64", all_node:%"PRIu64"\n",
2020-01-06 14:27:40 +08:00
i, node->data, node->count, g_http_data_htable->hlist_head[i].count, g_http_data_htable->sum);
}
}
#endif
2020-01-06 17:48:06 +08:00
//printf("***************************top%d***************************\n", g_topn);
2020-01-06 14:27:40 +08:00
for(j = 0; j < g_topn; j++)
{
flag = 0;
for(i = 0; i < PERSIST_HASH_SIZE; i++)
{
node = g_http_data_htable->hlist_head[i].next;
for(; node != NULL; node = node->next)
{
if (!flag)
{
max_count = node->count;
max_node = node;
flag = 1;
}
if(max_count < node->count)
{
max_count = node->count;
max_node = node;
}
}
}
if(max_node && max_node->count)
{
2020-01-06 17:48:06 +08:00
//printf("top%d: %s, count:%"PRIu64"\n",g_topn, max_node->data, max_node->count);
2020-01-06 14:27:40 +08:00
MESA_handle_runtime_log(g_http_count_log_handler, RLOG_LV_DEBUG, HTTP_COUNT_PLUGNAME,
"top%d: %s, count:%d\n", g_topn, max_node->data, max_node->count);
if(g_type == HTTP_TYPE)
2020-01-06 16:14:59 +08:00
fprintf(fp, "%-100s %"PRIu64"\n", max_node->data, max_node->count);
2020-01-06 14:27:40 +08:00
else if(g_type == USER_AGENT_TYPE)
2020-01-06 16:14:59 +08:00
fprintf(fp, "%-180s %"PRIu64"\n", max_node->data, max_node->count);
2020-01-06 14:27:40 +08:00
max_node->count = 0;
}
}
OUT:
if(fp)
fclose(fp);
if(g_http_count_log_handler)
MESA_destroy_runtime_log_handle(g_http_count_log_handler);
free_all();
return ;
}