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
|
#include <errno.h>
#include <assert.h>
#include <unistd.h>
#include <arpa/inet.h>
#include <sys/socket.h>
#include <MESA/MESA_prof_load.h>
#include <fieldstat/fieldstat_easy.h>
#include "log.h"
#include "utils.h"
#include "sf_status.h"
#include "uthash.h"
struct metric
{
struct sf_status_key key;
int sf_status;
int sf_latency;
UT_hash_handle hh;
};
struct config
{
int output_kafka_interval_ms;
char data_center[256];
char device_group[256];
char device_id[256];
};
struct sf_status
{
struct config cfg;
int sf_status_idx;
int sf_latency_idx;
struct kafka *kfk;
struct fieldstat_easy *fs;
struct metric *htable;
};
/******************************************************************************
* Public API
******************************************************************************/
struct sf_status *sf_status_create(const char *profile, struct kafka *kfk)
{
struct sf_status *handle = (struct sf_status *)calloc(1, sizeof(struct sf_status));
if (!handle)
{
return NULL;
}
MESA_load_profile_int_def(profile, "metrics", "output_kafka_interval_ms", &(handle->cfg.output_kafka_interval_ms), 1000);
MESA_load_profile_string_def(profile, "metrics", "data_center", handle->cfg.data_center, sizeof(handle->cfg.data_center), "");
MESA_load_profile_string_def(profile, "metrics", "device_group", handle->cfg.device_group, sizeof(handle->cfg.device_group), "");
MESA_load_profile_string_def(profile, "metrics", "device_id", handle->cfg.device_id, sizeof(handle->cfg.device_id), "");
const struct fieldstat_tag tags[] = {
{"data_center", TAG_CSTRING, {.value_str = handle->cfg.data_center}},
{"device_group", TAG_CSTRING, {.value_str = handle->cfg.device_group}},
{"device_id", TAG_CSTRING, {.value_str = handle->cfg.device_id}},
};
handle->kfk = kfk;
handle->fs = fieldstat_easy_new(1, "service_function_status", tags, sizeof(tags) / sizeof(tags[0]));
if (!handle->fs)
{
goto error_out;
}
handle->sf_status_idx = fieldstat_easy_register_counter(handle->fs, "sf_status");
handle->sf_latency_idx = fieldstat_easy_register_counter(handle->fs, "sf_latency_us");
return handle;
error_out:
sf_status_destory(handle);
return NULL;
}
void sf_status_destory(struct sf_status *handle)
{
if (handle)
{
struct metric *temp = NULL;
struct metric *node = NULL;
HASH_ITER(hh, handle->htable, node, temp)
{
HASH_DELETE(hh, handle->htable, node);
free(node);
node = NULL;
}
if (handle->fs)
{
fieldstat_easy_free(handle->fs);
handle->fs = NULL;
}
free(handle);
handle = NULL;
}
}
void sf_status_delete(struct sf_status *handle, const struct sf_status_key *key)
{
if (!handle)
{
return;
}
struct metric *temp = NULL;
HASH_FIND(hh, handle->htable, key, sizeof(struct sf_status_key), temp);
if (temp)
{
HASH_DELETE(hh, handle->htable, temp);
free(temp);
temp = NULL;
}
}
void sf_status_update(struct sf_status *handle, const struct sf_status_key *key, int sf_status, int sf_latency)
{
if (!handle)
{
return;
}
struct metric *temp = NULL;
HASH_FIND(hh, handle->htable, key, sizeof(struct sf_status_key), temp);
if (temp)
{
temp->sf_status = sf_status;
temp->sf_latency = sf_latency;
}
else
{
temp = (struct metric *)calloc(1, sizeof(struct metric));
temp->key.vsys_id = key->vsys_id;
temp->key.sf_profile_id = key->sf_profile_id;
temp->sf_status = sf_status;
temp->sf_latency = sf_latency;
HASH_ADD(hh, handle->htable, key, sizeof(struct sf_status_key), temp);
}
}
void sf_status_output(struct sf_status *handle)
{
if (!handle)
{
return;
}
struct metric *temp = NULL;
struct metric *node = NULL;
HASH_ITER(hh, handle->htable, node, temp)
{
const struct fieldstat_tag tags[] = {
{"vsys_id", TAG_INTEGER, {.value_longlong = node->key.vsys_id}},
{"sf_profile_id", TAG_INTEGER, {.value_longlong = node->key.sf_profile_id}},
};
fieldstat_easy_counter_set(handle->fs, 0, handle->sf_status_idx, tags, sizeof(tags) / sizeof(tags[0]), node->sf_status);
fieldstat_easy_counter_set(handle->fs, 0, handle->sf_latency_idx, tags, sizeof(tags) / sizeof(tags[0]), node->sf_latency);
}
char **ptr = NULL;
size_t len = 0;
fieldstat_easy_output_array_and_reset(handle->fs, &ptr, &len);
if (ptr)
{
for (size_t i = 0; i < len; i++)
{
kafka_send(handle->kfk, TOPIC_RULE_HITS, ptr[i], strlen(ptr[i]));
free(ptr[i]);
ptr[i] = NULL;
}
free(ptr);
}
}
int sf_status_get_ouput_interval_ms(struct sf_status *handle)
{
if (!handle)
{
return 0;
}
return handle->cfg.output_kafka_interval_ms;
}
|