summaryrefslogtreecommitdiff
path: root/src/entry/Maat_stat.cpp
blob: 78e0307982fc8ee4e8090849f435d92f2870d005 (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
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
#include "Maat_rule_internal.h"
#include "Maat_table_schema.h"
#include "Maat_garbage_collection.h"
#include "alignment_int64.h"
#include <time.h>
#include <MESA/field_stat2.h>
enum MAAT_FS_STATUS{
	STATUS_VERSION=0,
	STATUS_THRED_NUM,
	STATUS_TABLE_NUM,
	STATUS_PLUGIN_CACHE_NUM,
	STATUS_PLUGIN_ACC_NUM,
	STATUS_GROUP_REF_NUM,
	STATUS_GROUP_REF_NOT_NUM,
	STATUS_COMPILE_RULE_NUM,
	STATUS_POSTPONE_QSIZE,
	STATUS_OUTER_MID_NUM,
	STATUS_INNER_MID_NUM,
	STATUS_GARBAGE_QSIZE,
	STATUS_TOTAL_SCAN_LEN,
	STATUS_TOTAL_SCAN_CNT,
	STATUS_UPDATE_ERR_CNT,
	STATUS_ICONV_ERR_CNT,
	STATUS_SCAN_ERR_CNT,
	STATUS_ZOMBIE_RS_STREAM,
	STATUS_NOT_GROUP_HIT,
	STATUS_CMD_NUM,
	STATUS_CMD_Q_SIZE,
	STATUS_CMD_LINE_NUM
};

enum MAAT_FS_COLUMN
{
	COLUMN_TABLE_RULE_NUM=0,
	COLUMN_TABLE_REGEX_NUM,
	COLUMN_TABLE_STREAM_NUM,
	COLUMN_TABLE_SCAN_CNT,
	COLUMN_TABLE_SCAN_BYTES,
	COLUMN_TABLE_CPU_TIME,//microseconds
	COLUMN_TABLE_HIT_CNT,
};
#define	MAX_CONJ_NAME_LEN	22
void maat_stat_init(struct _Maat_feather_t* feather)
{
	int value=0;
	int i=0,j=0,offset=0;
	struct Maat_table_schema* p_table=NULL;
	char conj_table_name[(MAX_TABLE_NAME_LEN+1)*MAX_CONJUNCTION_TABLE_NUM]={0};

	feather->stat_handle=FS_create_handle();
	FS_set_para(feather->stat_handle, OUTPUT_DEVICE, feather->stat_file, strlen(feather->stat_file)+1);
	value=1;
	FS_set_para(feather->stat_handle, PRINT_MODE, &value, sizeof(value));
	value=0;
	FS_set_para(feather->stat_handle, CREATE_THREAD, &value, sizeof(value));
	
	FS_set_para(feather->stat_handle, APP_NAME, feather->instance_name, strlen(feather->instance_name)+1);
	FS_set_para(feather->stat_handle, OUTPUT_PROMETHEUS, &feather->output_prometheus, sizeof(feather->output_prometheus));
	
	feather->fs_status_id[STATUS_VERSION]=FS_register(feather->stat_handle, FS_STYLE_STATUS, FS_CALC_CURRENT, "version");
	feather->fs_status_id[STATUS_THRED_NUM]=FS_register(feather->stat_handle, FS_STYLE_STATUS, FS_CALC_CURRENT, "threads");
	feather->fs_status_id[STATUS_TABLE_NUM]=FS_register(feather->stat_handle, FS_STYLE_STATUS, FS_CALC_CURRENT, "tables");

	feather->fs_status_id[STATUS_PLUGIN_CACHE_NUM]=FS_register(feather->stat_handle, FS_STYLE_STATUS, FS_CALC_CURRENT, "plug_cached");
	feather->fs_status_id[STATUS_PLUGIN_ACC_NUM]=FS_register(feather->stat_handle, FS_STYLE_STATUS, FS_CALC_CURRENT, "plug_acc");

	feather->fs_status_id[STATUS_GROUP_REF_NUM]=FS_register(feather->stat_handle, FS_STYLE_STATUS, FS_CALC_CURRENT, "group");
	feather->fs_status_id[STATUS_GROUP_REF_NOT_NUM]=FS_register(feather->stat_handle, FS_STYLE_STATUS, FS_CALC_CURRENT, "not_grp");
	feather->fs_status_id[STATUS_COMPILE_RULE_NUM]=FS_register(feather->stat_handle, FS_STYLE_STATUS, FS_CALC_CURRENT, "compile");

	feather->fs_status_id[STATUS_POSTPONE_QSIZE]=FS_register(feather->stat_handle, FS_STYLE_STATUS, FS_CALC_CURRENT, "postponed");
	feather->fs_status_id[STATUS_GARBAGE_QSIZE]=FS_register(feather->stat_handle, FS_STYLE_STATUS, FS_CALC_CURRENT, "garbage_num");	

	feather->fs_status_id[STATUS_OUTER_MID_NUM]=FS_register(feather->stat_handle, FS_STYLE_STATUS, FS_CALC_CURRENT, "outer_mid");
	feather->fs_status_id[STATUS_INNER_MID_NUM]=FS_register(feather->stat_handle, FS_STYLE_STATUS, FS_CALC_CURRENT, "inner_mid");
	feather->fs_status_id[STATUS_ZOMBIE_RS_STREAM]=FS_register(feather->stat_handle, FS_STYLE_STATUS, FS_CALC_CURRENT, "z_stream");

	feather->fs_status_id[STATUS_NOT_GROUP_HIT]=FS_register(feather->stat_handle, FS_STYLE_STATUS, FS_CALC_CURRENT, "nt_grp_hit");

	feather->fs_status_id[STATUS_TOTAL_SCAN_LEN]=FS_register(feather->stat_handle, FS_STYLE_STATUS, FS_CALC_CURRENT, "scan_bytes");
	feather->fs_status_id[STATUS_TOTAL_SCAN_CNT]=FS_register(feather->stat_handle, FS_STYLE_STATUS, FS_CALC_CURRENT, "scan_times");
	feather->fs_status_id[STATUS_UPDATE_ERR_CNT]=FS_register(feather->stat_handle, FS_STYLE_STATUS, FS_CALC_CURRENT, "update_err");
	feather->fs_status_id[STATUS_ICONV_ERR_CNT]=FS_register(feather->stat_handle, FS_STYLE_STATUS, FS_CALC_CURRENT, "iconv_err");
	feather->fs_status_id[STATUS_SCAN_ERR_CNT]=FS_register(feather->stat_handle, FS_STYLE_STATUS, FS_CALC_CURRENT, "scan_error");
	feather->fs_status_id[STATUS_CMD_NUM]=FS_register(feather->stat_handle, FS_STYLE_STATUS, FS_CALC_CURRENT, "cmd_commit");
	feather->fs_status_id[STATUS_CMD_Q_SIZE]=FS_register(feather->stat_handle, FS_STYLE_STATUS, FS_CALC_CURRENT, "cmd_in_q");
	feather->fs_status_id[STATUS_CMD_LINE_NUM]=FS_register(feather->stat_handle, FS_STYLE_STATUS, FS_CALC_SPEED, "line_cmd/s");

	feather->fs_column_id[COLUMN_TABLE_RULE_NUM]=FS_register(feather->stat_handle, FS_STYLE_COLUMN, FS_CALC_CURRENT, "rule");
	feather->fs_column_id[COLUMN_TABLE_REGEX_NUM]=FS_register(feather->stat_handle, FS_STYLE_COLUMN, FS_CALC_CURRENT, "reg/v6");
	feather->fs_column_id[COLUMN_TABLE_STREAM_NUM]=FS_register(feather->stat_handle, FS_STYLE_COLUMN, FS_CALC_CURRENT, "stream");
	feather->fs_column_id[COLUMN_TABLE_SCAN_BYTES]=FS_register(feather->stat_handle, FS_STYLE_COLUMN, FS_CALC_SPEED, "IN_Bps");
	if(feather->perf_on==1)
	{
		feather->fs_column_id[COLUMN_TABLE_CPU_TIME]=FS_register(feather->stat_handle, FS_STYLE_COLUMN, FS_CALC_SPEED, "cpu_us");
		value=feather->fs_column_id[COLUMN_TABLE_CPU_TIME];
		FS_set_para(feather->stat_handle, ID_INVISBLE, &value, sizeof(value));
		FS_register_ratio(feather->stat_handle,
						feather->fs_column_id[COLUMN_TABLE_SCAN_BYTES], 
						feather->fs_column_id[COLUMN_TABLE_CPU_TIME], 
						1000000,		//microsecond to second
						FS_STYLE_COLUMN,
						FS_CALC_SPEED,
						"PROC_Bps");
	}
	feather->fs_column_id[COLUMN_TABLE_SCAN_CNT]=FS_register(feather->stat_handle, FS_STYLE_COLUMN, FS_CALC_SPEED, "IN_Tps");
	if(feather->perf_on==1)
	{	
		FS_register_ratio(feather->stat_handle,
						feather->fs_column_id[COLUMN_TABLE_SCAN_CNT], 
						feather->fs_column_id[COLUMN_TABLE_CPU_TIME], 
						1000000,		//microsecond to second
						FS_STYLE_COLUMN,
						FS_CALC_SPEED,
						"PROC_Tps");
	}
	feather->fs_column_id[COLUMN_TABLE_HIT_CNT]=FS_register(feather->stat_handle, FS_STYLE_COLUMN, FS_CALC_SPEED, "hit_cnt");
	value=feather->fs_column_id[COLUMN_TABLE_HIT_CNT];
	FS_set_para(feather->stat_handle, ID_INVISBLE, &value, sizeof(value));
	FS_register_ratio(feather->stat_handle,
					feather->fs_column_id[COLUMN_TABLE_HIT_CNT], 
					feather->fs_column_id[COLUMN_TABLE_SCAN_CNT], 
					1,
					FS_STYLE_COLUMN,
					FS_CALC_SPEED,
					"hit_rate");
	feather->total_stat_id=FS_register(feather->stat_handle, FS_STYLE_LINE, FS_CALC_CURRENT, "Sum");
	size_t max_table_num=Maat_table_manager_get_size(feather->table_mgr);
	for(i=0; i<(int)max_table_num; i++)
	{
		p_table=Maat_table_manager_get_by_id_raw(feather->table_mgr, i);
		if(p_table==NULL||p_table->table_type==TABLE_TYPE_PLUGIN
						||p_table->table_type==TABLE_TYPE_GROUP
						||p_table->table_type==TABLE_TYPE_COMPILE)
		{
			continue;
		}
		offset=0;
		for(j=0;j<p_table->conj_cnt;j++)
		{
			offset+=snprintf(conj_table_name+offset,sizeof(conj_table_name)-offset,
							"%s/", p_table->table_name[j]);
		}
		conj_table_name[offset-1]='\0';//delete the last slash
		if(strlen(conj_table_name)>MAX_CONJ_NAME_LEN)
		{
			conj_table_name[MAX_CONJ_NAME_LEN]='\0';
		}
		p_table->stat_line_id=FS_register(feather->stat_handle,
										 FS_STYLE_LINE, FS_CALC_CURRENT,
										 conj_table_name);
	}
	FS_start(feather->stat_handle);
	return;
}

void maat_stat_output(struct _Maat_feather_t* feather)
{
	long value=0;
	long long total_cfg_num=0, table_regex_ipv6_num=0, total_input_bytes=0, total_regex_num=0,total_hit_cnt=0;
	long long total_scan_cnt=0, total_cpu_time=0,total_stream_cnt=0,active_thread_num=0;
	long long table_stream_num=0,table_scan_cnt=0,table_input_bytes=0,table_scan_cpu_time=0,table_hit_cnt=0;
	long long outer_mid_cnt=0,inner_mid_cnt=0;
	long long not_grp_hit_cnt=0;
	long long total_update_error=0,total_iconv_error=0;
	long long compile_rule_num=0, group_rule_num=0, not_group_rule_num=0, plugin_cache_num=0, plugin_acc_num=0;
	int i=0;
	time_t now;
	struct Maat_table_schema* p_table=NULL;
	struct Maat_table_runtime* table_rt=NULL;
	time(&now);
	if(feather->scanner==NULL)
	{
		return;
	}
	active_thread_num=alignment_int64_array_cnt(feather->thread_call_cnt, feather->scan_thread_num);
	outer_mid_cnt=alignment_int64_array_sum(feather->outer_mid_cnt, feather->scan_thread_num);
	inner_mid_cnt=alignment_int64_array_sum(feather->compile_mid_cnt, feather->scan_thread_num);
	not_grp_hit_cnt=alignment_int64_array_sum(feather->not_grp_hit_cnt, feather->scan_thread_num);

	FS_operate(feather->stat_handle, feather->fs_status_id[STATUS_VERSION], 0, FS_OP_SET, feather->maat_version);
	FS_operate(feather->stat_handle, feather->fs_status_id[STATUS_THRED_NUM], 0, FS_OP_SET, active_thread_num);
	FS_operate(feather->stat_handle, feather->fs_status_id[STATUS_TABLE_NUM], 0, FS_OP_SET, Maat_table_manager_get_count(feather->table_mgr));

	FS_operate(feather->stat_handle, feather->fs_status_id[STATUS_OUTER_MID_NUM], 0, FS_OP_SET, outer_mid_cnt);
	FS_operate(feather->stat_handle, feather->fs_status_id[STATUS_INNER_MID_NUM], 0, FS_OP_SET, inner_mid_cnt);
	FS_operate(feather->stat_handle, feather->fs_status_id[STATUS_NOT_GROUP_HIT], 0, FS_OP_SET, not_grp_hit_cnt);
	FS_operate(feather->stat_handle, feather->fs_status_id[STATUS_CMD_NUM], 0, FS_OP_SET, feather->cmd_acc_num);
	FS_operate(feather->stat_handle, feather->fs_status_id[STATUS_CMD_Q_SIZE], 0, FS_OP_SET, feather->cmd_q_cnt);
	FS_operate(feather->stat_handle, feather->fs_status_id[STATUS_CMD_LINE_NUM], 0, FS_OP_SET, feather->line_cmd_acc_num);

	value=Maat_garbage_bin_get_size(feather->garbage_bin);
	FS_operate(feather->stat_handle, feather->fs_status_id[STATUS_GARBAGE_QSIZE], 0,FS_OP_SET,value);
	feather->update_err_cnt=0;
	feather->iconv_err_cnt=0;
	size_t max_table_num=Maat_table_manager_get_size(feather->table_mgr);
	for(i=0; i<(int)max_table_num; i++)
	{
		table_stream_num=0;
		table_scan_cnt=0;
		table_input_bytes=0;
		table_scan_cpu_time=0;
		table_hit_cnt=0;
		table_regex_ipv6_num=0;
		
		p_table=Maat_table_manager_get_by_id_raw(feather->table_mgr, i);
		if(p_table==NULL)
		{
			continue;
		}
		table_rt=Maat_table_runtime_get(feather->scanner->table_rt_mgr, i);
		switch(p_table->table_type)
		{
			case TABLE_TYPE_PLUGIN:
				plugin_cache_num+=Maat_table_runtime_plugin_cached_row_count(table_rt);
				plugin_acc_num+=table_rt->plugin.acc_line_num;
				break;
			case TABLE_TYPE_GROUP:
				group_rule_num+=table_rt->origin_rule_num;
				not_group_rule_num+=table_rt->group2compile.not_flag_group;
				break;
			case TABLE_TYPE_COMPILE:
				compile_rule_num+=table_rt->origin_rule_num;
				break;
			case TABLE_TYPE_EXPR:
			case TABLE_TYPE_EXPR_PLUS:
				table_regex_ipv6_num=table_rt->expr.regex_rule_cnt;
				total_iconv_error=p_table->expr.iconv_err_cnt;
				break;
			case TABLE_TYPE_IP:
			case TABLE_TYPE_IP_PLUS:
				table_regex_ipv6_num=table_rt->ip.ipv6_rule_cnt;
				break;
			default:
				break;
		}
		if(p_table->table_type==TABLE_TYPE_PLUGIN||
			p_table->table_type==TABLE_TYPE_GROUP||
			p_table->table_type==TABLE_TYPE_COMPILE)
		{
			continue;
		}
		FS_operate(feather->stat_handle,
					p_table->stat_line_id,
					feather->fs_column_id[COLUMN_TABLE_RULE_NUM],
					FS_OP_SET,
					table_rt->origin_rule_num);
		total_cfg_num+=table_rt->origin_rule_num;
		
		FS_operate(feather->stat_handle,
					p_table->stat_line_id,
					feather->fs_column_id[COLUMN_TABLE_REGEX_NUM],
					FS_OP_SET,
					table_regex_ipv6_num);
		total_regex_num+=table_regex_ipv6_num;

		table_stream_num=alignment_int64_array_sum(table_rt->stream_num,feather->scan_thread_num);
		alignment_int64_array_reset(table_rt->stream_num,feather->scan_thread_num);
		FS_operate(feather->stat_handle,
					p_table->stat_line_id,
					feather->fs_column_id[COLUMN_TABLE_STREAM_NUM],
					FS_OP_ADD,
					table_stream_num);
		total_stream_cnt+= table_stream_num;
		

		table_scan_cnt=alignment_int64_array_sum(table_rt->scan_cnt,feather->scan_thread_num);
		alignment_int64_array_reset(table_rt->scan_cnt,feather->scan_thread_num);
		FS_operate(feather->stat_handle,
					p_table->stat_line_id,
					feather->fs_column_id[COLUMN_TABLE_SCAN_CNT],
					FS_OP_ADD,
					table_scan_cnt);
		total_scan_cnt+=table_scan_cnt;

		table_input_bytes=alignment_int64_array_sum(table_rt->input_bytes,feather->scan_thread_num);
		alignment_int64_array_reset(table_rt->input_bytes,feather->scan_thread_num);
		FS_operate(feather->stat_handle,
					p_table->stat_line_id,
					feather->fs_column_id[COLUMN_TABLE_SCAN_BYTES],
					FS_OP_ADD,
					table_input_bytes);
		total_input_bytes+=table_input_bytes;
		if(feather->perf_on==1)
		{
			table_scan_cpu_time=alignment_int64_array_sum(table_rt->scan_cpu_time,feather->scan_thread_num);
			alignment_int64_array_reset(table_rt->scan_cpu_time,feather->scan_thread_num);
			table_scan_cpu_time/=1000;
			FS_operate(feather->stat_handle,
						p_table->stat_line_id,
						feather->fs_column_id[COLUMN_TABLE_CPU_TIME],
						FS_OP_ADD,
						table_scan_cpu_time);
			total_cpu_time+=table_scan_cpu_time;
		}
		
		table_hit_cnt=alignment_int64_array_sum(table_rt->hit_cnt,feather->scan_thread_num);
		alignment_int64_array_reset(table_rt->hit_cnt,feather->scan_thread_num);
		FS_operate(feather->stat_handle,
					p_table->stat_line_id,
					feather->fs_column_id[COLUMN_TABLE_HIT_CNT],
					FS_OP_ADD,
					table_hit_cnt);
		total_update_error+=p_table->udpate_err_cnt;
	}
	FS_operate(feather->stat_handle,
				feather->total_stat_id, 
				feather->fs_column_id[COLUMN_TABLE_RULE_NUM], 
				FS_OP_SET,
				total_cfg_num);
	FS_operate(feather->stat_handle,
				feather->total_stat_id, 
				feather->fs_column_id[COLUMN_TABLE_REGEX_NUM], 
				FS_OP_SET,
				total_regex_num);
	FS_operate(feather->stat_handle,
				feather->total_stat_id, 
				feather->fs_column_id[COLUMN_TABLE_STREAM_NUM], 
				FS_OP_ADD,
				total_stream_cnt);
	FS_operate(feather->stat_handle,
				feather->total_stat_id, 
				feather->fs_column_id[COLUMN_TABLE_SCAN_CNT], 
				FS_OP_ADD,
				total_scan_cnt);
	FS_operate(feather->stat_handle,
				feather->total_stat_id, 
				feather->fs_column_id[COLUMN_TABLE_SCAN_BYTES], 
				FS_OP_ADD,
				total_input_bytes);
	if(feather->perf_on==1)
	{
		FS_operate(feather->stat_handle,
					feather->total_stat_id, 
					feather->fs_column_id[COLUMN_TABLE_CPU_TIME], 
					FS_OP_ADD,
					total_cpu_time);
	}
	total_hit_cnt=alignment_int64_array_sum(feather->hit_cnt, feather->scan_thread_num);
	alignment_int64_array_reset(feather->hit_cnt,feather->scan_thread_num);
	FS_operate(feather->stat_handle,
				feather->total_stat_id, 
				feather->fs_column_id[COLUMN_TABLE_HIT_CNT], 
				FS_OP_ADD,
				total_hit_cnt);
	feather->total_scan_bytes+=total_input_bytes;
	feather->total_scan_cnt+=total_scan_cnt;
	FS_operate(feather->stat_handle, feather->fs_status_id[STATUS_TOTAL_SCAN_LEN], 0,FS_OP_SET,feather->total_scan_bytes);
	FS_operate(feather->stat_handle, feather->fs_status_id[STATUS_TOTAL_SCAN_CNT], 0,FS_OP_SET,feather->total_scan_cnt);
	feather->update_err_cnt=total_update_error;
	feather->iconv_err_cnt=total_iconv_error;
	FS_operate(feather->stat_handle, feather->fs_status_id[STATUS_UPDATE_ERR_CNT], 0,FS_OP_SET,feather->update_err_cnt);
	FS_operate(feather->stat_handle, feather->fs_status_id[STATUS_ICONV_ERR_CNT], 0,FS_OP_SET,feather->iconv_err_cnt);
	FS_operate(feather->stat_handle, feather->fs_status_id[STATUS_SCAN_ERR_CNT], 0,FS_OP_SET,feather->scan_err_cnt);
	FS_operate(feather->stat_handle, feather->fs_status_id[STATUS_ZOMBIE_RS_STREAM], 0,FS_OP_SET,feather->zombie_rs_stream);

	FS_operate(feather->stat_handle, feather->fs_status_id[STATUS_PLUGIN_CACHE_NUM], 0,FS_OP_SET,plugin_cache_num);
	FS_operate(feather->stat_handle, feather->fs_status_id[STATUS_PLUGIN_ACC_NUM], 0,FS_OP_SET,plugin_acc_num);
	FS_operate(feather->stat_handle, feather->fs_status_id[STATUS_GROUP_REF_NUM], 0,FS_OP_SET,group_rule_num);
	FS_operate(feather->stat_handle, feather->fs_status_id[STATUS_GROUP_REF_NOT_NUM], 0,FS_OP_SET,not_group_rule_num);
	FS_operate(feather->stat_handle, feather->fs_status_id[STATUS_COMPILE_RULE_NUM], 0,FS_OP_SET,compile_rule_num);

	FS_operate(feather->stat_handle, feather->fs_status_id[STATUS_POSTPONE_QSIZE], 0,FS_OP_SET,feather->postpone_q_size);

	FS_passive_output(feather->stat_handle);
	return;
}