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>
27 # error "No applicable input method."
30 #define TCP_STATE_LISTEN 10
31 #define TCP_STATE_MAX 11
33 #define PORT_COLLECT_LOCAL 0x01
34 #define PORT_COLLECT_REMOTE 0x02
35 #define PORT_IS_LISTENING 0x04
37 typedef struct port_entry_s
41 uint32_t count_local[TCP_STATE_MAX + 1];
42 uint32_t count_remote[TCP_STATE_MAX + 1];
43 struct port_entry_s *next;
46 static const char *config_keys[] =
52 static int config_keys_num = STATIC_ARRAY_SIZE (config_keys);
54 static const char *tcp_state[] =
70 static int port_collect_listening = 0;
71 static port_entry_t *port_list_head = NULL;
73 static void conn_submit_port_entry (port_entry_t *pe)
76 value_list_t vl = VALUE_LIST_INIT;
81 vl.time = time (NULL);
82 strcpy (vl.host, hostname_g);
83 strcpy (vl.plugin, "tcpconns");
85 if (((port_collect_listening != 0) && (pe->flags & PORT_IS_LISTENING))
86 || (pe->flags & PORT_COLLECT_LOCAL))
88 snprintf (vl.plugin_instance, sizeof (vl.plugin_instance),
89 "%hu-local", pe->port);
90 vl.plugin_instance[sizeof (vl.plugin_instance) - 1] = '\0';
92 for (i = 1; i <= TCP_STATE_MAX; i++)
94 vl.values[0].gauge = pe->count_local[i];
96 strncpy (vl.type_instance, tcp_state[i], sizeof (vl.type_instance));
97 vl.type_instance[sizeof (vl.type_instance) - 1] = '\0';
99 plugin_dispatch_values ("tcp_connections", &vl);
103 if (pe->flags & PORT_COLLECT_REMOTE)
105 snprintf (vl.plugin_instance, sizeof (vl.plugin_instance),
106 "%hu-remote", pe->port);
107 vl.plugin_instance[sizeof (vl.plugin_instance) - 1] = '\0';
109 for (i = 1; i <= TCP_STATE_MAX; i++)
111 vl.values[0].gauge = pe->count_remote[i];
113 strncpy (vl.type_instance, tcp_state[i], sizeof (vl.type_instance));
114 vl.type_instance[sizeof (vl.type_instance) - 1] = '\0';
116 plugin_dispatch_values ("tcp_connections", &vl);
119 } /* void conn_submit */
121 static void conn_submit_all (void)
125 for (pe = port_list_head; pe != NULL; pe = pe->next)
126 conn_submit_port_entry (pe);
127 } /* void conn_submit_all */
129 static port_entry_t *conn_get_port_entry (uint16_t port, int create)
133 ret = port_list_head;
136 if (ret->port == port)
141 if ((ret == NULL) && (create != 0))
143 ret = (port_entry_t *) malloc (sizeof (port_entry_t));
146 memset (ret, '\0', sizeof (port_entry_t));
149 ret->next = port_list_head;
150 port_list_head = ret;
154 } /* port_entry_t *conn_get_port_entry */
156 /* Removes ports that were added automatically due to the `ListeningPorts'
157 * setting but which are no longer listening. */
158 static void conn_reset_port_entry (void)
160 port_entry_t *prev = NULL;
161 port_entry_t *pe = port_list_head;
165 /* If this entry was created while reading the files (ant not when handling
166 * the configuration) remove it now. */
167 if ((pe->flags & (PORT_COLLECT_LOCAL
168 | PORT_COLLECT_REMOTE
169 | PORT_IS_LISTENING)) == 0)
171 port_entry_t *next = pe->next;
173 DEBUG ("tcpconns plugin: Removing temporary entry "
174 "for listening port %hu", pe->port);
177 port_list_head = next;
187 memset (pe->count_local, '\0', sizeof (pe->count_local));
188 memset (pe->count_remote, '\0', sizeof (pe->count_remote));
189 pe->flags &= ~PORT_IS_LISTENING;
193 } /* void conn_reset_port_entry */
195 static int conn_handle_ports (uint16_t port_local, uint16_t port_remote, uint8_t state)
197 port_entry_t *pe = NULL;
199 if ((state == 0) || (state > TCP_STATE_MAX))
201 NOTICE ("tcpconns plugin: Ignoring connection with unknown state 0x%02x.",
206 /* Listening sockets */
207 if ((state == TCP_STATE_LISTEN) && (port_collect_listening != 0))
209 pe = conn_get_port_entry (port_local, 1 /* create */);
211 pe->flags |= PORT_IS_LISTENING;
214 DEBUG ("tcpconns plugin: Connection %hu <-> %hu (%s)",
215 port_local, port_remote, tcp_state[state]);
217 pe = conn_get_port_entry (port_local, 0 /* no create */);
219 pe->count_local[state]++;
221 pe = conn_get_port_entry (port_remote, 0 /* no create */);
223 pe->count_remote[state]++;
226 } /* int conn_handle_ports */
228 static int conn_handle_line (char *buffer)
235 char *port_local_str;
236 char *port_remote_str;
238 uint16_t port_remote;
242 int buffer_len = strlen (buffer);
244 while ((buffer_len > 0) && (buffer[buffer_len - 1] < 32))
245 buffer[--buffer_len] = '\0';
249 fields_len = strsplit (buffer, fields, STATIC_ARRAY_SIZE (fields));
252 DEBUG ("tcpconns plugin: Got %i fields, expected at least 12.", fields_len);
256 port_local_str = strchr (fields[1], ':');
257 port_remote_str = strchr (fields[2], ':');
259 if ((port_local_str == NULL) || (port_remote_str == NULL))
263 if ((*port_local_str == '\0') || (*port_remote_str == '\0'))
267 port_local = (uint16_t) strtol (port_local_str, &endptr, 16);
268 if ((endptr == NULL) || (*endptr != '\0'))
272 port_remote = (uint16_t) strtol (port_remote_str, &endptr, 16);
273 if ((endptr == NULL) || (*endptr != '\0'))
277 state = (uint8_t) strtol (fields[3], &endptr, 16);
278 if ((endptr == NULL) || (*endptr != '\0'))
281 return (conn_handle_ports (port_local, port_remote, state));
282 } /* int conn_handle_line */
284 static int conn_read_file (const char *file)
289 fh = fopen (file, "r");
293 ERROR ("tcpconns plugin: fopen (%s) failed: %s",
294 file, sstrerror (errno, errbuf, sizeof (errbuf)));
298 while (fgets (buffer, sizeof (buffer), fh) != NULL)
300 conn_handle_line (buffer);
301 } /* while (fgets) */
306 } /* int conn_read_file */
308 static int conn_config (const char *key, const char *value)
310 if (strcasecmp (key, "ListeningPorts") == 0)
312 if ((strcasecmp (value, "Yes") == 0)
313 || (strcasecmp (value, "True") == 0)
314 || (strcasecmp (value, "On") == 0))
315 port_collect_listening = 1;
317 port_collect_listening = 0;
319 else if ((strcasecmp (key, "LocalPort") == 0)
320 || (strcasecmp (key, "RemotePort") == 0))
323 int port = atoi (value);
325 if ((port < 1) || (port > 65535))
327 ERROR ("tcpconns plugin: Invalid port: %i", port);
331 pe = conn_get_port_entry ((uint16_t) port, 1 /* create */);
334 ERROR ("tcpconns plugin: conn_get_port_entry failed.");
338 if (strcasecmp (key, "LocalPort") == 0)
339 pe->flags |= PORT_COLLECT_LOCAL;
341 pe->flags |= PORT_COLLECT_REMOTE;
349 } /* int conn_config */
351 static int conn_init (void)
353 if (port_list_head == NULL)
354 port_collect_listening = 1;
357 } /* int conn_init */
359 static int conn_read (void)
361 conn_reset_port_entry ();
363 conn_read_file ("/proc/net/tcp");
364 conn_read_file ("/proc/net/tcp6");
369 } /* int conn_read */
371 void module_register (void)
373 plugin_register_config ("tcpconns", conn_config,
374 config_keys, config_keys_num);
375 plugin_register_init ("tcpconns", conn_init);
376 plugin_register_read ("tcpconns", conn_read);
377 } /* void module_register */
380 * vim: set shiftwidth=2 softtabstop=2 tabstop=8 :