2 * collectd - src/conntrack.c
3 * Copyright (C) 2009 Tomasz Pala
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 * Tomasz Pala <gotar at pld-linux.org>
20 * based on entropy.c by:
21 * Florian octo Forster <octo at collectd.org>
30 #error "No applicable input method."
33 #define CONNTRACK_FILE "/proc/sys/net/netfilter/nf_conntrack_count"
34 #define CONNTRACK_MAX_FILE "/proc/sys/net/netfilter/nf_conntrack_max"
35 #define CONNTRACK_FILE_OLD "/proc/sys/net/ipv4/netfilter/ip_conntrack_count"
36 #define CONNTRACK_MAX_FILE_OLD "/proc/sys/net/ipv4/netfilter/ip_conntrack_max"
38 static const char *config_keys[] = {"OldFiles"};
39 static int config_keys_num = STATIC_ARRAY_SIZE(config_keys);
41 Each table/chain combo that will be queried goes into this list
44 static int old_files = 0;
46 static int conntrack_config(const char *key, const char *value) {
47 if (strcmp(key, "OldFiles") == 0)
53 static void conntrack_submit(const char *type, const char *type_instance,
55 value_list_t vl = VALUE_LIST_INIT;
57 vl.values = &conntrack;
59 sstrncpy(vl.host, hostname_g, sizeof(vl.host));
60 sstrncpy(vl.plugin, "conntrack", sizeof(vl.plugin));
61 sstrncpy(vl.type, type, sizeof(vl.type));
62 if (type_instance != NULL)
63 sstrncpy(vl.type_instance, type_instance, sizeof(vl.type_instance));
65 plugin_dispatch_values(&vl);
66 } /* static void conntrack_submit */
68 static int conntrack_read(void) {
69 value_t conntrack, conntrack_max, conntrack_pct;
71 char buffer[64] = {0};
74 fh = fopen(old_files ? CONNTRACK_FILE_OLD : CONNTRACK_FILE, "r");
78 if (fgets(buffer, sizeof(buffer), fh) == NULL) {
84 /* strip trailing newline. */
85 buffer_len = strlen(buffer);
86 while ((buffer_len > 0) && isspace((int)buffer[buffer_len - 1])) {
87 buffer[buffer_len - 1] = 0;
91 if (parse_value(buffer, &conntrack, DS_TYPE_GAUGE) != 0)
94 conntrack_submit("conntrack", NULL, conntrack);
96 fh = fopen(old_files ? CONNTRACK_MAX_FILE_OLD : CONNTRACK_MAX_FILE, "r");
100 memset(buffer, 0, sizeof(buffer));
101 if (fgets(buffer, sizeof(buffer), fh) == NULL) {
107 /* strip trailing newline. */
108 buffer_len = strlen(buffer);
109 while ((buffer_len > 0) && isspace((int)buffer[buffer_len - 1])) {
110 buffer[buffer_len - 1] = 0;
114 if (parse_value(buffer, &conntrack_max, DS_TYPE_GAUGE) != 0)
117 conntrack_submit("conntrack", "max", conntrack_max);
118 conntrack_pct.gauge = (conntrack.gauge / conntrack_max.gauge) * 100;
119 conntrack_submit("percent", "used", conntrack_pct);
122 } /* static int conntrack_read */
124 void module_register(void) {
125 plugin_register_config("conntrack", conntrack_config, config_keys,
127 plugin_register_read("conntrack", conntrack_read);
128 } /* void module_register */