diff options
Diffstat (limited to 'entry/src/kni_fieldstat.cpp')
| -rw-r--r-- | entry/src/kni_fieldstat.cpp | 235 |
1 files changed, 235 insertions, 0 deletions
diff --git a/entry/src/kni_fieldstat.cpp b/entry/src/kni_fieldstat.cpp new file mode 100644 index 0000000..a72764f --- /dev/null +++ b/entry/src/kni_fieldstat.cpp @@ -0,0 +1,235 @@ +#include <stdlib.h> +#include "kni_fieldstat.h" +#include "kni_utils.h" + +static int read_fieldstat_tags(struct proxy_metric_tag *metric_tags, + struct fieldstat_tag tags[]) +{ + int n_tags = 0; + + tags[n_tags].key = "vsys_id"; + tags[n_tags].value_type = 0; + tags[n_tags].value_int = metric_tags->vsys_id; + n_tags++; + + tags[n_tags].key = "rule_id"; + tags[n_tags].value_type = 0; + tags[n_tags].value_int = metric_tags->rule_id; + n_tags++; + + if(metric_tags->pinning_status == 1) + { + tags[n_tags].key = "pinning_status"; + tags[n_tags].value_type = 0; + tags[n_tags].value_int = metric_tags->pinning_status; + n_tags++; + } + + tags[n_tags].key = "action"; + tags[n_tags].value_type = 0; + // tags[n_tags].value_int = (hit_no_intercept == 1 ? 3 : 2); + tags[n_tags].value_int = metric_tags->action; + n_tags++; + + return n_tags; +} + + +void proxy_set_metric_value(struct proxy_fieldstat *pxy_fs, + struct proxy_metric_tag *metric_tags, + struct proxy_metric_value *metric_value, + int thread_id) +{ + int n_tags = 0; + const char *metric_row_name = "proxy_rule_hits"; + struct fieldstat_tag fieldstat_tags[5]; + + n_tags = read_fieldstat_tags(metric_tags, fieldstat_tags); + if(metric_value->hit_count > 0) + { + fieldstat_dynamic_table_metric_value_incrby( + pxy_fs->instance, + pxy_fs->table_id, + pxy_fs->column_ids[PROXY_METRIC_COLUMN_HIT_COUNT], + metric_row_name, + metric_value->hit_count, + fieldstat_tags, + (size_t)n_tags, + thread_id); + } + + if(metric_value->in_bytes > 0) + { + fieldstat_dynamic_table_metric_value_incrby( + pxy_fs->instance, + pxy_fs->table_id, + pxy_fs->column_ids[PROXY_METRIC_COLUMN_IN_BYTES], + metric_row_name, + metric_value->in_bytes, + fieldstat_tags, + (size_t)n_tags, + thread_id); + } + + if(metric_value->in_pkts > 0) + { + fieldstat_dynamic_table_metric_value_incrby( + pxy_fs->instance, + pxy_fs->table_id, + pxy_fs->column_ids[PROXY_METRIC_COLUMN_IN_PKTS], + metric_row_name, + metric_value->in_pkts, + fieldstat_tags, + (size_t)n_tags, + thread_id); + } + + if(metric_value->out_bytes > 0) + { + fieldstat_dynamic_table_metric_value_incrby( + pxy_fs->instance, + pxy_fs->table_id, + pxy_fs->column_ids[PROXY_METRIC_COLUMN_OUT_BYTES], + metric_row_name, + metric_value->out_bytes, + fieldstat_tags, + (size_t)n_tags, + thread_id); + } + + if(metric_value->out_pkts > 0) + { + fieldstat_dynamic_table_metric_value_incrby( + pxy_fs->instance, + pxy_fs->table_id, + pxy_fs->column_ids[PROXY_METRIC_COLUMN_OUT_PKTS], + metric_row_name, + metric_value->out_pkts, + fieldstat_tags, + (size_t)n_tags, + thread_id); + } + +} + +struct proxy_fieldstat *proxy_fieldstat_new(char *app_name, int n_thread, + const char *telegraf_ip, + unsigned short telegraf_port, + int interval_ms, + void *local_logger) +{ + struct proxy_fieldstat *pxy_fs = NULL; + + const char *column_field[PROXY_METRIC_COLUMN_MAX] = { + "hit_count", "in_bytes","out_bytes", "in_pkts","out_pkts"}; + enum field_type column_type[PROXY_METRIC_COLUMN_MAX] = { + FIELD_TYPE_COUNTER, + FIELD_TYPE_COUNTER, + FIELD_TYPE_COUNTER, + FIELD_TYPE_COUNTER, + FIELD_TYPE_COUNTER + }; + + pxy_fs = (struct proxy_fieldstat *)calloc(1,sizeof(struct proxy_fieldstat)); + pxy_fs->instance = fieldstat_dynamic_instance_new(app_name, n_thread); + + if(pxy_fs->instance == NULL) + { + goto error; + } + + pxy_fs->n_thread = n_thread; + fieldstat_dynamic_set_line_protocol_server(pxy_fs->instance, telegraf_ip, + telegraf_port); + fieldstat_dynamic_set_output_interval(pxy_fs->instance, interval_ms); + + pxy_fs->table_id = fieldstat_register_dynamic_table( + pxy_fs->instance, + "proxy_rule_hits", + column_field, + column_type, + (size_t)PROXY_METRIC_COLUMN_MAX, + pxy_fs->column_ids); + + if(pxy_fs->table_id < 0) + { + goto error; + } + + fieldstat_dynamic_instance_start(pxy_fs->instance); + return pxy_fs; + +error: + if(pxy_fs) + { + free(pxy_fs); + pxy_fs = NULL; + } + return NULL; +} + +void proxy_fieldstat_free(struct proxy_fieldstat *pxy_fs) +{ + if(pxy_fs) + { + if(pxy_fs->instance) + { + fieldstat_dynamic_instance_free(pxy_fs->instance); + pxy_fs->instance = NULL; + } + free(pxy_fs); + pxy_fs = NULL; + } +} + + +struct proxy_fieldstat *proxy_fieldstat_init(const char *profile, + const char *section, + int n_thread, + void *logger) +{ + int interval_ms = 0; + unsigned short telegraf_port = 0; + char telegraf_ip[KNI_ADDR_MAX] = {0}; + char app_name[KNI_STRING_MAX] = {0}; + struct proxy_fieldstat *pxy_fs = NULL; + + + MESA_load_profile_string_def(profile, section, "app_name", app_name, + sizeof(app_name), "proxy_rule_hits"); + + MESA_load_profile_string_nodef(profile, section, "telegraf_ip", + telegraf_ip, sizeof(telegraf_ip)); + + MESA_load_profile_short_nodef(profile, section, "telegraf_port", + (short *)&(telegraf_port)); + + MESA_load_profile_int_def(profile, section, "interval_ms", &interval_ms, + 1000); + + pxy_fs = proxy_fieldstat_new(app_name, n_thread, telegraf_ip, + telegraf_port, interval_ms, logger); + if (pxy_fs == NULL) + { + KNI_LOG_ERROR(logger, "proxy fieldstat init failed, error to create fieldstat metric."); + return NULL; + } + KNI_LOG_ERROR(logger, "proxy fieldstat telegraf_ip : %s", telegraf_ip); + KNI_LOG_ERROR(logger, "proxy fieldstat telegraf_port : %d", telegraf_port); + KNI_LOG_ERROR(logger, "proxy fieldstat app_name : %s", app_name); + KNI_LOG_ERROR(logger, "proxy fieldstat interval_ms : %d", interval_ms); + + return pxy_fs; +} + +void proxy_fieldstat_destory(struct proxy_fieldstat *pxy_fs, void *logger) +{ + if(pxy_fs) + { + proxy_fieldstat_free(pxy_fs); + pxy_fs = NULL; + } + KNI_LOG_ERROR(logger, "Destory proxy fieldstat!"); + return; +} + |
