forked from Excello-cz/netdata-plugins
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsmtp.c
More file actions
202 lines (173 loc) · 5.89 KB
/
Copy pathsmtp.c
File metadata and controls
202 lines (173 loc) · 5.89 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
/* SPDX-License-Identifier: GPL-3.0-or-later */
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "callbacks.h"
#include "netdata.h"
#include "smtp.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 100
struct statistics {
int tcp_ok;
int tcp_deny;
int tcp_status;
int tcp_status_sum;
int tcp_status_count;
int tcp_end_status_0;
int tcp_end_status_256;
int tcp_end_status_25600;
int tcp_end_status_others;
int smtp;
int esmtps;
int esmtps_tls_1;
int esmtps_tls_1_1;
int esmtps_tls_1_2;
int esmtps_tls_1_3;
int esmtps_unknown;
};
static
void *
smtp_data_init() {
struct statistics * ret;
ret = calloc(1, sizeof * ret);
return ret;
}
static
void
process_smtp(const char * line, struct statistics * data) {
char * ptr;
int val;
if (strstr(line, "tcpserver: ok")) {
data->tcp_ok++;
} else if (strstr(line, "tcpserver: deny")) {
data->tcp_deny++;
} else if ((ptr = strstr(line, "tcpserver: status: "))) {
val = strtoul(ptr + sizeof "tcpserver: status: " - 1, 0, 0);
data->tcp_status_sum += val;
data->tcp_status_count++;
} else if ((ptr = strstr(line, "tcpserver: end "))) {
ptr = strstr(ptr, "status ");
if (ptr) {
val = strtoul(ptr + sizeof "status " - 1, 0, 0);
switch (val) {
case 0:
data->tcp_end_status_0++;
break;
case 256:
data->tcp_end_status_256++;
break;
case 25600:
data->tcp_end_status_25600++;
break;
default:
data->tcp_end_status_others++;
break;
}
}
} else if ((ptr = strstr(line, "uses ESMTPS"))) {
data->esmtps++;
if (strstr(ptr, "TLSv1,")) {
data->esmtps_tls_1++;
} else if (strstr(ptr, "TLSv1.1,")) {
data->esmtps_tls_1_1++;
} else if (strstr(ptr, "TLSv1.2,")) {
data->esmtps_tls_1_2++;
} else if (strstr(ptr, "TLSv1.3,")) {
data->esmtps_tls_1_3++;
} else {
data->esmtps_unknown++;
}
} else if ((ptr = strstr(line, "uses SMTP"))) {
data->smtp++;
}
}
static
int
print_smtp_header(const char * name) {
char title[BUFSIZ];
sprintf(title, "Qmail SMTPD for %s", name);
nd_chart("qmail", name, "", "smtpd qmail", title, "# smtpd connections",
"smtpd", "qmail.qmail_smtpd", ND_CHART_TYPE_AREA);
nd_dimension("tcp_ok", "TCP OK", ND_ALG_ABSOLUTE, 1, 1, ND_VISIBLE);
nd_dimension("tcp_deny", "TCP Deny", ND_ALG_ABSOLUTE, -1, 1, ND_VISIBLE);
sprintf(title, "Qmail SMTPD Open Sessions for %s", name);
nd_chart("qmail", name, "status", "smtpd statuses", title,
"average # sessions", "smtpd", "qmail.qmail_smtpd_status", ND_CHART_TYPE_LINE);
nd_dimension("tcp_status_average", "session average", ND_ALG_ABSOLUTE, 1, FRACTIONAL_CONVERSION, ND_VISIBLE);
sprintf(title, "Qmail SMTPD End Statuses for %s", name);
nd_chart("qmail", name, "end_status", "smtpd end statuses", title,
"# smtpd end statuses", "smtpd", "qmail.qmail_smtpd_end_status", ND_CHART_TYPE_LINE);
nd_dimension("tcp_end_status_0", "0", ND_ALG_ABSOLUTE, 1, 1, ND_VISIBLE);
nd_dimension("tcp_end_status_256", "256", ND_ALG_ABSOLUTE, 1, 1, ND_VISIBLE);
nd_dimension("tcp_end_status_25600", "25600", ND_ALG_ABSOLUTE, 1, 1, ND_VISIBLE);
nd_dimension("tcp_end_status_others", "other", ND_ALG_ABSOLUTE, 1, 1, ND_VISIBLE);
sprintf(title, "Qmail SMTPD smtp type for %s", name);
nd_chart("qmail", name, "smtp_type", "smtp type", title, "# smtp protocols",
"smtpd", "qmail.smtp_type", ND_CHART_TYPE_LINE);
nd_dimension("smtp", "SMTP", ND_ALG_ABSOLUTE, 1, 1, ND_VISIBLE);
nd_dimension("esmtps", "ESMTPS", ND_ALG_ABSOLUTE, 1, 1, ND_VISIBLE);
sprintf(title, "Qmail SMTPD tls connection types for %s", name);
nd_chart("qmail", name, "tls", "tls version", title, "# tls versions",
"smtpd", "qmail.qmail_smtpd_tls", ND_CHART_TYPE_LINE);
nd_dimension("tls1", "TLS_1", ND_ALG_ABSOLUTE, 1, 1, ND_VISIBLE);
nd_dimension("tls1.1", "TLS_1.1", ND_ALG_ABSOLUTE, 1, 1, ND_VISIBLE);
nd_dimension("tls1.2", "TLS_1.2", ND_ALG_ABSOLUTE, 1, 1, ND_VISIBLE);
nd_dimension("tls1.3", "TLS_1.3", ND_ALG_ABSOLUTE, 1, 1, ND_VISIBLE);
nd_dimension("unknown", "unknown", ND_ALG_ABSOLUTE, 1, 1, ND_VISIBLE);
return fflush(stdout);
}
static
int
print_smtp_data(const char * name, const struct statistics * data, const unsigned long time) {
nd_begin_time("qmail", name, "", time);
nd_set("tcp_ok", data->tcp_ok);
nd_set("tcp_deny", data->tcp_deny);
nd_end();
nd_begin_time("qmail", name, "status", time);
nd_set("tcp_status_average", data->tcp_status);
nd_end();
nd_begin_time("qmail", name, "end_status", time);
nd_set("tcp_end_status_0", data->tcp_end_status_0);
nd_set("tcp_end_status_256", data->tcp_end_status_256);
nd_set("tcp_end_status_25600", data->tcp_end_status_25600);
nd_set("tcp_end_status_others", data->tcp_end_status_others);
nd_end();
nd_begin_time("qmail", name, "smtp_type", time);
nd_set("smtp", data->smtp);
nd_set("esmtps", data->esmtps);
nd_end();
nd_begin_time("qmail", name, "tls", time);
nd_set("tls1", data->esmtps_tls_1);
nd_set("tls1.1", data->esmtps_tls_1_1);
nd_set("tls1.2", data->esmtps_tls_1_2);
nd_set("tls1.3", data->esmtps_tls_1_3);
nd_set("unknown", data->esmtps_unknown);
nd_end();
return fflush(stdout);
}
static
void
clear_smtp_data(struct statistics * data) {
int tmp = data->tcp_status;
memset(data, 0, sizeof * data);
data->tcp_status = tmp;
}
static
void
postprocess_data(struct statistics * data) {
if (data->tcp_status_count)
data->tcp_status = data->tcp_status_sum * FRACTIONAL_CONVERSION / data->tcp_status_count;
}
static
struct stat_func smtp = {
.init = &smtp_data_init,
.fini = &free,
.print_hdr = &print_smtp_header,
.print = (int (*)(const char *, const void *, unsigned long))&print_smtp_data,
.process = (void (*)(const char *, void *))&process_smtp,
.postprocess = (void (*)(void *))&postprocess_data,
.clear = (void (*)(void *))&clear_smtp_data,
};
struct stat_func * smtp_func = &smtp;