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>
36 # include <net/route.h>
37 # include <netinet/in.h>
38 # include <netinet/in_systm.h>
39 # include <netinet/ip.h>
40 # include <netinet/ip6.h>
41 # include <netinet/in_pcb.h>
42 # include <netinet/ip_var.h>
43 # include <netinet/tcp.h>
44 # include <netinet/tcpip.h>
45 # include <netinet/tcp_seq.h>
46 # include <netinet/tcp_var.h>
47 #endif /* HAVE_SYSCTLBYNAME */
50 static const char *tcp_state[] =
66 # define TCP_STATE_LISTEN 10
67 # define TCP_STATE_MIN 1
68 # define TCP_STATE_MAX 11
69 /* #endif KERNEL_LINUX */
71 #elif HAVE_SYSCTLBYNAME
72 static const char *tcp_state[] =
87 # define TCP_STATE_LISTEN 1
88 # define TCP_STATE_MIN 0
89 # define TCP_STATE_MAX 10
90 #endif /* HAVE_SYSCTLBYNAME */
92 #define PORT_COLLECT_LOCAL 0x01
93 #define PORT_COLLECT_REMOTE 0x02
94 #define PORT_IS_LISTENING 0x04
96 typedef struct port_entry_s
100 uint32_t count_local[TCP_STATE_MAX + 1];
101 uint32_t count_remote[TCP_STATE_MAX + 1];
102 struct port_entry_s *next;
105 static const char *config_keys[] =
111 static int config_keys_num = STATIC_ARRAY_SIZE (config_keys);
113 static int port_collect_listening = 0;
114 static port_entry_t *port_list_head = NULL;
116 static void conn_submit_port_entry (port_entry_t *pe)
119 value_list_t vl = VALUE_LIST_INIT;
124 vl.time = time (NULL);
125 strcpy (vl.host, hostname_g);
126 strcpy (vl.plugin, "tcpconns");
128 if (((port_collect_listening != 0) && (pe->flags & PORT_IS_LISTENING))
129 || (pe->flags & PORT_COLLECT_LOCAL))
131 snprintf (vl.plugin_instance, sizeof (vl.plugin_instance),
132 "%hu-local", pe->port);
133 vl.plugin_instance[sizeof (vl.plugin_instance) - 1] = '\0';
135 for (i = 1; i <= TCP_STATE_MAX; i++)
137 vl.values[0].gauge = pe->count_local[i];
139 strncpy (vl.type_instance, tcp_state[i], sizeof (vl.type_instance));
140 vl.type_instance[sizeof (vl.type_instance) - 1] = '\0';
142 plugin_dispatch_values ("tcp_connections", &vl);
146 if (pe->flags & PORT_COLLECT_REMOTE)
148 snprintf (vl.plugin_instance, sizeof (vl.plugin_instance),
149 "%hu-remote", pe->port);
150 vl.plugin_instance[sizeof (vl.plugin_instance) - 1] = '\0';
152 for (i = 1; i <= TCP_STATE_MAX; i++)
154 vl.values[0].gauge = pe->count_remote[i];
156 strncpy (vl.type_instance, tcp_state[i], sizeof (vl.type_instance));
157 vl.type_instance[sizeof (vl.type_instance) - 1] = '\0';
159 plugin_dispatch_values ("tcp_connections", &vl);
162 } /* void conn_submit */
164 static void conn_submit_all (void)
168 for (pe = port_list_head; pe != NULL; pe = pe->next)
169 conn_submit_port_entry (pe);
170 } /* void conn_submit_all */
172 static port_entry_t *conn_get_port_entry (uint16_t port, int create)
176 ret = port_list_head;
179 if (ret->port == port)
184 if ((ret == NULL) && (create != 0))
186 ret = (port_entry_t *) malloc (sizeof (port_entry_t));
189 memset (ret, '\0', sizeof (port_entry_t));
192 ret->next = port_list_head;
193 port_list_head = ret;
197 } /* port_entry_t *conn_get_port_entry */
199 /* Removes ports that were added automatically due to the `ListeningPorts'
200 * setting but which are no longer listening. */
201 static void conn_reset_port_entry (void)
203 port_entry_t *prev = NULL;
204 port_entry_t *pe = port_list_head;
208 /* If this entry was created while reading the files (ant not when handling
209 * the configuration) remove it now. */
210 if ((pe->flags & (PORT_COLLECT_LOCAL
211 | PORT_COLLECT_REMOTE
212 | PORT_IS_LISTENING)) == 0)
214 port_entry_t *next = pe->next;
216 DEBUG ("tcpconns plugin: Removing temporary entry "
217 "for listening port %hu", pe->port);
220 port_list_head = next;
230 memset (pe->count_local, '\0', sizeof (pe->count_local));
231 memset (pe->count_remote, '\0', sizeof (pe->count_remote));
232 pe->flags &= ~PORT_IS_LISTENING;
236 } /* void conn_reset_port_entry */
238 static int conn_handle_ports (uint16_t port_local, uint16_t port_remote, uint8_t state)
240 port_entry_t *pe = NULL;
242 if ((state > TCP_STATE_MAX)
243 #if TCP_STATE_MIN > 0
244 || (state < TCP_STATE_MIN)
248 NOTICE ("tcpconns plugin: Ignoring connection with unknown state 0x%02x.",
253 /* Listening sockets */
254 if ((state == TCP_STATE_LISTEN) && (port_collect_listening != 0))
256 pe = conn_get_port_entry (port_local, 1 /* create */);
258 pe->flags |= PORT_IS_LISTENING;
261 DEBUG ("tcpconns plugin: Connection %hu <-> %hu (%s)",
262 port_local, port_remote, tcp_state[state]);
264 pe = conn_get_port_entry (port_local, 0 /* no create */);
266 pe->count_local[state]++;
268 pe = conn_get_port_entry (port_remote, 0 /* no create */);
270 pe->count_remote[state]++;
273 } /* int conn_handle_ports */
276 static int conn_handle_line (char *buffer)
283 char *port_local_str;
284 char *port_remote_str;
286 uint16_t port_remote;
290 int buffer_len = strlen (buffer);
292 while ((buffer_len > 0) && (buffer[buffer_len - 1] < 32))
293 buffer[--buffer_len] = '\0';
297 fields_len = strsplit (buffer, fields, STATIC_ARRAY_SIZE (fields));
300 DEBUG ("tcpconns plugin: Got %i fields, expected at least 12.", fields_len);
304 port_local_str = strchr (fields[1], ':');
305 port_remote_str = strchr (fields[2], ':');
307 if ((port_local_str == NULL) || (port_remote_str == NULL))
311 if ((*port_local_str == '\0') || (*port_remote_str == '\0'))
315 port_local = (uint16_t) strtol (port_local_str, &endptr, 16);
316 if ((endptr == NULL) || (*endptr != '\0'))
320 port_remote = (uint16_t) strtol (port_remote_str, &endptr, 16);
321 if ((endptr == NULL) || (*endptr != '\0'))
325 state = (uint8_t) strtol (fields[3], &endptr, 16);
326 if ((endptr == NULL) || (*endptr != '\0'))
329 return (conn_handle_ports (port_local, port_remote, state));
330 } /* int conn_handle_line */
332 static int conn_read_file (const char *file)
337 fh = fopen (file, "r");
341 while (fgets (buffer, sizeof (buffer), fh) != NULL)
343 conn_handle_line (buffer);
344 } /* while (fgets) */
349 } /* int conn_read_file */
350 /* #endif KERNEL_LINUX */
352 #elif HAVE_SYSCTLBYNAME
353 #endif /* HAVE_SYSCTLBYNAME */
355 static int conn_config (const char *key, const char *value)
357 if (strcasecmp (key, "ListeningPorts") == 0)
359 if ((strcasecmp (value, "Yes") == 0)
360 || (strcasecmp (value, "True") == 0)
361 || (strcasecmp (value, "On") == 0))
362 port_collect_listening = 1;
364 port_collect_listening = 0;
366 else if ((strcasecmp (key, "LocalPort") == 0)
367 || (strcasecmp (key, "RemotePort") == 0))
370 int port = atoi (value);
372 if ((port < 1) || (port > 65535))
374 ERROR ("tcpconns plugin: Invalid port: %i", port);
378 pe = conn_get_port_entry ((uint16_t) port, 1 /* create */);
381 ERROR ("tcpconns plugin: conn_get_port_entry failed.");
385 if (strcasecmp (key, "LocalPort") == 0)
386 pe->flags |= PORT_COLLECT_LOCAL;
388 pe->flags |= PORT_COLLECT_REMOTE;
396 } /* int conn_config */
399 static int conn_init (void)
401 if (port_list_head == NULL)
402 port_collect_listening = 1;
405 } /* int conn_init */
407 static int conn_read (void)
411 conn_reset_port_entry ();
413 if (conn_read_file ("/proc/net/tcp") != 0)
415 if (conn_read_file ("/proc/net/tcp6") != 0)
424 ERROR ("tcpconns plugin: Neither /proc/net/tcp nor /proc/net/tcp6 "
430 } /* int conn_read */
431 /* #endif KERNEL_LINUX */
433 #elif HAVE_SYSCTLBYNAME
434 static int conn_read (void)
440 struct xinpgen *in_orig;
441 struct xinpgen *in_ptr;
443 conn_reset_port_entry ();
446 status = sysctlbyname ("net.inet.tcp.pcblist", NULL, &buffer_len, 0, 0);
449 ERROR ("tcpconns plugin: sysctlbyname failed.");
453 buffer = (char *) malloc (buffer_len);
456 ERROR ("tcpconns plugin: malloc failed.");
460 status = sysctlbyname ("net.inet.tcp.pcblist", buffer, &buffer_len, 0, 0);
463 ERROR ("tcpconns plugin: sysctlbyname failed.");
468 if (buffer_len <= sizeof (struct xinpgen))
470 ERROR ("tcpconns plugin: (buffer_len <= sizeof (struct xinpgen))");
475 in_orig = (struct xinpgen *) buffer;
476 for (in_ptr = (struct xinpgen *) (((char *) in_orig) + in_orig->xig_len);
477 in_ptr->xig_len > sizeof (struct xinpgen);
478 in_ptr = (struct xinpgen *) (((char *) in_ptr) + in_ptr->xig_len))
480 struct tcpcb *tp = &((struct xtcpcb *) in_ptr)->xt_tp;
481 struct inpcb *inp = &((struct xtcpcb *) in_ptr)->xt_inp;
482 struct xsocket *so = &((struct xtcpcb *) in_ptr)->xt_socket;
484 /* Ignore non-TCP sockets */
485 if (so->xso_protocol != IPPROTO_TCP)
488 /* Ignore PCBs which were freed during copyout. */
489 if (inp->inp_gencnt > in_orig->xig_gen)
492 if (((inp->inp_vflag & INP_IPV4) == 0)
493 && ((inp->inp_vflag & INP_IPV6) == 0))
496 conn_handle_ports (inp->inp_lport, inp->inp_fport, tp->t_state);
506 } /* int conn_read */
507 #endif /* HAVE_SYSCTLBYNAME */
509 void module_register (void)
511 plugin_register_config ("tcpconns", conn_config,
512 config_keys, config_keys_num);
514 plugin_register_init ("tcpconns", conn_init);
516 plugin_register_read ("tcpconns", conn_read);
517 } /* void module_register */
520 * vim: set shiftwidth=2 softtabstop=2 tabstop=8 :