X-Git-Url: https://git.octo.it/?a=blobdiff_plain;f=src%2Fconntrack.c;h=49a33551d6ec4167adf9e74171092cb5cfd8a0f9;hb=8aad55ab7d737a97d5927458b2b00885e27cae4d;hp=33236c45975c4d9c5c519e4defbfcd029c0b0821;hpb=ab55c79a0fee35932995d9f492730dc131034d52;p=collectd.git diff --git a/src/conntrack.c b/src/conntrack.c index 33236c45..49a33551 100644 --- a/src/conntrack.c +++ b/src/conntrack.c @@ -8,7 +8,7 @@ * * 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 + * 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 @@ -18,7 +18,7 @@ * Authors: * Tomasz Pala * based on entropy.c by: - * Florian octo Forster + * Florian octo Forster **/ #include "collectd.h" @@ -30,8 +30,31 @@ #endif #define CONNTRACK_FILE "/proc/sys/net/netfilter/nf_conntrack_count" +#define CONNTRACK_MAX_FILE "/proc/sys/net/netfilter/nf_conntrack_max" +#define CONNTRACK_FILE_OLD "/proc/sys/net/ipv4/netfilter/ip_conntrack_count" +#define CONNTRACK_MAX_FILE_OLD "/proc/sys/net/ipv4/netfilter/ip_conntrack_max" -static void conntrack_submit (value_t conntrack) +static const char *config_keys[] = +{ + "OldFiles" +}; +static int config_keys_num = STATIC_ARRAY_SIZE (config_keys); +/* + Each table/chain combo that will be queried goes into this list +*/ + +static int old_files = 0; + +static int conntrack_config(const char *key, const char *value) +{ + if (strcmp(key, "OldFiles") == 0) + old_files = 1; + + return 0; +} + +static void conntrack_submit (const char *type, const char *type_instance, + value_t conntrack) { value_list_t vl = VALUE_LIST_INIT; @@ -39,19 +62,22 @@ static void conntrack_submit (value_t conntrack) vl.values_len = 1; sstrncpy (vl.host, hostname_g, sizeof (vl.host)); sstrncpy (vl.plugin, "conntrack", sizeof (vl.plugin)); - sstrncpy (vl.type, "conntrack", sizeof (vl.type)); + sstrncpy (vl.type, type, sizeof (vl.type)); + if (type_instance != NULL) + sstrncpy (vl.type_instance, type_instance, + sizeof (vl.type_instance)); plugin_dispatch_values (&vl); } /* static void conntrack_submit */ static int conntrack_read (void) { - value_t conntrack; + value_t conntrack, conntrack_max, conntrack_pct; FILE *fh; char buffer[64]; size_t buffer_len; - fh = fopen (CONNTRACK_FILE, "r"); + fh = fopen (old_files?CONNTRACK_FILE_OLD:CONNTRACK_FILE, "r"); if (fh == NULL) return (-1); @@ -74,12 +100,42 @@ static int conntrack_read (void) if (parse_value (buffer, &conntrack, DS_TYPE_GAUGE) != 0) return (-1); - conntrack_submit (conntrack); + conntrack_submit ("conntrack", NULL, conntrack); + + fh = fopen (old_files?CONNTRACK_MAX_FILE_OLD:CONNTRACK_MAX_FILE, "r"); + if (fh == NULL) + return (-1); + + memset (buffer, 0, sizeof (buffer)); + if (fgets (buffer, sizeof (buffer), fh) == NULL) + { + fclose (fh); + return (-1); + } + fclose (fh); + + /* strip trailing newline. */ + buffer_len = strlen (buffer); + while ((buffer_len > 0) && isspace ((int) buffer[buffer_len - 1])) + { + buffer[buffer_len - 1] = 0; + buffer_len--; + } + + if (parse_value (buffer, &conntrack_max, DS_TYPE_GAUGE) != 0) + return (-1); + + conntrack_submit ("conntrack", "max", conntrack_max); + conntrack_pct.gauge = (conntrack.gauge / conntrack_max.gauge) * 100; + conntrack_submit ("percent", "used", conntrack_pct); + return (0); } /* static int conntrack_read */ void module_register (void) { + plugin_register_config ("conntrack", conntrack_config, + config_keys, config_keys_num); plugin_register_read ("conntrack", conntrack_read); } /* void module_register */