summary | shortlog | log | commit | commitdiff | tree
raw | patch | inline | side by side (parent: 44b7ae5)
raw | patch | inline | side by side (parent: 44b7ae5)
author | Florian Forster <octo@noris.net> | |
Tue, 9 Oct 2007 12:40:33 +0000 (14:40 +0200) | ||
committer | Florian Forster <octo@noris.net> | |
Tue, 9 Oct 2007 12:40:33 +0000 (14:40 +0200) |
README | patch | blob | history | |
configure.in | patch | blob | history | |
src/Makefile.am | patch | blob | history | |
src/collectd.conf.pod | patch | blob | history | |
src/tcpconns.c | [new file with mode: 0644] | patch | blob |
src/types.db | patch | blob | history |
index 57ae4550a0b1d1de25eb11e033671e6b764b4989..d87c960f69660f2d26ee367541c5246304396887 100644 (file)
--- a/README
+++ b/README
- tape
Bytes and operations read and written on tape devices. Solaris only.
+ - tcpconns
+ Number of TCP connections to specific local and remote ports.
+
- users
Users currently logged in.
diff --git a/configure.in b/configure.in
index d904920d5c5501ebbe2c41aaa079e34bcbc40e89..b09687377a92f036a0c1f24b27b9dc41770f3527 100644 (file)
--- a/configure.in
+++ b/configure.in
plugin_serial="no"
plugin_swap="no"
plugin_tape="no"
+plugin_tcpconns="no"
plugin_users="no"
plugin_vserver="no"
plugin_wireless="no"
plugin_processes="yes"
plugin_serial="yes"
plugin_swap="yes"
+ plugin_tcpconns="yes"
plugin_vserver="yes"
plugin_wireless="yes"
fi
AC_PLUGIN([swap], [$plugin_swap], [Swap usage statistics])
AC_PLUGIN([syslog], [$have_syslog], [Syslog logging plugin])
AC_PLUGIN([tape], [$plugin_tape], [Tape drive statistics])
+AC_PLUGIN([tcpconns], [$plugin_tcpconns], [TCP connection statistics])
AC_PLUGIN([unixsock], [yes], [Unixsock communication plugin])
AC_PLUGIN([users], [$plugin_users], [User statistics])
AC_PLUGIN([vserver], [$plugin_vserver], [Linux VServer statistics])
swap . . . . . . . $enable_swap
syslog . . . . . . $enable_syslog
tape . . . . . . . $enable_tape
+ tcpconns . . . . . $enable_tcpconns
unixsock . . . . . $enable_unixsock
users . . . . . . . $enable_users
vserver . . . . . . $enable_vserver
diff --git a/src/Makefile.am b/src/Makefile.am
index 9818580462860123c3640a65ce81789858f3e683..e5578ade483860e682f35a65733b1da39fdc6031 100644 (file)
--- a/src/Makefile.am
+++ b/src/Makefile.am
collectd_DEPENDENCIES += tape.la
endif
+if BUILD_PLUGIN_TCPCONNS
+pkglib_LTLIBRARIES += tcpconns.la
+tcpconns_la_SOURCES = tcpconns.c
+tcpconns_la_LDFLAGS = -module -avoid-version
+collectd_LDADD += "-dlopen" tcpconns.la
+collectd_DEPENDENCIES += tcpconns.la
+endif
+
if BUILD_PLUGIN_UNIXSOCK
pkglib_LTLIBRARIES += unixsock.la
unixsock_la_SOURCES = unixsock.c utils_cmd_putval.h utils_cmd_putval.c
diff --git a/src/collectd.conf.pod b/src/collectd.conf.pod
index 4dcbcd1bb9bf4049e5066c9470dade81c94c9766..b6503bb23642dde3287a83a2eac8fdb943b6ed6a 100644 (file)
--- a/src/collectd.conf.pod
+++ b/src/collectd.conf.pod
=back
+=head2 Plugin C<tcpconns>
+
+The C<tcpconns plugin> counts the number of currently established TCP
+connections based on the local port and/or the remote port. Since there may be
+a lot of connections the default if to count all connections with a local port,
+for which a listening socket is opened. You can use the following options to
+fine-tune the ports you are interested in:
+
+=over 4
+
+=item B<ListeningPorts> I<true>|I<false>
+
+If this option is set to I<true>, statistics for all local ports for which a
+listening socket exists are collected. The default depends on B<LocalPort> and
+B<RemotePort> (see below): If no port at all is specifically selected, the
+default is to collect listening ports. If specific ports (no matter if local or
+remote ports) are selected, this option defaults to I<false>, i.E<nbsp>e. only
+the selected ports will be collected unless this option is set to I<true>
+specifically.
+
+=item B<LocalPort> I<Port>
+
+Count the connections to a specific local port. This can be used to see how
+many connections are handeled by a specific daemon, e.E<nbsp>g. the mailserver.
+You have to specify the port in numeric form, so for the mailserver example
+you'd need to set B<25>.
+
+=item B<RemotePort> I<Port>
+
+Count the connections to a specific remote port. This is usefull to see how
+much a remote service is used. This is most useful if you want to know how many
+connections a local service has opened to remote services, e.E<nbsp>g. how many
+connections a mail server or news server has to other mail or news servers, or
+how many connections a web proxy holds to web servers. You have to give the
+port in numeric form.
+
+=back
+
=head2 Plugin C<unixsock>
=over 4
diff --git a/src/tcpconns.c b/src/tcpconns.c
--- /dev/null
+++ b/src/tcpconns.c
@@ -0,0 +1,346 @@
+/**
+ * collectd - src/tcpconns.c
+ * Copyright (C) 2007 Florian octo Forster
+ *
+ * This program is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU General Public License as published by the
+ * Free Software Foundation; only version 2 of the License is applicable.
+ *
+ * This program is distributed in the hope that it will be useful, but
+ * WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License along
+ * with this program; if not, write to the Free Software Foundation, Inc.,
+ * 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
+ *
+ * Author:
+ * Florian octo Forster <octo at verplant.org>
+ **/
+
+#include "collectd.h"
+#include "common.h"
+#include "plugin.h"
+
+#if !KERNEL_LINUX
+# error "No applicable input method."
+#endif
+
+#define PORT_COLLECT_LOCAL 0x01
+#define PORT_COLLECT_REMOTE 0x02
+#define PORT_IS_LISTENING 0x04
+
+typedef struct port_entry_s
+{
+ uint16_t port;
+ uint16_t flags;
+ uint32_t count_local;
+ uint32_t count_remote;
+ struct port_entry_s *next;
+} port_entry_t;
+
+static const char *config_keys[] =
+{
+ "ListeningPorts",
+ "LocalPort",
+ "RemotePort"
+};
+static int config_keys_num = STATIC_ARRAY_SIZE (config_keys);
+
+static int port_collect_listening = 0;
+static port_entry_t *port_list_head = NULL;
+
+static void conn_submit_port_entry (port_entry_t *pe)
+{
+ value_t values[1];
+ value_list_t vl = VALUE_LIST_INIT;
+
+ vl.values = values;
+ vl.values_len = 1;
+ vl.time = time (NULL);
+ strcpy (vl.host, hostname_g);
+ strcpy (vl.plugin, "tcpconns");
+ snprintf (vl.type_instance, sizeof (vl.type_instance), "%hu", pe->port);
+ vl.type_instance[sizeof (vl.type_instance) - 1] = '\0';
+
+ if (((port_collect_listening != 0) && (pe->flags & PORT_IS_LISTENING))
+ || (pe->flags & PORT_COLLECT_LOCAL))
+ {
+ values[0].gauge = pe->count_local;
+ strcpy (vl.plugin_instance, "local");
+ plugin_dispatch_values ("tcp_connections", &vl);
+ }
+ if (pe->flags & PORT_COLLECT_REMOTE)
+ {
+ values[0].gauge = pe->count_remote;
+ strcpy (vl.plugin_instance, "remote");
+ plugin_dispatch_values ("tcp_connections", &vl);
+ }
+} /* void conn_submit */
+
+static void conn_submit_all (void)
+{
+ port_entry_t *pe;
+
+ for (pe = port_list_head; pe != NULL; pe = pe->next)
+ conn_submit_port_entry (pe);
+} /* void conn_submit_all */
+
+static port_entry_t *conn_get_port_entry (uint16_t port, int create)
+{
+ port_entry_t *ret;
+
+ ret = port_list_head;
+ while (ret != NULL)
+ {
+ if (ret->port == port)
+ break;
+ ret = ret->next;
+ }
+
+ if ((ret == NULL) && (create != 0))
+ {
+ ret = (port_entry_t *) malloc (sizeof (port_entry_t));
+ if (ret == NULL)
+ return (NULL);
+ memset (ret, '\0', sizeof (port_entry_t));
+
+ ret->port = port;
+ ret->next = port_list_head;
+ port_list_head = ret;
+ }
+
+ return (ret);
+} /* port_entry_t *conn_get_port_entry */
+
+static void conn_reset_port_entry (void)
+{
+ port_entry_t *prev = NULL;
+ port_entry_t *pe = port_list_head;
+
+ while (pe != NULL)
+ {
+ /* If this entry was created while reading the files (ant not when handling
+ * the configuration) remove it now. */
+ if ((pe->flags & (PORT_COLLECT_LOCAL | PORT_COLLECT_REMOTE)) == 0)
+ {
+ port_entry_t *next = pe->next;
+
+ DEBUG ("tcpconns plugin: Removing temporary entry "
+ "for listening port %hu", pe->port);
+
+ if (prev == NULL)
+ port_list_head = next;
+ else
+ prev->next = next;
+
+ sfree (pe);
+ pe = next;
+
+ continue;
+ }
+
+ pe->count_local = 0;
+ pe->count_remote = 0;
+
+ pe->flags &= ~PORT_IS_LISTENING;
+
+ pe = pe->next;
+ }
+} /* void conn_reset_port_entry */
+
+static int conn_handle_ports (uint16_t port_local, uint16_t port_remote, uint8_t state)
+{
+ /* Listening sockets */
+ if ((state == 0x0a) && (port_collect_listening != 0))
+ {
+ port_entry_t *pe;
+
+ DEBUG ("tcpconns plugin: Adding listening port %hu", port_local);
+
+ pe = conn_get_port_entry (port_local, 1 /* create */);
+ if (pe != NULL)
+ {
+ pe->count_local++;
+ pe->flags |= PORT_IS_LISTENING;
+ }
+ }
+ /* Established connections */
+ else if (state == 0x01)
+ {
+ port_entry_t *pe;
+
+ DEBUG ("tcpconns plugin: Established connection %hu <-> %hu",
+ port_local, port_remote);
+
+ pe = conn_get_port_entry (port_local, 0 /* no create */);
+ if ((pe != NULL) && (pe->flags & PORT_COLLECT_LOCAL))
+ pe->count_local++;
+
+ pe = conn_get_port_entry (port_remote, 0 /* no create */);
+ if ((pe != NULL) && (pe->flags & PORT_COLLECT_REMOTE))
+ pe->count_remote++;
+ }
+ else
+ {
+ DEBUG ("tcpconns plugin: Ignoring unknown state 0x%x", state);
+ }
+
+ return (0);
+} /* int conn_handle_ports */
+
+static int conn_handle_line (char *buffer)
+{
+ char *fields[32];
+ int fields_len;
+
+ char *endptr;
+
+ char *port_local_str;
+ char *port_remote_str;
+ uint16_t port_local;
+ uint16_t port_remote;
+
+ uint8_t state;
+
+ int buffer_len = strlen (buffer);
+
+ while ((buffer_len > 0) && (buffer[buffer_len - 1] < 32))
+ buffer[--buffer_len] = '\0';
+ if (buffer_len <= 0)
+ return (-1);
+
+ fields_len = strsplit (buffer, fields, STATIC_ARRAY_SIZE (fields));
+ if (fields_len < 12)
+ {
+ DEBUG ("tcpconns plugin: Got %i fields, expected at least 12.", fields_len);
+ return (-1);
+ }
+
+ port_local_str = strchr (fields[1], ':');
+ port_remote_str = strchr (fields[2], ':');
+
+ if ((port_local_str == NULL) || (port_remote_str == NULL))
+ return (-1);
+ port_local_str++;
+ port_remote_str++;
+ if ((*port_local_str == '\0') || (*port_remote_str == '\0'))
+ return (-1);
+
+ endptr = NULL;
+ port_local = (uint16_t) strtol (port_local_str, &endptr, 16);
+ if ((endptr == NULL) || (*endptr != '\0'))
+ return (-1);
+
+ endptr = NULL;
+ port_remote = (uint16_t) strtol (port_remote_str, &endptr, 16);
+ if ((endptr == NULL) || (*endptr != '\0'))
+ return (-1);
+
+ endptr = NULL;
+ state = (uint8_t) strtol (fields[3], &endptr, 16);
+ if ((endptr == NULL) || (*endptr != '\0'))
+ return (-1);
+
+ return (conn_handle_ports (port_local, port_remote, state));
+} /* int conn_handle_line */
+
+static int conn_read_file (const char *file)
+{
+ FILE *fh;
+ char buffer[1024];
+
+ fh = fopen (file, "r");
+ if (fh == NULL)
+ {
+ char errbuf[1024];
+ ERROR ("tcpconns plugin: fopen (%s) failed: %s",
+ file, sstrerror (errno, errbuf, sizeof (errbuf)));
+ return (-1);
+ }
+
+ while (fgets (buffer, sizeof (buffer), fh) != NULL)
+ {
+ conn_handle_line (buffer);
+ } /* while (fgets) */
+
+ fclose (fh);
+
+ return (0);
+} /* int conn_read_file */
+
+static int conn_config (const char *key, const char *value)
+{
+ if (strcasecmp (key, "ListeningPorts") == 0)
+ {
+ if ((strcasecmp (value, "Yes") == 0)
+ || (strcasecmp (value, "True") == 0)
+ || (strcasecmp (value, "On") == 0))
+ port_collect_listening = 1;
+ else
+ port_collect_listening = 0;
+ }
+ else if ((strcasecmp (key, "LocalPort") == 0)
+ || (strcasecmp (key, "RemotePort") == 0))
+ {
+ port_entry_t *pe;
+ int port = atoi (value);
+
+ if ((port < 1) || (port > 65535))
+ {
+ ERROR ("tcpconns plugin: Invalid port: %i", port);
+ return (1);
+ }
+
+ pe = conn_get_port_entry ((uint16_t) port, 1 /* create */);
+ if (pe == NULL)
+ {
+ ERROR ("tcpconns plugin: conn_get_port_entry failed.");
+ return (1);
+ }
+
+ if (strcasecmp (key, "LocalPort") == 0)
+ pe->flags |= PORT_COLLECT_LOCAL;
+ else
+ pe->flags |= PORT_COLLECT_REMOTE;
+ }
+ else
+ {
+ return (-1);
+ }
+
+ return (0);
+} /* int conn_config */
+
+static int conn_init (void)
+{
+ if (port_list_head == NULL)
+ port_collect_listening = 1;
+
+ return (0);
+} /* int conn_init */
+
+static int conn_read (void)
+{
+ conn_reset_port_entry ();
+
+ conn_read_file ("/proc/net/tcp");
+ conn_read_file ("/proc/net/tcp6");
+
+ conn_submit_all ();
+
+ return (0);
+} /* int conn_read */
+
+void module_register (void)
+{
+ plugin_register_config ("tcpconns", conn_config,
+ config_keys, config_keys_num);
+ plugin_register_init ("tcpconns", conn_init);
+ plugin_register_read ("tcpconns", conn_read);
+} /* void module_register */
+
+/*
+ * vim: set shiftwidth=2 softtabstop=2 tabstop=8 :
+ */
diff --git a/src/types.db b/src/types.db
index 884dfcd4b4e5b0a102ee5b8a3ab5a509160cb6eb..7973cd44d266b74115f9991239db3cf581b6f00e 100644 (file)
--- a/src/types.db
+++ b/src/types.db
ps_rss value:GAUGE:0:9223372036854775807
ps_state value:GAUGE:0:65535
spam_score value:GAUGE:U:U
+tcp_connections value:GAUGE:0:4294967295
temperature value:GAUGE:-273.15:U
time_dispersion seconds:GAUGE:-1000000:1000000
time_offset seconds:GAUGE:-1000000:1000000