{GPL, other}: Relicense to MIT license.
[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 #include "common.h"
26 #include "plugin.h"
27
28 #if !KERNEL_LINUX
29 # error "No applicable input method."
30 #endif
31
32 #define CONNTRACK_FILE "/proc/sys/net/netfilter/nf_conntrack_count"
33 #define CONNTRACK_MAX_FILE "/proc/sys/net/netfilter/nf_conntrack_max"
34
35 static void conntrack_submit (const char *type, const char *type_instance,
36                               value_t conntrack)
37 {
38         value_list_t vl = VALUE_LIST_INIT;
39
40         vl.values = &conntrack;
41         vl.values_len = 1;
42         sstrncpy (vl.host, hostname_g, sizeof (vl.host));
43         sstrncpy (vl.plugin, "conntrack", sizeof (vl.plugin));
44         sstrncpy (vl.type, type, sizeof (vl.type));
45         if (type_instance != NULL)
46                 sstrncpy (vl.type_instance, type_instance,
47                           sizeof (vl.type_instance));
48
49         plugin_dispatch_values (&vl);
50 } /* static void conntrack_submit */
51
52 static int conntrack_read (void)
53 {
54         value_t conntrack, conntrack_max, conntrack_pct;
55         FILE *fh;
56         char buffer[64];
57         size_t buffer_len;
58
59         fh = fopen (CONNTRACK_FILE, "r");
60         if (fh == NULL)
61                 return (-1);
62
63         memset (buffer, 0, sizeof (buffer));
64         if (fgets (buffer, sizeof (buffer), fh) == NULL)
65         {
66                 fclose (fh);
67                 return (-1);
68         }
69         fclose (fh);
70
71         /* strip trailing newline. */
72         buffer_len = strlen (buffer);
73         while ((buffer_len > 0) && isspace ((int) buffer[buffer_len - 1]))
74         {
75                 buffer[buffer_len - 1] = 0;
76                 buffer_len--;
77         }
78
79         if (parse_value (buffer, &conntrack, DS_TYPE_GAUGE) != 0)
80                 return (-1);
81
82         conntrack_submit ("conntrack", NULL, conntrack);
83
84         fh = fopen (CONNTRACK_MAX_FILE, "r");
85         if (fh == NULL)
86                 return (-1);
87
88         memset (buffer, 0, sizeof (buffer));
89         if (fgets (buffer, sizeof (buffer), fh) == NULL)
90         {
91                 fclose (fh);
92                 return (-1);
93         }
94         fclose (fh);
95
96         /* strip trailing newline. */
97         buffer_len = strlen (buffer);
98         while ((buffer_len > 0) && isspace ((int) buffer[buffer_len - 1]))
99         {
100                 buffer[buffer_len - 1] = 0;
101                 buffer_len--;
102         }
103
104         if (parse_value (buffer, &conntrack_max, DS_TYPE_GAUGE) != 0)
105                 return (-1);
106
107         conntrack_submit ("conntrack", "max", conntrack_max);
108         conntrack_pct.gauge = (conntrack.gauge / conntrack_max.gauge) * 100;
109         conntrack_submit ("percent", "used", conntrack_pct);
110
111
112         return (0);
113 } /* static int conntrack_read */
114
115 void module_register (void)
116 {
117         plugin_register_read ("conntrack", conntrack_read);
118 } /* void module_register */