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 **/
24 #include "collectd.h"
26 #include "common.h"
27 #include "plugin.h"
29 #if !KERNEL_LINUX
30 # error "No applicable input method."
31 #endif
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[] =
39 {
40 "OldFiles"
41 };
42 static int config_keys_num = STATIC_ARRAY_SIZE (config_keys);
43 /*
44 Each table/chain combo that will be queried goes into this list
45 */
47 static int old_files = 0;
49 static int conntrack_config(const char *key, const char *value)
50 {
51 if (strcmp(key, "OldFiles") == 0)
52 old_files = 1;
54 return 0;
55 }
57 static void conntrack_submit (const char *type, const char *type_instance,
58 value_t conntrack)
59 {
60 value_list_t vl = VALUE_LIST_INIT;
62 vl.values = &conntrack;
63 vl.values_len = 1;
64 sstrncpy (vl.host, hostname_g, sizeof (vl.host));
65 sstrncpy (vl.plugin, "conntrack", sizeof (vl.plugin));
66 sstrncpy (vl.type, type, sizeof (vl.type));
67 if (type_instance != NULL)
68 sstrncpy (vl.type_instance, type_instance,
69 sizeof (vl.type_instance));
71 plugin_dispatch_values (&vl);
72 } /* static void conntrack_submit */
74 static int conntrack_read (void)
75 {
76 value_t conntrack, conntrack_max, conntrack_pct;
77 FILE *fh;
78 char buffer[64] = { 0 };
79 size_t buffer_len;
81 fh = fopen (old_files?CONNTRACK_FILE_OLD:CONNTRACK_FILE, "r");
82 if (fh == NULL)
83 return (-1);
85 if (fgets (buffer, sizeof (buffer), fh) == NULL)
86 {
87 fclose (fh);
88 return (-1);
89 }
90 fclose (fh);
92 /* strip trailing newline. */
93 buffer_len = strlen (buffer);
94 while ((buffer_len > 0) && isspace ((int) buffer[buffer_len - 1]))
95 {
96 buffer[buffer_len - 1] = 0;
97 buffer_len--;
98 }
100 if (parse_value (buffer, &conntrack, DS_TYPE_GAUGE) != 0)
101 return (-1);
103 conntrack_submit ("conntrack", NULL, conntrack);
105 fh = fopen (old_files?CONNTRACK_MAX_FILE_OLD:CONNTRACK_MAX_FILE, "r");
106 if (fh == NULL)
107 return (-1);
109 memset (buffer, 0, sizeof (buffer));
110 if (fgets (buffer, sizeof (buffer), fh) == NULL)
111 {
112 fclose (fh);
113 return (-1);
114 }
115 fclose (fh);
117 /* strip trailing newline. */
118 buffer_len = strlen (buffer);
119 while ((buffer_len > 0) && isspace ((int) buffer[buffer_len - 1]))
120 {
121 buffer[buffer_len - 1] = 0;
122 buffer_len--;
123 }
125 if (parse_value (buffer, &conntrack_max, DS_TYPE_GAUGE) != 0)
126 return (-1);
128 conntrack_submit ("conntrack", "max", conntrack_max);
129 conntrack_pct.gauge = (conntrack.gauge / conntrack_max.gauge) * 100;
130 conntrack_submit ("percent", "used", conntrack_pct);
133 return (0);
134 } /* static int conntrack_read */
136 void module_register (void)
137 {
138 plugin_register_config ("conntrack", conntrack_config,
139 config_keys, config_keys_num);
140 plugin_register_read ("conntrack", conntrack_read);
141 } /* void module_register */