Debian Codegraph @

src/netstat_udp.c

raw
1 /**
2 * collectd - src/netstat-udp.c
3 * Copyright (C) 2015 Håvard Eidnes
4 *
5 * This program is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License as published by the
7 * Free Software Foundation; only version 2 of the License is applicable.
8 *
9 * This program is distributed in the hope that it will be useful, but
10 * WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License along
15 * with this program; if not, write to the Free Software Foundation, Inc.,
16 * 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
17 *
18 * Authors:
19 * Håvard Eidnes <he at NetBSD.org>
20 **/
21
22 #include "collectd.h"
23 #include "plugin.h"
24 #include "utils/common/common.h"
25
26 #if !defined(KERNEL_NETBSD)
27 #error "No applicable input method."
28 #endif
29
30 #include <sys/cdefs.h>
31 #include <sys/sysctl.h>
32 #include <sys/types.h>
33
34 #include <netinet/in.h>
35 #include <netinet/ip_var.h>
36 #include <netinet/udp.h>
37 #include <netinet/udp_var.h>
38 #include <netinet6/udp6_var.h>
39
40 static int netstat_udp_init(void) { return (0); } /* int netstat_udp_init */
41
42 #define SUBMIT_VARS(...) \
43 plugin_dispatch_multivalue(vl, 0, DS_TYPE_DERIVE, __VA_ARGS__, NULL)
44
45 static int netstat_udp_internal(value_list_t *vl) {
46 uint64_t udpstat[UDP_NSTATS];
47 uint64_t udp6stat[UDP6_NSTATS];
48 size_t size;
49 uint64_t delivered, delivered6;
50 int err;
51
52 size = sizeof(udpstat);
53 if (sysctlbyname("net.inet.udp.stats", udpstat, &size, NULL, 0) == -1) {
54 ERROR("netstat-udp plugin: could not get udp stats");
55 return -1;
56 }
57
58 delivered = udpstat[UDP_STAT_IPACKETS] - udpstat[UDP_STAT_HDROPS] -
59 udpstat[UDP_STAT_BADLEN] - udpstat[UDP_STAT_BADSUM] -
60 udpstat[UDP_STAT_NOPORT] - udpstat[UDP_STAT_NOPORTBCAST] -
61 udpstat[UDP_STAT_FULLSOCK];
62
63 err = SUBMIT_VARS("udp-received", (derive_t)udpstat[UDP_STAT_IPACKETS],
64 "udp-bad-header", (derive_t)udpstat[UDP_STAT_HDROPS],
65 "udp-bad-length", (derive_t)udpstat[UDP_STAT_BADLEN],
66 "udp-bad-checksum", (derive_t)udpstat[UDP_STAT_BADSUM],
67 "udp-no-port", (derive_t)udpstat[UDP_STAT_NOPORT],
68 "udp-no-port-broadcast",
69 (derive_t)udpstat[UDP_STAT_NOPORTBCAST], "udp-full-socket",
70 (derive_t)udpstat[UDP_STAT_FULLSOCK], "udp-delivered",
71 (derive_t)delivered);
72 if (err != 0) {
73 ERROR("netstat-udp plugin: could not submit, err=%d\n", err);
74 }
75
76 size = sizeof(udp6stat);
77 if (sysctlbyname("net.inet6.udp6.stats", udp6stat, &size, NULL, 0) == -1) {
78 ERROR("netstat-udp plugin: could not get udp6 stats");
79 return -1;
80 }
81
82 delivered6 = udp6stat[UDP6_STAT_IPACKETS] - udp6stat[UDP6_STAT_HDROPS] -
83 udp6stat[UDP6_STAT_BADLEN] - udp6stat[UDP6_STAT_BADSUM] -
84 udp6stat[UDP6_STAT_NOSUM] - udp6stat[UDP6_STAT_NOPORT] -
85 udp6stat[UDP6_STAT_NOPORTMCAST] - udp6stat[UDP6_STAT_FULLSOCK];
86
87 err = SUBMIT_VARS("udp6-received", (derive_t)udp6stat[UDP6_STAT_IPACKETS],
88 "udp6-bad-header", (derive_t)udp6stat[UDP6_STAT_HDROPS],
89 "udp6-bad-length", (derive_t)udp6stat[UDP6_STAT_BADLEN],
90 "udp6-bad-checksum", (derive_t)udp6stat[UDP6_STAT_BADSUM],
91 "udp6-no-checksum", (derive_t)udp6stat[UDP6_STAT_NOSUM],
92 "udp6-no-port", (derive_t)udp6stat[UDP6_STAT_NOPORT],
93 "udp6-no-port-multicast",
94 (derive_t)udp6stat[UDP6_STAT_NOPORTMCAST],
95 "udp6-full-socket", (derive_t)udp6stat[UDP6_STAT_FULLSOCK],
96 "udp6-delivered", (derive_t)delivered6);
97 if (err != 0) {
98 ERROR("netstat-udp plugin ipv6: could not submit, err=%d\n", err);
99 }
100
101 return (0);
102 } /* }}} int netstat_udp_internal */
103
104 static int netstat_udp_read(void) /* {{{ */
105 {
106 value_t v[1];
107 value_list_t vl = VALUE_LIST_INIT;
108
109 vl.values = v;
110 vl.values_len = STATIC_ARRAY_SIZE(v);
111 sstrncpy(vl.host, hostname_g, sizeof(vl.host));
112 sstrncpy(vl.plugin, "netstat_udp", sizeof(vl.plugin));
113 sstrncpy(vl.type, "packets", sizeof(vl.type));
114 vl.time = cdtime();
115
116 return (netstat_udp_internal(&vl));
117 } /* }}} int netstat_udp_read */
118
119 void module_register(void) {
120 plugin_register_init("netstat_udp", netstat_udp_init);
121 plugin_register_read("netstat_udp", netstat_udp_read);
122 } /* void module_register */