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));
140 sstrncpy (vl.type, "tcp_connections", sizeof (vl.type));
142 if (((port_collect_listening != 0) && (pe->flags & PORT_IS_LISTENING))
143 || (pe->flags & PORT_COLLECT_LOCAL))
145 ssnprintf (vl.plugin_instance, sizeof (vl.plugin_instance),
146 "%hu-local", pe->port);
148 for (i = 1; i <= TCP_STATE_MAX; i++)
150 vl.values[0].gauge = pe->count_local[i];
152 sstrncpy (vl.type_instance, tcp_state[i], sizeof (vl.type_instance));
154 plugin_dispatch_values (&vl);
158 if (pe->flags & PORT_COLLECT_REMOTE)
160 ssnprintf (vl.plugin_instance, sizeof (vl.plugin_instance),
161 "%hu-remote", pe->port);
163 for (i = 1; i <= TCP_STATE_MAX; i++)
165 vl.values[0].gauge = pe->count_remote[i];
167 sstrncpy (vl.type_instance, tcp_state[i], sizeof (vl.type_instance));
169 plugin_dispatch_values (&vl);
172 } /* void conn_submit */
174 static void conn_submit_all (void)
178 for (pe = port_list_head; pe != NULL; pe = pe->next)
179 conn_submit_port_entry (pe);
180 } /* void conn_submit_all */
182 static port_entry_t *conn_get_port_entry (uint16_t port, int create)
186 ret = port_list_head;
189 if (ret->port == port)
194 if ((ret == NULL) && (create != 0))
196 ret = (port_entry_t *) malloc (sizeof (port_entry_t));
199 memset (ret, '\0', sizeof (port_entry_t));
202 ret->next = port_list_head;
203 port_list_head = ret;
207 } /* port_entry_t *conn_get_port_entry */
209 /* Removes ports that were added automatically due to the `ListeningPorts'
210 * setting but which are no longer listening. */
211 static void conn_reset_port_entry (void)
213 port_entry_t *prev = NULL;
214 port_entry_t *pe = port_list_head;
218 /* If this entry was created while reading the files (ant not when handling
219 * the configuration) remove it now. */
220 if ((pe->flags & (PORT_COLLECT_LOCAL
221 | PORT_COLLECT_REMOTE
222 | PORT_IS_LISTENING)) == 0)
224 port_entry_t *next = pe->next;
226 DEBUG ("tcpconns plugin: Removing temporary entry "
227 "for listening port %hu", pe->port);
230 port_list_head = next;
240 memset (pe->count_local, '\0', sizeof (pe->count_local));
241 memset (pe->count_remote, '\0', sizeof (pe->count_remote));
242 pe->flags &= ~PORT_IS_LISTENING;
246 } /* void conn_reset_port_entry */
248 static int conn_handle_ports (uint16_t port_local, uint16_t port_remote, uint8_t state)
250 port_entry_t *pe = NULL;
252 if ((state > TCP_STATE_MAX)
253 #if TCP_STATE_MIN > 0
254 || (state < TCP_STATE_MIN)
258 NOTICE ("tcpconns plugin: Ignoring connection with unknown state 0x%02x.",
263 /* Listening sockets */
264 if ((state == TCP_STATE_LISTEN) && (port_collect_listening != 0))
266 pe = conn_get_port_entry (port_local, 1 /* create */);
268 pe->flags |= PORT_IS_LISTENING;
271 DEBUG ("tcpconns plugin: Connection %hu <-> %hu (%s)",
272 port_local, port_remote, tcp_state[state]);
274 pe = conn_get_port_entry (port_local, 0 /* no create */);
276 pe->count_local[state]++;
278 pe = conn_get_port_entry (port_remote, 0 /* no create */);
280 pe->count_remote[state]++;
283 } /* int conn_handle_ports */
286 static int conn_handle_line (char *buffer)
293 char *port_local_str;
294 char *port_remote_str;
296 uint16_t port_remote;
300 int buffer_len = strlen (buffer);
302 while ((buffer_len > 0) && (buffer[buffer_len - 1] < 32))
303 buffer[--buffer_len] = '\0';
307 fields_len = strsplit (buffer, fields, STATIC_ARRAY_SIZE (fields));
310 DEBUG ("tcpconns plugin: Got %i fields, expected at least 12.", fields_len);
314 port_local_str = strchr (fields[1], ':');
315 port_remote_str = strchr (fields[2], ':');
317 if ((port_local_str == NULL) || (port_remote_str == NULL))
321 if ((*port_local_str == '\0') || (*port_remote_str == '\0'))
325 port_local = (uint16_t) strtol (port_local_str, &endptr, 16);
326 if ((endptr == NULL) || (*endptr != '\0'))
330 port_remote = (uint16_t) strtol (port_remote_str, &endptr, 16);
331 if ((endptr == NULL) || (*endptr != '\0'))
335 state = (uint8_t) strtol (fields[3], &endptr, 16);
336 if ((endptr == NULL) || (*endptr != '\0'))
339 return (conn_handle_ports (port_local, port_remote, state));
340 } /* int conn_handle_line */
342 static int conn_read_file (const char *file)
347 fh = fopen (file, "r");
351 while (fgets (buffer, sizeof (buffer), fh) != NULL)
353 conn_handle_line (buffer);
354 } /* while (fgets) */
359 } /* int conn_read_file */
360 /* #endif KERNEL_LINUX */
362 #elif HAVE_SYSCTLBYNAME
363 #endif /* HAVE_SYSCTLBYNAME */
365 static int conn_config (const char *key, const char *value)
367 if (strcasecmp (key, "ListeningPorts") == 0)
369 if ((strcasecmp (value, "Yes") == 0)
370 || (strcasecmp (value, "True") == 0)
371 || (strcasecmp (value, "On") == 0))
372 port_collect_listening = 1;
374 port_collect_listening = 0;
376 else if ((strcasecmp (key, "LocalPort") == 0)
377 || (strcasecmp (key, "RemotePort") == 0))
380 int port = atoi (value);
382 if ((port < 1) || (port > 65535))
384 ERROR ("tcpconns plugin: Invalid port: %i", port);
388 pe = conn_get_port_entry ((uint16_t) port, 1 /* create */);
391 ERROR ("tcpconns plugin: conn_get_port_entry failed.");
395 if (strcasecmp (key, "LocalPort") == 0)
396 pe->flags |= PORT_COLLECT_LOCAL;
398 pe->flags |= PORT_COLLECT_REMOTE;
406 } /* int conn_config */
409 static int conn_init (void)
411 if (port_list_head == NULL)
412 port_collect_listening = 1;
415 } /* int conn_init */
417 static int conn_read (void)
421 conn_reset_port_entry ();
423 if (conn_read_file ("/proc/net/tcp") != 0)
425 if (conn_read_file ("/proc/net/tcp6") != 0)
434 ERROR ("tcpconns plugin: Neither /proc/net/tcp nor /proc/net/tcp6 "
440 } /* int conn_read */
441 /* #endif KERNEL_LINUX */
443 #elif HAVE_SYSCTLBYNAME
444 static int conn_read (void)
450 struct xinpgen *in_orig;
451 struct xinpgen *in_ptr;
453 conn_reset_port_entry ();
456 status = sysctlbyname ("net.inet.tcp.pcblist", NULL, &buffer_len, 0, 0);
459 ERROR ("tcpconns plugin: sysctlbyname failed.");
463 buffer = (char *) malloc (buffer_len);
466 ERROR ("tcpconns plugin: malloc failed.");
470 status = sysctlbyname ("net.inet.tcp.pcblist", buffer, &buffer_len, 0, 0);
473 ERROR ("tcpconns plugin: sysctlbyname failed.");
478 if (buffer_len <= sizeof (struct xinpgen))
480 ERROR ("tcpconns plugin: (buffer_len <= sizeof (struct xinpgen))");
485 in_orig = (struct xinpgen *) buffer;
486 for (in_ptr = (struct xinpgen *) (((char *) in_orig) + in_orig->xig_len);
487 in_ptr->xig_len > sizeof (struct xinpgen);
488 in_ptr = (struct xinpgen *) (((char *) in_ptr) + in_ptr->xig_len))
490 struct tcpcb *tp = &((struct xtcpcb *) in_ptr)->xt_tp;
491 struct inpcb *inp = &((struct xtcpcb *) in_ptr)->xt_inp;
492 struct xsocket *so = &((struct xtcpcb *) in_ptr)->xt_socket;
494 /* Ignore non-TCP sockets */
495 if (so->xso_protocol != IPPROTO_TCP)
498 /* Ignore PCBs which were freed during copyout. */
499 if (inp->inp_gencnt > in_orig->xig_gen)
502 if (((inp->inp_vflag & INP_IPV4) == 0)
503 && ((inp->inp_vflag & INP_IPV6) == 0))
506 conn_handle_ports (inp->inp_lport, inp->inp_fport, tp->t_state);
516 } /* int conn_read */
517 #endif /* HAVE_SYSCTLBYNAME */
519 void module_register (void)
521 plugin_register_config ("tcpconns", conn_config,
522 config_keys, config_keys_num);
524 plugin_register_init ("tcpconns", conn_init);
526 plugin_register_read ("tcpconns", conn_read);
527 } /* void module_register */
530 * vim: set shiftwidth=2 softtabstop=2 tabstop=8 :