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 strcpy (vl.host, hostname_g);
139 strcpy (vl.plugin, "tcpconns");
140 strcpy (vl.type, "tcp_connections");
142 if (((port_collect_listening != 0) && (pe->flags & PORT_IS_LISTENING))
143 || (pe->flags & PORT_COLLECT_LOCAL))
145 snprintf (vl.plugin_instance, sizeof (vl.plugin_instance),
146 "%hu-local", pe->port);
147 vl.plugin_instance[sizeof (vl.plugin_instance) - 1] = '\0';
149 for (i = 1; i <= TCP_STATE_MAX; i++)
151 vl.values[0].gauge = pe->count_local[i];
153 strncpy (vl.type_instance, tcp_state[i], sizeof (vl.type_instance));
154 vl.type_instance[sizeof (vl.type_instance) - 1] = '\0';
156 plugin_dispatch_values (&vl);
160 if (pe->flags & PORT_COLLECT_REMOTE)
162 snprintf (vl.plugin_instance, sizeof (vl.plugin_instance),
163 "%hu-remote", pe->port);
164 vl.plugin_instance[sizeof (vl.plugin_instance) - 1] = '\0';
166 for (i = 1; i <= TCP_STATE_MAX; i++)
168 vl.values[0].gauge = pe->count_remote[i];
170 strncpy (vl.type_instance, tcp_state[i], sizeof (vl.type_instance));
171 vl.type_instance[sizeof (vl.type_instance) - 1] = '\0';
173 plugin_dispatch_values (&vl);
176 } /* void conn_submit */
178 static void conn_submit_all (void)
182 for (pe = port_list_head; pe != NULL; pe = pe->next)
183 conn_submit_port_entry (pe);
184 } /* void conn_submit_all */
186 static port_entry_t *conn_get_port_entry (uint16_t port, int create)
190 ret = port_list_head;
193 if (ret->port == port)
198 if ((ret == NULL) && (create != 0))
200 ret = (port_entry_t *) malloc (sizeof (port_entry_t));
203 memset (ret, '\0', sizeof (port_entry_t));
206 ret->next = port_list_head;
207 port_list_head = ret;
211 } /* port_entry_t *conn_get_port_entry */
213 /* Removes ports that were added automatically due to the `ListeningPorts'
214 * setting but which are no longer listening. */
215 static void conn_reset_port_entry (void)
217 port_entry_t *prev = NULL;
218 port_entry_t *pe = port_list_head;
222 /* If this entry was created while reading the files (ant not when handling
223 * the configuration) remove it now. */
224 if ((pe->flags & (PORT_COLLECT_LOCAL
225 | PORT_COLLECT_REMOTE
226 | PORT_IS_LISTENING)) == 0)
228 port_entry_t *next = pe->next;
230 DEBUG ("tcpconns plugin: Removing temporary entry "
231 "for listening port %hu", pe->port);
234 port_list_head = next;
244 memset (pe->count_local, '\0', sizeof (pe->count_local));
245 memset (pe->count_remote, '\0', sizeof (pe->count_remote));
246 pe->flags &= ~PORT_IS_LISTENING;
250 } /* void conn_reset_port_entry */
252 static int conn_handle_ports (uint16_t port_local, uint16_t port_remote, uint8_t state)
254 port_entry_t *pe = NULL;
256 if ((state > TCP_STATE_MAX)
257 #if TCP_STATE_MIN > 0
258 || (state < TCP_STATE_MIN)
262 NOTICE ("tcpconns plugin: Ignoring connection with unknown state 0x%02x.",
267 /* Listening sockets */
268 if ((state == TCP_STATE_LISTEN) && (port_collect_listening != 0))
270 pe = conn_get_port_entry (port_local, 1 /* create */);
272 pe->flags |= PORT_IS_LISTENING;
275 DEBUG ("tcpconns plugin: Connection %hu <-> %hu (%s)",
276 port_local, port_remote, tcp_state[state]);
278 pe = conn_get_port_entry (port_local, 0 /* no create */);
280 pe->count_local[state]++;
282 pe = conn_get_port_entry (port_remote, 0 /* no create */);
284 pe->count_remote[state]++;
287 } /* int conn_handle_ports */
290 static int conn_handle_line (char *buffer)
297 char *port_local_str;
298 char *port_remote_str;
300 uint16_t port_remote;
304 int buffer_len = strlen (buffer);
306 while ((buffer_len > 0) && (buffer[buffer_len - 1] < 32))
307 buffer[--buffer_len] = '\0';
311 fields_len = strsplit (buffer, fields, STATIC_ARRAY_SIZE (fields));
314 DEBUG ("tcpconns plugin: Got %i fields, expected at least 12.", fields_len);
318 port_local_str = strchr (fields[1], ':');
319 port_remote_str = strchr (fields[2], ':');
321 if ((port_local_str == NULL) || (port_remote_str == NULL))
325 if ((*port_local_str == '\0') || (*port_remote_str == '\0'))
329 port_local = (uint16_t) strtol (port_local_str, &endptr, 16);
330 if ((endptr == NULL) || (*endptr != '\0'))
334 port_remote = (uint16_t) strtol (port_remote_str, &endptr, 16);
335 if ((endptr == NULL) || (*endptr != '\0'))
339 state = (uint8_t) strtol (fields[3], &endptr, 16);
340 if ((endptr == NULL) || (*endptr != '\0'))
343 return (conn_handle_ports (port_local, port_remote, state));
344 } /* int conn_handle_line */
346 static int conn_read_file (const char *file)
351 fh = fopen (file, "r");
355 while (fgets (buffer, sizeof (buffer), fh) != NULL)
357 conn_handle_line (buffer);
358 } /* while (fgets) */
363 } /* int conn_read_file */
364 /* #endif KERNEL_LINUX */
366 #elif HAVE_SYSCTLBYNAME
367 #endif /* HAVE_SYSCTLBYNAME */
369 static int conn_config (const char *key, const char *value)
371 if (strcasecmp (key, "ListeningPorts") == 0)
373 if ((strcasecmp (value, "Yes") == 0)
374 || (strcasecmp (value, "True") == 0)
375 || (strcasecmp (value, "On") == 0))
376 port_collect_listening = 1;
378 port_collect_listening = 0;
380 else if ((strcasecmp (key, "LocalPort") == 0)
381 || (strcasecmp (key, "RemotePort") == 0))
384 int port = atoi (value);
386 if ((port < 1) || (port > 65535))
388 ERROR ("tcpconns plugin: Invalid port: %i", port);
392 pe = conn_get_port_entry ((uint16_t) port, 1 /* create */);
395 ERROR ("tcpconns plugin: conn_get_port_entry failed.");
399 if (strcasecmp (key, "LocalPort") == 0)
400 pe->flags |= PORT_COLLECT_LOCAL;
402 pe->flags |= PORT_COLLECT_REMOTE;
410 } /* int conn_config */
413 static int conn_init (void)
415 if (port_list_head == NULL)
416 port_collect_listening = 1;
419 } /* int conn_init */
421 static int conn_read (void)
425 conn_reset_port_entry ();
427 if (conn_read_file ("/proc/net/tcp") != 0)
429 if (conn_read_file ("/proc/net/tcp6") != 0)
438 ERROR ("tcpconns plugin: Neither /proc/net/tcp nor /proc/net/tcp6 "
444 } /* int conn_read */
445 /* #endif KERNEL_LINUX */
447 #elif HAVE_SYSCTLBYNAME
448 static int conn_read (void)
454 struct xinpgen *in_orig;
455 struct xinpgen *in_ptr;
457 conn_reset_port_entry ();
460 status = sysctlbyname ("net.inet.tcp.pcblist", NULL, &buffer_len, 0, 0);
463 ERROR ("tcpconns plugin: sysctlbyname failed.");
467 buffer = (char *) malloc (buffer_len);
470 ERROR ("tcpconns plugin: malloc failed.");
474 status = sysctlbyname ("net.inet.tcp.pcblist", buffer, &buffer_len, 0, 0);
477 ERROR ("tcpconns plugin: sysctlbyname failed.");
482 if (buffer_len <= sizeof (struct xinpgen))
484 ERROR ("tcpconns plugin: (buffer_len <= sizeof (struct xinpgen))");
489 in_orig = (struct xinpgen *) buffer;
490 for (in_ptr = (struct xinpgen *) (((char *) in_orig) + in_orig->xig_len);
491 in_ptr->xig_len > sizeof (struct xinpgen);
492 in_ptr = (struct xinpgen *) (((char *) in_ptr) + in_ptr->xig_len))
494 struct tcpcb *tp = &((struct xtcpcb *) in_ptr)->xt_tp;
495 struct inpcb *inp = &((struct xtcpcb *) in_ptr)->xt_inp;
496 struct xsocket *so = &((struct xtcpcb *) in_ptr)->xt_socket;
498 /* Ignore non-TCP sockets */
499 if (so->xso_protocol != IPPROTO_TCP)
502 /* Ignore PCBs which were freed during copyout. */
503 if (inp->inp_gencnt > in_orig->xig_gen)
506 if (((inp->inp_vflag & INP_IPV4) == 0)
507 && ((inp->inp_vflag & INP_IPV6) == 0))
510 conn_handle_ports (inp->inp_lport, inp->inp_fport, tp->t_state);
520 } /* int conn_read */
521 #endif /* HAVE_SYSCTLBYNAME */
523 void module_register (void)
525 plugin_register_config ("tcpconns", conn_config,
526 config_keys, config_keys_num);
528 plugin_register_init ("tcpconns", conn_init);
530 plugin_register_read ("tcpconns", conn_read);
531 } /* void module_register */
534 * vim: set shiftwidth=2 softtabstop=2 tabstop=8 :