forked from Excello-cz/netdata-plugins
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscanner.c
More file actions
323 lines (286 loc) · 11.3 KB
/
Copy pathscanner.c
File metadata and controls
323 lines (286 loc) · 11.3 KB
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
/* SPDX-License-Identifier: GPL-3.0-or-later */
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "netdata.h"
#include "callbacks.h"
#include "scanner.h"
/* Netdata collects integer values only. We have to multiply collected value by
* this constant and set the DIMENSION divider to the same value if we need
* fractional values. */
#define FRACTIONAL_CONVERSION 1000000
struct scanner_statistics {
int clear;
int clamdscan;
int spam_tagged;
int spam_rejected;
int spam_deleted;
int other;
int sc_0;
int sc_1;
int cc_0;
int cc_1;
int scan_duration_sc_0_cc_0;
int scan_duration_sc_0_cc_0_count;
unsigned int scan_duration_sc_0_cc_0_sum;
int scan_duration_sc_0_cc_1;
int scan_duration_sc_0_cc_1_count;
unsigned int scan_duration_sc_0_cc_1_sum;
int scan_duration_sc_1_cc_0;
int scan_duration_sc_1_cc_0_count;
unsigned int scan_duration_sc_1_cc_0_sum;
int scan_duration_sc_1_cc_1;
int scan_duration_sc_1_cc_1_count;
unsigned int scan_duration_sc_1_cc_1_sum;
/* only the clamav results, nothing done by scanners */
int scan_duration_cc_0;
int scan_duration_cc_0_count;
unsigned int scan_duration_cc_0_sum;
int scan_duration_cc_1;
int scan_duration_cc_1_count;
unsigned int scan_duration_cc_1_sum;
/* only the scanner results, nothing done by clamav */
int scan_duration_sc_0;
int scan_duration_sc_0_count;
unsigned int scan_duration_sc_0_sum;
int scan_duration_sc_1;
int scan_duration_sc_1_count;
unsigned int scan_duration_sc_1_sum;
/* whitelist and the others */
int scan_duration__;
int scan_duration__count;
unsigned int scan_duration__sum;
};
static
void *
scanner_data_init() {
struct scanner_statistics * ret;
ret = calloc(1, sizeof * ret);
return ret;
}
static
void
scanner_clear(struct scanner_statistics * data) {
memset(data, 0, sizeof * data);
}
static
const char *
get_next_field(char * restrict buf, size_t size, const char * restrict line) {
const char * s = line;
char * d = buf;
for (; *s && *s != '\t' && d - buf < size; d++, s++) {
*d = *s;
}
if (d - buf >= size) {
return NULL;
}
*d = '\0';
return s;
}
static
void
scanner_process(const char * line, struct scanner_statistics * data) {
int sc_stat = -1;
int cc_stat = -1;
char buf[256];
/* Skip date */
if ((line = get_next_field(buf, sizeof buf, line)) == NULL) {
fprintf(stderr, "scanner.plugin: cannot skip date\n");
return;
}
/* Load scan status */
if ((line = get_next_field(buf, sizeof buf, line + 1)) == NULL) {
fprintf(stderr, "scanner.plugin: cannot get status\n");
return;
}
if (strstr(buf, "Clear")) {
data->clear++;
} else if (strstr(buf, "CLAMDSCAN")) {
data->clamdscan++;
} else if (strstr(buf, ":SPAM-TAGGED")) {
data->spam_tagged++;
} else if (strstr(buf, ":SPAM-REJECTED")) {
data->spam_rejected++;
} else if (strstr(buf, ":SPAM-DELETED")) {
data->spam_deleted++;
} else {
data->other++;
}
if (strstr(buf, ":SC:0")) {
data->sc_0++;
sc_stat = 0;
} else if (strstr(buf, ":SC:1")) {
data->sc_1++;
sc_stat = 1;
}
if (strstr(buf, ":CC:0")) {
data->cc_0++;
cc_stat = 0;
} else if (strstr(buf, ":CC:1")) {
data->cc_1++;
cc_stat = 1;
}
/* Load time */
if ((line = get_next_field(buf, sizeof buf, line + 1)) == NULL) {
fprintf(stderr, "scanner.plugin: cannot get processing time\n");
return;
}
int duration = atof(buf) * FRACTIONAL_CONVERSION;
if (sc_stat == -1 && cc_stat == -1) {
data->scan_duration__count++;
data->scan_duration__sum += duration;
}
else if(sc_stat == -1 && cc_stat == 0) {
data->scan_duration_cc_0_count++;
data->scan_duration_cc_0_sum += duration;
}
else if(sc_stat == -1 && cc_stat == 1) {
data->scan_duration_cc_1_count++;
data->scan_duration_cc_1_sum += duration;
}
else if(sc_stat == 0 && cc_stat == -1) {
data->scan_duration_sc_0_count++;
data->scan_duration_sc_0_sum += duration;
}
else if(sc_stat == 0 && cc_stat == 0) {
data->scan_duration_sc_0_cc_0_count++;
data->scan_duration_sc_0_cc_0_sum += duration;
}
else if(sc_stat == 0 && cc_stat == 1) {
data->scan_duration_sc_0_cc_1_count++;
data->scan_duration_sc_0_cc_1_sum += duration;
}
else if(sc_stat == 1 && cc_stat == -1) {
data->scan_duration_sc_1_count++;
data->scan_duration_sc_1_sum += duration;
}
else if(sc_stat == 1 && cc_stat == 0) {
data->scan_duration_sc_1_cc_0_count++;
data->scan_duration_sc_1_cc_0_sum += duration;
}
else if(sc_stat == 1 && cc_stat == 1) {
data->scan_duration_sc_1_cc_1_count++;
data->scan_duration_sc_1_cc_1_sum += duration;
}
}
static
int
scanner_print_hdr(const char * name) {
nd_chart("scannerd", name, "type", "", "", "volume", "scannerd", "scannerd.scannerd_type", ND_CHART_TYPE_STACKED);
nd_dimension("clear", "Clear", ND_ALG_ABSOLUTE, 1, 1, ND_VISIBLE);
nd_dimension("clamdscan", "Clamdscan", ND_ALG_ABSOLUTE, 1, 1, ND_VISIBLE);
nd_dimension("spam_tagged", "SPAM Tagged", ND_ALG_ABSOLUTE, 1, 1, ND_VISIBLE);
nd_dimension("spam_rejected", "SPAM Rejected", ND_ALG_ABSOLUTE, 1, 1, ND_VISIBLE);
nd_dimension("spam_deleted", "SPAM Deleted", ND_ALG_ABSOLUTE, 1, 1, ND_VISIBLE);
nd_dimension("other", "Other", ND_ALG_ABSOLUTE, 1, 1, ND_VISIBLE);
nd_chart("scannerd", name, "cached", "", "Cached results", "percentage", "scannerd", "scannerd.scannerd_sc", ND_CHART_TYPE_STACKED);
nd_dimension("sc_0", "SC:0", ND_ALG_PERCENTAGE_OF_ABSOLUTE_ROW, 1, 1, ND_VISIBLE);
nd_dimension("sc_1", "SC:1", ND_ALG_PERCENTAGE_OF_ABSOLUTE_ROW, 1, 1, ND_VISIBLE);
nd_dimension("cc_0", "CC:0", ND_ALG_PERCENTAGE_OF_ABSOLUTE_ROW, 1, 1, ND_VISIBLE);
nd_dimension("cc_1", "CC:1", ND_ALG_PERCENTAGE_OF_ABSOLUTE_ROW, 1, 1, ND_VISIBLE);
nd_chart("scannerd", name, "duration", "", "Scan duration", "duration", "scannerd", "scannerd.scannerd_scan_duration", ND_CHART_TYPE_LINE);
nd_dimension("scan_duration_sc_0_cc_0", "SC:0_CC:0", ND_ALG_ABSOLUTE, 1, FRACTIONAL_CONVERSION, ND_VISIBLE);
nd_dimension("scan_duration_sc_0_cc_1", "SC:0_CC:1", ND_ALG_ABSOLUTE, 1, FRACTIONAL_CONVERSION, ND_VISIBLE);
nd_dimension("scan_duration_sc_1_cc_0", "SC:1_CC:0", ND_ALG_ABSOLUTE, 1, FRACTIONAL_CONVERSION, ND_VISIBLE);
nd_dimension("scan_duration_sc_1_cc_1", "SC:1_CC:1", ND_ALG_ABSOLUTE, 1, FRACTIONAL_CONVERSION, ND_VISIBLE);
nd_dimension("scan_duration_cc_0", "CC:0", ND_ALG_ABSOLUTE, 1, FRACTIONAL_CONVERSION, ND_VISIBLE);
nd_dimension("scan_duration_cc_1", "CC:1", ND_ALG_ABSOLUTE, 1, FRACTIONAL_CONVERSION, ND_VISIBLE);
nd_dimension("scan_duration_sc_0", "SC:0", ND_ALG_ABSOLUTE, 1, FRACTIONAL_CONVERSION, ND_VISIBLE);
nd_dimension("scan_duration_sc_1", "SC:1", ND_ALG_ABSOLUTE, 1, FRACTIONAL_CONVERSION, ND_VISIBLE);
nd_dimension("scan_duration__", "__", ND_ALG_ABSOLUTE, 1, FRACTIONAL_CONVERSION, ND_VISIBLE);
nd_chart("scannerd", name, "duration_ratio", "", "Scan duration ratio", "percentage", "scannerd", "scannerd.scannerd_scan_duration_ratio", ND_CHART_TYPE_STACKED);
nd_dimension("scan_duration_sc_0_cc_0", "SC:0_CC:0", ND_ALG_PERCENTAGE_OF_ABSOLUTE_ROW, 1, FRACTIONAL_CONVERSION, ND_VISIBLE);
nd_dimension("scan_duration_sc_0_cc_1", "SC:0_CC:1", ND_ALG_PERCENTAGE_OF_ABSOLUTE_ROW, 1, FRACTIONAL_CONVERSION, ND_VISIBLE);
nd_dimension("scan_duration_sc_1_cc_0", "SC:1_CC:0", ND_ALG_PERCENTAGE_OF_ABSOLUTE_ROW, 1, FRACTIONAL_CONVERSION, ND_VISIBLE);
nd_dimension("scan_duration_sc_1_cc_1", "SC:1_CC:1", ND_ALG_PERCENTAGE_OF_ABSOLUTE_ROW, 1, FRACTIONAL_CONVERSION, ND_VISIBLE);
nd_dimension("scan_duration_cc_0", "CC:0", ND_ALG_PERCENTAGE_OF_ABSOLUTE_ROW, 1, FRACTIONAL_CONVERSION, ND_VISIBLE);
nd_dimension("scan_duration_cc_1", "CC:1", ND_ALG_PERCENTAGE_OF_ABSOLUTE_ROW, 1, FRACTIONAL_CONVERSION, ND_VISIBLE);
nd_dimension("scan_duration_sc_0", "SC:0", ND_ALG_PERCENTAGE_OF_ABSOLUTE_ROW, 1, FRACTIONAL_CONVERSION, ND_VISIBLE);
nd_dimension("scan_duration_sc_1", "SC:1", ND_ALG_PERCENTAGE_OF_ABSOLUTE_ROW, 1, FRACTIONAL_CONVERSION, ND_VISIBLE);
nd_dimension("scan_duration__", "__", ND_ALG_PERCENTAGE_OF_ABSOLUTE_ROW, 1, FRACTIONAL_CONVERSION, ND_VISIBLE);
return fflush(stdout);
}
static
int
scanner_print(const char * name, const struct scanner_statistics * data,
const unsigned long time) {
nd_begin_time("scannerd", name, "type", time);
nd_set("clear", data->clear);
nd_set("clamdscan", data->clamdscan);
nd_set("spam_tagged", data->spam_tagged);
nd_set("spam_rejected", data->spam_rejected);
nd_set("spam_deleted", data->spam_deleted);
nd_set("other", data->other);
nd_end();
nd_begin_time("scannerd", name, "cached", time);
nd_set("sc_0", data->sc_0);
nd_set("sc_1", data->sc_1);
nd_set("cc_0", data->cc_0);
nd_set("cc_1", data->cc_1);
nd_end();
nd_begin_time("scannerd", name, "duration", time);
nd_set("scan_duration_sc_0_cc_0", data->scan_duration_sc_0_cc_0);
nd_set("scan_duration_sc_0_cc_1", data->scan_duration_sc_0_cc_1);
nd_set("scan_duration_sc_1_cc_0", data->scan_duration_sc_1_cc_0);
nd_set("scan_duration_sc_1_cc_1", data->scan_duration_sc_1_cc_1);
nd_set("scan_duration_cc_0", data->scan_duration_cc_0);
nd_set("scan_duration_cc_1", data->scan_duration_cc_1);
nd_set("scan_duration_sc_0", data->scan_duration_sc_0);
nd_set("scan_duration_sc_1", data->scan_duration_sc_1);
nd_set("scan_duration__", data->scan_duration__);
nd_end();
nd_begin_time("scannerd", name, "duration_ratio", time);
nd_set("scan_duration_sc_0_cc_0", data->scan_duration_sc_0_cc_0);
nd_set("scan_duration_sc_0_cc_1", data->scan_duration_sc_0_cc_1);
nd_set("scan_duration_sc_1_cc_0", data->scan_duration_sc_1_cc_0);
nd_set("scan_duration_sc_1_cc_1", data->scan_duration_sc_1_cc_1);
nd_set("scan_duration_cc_0", data->scan_duration_cc_0);
nd_set("scan_duration_cc_1", data->scan_duration_cc_1);
nd_set("scan_duration_sc_0", data->scan_duration_sc_0);
nd_set("scan_duration_sc_1", data->scan_duration_sc_1);
nd_set("scan_duration__", data->scan_duration__);
nd_end();
return fflush(stdout);
}
static
void
postprocess_data(struct scanner_statistics * data) {
if (data->scan_duration_sc_0_cc_0_count) {
data->scan_duration_sc_0_cc_0 = data->scan_duration_sc_0_cc_0_sum / data->scan_duration_sc_0_cc_0_count;
}
if (data->scan_duration_sc_0_cc_1_count) {
data->scan_duration_sc_0_cc_1 = data->scan_duration_sc_0_cc_1_sum / data->scan_duration_sc_0_cc_1_count;
}
if (data->scan_duration_sc_1_cc_0_count) {
data->scan_duration_sc_1_cc_0 = data->scan_duration_sc_1_cc_0_sum / data->scan_duration_sc_1_cc_0_count;
}
if (data->scan_duration_sc_1_cc_1_count) {
data->scan_duration_sc_1_cc_1 = data->scan_duration_sc_1_cc_1_sum / data->scan_duration_sc_1_cc_1_count;
}
if (data->scan_duration_cc_0_count) {
data->scan_duration_cc_0 = data->scan_duration_cc_0_sum / data->scan_duration_cc_0_count;
}
if (data->scan_duration_cc_1_count) {
data->scan_duration_cc_1 = data->scan_duration_cc_1_sum / data->scan_duration_cc_1_count;
}
if (data->scan_duration_sc_0_count) {
data->scan_duration_sc_0 = data->scan_duration_sc_0_sum / data->scan_duration_sc_0_count;
}
if (data->scan_duration_sc_1_count) {
data->scan_duration_sc_1 = data->scan_duration_sc_1_sum / data->scan_duration_sc_1_count;
}
if (data->scan_duration__count) {
data->scan_duration__ = data->scan_duration__sum / data->scan_duration__count;
}
}
static
struct stat_func scanner = {
.init = &scanner_data_init,
.fini = &free,
.print_hdr = scanner_print_hdr,
.print = (int (*)(const char *, const void *, unsigned long))scanner_print,
.process = (void (*)(const char *, void *))scanner_process,
.postprocess = (void (*)(void *))&postprocess_data,
.clear = (void (*)(void *))&scanner_clear,
};
struct stat_func * scanner_func = &scanner;