TSG-21854 TFE使用fieldstat4序列化Manipulation Policy的metric并输出到kafka
This commit is contained in:
@@ -5,7 +5,7 @@
|
||||
#include "tfe_resource.h"
|
||||
#include "tfe_packet_io.h"
|
||||
|
||||
void tfe_set_intercept_metric(struct tfe_fieldstat_metric_t *fieldstat, struct session_ctx *s_ctx, int thread_id, int is_session_close)
|
||||
void tfe_set_intercept_metric(struct tfe_fieldstat_easy_t *fieldstat, struct session_ctx *s_ctx, int thread_id, int is_session_close)
|
||||
{
|
||||
int ret;
|
||||
int hit_count = 0;
|
||||
@@ -106,110 +106,107 @@ void tfe_set_intercept_metric(struct tfe_fieldstat_metric_t *fieldstat, struct s
|
||||
struct fieldstat_tag temp_tags[TAG_MAX] = {0};
|
||||
|
||||
temp_tags[nr_tags].key = "vsys_id";
|
||||
temp_tags[nr_tags].value_type = 0;
|
||||
temp_tags[nr_tags].value_int = vsys_id;
|
||||
temp_tags[nr_tags].type = TAG_INTEGER;
|
||||
temp_tags[nr_tags].value_longlong = vsys_id;
|
||||
nr_tags++;
|
||||
|
||||
temp_tags[nr_tags].key = "rule_id";
|
||||
temp_tags[nr_tags].value_type = 0;
|
||||
temp_tags[nr_tags].value_int = rule_id;
|
||||
temp_tags[nr_tags].type = TAG_INTEGER;
|
||||
temp_tags[nr_tags].value_longlong = rule_id;
|
||||
nr_tags++;
|
||||
|
||||
uint8_t pinning_status = 0;
|
||||
if (tfe_cmsg_get_value(cmsg, TFE_CMSG_SSL_PINNING_STATE, (unsigned char *)&pinning_status, sizeof(pinning_status), &out_size) == 0)
|
||||
{
|
||||
temp_tags[nr_tags].key = "pinning_status";
|
||||
temp_tags[nr_tags].value_type = 0;
|
||||
temp_tags[nr_tags].value_int = pinning_status;
|
||||
temp_tags[nr_tags].type = TAG_INTEGER;
|
||||
temp_tags[nr_tags].value_longlong = pinning_status;
|
||||
nr_tags++;
|
||||
}
|
||||
|
||||
// action : 2 Intercept; 3 No Intercept
|
||||
temp_tags[nr_tags].key = "action";
|
||||
temp_tags[nr_tags].value_type = 0;
|
||||
temp_tags[nr_tags].value_int = (hit_no_intercept == 1 ? 3 : 2);
|
||||
temp_tags[nr_tags].type = TAG_INTEGER;
|
||||
temp_tags[nr_tags].value_longlong = (hit_no_intercept == 1 ? 3 : 2);
|
||||
nr_tags++;
|
||||
|
||||
// sub_action not need for intercept metrics
|
||||
|
||||
if (hit_count > 0)
|
||||
{
|
||||
fieldstat_dynamic_table_metric_value_incrby(fieldstat->instance, fieldstat->table_id, fieldstat->column_array[COLUMN_HIT_COUNT], "proxy_rule_hits", hit_count, temp_tags, (size_t)nr_tags, thread_id);
|
||||
fieldstat_easy_counter_incrby(fieldstat->fseasy, thread_id, fieldstat->counter_array[COLUMN_HIT_COUNT], temp_tags, (size_t)nr_tags, hit_count);
|
||||
}
|
||||
|
||||
if (in_pkts > 0)
|
||||
{
|
||||
fieldstat_dynamic_table_metric_value_incrby(fieldstat->instance, fieldstat->table_id, fieldstat->column_array[COLUMN_IN_PKTS], "proxy_rule_hits", in_pkts, temp_tags, (size_t)nr_tags, thread_id);
|
||||
fieldstat_easy_counter_incrby(fieldstat->fseasy, thread_id, fieldstat->counter_array[COLUMN_IN_PKTS], temp_tags, (size_t)nr_tags, in_pkts);
|
||||
}
|
||||
|
||||
if (in_bytes > 0)
|
||||
{
|
||||
fieldstat_dynamic_table_metric_value_incrby(fieldstat->instance, fieldstat->table_id, fieldstat->column_array[COLUMN_IN_BYTES], "proxy_rule_hits", in_bytes, temp_tags, (size_t)nr_tags, thread_id);
|
||||
fieldstat_easy_counter_incrby(fieldstat->fseasy, thread_id, fieldstat->counter_array[COLUMN_IN_BYTES], temp_tags, (size_t)nr_tags, in_bytes);
|
||||
}
|
||||
|
||||
if (out_pkts > 0)
|
||||
{
|
||||
fieldstat_dynamic_table_metric_value_incrby(fieldstat->instance, fieldstat->table_id, fieldstat->column_array[COLUMN_OUT_PKTS], "proxy_rule_hits", out_pkts, temp_tags, (size_t)nr_tags, thread_id);
|
||||
fieldstat_easy_counter_incrby(fieldstat->fseasy, thread_id, fieldstat->counter_array[COLUMN_OUT_PKTS], temp_tags, (size_t)nr_tags, out_pkts);
|
||||
}
|
||||
|
||||
if (out_bytes > 0)
|
||||
{
|
||||
fieldstat_dynamic_table_metric_value_incrby(fieldstat->instance, fieldstat->table_id, fieldstat->column_array[COLUMN_OUT_BYTES], "proxy_rule_hits", out_bytes, temp_tags, (size_t)nr_tags, thread_id);
|
||||
fieldstat_easy_counter_incrby(fieldstat->fseasy, thread_id, fieldstat->counter_array[COLUMN_OUT_BYTES], temp_tags, (size_t)nr_tags, out_bytes);
|
||||
}
|
||||
}
|
||||
|
||||
int tfe_fieldstat_metric_incrby(struct tfe_fieldstat_metric_t *fieldstat, unsigned int column_id, long long value, const struct fieldstat_tag tags[], int n_tags, int thread_id)
|
||||
int tfe_fieldstat_easy_incrby(struct tfe_fieldstat_easy_t *fieldstat, unsigned int counter_id, long long value, const struct fieldstat_tag tags[], int n_tags, int thread_id)
|
||||
{
|
||||
return fieldstat_dynamic_table_metric_value_incrby(fieldstat->instance, fieldstat->table_id, column_id, "proxy_rule_hits", value, tags, (size_t)n_tags, thread_id);
|
||||
return fieldstat_easy_counter_incrby(fieldstat->fseasy, thread_id, counter_id, tags, (size_t)n_tags, value);
|
||||
}
|
||||
|
||||
struct tfe_fieldstat_metric_t *tfe_fieldstat_metric_create(char *telegraf_ip, int telegraf_port, char *app_name, int cycle, int max_thread, void *local_logger)
|
||||
struct tfe_fieldstat_easy_t *tfe_fieldstat_easy_create(char *app_name, char *outpath, int cycle, int max_thread, void *local_logger)
|
||||
{
|
||||
int i=0;
|
||||
const char *counter_field[COLUMN_MAX] = {"hit_count", "in_bytes", "out_bytes", "in_pkts", "out_pkts"};
|
||||
struct fieldstat_tag metric_tags[TAG_MAX - 1] = {{"vsys_id", TAG_INTEGER, -1}, {"rule_id", TAG_INTEGER, -1}, {"action", TAG_INTEGER, -1}, {"sub_action", TAG_CSTRING, -1}};
|
||||
|
||||
struct fieldstat_tag metric_tags[TAG_MAX - 1] = {{"vsys_id", 0, -1}, {"rule_id", 0, -1}, {"action", 0, -1}, {"sub_action", 2, -1}};
|
||||
const char *column_field[COLUMN_MAX] = {"hit_count", "in_bytes", "out_bytes", "in_pkts", "out_pkts"};
|
||||
enum field_type column_type[COLUMN_MAX] = {FIELD_TYPE_COUNTER, FIELD_TYPE_COUNTER, FIELD_TYPE_COUNTER, FIELD_TYPE_COUNTER, FIELD_TYPE_COUNTER};
|
||||
|
||||
struct tfe_fieldstat_metric_t *fieldstat = ALLOC(struct tfe_fieldstat_metric_t, 1);
|
||||
|
||||
fieldstat->instance = fieldstat_dynamic_instance_new(app_name, max_thread);
|
||||
if(!fieldstat->instance)
|
||||
struct tfe_fieldstat_easy_t *fieldstat = ALLOC(struct tfe_fieldstat_easy_t, 1);
|
||||
|
||||
fieldstat->fseasy = fieldstat_easy_new(max_thread, app_name, NULL, 0);
|
||||
if(!fieldstat->fseasy)
|
||||
{
|
||||
TFE_LOG_ERROR(local_logger, "fieldstat3 dynamic instance init failed.");
|
||||
TFE_LOG_ERROR(local_logger, "fieldstat4 easy instance init failed.");
|
||||
FREE(&fieldstat);
|
||||
return NULL;
|
||||
}
|
||||
fieldstat_easy_enable_auto_output(fieldstat->fseasy, outpath, cycle);
|
||||
|
||||
fieldstat->max_thread=max_thread;
|
||||
fieldstat_dynamic_set_line_protocol_server(fieldstat->instance, telegraf_ip, telegraf_port);
|
||||
fieldstat_dynamic_set_output_interval(fieldstat->instance, cycle);
|
||||
for(int i=0; i<COLUMN_MAX; i++)
|
||||
{
|
||||
fieldstat->counter_array[i]=fieldstat_easy_register_counter(fieldstat->fseasy, counter_field[i]);
|
||||
if(fieldstat->counter_array[i] < 0)
|
||||
{
|
||||
TFE_LOG_ERROR(local_logger, "fieldstat4 easy register counter failed.");
|
||||
FREE(&fieldstat);
|
||||
return NULL;
|
||||
}
|
||||
}
|
||||
|
||||
fieldstat->table_id = fieldstat_register_dynamic_table(fieldstat->instance, "proxy_rule_hits", column_field, column_type, (size_t)COLUMN_MAX, fieldstat->column_array);
|
||||
if(fieldstat->table_id < 0)
|
||||
{
|
||||
TFE_LOG_ERROR(local_logger, "fieldstat3 register dynamic table failed.");
|
||||
FREE(&fieldstat);
|
||||
return NULL;
|
||||
}
|
||||
|
||||
fieldstat->tags = ALLOC(struct fieldstat_tag*, max_thread);
|
||||
for (i = 0; i < max_thread; i++)
|
||||
fieldstat->tags = ALLOC(struct fieldstat_tag*, max_thread);
|
||||
for (int i = 0; i < max_thread; i++)
|
||||
{
|
||||
fieldstat->tags[i] = ALLOC(struct fieldstat_tag, TAG_MAX-1);
|
||||
memcpy(fieldstat->tags[i], metric_tags, sizeof(struct fieldstat_tag) * (size_t)(TAG_MAX-1));
|
||||
}
|
||||
|
||||
fieldstat_dynamic_instance_start(fieldstat->instance);
|
||||
return fieldstat;
|
||||
return fieldstat;
|
||||
}
|
||||
|
||||
void tfe_fieldstat_metric_destroy(struct tfe_fieldstat_metric_t *fieldstat)
|
||||
void tfe_fieldstat_easy_destroy(struct tfe_fieldstat_easy_t *fieldstat)
|
||||
{
|
||||
if(fieldstat)
|
||||
if(fieldstat)
|
||||
{
|
||||
if(fieldstat->instance)
|
||||
if(fieldstat->fseasy)
|
||||
{
|
||||
fieldstat_dynamic_instance_free(fieldstat->instance);
|
||||
fieldstat_easy_free(fieldstat->fseasy);
|
||||
}
|
||||
|
||||
for (int i = 0; i < fieldstat->max_thread; i++)
|
||||
@@ -223,4 +220,3 @@ void tfe_fieldstat_metric_destroy(struct tfe_fieldstat_metric_t *fieldstat)
|
||||
FREE(&fieldstat);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -11,10 +11,9 @@
|
||||
#define MAAT_INPUT_FILE 2
|
||||
|
||||
static int scan_table_id[__SCAN_COMMON_TABLE_MAX];
|
||||
static struct tfe_fieldstat_metric_t *dynamic_fieldstat = NULL;
|
||||
static struct tfe_fieldstat_easy_t *fieldstat_easy = NULL;
|
||||
static char *device_tag=NULL;
|
||||
|
||||
struct tfe_fieldstat_metric_t *fieldstat_handle = NULL;
|
||||
struct kafka *kafka_handle = NULL;
|
||||
struct maat *maat_handle = NULL;
|
||||
|
||||
@@ -64,36 +63,33 @@ struct maat *tfe_get_maat_handle()
|
||||
return maat_handle;
|
||||
}
|
||||
|
||||
struct tfe_fieldstat_metric_t *tfe_get_fieldstat_handle()
|
||||
struct tfe_fieldstat_easy_t *tfe_get_fieldstat_handle()
|
||||
{
|
||||
return fieldstat_handle;
|
||||
return fieldstat_easy;
|
||||
}
|
||||
|
||||
static struct tfe_fieldstat_metric_t *create_fieldstat_instance(const char *profile, const char *section, int max_thread, void *logger)
|
||||
static struct tfe_fieldstat_easy_t *create_fieldstat4_instance(const char *profile, const char *section, int max_thread, void *logger)
|
||||
{
|
||||
int cycle=0;
|
||||
unsigned short telegraf_port=0;
|
||||
char telegraf_ip[TFE_STRING_MAX]={0};
|
||||
char app_name[TFE_STRING_MAX]={0};
|
||||
struct tfe_fieldstat_metric_t *dynamic_fieldstat=NULL;
|
||||
char outpath[TFE_STRING_MAX]={0};
|
||||
struct tfe_fieldstat_easy_t *fieldstat_easy=NULL;
|
||||
|
||||
MESA_load_profile_short_nodef(profile, section, "telegraf_port", (short *)&(telegraf_port));
|
||||
MESA_load_profile_string_nodef(profile, section, "telegraf_ip", telegraf_ip, sizeof(telegraf_ip));
|
||||
MESA_load_profile_string_def(profile, section, "app_name", app_name, sizeof(app_name), "metric");
|
||||
MESA_load_profile_int_def(profile, section, "cycle", &cycle, 1000);
|
||||
MESA_load_profile_string_def(profile, section, "app_name", app_name, sizeof(app_name), "proxy_rule_hits");
|
||||
MESA_load_profile_int_def(profile, section, "cycle", &cycle, 5);
|
||||
MESA_load_profile_string_def(profile, section, "outpath", outpath, sizeof(outpath), "metrics/porxy_fieldstat.json");
|
||||
|
||||
dynamic_fieldstat = tfe_fieldstat_metric_create(telegraf_ip, telegraf_port, app_name, cycle, max_thread, logger);
|
||||
if (dynamic_fieldstat == NULL)
|
||||
fieldstat_easy = tfe_fieldstat_easy_create(app_name, outpath, cycle, max_thread, logger);
|
||||
if (fieldstat_easy == NULL)
|
||||
{
|
||||
TFE_LOG_ERROR(logger, "tfe fieldstat init failed, error to create fieldstat metric.");
|
||||
return NULL;
|
||||
}
|
||||
TFE_LOG_INFO(logger, "tfe fieldstat telegraf_ip : %s", telegraf_ip);
|
||||
TFE_LOG_INFO(logger, "tfe fieldstat telegraf_port : %d", telegraf_port);
|
||||
TFE_LOG_INFO(logger, "tfe fieldstat app_name : %s", app_name);
|
||||
TFE_LOG_INFO(logger, "tfe fieldstat cycle : %d", cycle);
|
||||
TFE_LOG_INFO(logger, "tfe fieldstat outpath : %s", outpath);
|
||||
|
||||
return dynamic_fieldstat;
|
||||
return fieldstat_easy;
|
||||
}
|
||||
|
||||
static struct maat *create_maat_feather(const char *instance_name, const char *profile, const char *section, int max_thread, void *logger)
|
||||
@@ -227,7 +223,6 @@ error_out:
|
||||
|
||||
static char* create_device_tag(const char *profile, const char *section, void *logger)
|
||||
{
|
||||
char *c=NULL;
|
||||
char accept_path[TFE_PATH_MAX] = {0}, accept_tags[TFE_STRING_MAX] = {0};
|
||||
|
||||
MESA_load_profile_string_def(profile, section, "accept_path", accept_path, sizeof(accept_path), "");
|
||||
@@ -382,8 +377,8 @@ int tfe_env_init()
|
||||
return -1;
|
||||
}
|
||||
|
||||
dynamic_fieldstat = create_fieldstat_instance(profile_path, "proxy_hits", thread_num, g_default_logger);
|
||||
if (!dynamic_fieldstat)
|
||||
fieldstat_easy = create_fieldstat4_instance(profile_path, "proxy_hits", thread_num, g_default_logger);
|
||||
if(!fieldstat_easy)
|
||||
{
|
||||
return -1;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user