Merge pull request #3329 from efuss/fix-3311
[collectd.git] / src / conntrack.c
1 /**
2  * collectd - src/conntrack.c
3  * Copyright (C) 2009  Tomasz Pala
4  *
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.
8  *
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.
13  *
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
17  *
18  * Authors:
19  *   Tomasz Pala <gotar at pld-linux.org>
20  * based on entropy.c by:
21  *   Florian octo Forster <octo at collectd.org>
22  **/
23
24 #include "collectd.h"
25
26 #include "plugin.h"
27 #include "utils/common/common.h"
28
29 #if !KERNEL_LINUX
30 #error "No applicable input method."
31 #endif
32
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"
37
38 static const char *config_keys[] = {"OldFiles"};
39 static int config_keys_num = STATIC_ARRAY_SIZE(config_keys);
40 /*
41     Each table/chain combo that will be queried goes into this list
42 */
43
44 static int old_files;
45
46 static int conntrack_config(const char *key, const char *value) {
47   if (strcmp(key, "OldFiles") == 0)
48     old_files = 1;
49
50   return 0;
51 }
52
53 static void conntrack_submit(const char *type, const char *type_instance,
54                              value_t conntrack) {
55   value_list_t vl = VALUE_LIST_INIT;
56
57   vl.values = &conntrack;
58   vl.values_len = 1;
59   sstrncpy(vl.plugin, "conntrack", sizeof(vl.plugin));
60   sstrncpy(vl.type, type, sizeof(vl.type));
61   if (type_instance != NULL)
62     sstrncpy(vl.type_instance, type_instance, sizeof(vl.type_instance));
63
64   plugin_dispatch_values(&vl);
65 } /* static void conntrack_submit */
66
67 static int conntrack_read(void) {
68   value_t conntrack, conntrack_max, conntrack_pct;
69
70   char const *path = old_files ? CONNTRACK_FILE_OLD : CONNTRACK_FILE;
71   if (parse_value_file(path, &conntrack, DS_TYPE_GAUGE) != 0) {
72     ERROR("conntrack plugin: Reading \"%s\" failed.", path);
73     return -1;
74   }
75
76   path = old_files ? CONNTRACK_MAX_FILE_OLD : CONNTRACK_MAX_FILE;
77   if (parse_value_file(path, &conntrack_max, DS_TYPE_GAUGE) != 0) {
78     ERROR("conntrack plugin: Reading \"%s\" failed.", path);
79     return -1;
80   }
81
82   conntrack_pct.gauge = (conntrack.gauge / conntrack_max.gauge) * 100;
83
84   conntrack_submit("conntrack", NULL, conntrack);
85   conntrack_submit("conntrack", "max", conntrack_max);
86   conntrack_submit("percent", "used", conntrack_pct);
87
88   return 0;
89 } /* static int conntrack_read */
90
91 void module_register(void) {
92   plugin_register_config("conntrack", conntrack_config, config_keys,
93                          config_keys_num);
94   plugin_register_read("conntrack", conntrack_read);
95 } /* void module_register */