summaryrefslogtreecommitdiff
path: root/entry/src/kni_fieldstat.cpp
blob: 51eeeccdf0329c300c2075025d018ddb824e2ec1 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
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_active == 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;
}