2 * collectd - src/tcpconns.c
3 * Copyright (C) 2007 Florian octo Forster
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.
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.
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
19 * Florian octo Forster <octo at verplant.org>
26 #if !KERNEL_LINUX && !HAVE_SYSCTLBYNAME
27 # error "No applicable input method."
31 /* #endif KERNEL_LINUX */
33 #elif HAVE_SYSCTLBYNAME
34 # include <sys/socketvar.h>
35 # include <sys/sysctl.h>
37 /* Some includes needed for compiling on FreeBSD */
40 # include <sys/types.h>
43 # include <sys/socket.h>
49 # include <net/route.h>
50 # include <netinet/in.h>
51 # include <netinet/in_systm.h>
52 # include <netinet/ip.h>
53 # include <netinet/ip6.h>
54 # include <netinet/in_pcb.h>
55 # include <netinet/ip_var.h>
56 # include <netinet/tcp.h>
57 # include <netinet/tcpip.h>
58 # include <netinet/tcp_seq.h>
59 # include <netinet/tcp_var.h>
60 #endif /* HAVE_SYSCTLBYNAME */
63 static const char *tcp_state[] =
79 # define TCP_STATE_LISTEN 10
80 # define TCP_STATE_MIN 1
81 # define TCP_STATE_MAX 11
82 /* #endif KERNEL_LINUX */
84 #elif HAVE_SYSCTLBYNAME
85 static const char *tcp_state[] =
100 # define TCP_STATE_LISTEN 1
101 # define TCP_STATE_MIN 0
102 # define TCP_STATE_MAX 10
103 #endif /* HAVE_SYSCTLBYNAME */
105 #define PORT_COLLECT_LOCAL 0x01
106 #define PORT_COLLECT_REMOTE 0x02
107 #define PORT_IS_LISTENING 0x04
109 typedef struct port_entry_s
113 uint32_t count_local[TCP_STATE_MAX + 1];
114 uint32_t count_remote[TCP_STATE_MAX + 1];
115 struct port_entry_s *next;
118 static const char *config_keys[] =
124 static int config_keys_num = STATIC_ARRAY_SIZE (config_keys);
126 static int port_collect_listening = 0;
127 static port_entry_t *port_list_head = NULL;
129 static void conn_submit_port_entry (port_entry_t *pe)
132 value_list_t vl = VALUE_LIST_INIT;
137 vl.time = time (NULL);
138 sstrncpy (vl.host, hostname_g, sizeof (vl.host));
139 sstrncpy (vl.plugin, "tcpconns", sizeof (vl.plugin));
141 if (((port_collect_listening != 0) && (pe->flags & PORT_IS_LISTENING))
142 || (pe->flags & PORT_COLLECT_LOCAL))
144 snprintf (vl.plugin_instance, sizeof (vl.plugin_instance),
145 "%hu-local", pe->port);
146 vl.plugin_instance[sizeof (vl.plugin_instance) - 1] = '\0';
148 for (i = 1; i <= TCP_STATE_MAX; i++)
150 vl.values[0].gauge = pe->count_local[i];
152 strncpy (vl.type_instance, tcp_state[i], sizeof (vl.type_instance));
153 vl.type_instance[sizeof (vl.type_instance) - 1] = '\0';
155 plugin_dispatch_values ("tcp_connections", &vl);
159 if (pe->flags & PORT_COLLECT_REMOTE)
161 snprintf (vl.plugin_instance, sizeof (vl.plugin_instance),
162 "%hu-remote", pe->port);
163 vl.plugin_instance[sizeof (vl.plugin_instance) - 1] = '\0';
165 for (i = 1; i <= TCP_STATE_MAX; i++)
167 vl.values[0].gauge = pe->count_remote[i];
169 strncpy (vl.type_instance, tcp_state[i], sizeof (vl.type_instance));
170 vl.type_instance[sizeof (vl.type_instance) - 1] = '\0';
172 plugin_dispatch_values ("tcp_connections", &vl);
175 } /* void conn_submit */
177 static void conn_submit_all (void)
181 for (pe = port_list_head; pe != NULL; pe = pe->next)
182 conn_submit_port_entry (pe);
183 } /* void conn_submit_all */
185 static port_entry_t *conn_get_port_entry (uint16_t port, int create)
189 ret = port_list_head;
192 if (ret->port == port)
197 if ((ret == NULL) && (create != 0))
199 ret = (port_entry_t *) malloc (sizeof (port_entry_t));
202 memset (ret, '\0', sizeof (port_entry_t));
205 ret->next = port_list_head;
206 port_list_head = ret;
210 } /* port_entry_t *conn_get_port_entry */
212 /* Removes ports that were added automatically due to the `ListeningPorts'
213 * setting but which are no longer listening. */
214 static void conn_reset_port_entry (void)
216 port_entry_t *prev = NULL;
217 port_entry_t *pe = port_list_head;
221 /* If this entry was created while reading the files (ant not when handling
222 * the configuration) remove it now. */
223 if ((pe->flags & (PORT_COLLECT_LOCAL
224 | PORT_COLLECT_REMOTE
225 | PORT_IS_LISTENING)) == 0)
227 port_entry_t *next = pe->next;
229 DEBUG ("tcpconns plugin: Removing temporary entry "
230 "for listening port %hu", pe->port);
233 port_list_head = next;
243 memset (pe->count_local, '\0', sizeof (pe->count_local));
244 memset (pe->count_remote, '\0', sizeof (pe->count_remote));
245 pe->flags &= ~PORT_IS_LISTENING;
249 } /* void conn_reset_port_entry */
251 static int conn_handle_ports (uint16_t port_local, uint16_t port_remote, uint8_t state)
253 port_entry_t *pe = NULL;
255 if ((state > TCP_STATE_MAX)
256 #if TCP_STATE_MIN > 0
257 || (state < TCP_STATE_MIN)
261 NOTICE ("tcpconns plugin: Ignoring connection with unknown state 0x%02x.",
266 /* Listening sockets */
267 if ((state == TCP_STATE_LISTEN) && (port_collect_listening != 0))
269 pe = conn_get_port_entry (port_local, 1 /* create */);
271 pe->flags |= PORT_IS_LISTENING;
274 DEBUG ("tcpconns plugin: Connection %hu <-> %hu (%s)",
275 port_local, port_remote, tcp_state[state]);
277 pe = conn_get_port_entry (port_local, 0 /* no create */);
279 pe->count_local[state]++;
281 pe = conn_get_port_entry (port_remote, 0 /* no create */);
283 pe->count_remote[state]++;
286 } /* int conn_handle_ports */
289 static int conn_handle_line (char *buffer)
296 char *port_local_str;
297 char *port_remote_str;
299 uint16_t port_remote;
303 int buffer_len = strlen (buffer);
305 while ((buffer_len > 0) && (buffer[buffer_len - 1] < 32))
306 buffer[--buffer_len] = '\0';
310 fields_len = strsplit (buffer, fields, STATIC_ARRAY_SIZE (fields));
313 DEBUG ("tcpconns plugin: Got %i fields, expected at least 12.", fields_len);
317 port_local_str = strchr (fields[1], ':');
318 port_remote_str = strchr (fields[2], ':');
320 if ((port_local_str == NULL) || (port_remote_str == NULL))
324 if ((*port_local_str == '\0') || (*port_remote_str == '\0'))
328 port_local = (uint16_t) strtol (port_local_str, &endptr, 16);
329 if ((endptr == NULL) || (*endptr != '\0'))
333 port_remote = (uint16_t) strtol (port_remote_str, &endptr, 16);
334 if ((endptr == NULL) || (*endptr != '\0'))
338 state = (uint8_t) strtol (fields[3], &endptr, 16);
339 if ((endptr == NULL) || (*endptr != '\0'))
342 return (conn_handle_ports (port_local, port_remote, state));
343 } /* int conn_handle_line */
345 static int conn_read_file (const char *file)
350 fh = fopen (file, "r");
354 while (fgets (buffer, sizeof (buffer), fh) != NULL)
356 conn_handle_line (buffer);
357 } /* while (fgets) */
362 } /* int conn_read_file */
363 /* #endif KERNEL_LINUX */
365 #elif HAVE_SYSCTLBYNAME
366 #endif /* HAVE_SYSCTLBYNAME */
368 static int conn_config (const char *key, const char *value)
370 if (strcasecmp (key, "ListeningPorts") == 0)
372 if ((strcasecmp (value, "Yes") == 0)
373 || (strcasecmp (value, "True") == 0)
374 || (strcasecmp (value, "On") == 0))
375 port_collect_listening = 1;
377 port_collect_listening = 0;
379 else if ((strcasecmp (key, "LocalPort") == 0)
380 || (strcasecmp (key, "RemotePort") == 0))
383 int port = atoi (value);
385 if ((port < 1) || (port > 65535))
387 ERROR ("tcpconns plugin: Invalid port: %i", port);
391 pe = conn_get_port_entry ((uint16_t) port, 1 /* create */);
394 ERROR ("tcpconns plugin: conn_get_port_entry failed.");
398 if (strcasecmp (key, "LocalPort") == 0)
399 pe->flags |= PORT_COLLECT_LOCAL;
401 pe->flags |= PORT_COLLECT_REMOTE;
409 } /* int conn_config */
412 static int conn_init (void)
414 if (port_list_head == NULL)
415 port_collect_listening = 1;
418 } /* int conn_init */
420 static int conn_read (void)
424 conn_reset_port_entry ();
426 if (conn_read_file ("/proc/net/tcp") != 0)
428 if (conn_read_file ("/proc/net/tcp6") != 0)
437 ERROR ("tcpconns plugin: Neither /proc/net/tcp nor /proc/net/tcp6 "
443 } /* int conn_read */
444 /* #endif KERNEL_LINUX */
446 #elif HAVE_SYSCTLBYNAME
447 static int conn_read (void)
453 struct xinpgen *in_orig;
454 struct xinpgen *in_ptr;
456 conn_reset_port_entry ();
459 status = sysctlbyname ("net.inet.tcp.pcblist", NULL, &buffer_len, 0, 0);
462 ERROR ("tcpconns plugin: sysctlbyname failed.");
466 buffer = (char *) malloc (buffer_len);
469 ERROR ("tcpconns plugin: malloc failed.");
473 status = sysctlbyname ("net.inet.tcp.pcblist", buffer, &buffer_len, 0, 0);
476 ERROR ("tcpconns plugin: sysctlbyname failed.");
481 if (buffer_len <= sizeof (struct xinpgen))
483 ERROR ("tcpconns plugin: (buffer_len <= sizeof (struct xinpgen))");
488 in_orig = (struct xinpgen *) buffer;
489 for (in_ptr = (struct xinpgen *) (((char *) in_orig) + in_orig->xig_len);
490 in_ptr->xig_len > sizeof (struct xinpgen);
491 in_ptr = (struct xinpgen *) (((char *) in_ptr) + in_ptr->xig_len))
493 struct tcpcb *tp = &((struct xtcpcb *) in_ptr)->xt_tp;
494 struct inpcb *inp = &((struct xtcpcb *) in_ptr)->xt_inp;
495 struct xsocket *so = &((struct xtcpcb *) in_ptr)->xt_socket;
497 /* Ignore non-TCP sockets */
498 if (so->xso_protocol != IPPROTO_TCP)
501 /* Ignore PCBs which were freed during copyout. */
502 if (inp->inp_gencnt > in_orig->xig_gen)
505 if (((inp->inp_vflag & INP_IPV4) == 0)
506 && ((inp->inp_vflag & INP_IPV6) == 0))
509 conn_handle_ports (inp->inp_lport, inp->inp_fport, tp->t_state);
519 } /* int conn_read */
520 #endif /* HAVE_SYSCTLBYNAME */
522 void module_register (void)
524 plugin_register_config ("tcpconns", conn_config,
525 config_keys, config_keys_num);
527 plugin_register_init ("tcpconns", conn_init);
529 plugin_register_read ("tcpconns", conn_read);
530 } /* void module_register */
533 * vim: set shiftwidth=2 softtabstop=2 tabstop=8 :