summaryrefslogtreecommitdiffstats
path: root/src/proc_net_snmp.c
blob: e0ac6a263eee9636959e13e622e02e2df8ee2576 (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
#ifdef HAVE_CONFIG_H
#include <config.h>
#endif
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#include "common.h"
#include "log.h"
#include "appconfig.h"
#include "procfile.h"
#include "rrd.h"
#include "plugin_proc.h"

#define RRD_TYPE_NET_SNMP			"ipv4"
#define RRD_TYPE_NET_SNMP_LEN		strlen(RRD_TYPE_NET_SNMP)

int do_proc_net_snmp(int update_every, unsigned long long dt) {
	static procfile *ff = NULL;
	static int do_ip_packets = -1, do_ip_fragsout = -1, do_ip_fragsin = -1, do_ip_errors = -1,
		do_tcp_sockets = -1, do_tcp_packets = -1, do_tcp_errors = -1, do_tcp_handshake = -1,
		do_udp_packets = -1, do_udp_errors = -1;

	if(do_ip_packets == -1)		do_ip_packets 		= config_get_boolean("plugin:proc:/proc/net/snmp", "ipv4 packets", 1);
	if(do_ip_fragsout == -1)	do_ip_fragsout 		= config_get_boolean("plugin:proc:/proc/net/snmp", "ipv4 fragments sent", 1);
	if(do_ip_fragsin == -1)		do_ip_fragsin 		= config_get_boolean("plugin:proc:/proc/net/snmp", "ipv4 fragments assembly", 1);
	if(do_ip_errors == -1)		do_ip_errors 		= config_get_boolean("plugin:proc:/proc/net/snmp", "ipv4 errors", 1);
	if(do_tcp_sockets == -1)	do_tcp_sockets 		= config_get_boolean("plugin:proc:/proc/net/snmp", "ipv4 TCP connections", 1);
	if(do_tcp_packets == -1)	do_tcp_packets 		= config_get_boolean("plugin:proc:/proc/net/snmp", "ipv4 TCP packets", 1);
	if(do_tcp_errors == -1)		do_tcp_errors 		= config_get_boolean("plugin:proc:/proc/net/snmp", "ipv4 TCP errors", 1);
	if(do_tcp_handshake == -1)	do_tcp_handshake 	= config_get_boolean("plugin:proc:/proc/net/snmp", "ipv4 TCP handshake issues", 1);
	if(do_udp_packets == -1)	do_udp_packets 		= config_get_boolean("plugin:proc:/proc/net/snmp", "ipv4 UDP packets", 1);
	if(do_udp_errors == -1)		do_udp_errors 		= config_get_boolean("plugin:proc:/proc/net/snmp", "ipv4 UDP errors", 1);

	if(dt) {};

	if(!ff) {
		char filename[FILENAME_MAX + 1];
		snprintfz(filename, FILENAME_MAX, "%s%s", global_host_prefix, "/proc/net/snmp");
		ff = procfile_open(config_get("plugin:proc:/proc/net/snmp", "filename to monitor", filename), " \t:", PROCFILE_FLAG_DEFAULT);
	}
	if(!ff) return 1;

	ff = procfile_readall(ff);
	if(!ff) return 0; // we return 0, so that we will retry to open it next time

	uint32_t lines = procfile_lines(ff), l;
	uint32_t words;

	RRDSET *st;

	for(l = 0; l < lines ;l++) {
		if(strcmp(procfile_lineword(ff, l, 0), "Ip") == 0) {
			l++;

			if(strcmp(procfile_lineword(ff, l, 0), "Ip") != 0) {
				error("Cannot read Ip line from /proc/net/snmp.");
				break;
			}

			words = procfile_linewords(ff, l);
			if(words < 20) {
				error("Cannot read /proc/net/snmp Ip line. Expected 20 params, read %d.", words);
				continue;
			}

			// see also http://net-snmp.sourceforge.net/docs/mibs/ip.html
			unsigned long long Forwarding, DefaultTTL, InReceives, InHdrErrors, InAddrErrors, ForwDatagrams, InUnknownProtos, InDiscards, InDelivers,
				OutRequests, OutDiscards, OutNoRoutes, ReasmTimeout, ReasmReqds, ReasmOKs, ReasmFails, FragOKs, FragFails, FragCreates;

			Forwarding		= strtoull(procfile_lineword(ff, l, 1), NULL, 10);
			DefaultTTL		= strtoull(procfile_lineword(ff, l, 2), NULL, 10);
			InReceives		= strtoull(procfile_lineword(ff, l, 3), NULL, 10);
			InHdrErrors		= strtoull(procfile_lineword(ff, l, 4), NULL, 10);
			InAddrErrors	= strtoull(procfile_lineword(ff, l, 5), NULL, 10);
			ForwDatagrams	= strtoull(procfile_lineword(ff, l, 6), NULL, 10);
			InUnknownProtos	= strtoull(procfile_lineword(ff, l, 7), NULL, 10);
			InDiscards		= strtoull(procfile_lineword(ff, l, 8), NULL, 10);
			InDelivers		= strtoull(procfile_lineword(ff, l, 9), NULL, 10);
			OutRequests		= strtoull(procfile_lineword(ff, l, 10), NULL, 10);
			OutDiscards		= strtoull(procfile_lineword(ff, l, 11), NULL, 10);
			OutNoRoutes		= strtoull(procfile_lineword(ff, l, 12), NULL, 10);
			ReasmTimeout	= strtoull(procfile_lineword(ff, l, 13), NULL, 10);
			ReasmReqds		= strtoull(procfile_lineword(ff, l, 14), NULL, 10);
			ReasmOKs		= strtoull(procfile_lineword(ff, l, 15), NULL, 10);
			ReasmFails		= strtoull(procfile_lineword(ff, l, 16), NULL, 10);
			FragOKs			= strtoull(procfile_lineword(ff, l, 17), NULL, 10);
			FragFails		= strtoull(procfile_lineword(ff, l, 18), NULL, 10);
			FragCreates		= strtoull(procfile_lineword(ff, l, 19), NULL, 10);

			// these are not counters
			if(Forwarding) {};		// is forwarding enabled?
			if(DefaultTTL) {};		// the default ttl on packets
			if(ReasmTimeout) {};	// Reassembly timeout

			// this counter is not used
			if(InDelivers) {};		// total number of packets delivered to IP user-protocols

			// --------------------------------------------------------------------

			if(do_ip_packets) {
				st = rrdset_find(RRD_TYPE_NET_SNMP ".packets");
				if(!st) {
					st = rrdset_create(RRD_TYPE_NET_SNMP, "packets", NULL, "packets", NULL, "IPv4 Packets", "packets/s", 3000, update_every, RRDSET_TYPE_LINE);

					rrddim_add(st, "received", NULL, 1, 1, RRDDIM_INCREMENTAL);
					rrddim_add(st, "sent", NULL, -1, 1, RRDDIM_INCREMENTAL);
					rrddim_add(st, "forwarded", NULL, 1, 1, RRDDIM_INCREMENTAL);
				}
				else rrdset_next(st);

				rrddim_set(st, "sent", OutRequests);
				rrddim_set(st, "received", InReceives);
				rrddim_set(st, "forwarded", ForwDatagrams);
				rrdset_done(st);
			}

			// --------------------------------------------------------------------

			if(do_ip_fragsout) {
				st = rrdset_find(RRD_TYPE_NET_SNMP ".fragsout");
				if(!st) {
					st = rrdset_create(RRD_TYPE_NET_SNMP, "fragsout", NULL, "fragments", NULL, "IPv4 Fragments Sent", "packets/s", 3010, update_every, RRDSET_TYPE_LINE);
					st->isdetail = 1;

					rrddim_add(st, "ok", NULL, 1, 1, RRDDIM_INCREMENTAL);
					rrddim_add(st, "failed", NULL, -1, 1, RRDDIM_INCREMENTAL);
					rrddim_add(st, "all", NULL, 1, 1, RRDDIM_INCREMENTAL);
				}
				else rrdset_next(st);

				rrddim_set(st, "ok", FragOKs);
				rrddim_set(st, "failed", FragFails);
				rrddim_set(st, "all", FragCreates);
				rrdset_done(st);
			}

			// --------------------------------------------------------------------

			if(do_ip_fragsin) {
				st = rrdset_find(RRD_TYPE_NET_SNMP ".fragsin");
				if(!st) {
					st = rrdset_create(RRD_TYPE_NET_SNMP, "fragsin", NULL, "fragments", NULL, "IPv4 Fragments Reassembly", "packets/s", 3011, update_every, RRDSET_TYPE_LINE);
					st->isdetail = 1;

					rrddim_add(st, "ok", NULL, 1, 1, RRDDIM_INCREMENTAL);
					rrddim_add(st, "failed", NULL, -1, 1, RRDDIM_INCREMENTAL);
					rrddim_add(st, "all", NULL, 1, 1, RRDDIM_INCREMENTAL);
				}
				else rrdset_next(st);

				rrddim_set(st, "ok", ReasmOKs);
				rrddim_set(st, "failed", ReasmFails);
				rrddim_set(st, "all", ReasmReqds);
				rrdset_done(st);
			}

			// --------------------------------------------------------------------

			if(do_ip_errors) {
				st = rrdset_find(RRD_TYPE_NET_SNMP ".errors");
				if(!st) {
					st = rrdset_create(RRD_TYPE_NET_SNMP, "errors", NULL, "errors", NULL, "IPv4 Errors", "packets/s", 3002, update_every, RRDSET_TYPE_LINE);
					st->isdetail = 1;

					rrddim_add(st, "InDiscards", NULL, 1, 1, RRDDIM_INCREMENTAL);
					rrddim_add(st, "OutDiscards", NULL, -1, 1, RRDDIM_INCREMENTAL);

					rrddim_add(st, "InHdrErrors", NULL, 1, 1, RRDDIM_INCREMENTAL);
					rrddim_add(st, "InAddrErrors", NULL, 1, 1, RRDDIM_INCREMENTAL);
					rrddim_add(st, "InUnknownProtos", NULL, 1, 1, RRDDIM_INCREMENTAL);

					rrddim_add(st, "OutNoRoutes", NULL, -1, 1, RRDDIM_INCREMENTAL);
				}
				else rrdset_next(st);

				rrddim_set(st, "InDiscards", InDiscards);
				rrddim_set(st, "OutDiscards", OutDiscards);
				rrddim_set(st, "InHdrErrors", InHdrErrors);
				rrddim_set(st, "InAddrErrors", InAddrErrors);
				rrddim_set(st, "InUnknownProtos", InUnknownProtos);
				rrddim_set(st, "OutNoRoutes", OutNoRoutes);
				rrdset_done(st);
			}
		}
		else if(strcmp(procfile_lineword(ff, l, 0), "Tcp") == 0) {
			l++;

			if(strcmp(procfile_lineword(ff, l, 0), "Tcp") != 0) {
				error("Cannot read Tcp line from /proc/net/snmp.");
				break;
			}

			words = procfile_linewords(ff, l);
			if(words < 15) {
				error("Cannot read /proc/net/snmp Tcp line. Expected 15 params, read %d.", words);
				continue;
			}

			unsigned long long RtoAlgorithm, RtoMin, RtoMax, MaxConn, ActiveOpens, PassiveOpens, AttemptFails, EstabResets,
				CurrEstab, InSegs, OutSegs, RetransSegs, InErrs, OutRsts;

			RtoAlgorithm	= strtoull(procfile_lineword(ff, l, 1), NULL, 10);
			RtoMin			= strtoull(procfile_lineword(ff, l, 2), NULL, 10);
			RtoMax			= strtoull(procfile_lineword(ff, l, 3), NULL, 10);
			MaxConn			= strtoull(procfile_lineword(ff, l, 4), NULL, 10);
			ActiveOpens		= strtoull(procfile_lineword(ff, l, 5), NULL, 10);
			PassiveOpens	= strtoull(procfile_lineword(ff, l, 6), NULL, 10);
			AttemptFails	= strtoull(procfile_lineword(ff, l, 7), NULL, 10);
			EstabResets		= strtoull(procfile_lineword(ff, l, 8), NULL, 10);
			CurrEstab		= strtoull(procfile_lineword(ff, l, 9), NULL, 10);
			InSegs			= strtoull(procfile_lineword(ff, l, 10), NULL, 10);
			OutSegs			= strtoull(procfile_lineword(ff, l, 11), NULL, 10);
			RetransSegs		= strtoull(procfile_lineword(ff, l, 12), NULL, 10);
			InErrs			= strtoull(procfile_lineword(ff, l, 13), NULL, 10);
			OutRsts			= strtoull(procfile_lineword(ff, l, 14), NULL, 10);

			// these are not counters
			if(RtoAlgorithm) {};
			if(RtoMin) {};
			if(RtoMax) {};
			if(MaxConn) {};

			// --------------------------------------------------------------------

			// see http://net-snmp.sourceforge.net/docs/mibs/tcp.html
			if(do_tcp_sockets) {
				st = rrdset_find(RRD_TYPE_NET_SNMP ".tcpsock");
				if(!st) {
					st = rrdset_create(RRD_TYPE_NET_SNMP, "tcpsock", NULL, "tcp", NULL, "IPv4 TCP Connections", "active connections", 2500, update_every, RRDSET_TYPE_LINE);

					rrddim_add(st, "connections", NULL, 1, 1, RRDDIM_ABSOLUTE);
				}
				else rrdset_next(st);

				rrddim_set(st, "connections", CurrEstab);
				rrdset_done(st);
			}

			// --------------------------------------------------------------------

			if(do_tcp_packets) {
				st = rrdset_find(RRD_TYPE_NET_SNMP ".tcppackets");
				if(!st) {
					st = rrdset_create(RRD_TYPE_NET_SNMP, "tcppackets", NULL, "tcp", NULL, "IPv4 TCP Packets", "packets/s", 2600, update_every, RRDSET_TYPE_LINE);

					rrddim_add(st, "received", NULL, 1, 1, RRDDIM_INCREMENTAL);
					rrddim_add(st, "sent", NULL, -1, 1, RRDDIM_INCREMENTAL);
				}
				else rrdset_next(st);

				rrddim_set(st, "received", InSegs);
				rrddim_set(st, "sent", OutSegs);
				rrdset_done(st);
			}

			// --------------------------------------------------------------------

			if(do_tcp_errors) {
				st = rrdset_find(RRD_TYPE_NET_SNMP ".tcperrors");
				if(!st) {
					st = rrdset_create(RRD_TYPE_NET_SNMP, "tcperrors", NULL, "tcp", NULL, "IPv4 TCP Errors", "packets/s", 2700, update_every, RRDSET_TYPE_LINE);
					st->isdetail = 1;

					rrddim_add(st, "InErrs", NULL, 1, 1, RRDDIM_INCREMENTAL);
					rrddim_add(st, "RetransSegs", NULL, -1, 1, RRDDIM_INCREMENTAL);
				}
				else rrdset_next(st);

				rrddim_set(st, "InErrs", InErrs);
				rrddim_set(st, "RetransSegs", RetransSegs);
				rrdset_done(st);
			}

			// --------------------------------------------------------------------

			if(do_tcp_handshake) {
				st = rrdset_find(RRD_TYPE_NET_SNMP ".tcphandshake");
				if(!st) {
					st = rrdset_create(RRD_TYPE_NET_SNMP, "tcphandshake", NULL, "tcp", NULL, "IPv4 TCP Handshake Issues", "events/s", 2900, update_every, RRDSET_TYPE_LINE);
					st->isdetail = 1;

					rrddim_add(st, "EstabResets", NULL, 1, 1, RRDDIM_INCREMENTAL);
					rrddim_add(st, "OutRsts", NULL, -1, 1, RRDDIM_INCREMENTAL);
					rrddim_add(st, "ActiveOpens", NULL, 1, 1, RRDDIM_INCREMENTAL);
					rrddim_add(st, "PassiveOpens", NULL, 1, 1, RRDDIM_INCREMENTAL);
					rrddim_add(st, "AttemptFails", NULL, 1, 1, RRDDIM_INCREMENTAL);
				}
				else rrdset_next(st);

				rrddim_set(st, "EstabResets", EstabResets);
				rrddim_set(st, "OutRsts", OutRsts);
				rrddim_set(st, "ActiveOpens", ActiveOpens);
				rrddim_set(st, "PassiveOpens", PassiveOpens);
				rrddim_set(st, "AttemptFails", AttemptFails);
				rrdset_done(st);
			}
		}
		else if(strcmp(procfile_lineword(ff, l, 0), "Udp") == 0) {
			l++;

			if(strcmp(procfile_lineword(ff, l, 0), "Udp") != 0) {
				error("Cannot read Udp line from /proc/net/snmp.");
				break;
			}

			words = procfile_linewords(ff, l);
			if(words < 7) {
				error("Cannot read /proc/net/snmp Udp line. Expected 7 params, read %d.", words);
				continue;
			}

			unsigned long long InDatagrams, NoPorts, InErrors, OutDatagrams, RcvbufErrors, SndbufErrors;

			InDatagrams		= strtoull(procfile_lineword(ff, l, 1), NULL, 10);
			NoPorts			= strtoull(procfile_lineword(ff, l, 2), NULL, 10);
			InErrors		= strtoull(procfile_lineword(ff, l, 3), NULL, 10);
			OutDatagrams	= strtoull(procfile_lineword(ff, l, 4), NULL, 10);
			RcvbufErrors	= strtoull(procfile_lineword(ff, l, 5), NULL, 10);
			SndbufErrors	= strtoull(procfile_lineword(ff, l, 6), NULL, 10);

			// --------------------------------------------------------------------

			// see http://net-snmp.sourceforge.net/docs/mibs/udp.html
			if(do_udp_packets) {
				st = rrdset_find(RRD_TYPE_NET_SNMP ".udppackets");
				if(!st) {
					st = rrdset_create(RRD_TYPE_NET_SNMP, "udppackets", NULL, "udp", NULL, "IPv4 UDP Packets", "packets/s", 2601, update_every, RRDSET_TYPE_LINE);

					rrddim_add(st, "received", NULL, 1, 1, RRDDIM_INCREMENTAL);
					rrddim_add(st, "sent", NULL, -1, 1, RRDDIM_INCREMENTAL);
				}
				else rrdset_next(st);

				rrddim_set(st, "received", InDatagrams);
				rrddim_set(st, "sent", OutDatagrams);
				rrdset_done(st);
			}

			// --------------------------------------------------------------------

			if(do_udp_errors) {
				st = rrdset_find(RRD_TYPE_NET_SNMP ".udperrors");
				if(!st) {
					st = rrdset_create(RRD_TYPE_NET_SNMP, "udperrors", NULL, "udp", NULL, "IPv4 UDP Errors", "events/s", 2701, update_every, RRDSET_TYPE_LINE);
					st->isdetail = 1;

					rrddim_add(st, "RcvbufErrors", NULL, 1, 1, RRDDIM_INCREMENTAL);
					rrddim_add(st, "SndbufErrors", NULL, -1, 1, RRDDIM_INCREMENTAL);
					rrddim_add(st, "InErrors", NULL, 1, 1, RRDDIM_INCREMENTAL);
					rrddim_add(st, "NoPorts", NULL, 1, 1, RRDDIM_INCREMENTAL);
				}
				else rrdset_next(st);

				rrddim_set(st, "InErrors", InErrors);
				rrddim_set(st, "NoPorts", NoPorts);
				rrddim_set(st, "RcvbufErrors", RcvbufErrors);
				rrddim_set(st, "SndbufErrors", SndbufErrors);
				rrdset_done(st);
			}
		}
	}

	return 0;
}