Merged branch 'sh/collectd-4.6' into sh/collectd-4.7.
[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 verplant.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
34 static void conntrack_submit (double conntrack)
35 {
36         value_t values[1];
37         value_list_t vl = VALUE_LIST_INIT;
38
39         values[0].gauge = conntrack;
40
41         vl.values = values;
42         vl.values_len = 1;
43         sstrncpy (vl.host, hostname_g, sizeof (vl.host));
44         sstrncpy (vl.plugin, "conntrack", sizeof (vl.plugin));
45         sstrncpy (vl.type, "conntrack", sizeof (vl.type));
46
47         plugin_dispatch_values (&vl);
48 } /* static void conntrack_submit */
49
50 static int conntrack_read (void)
51 {
52         double conntrack;
53         FILE *fh;
54         char buffer[64];
55
56         fh = fopen (CONNTRACK_FILE, "r");
57         if (fh == NULL)
58                 return (-1);
59
60         if (fgets (buffer, sizeof (buffer), fh) == NULL)
61         {
62                 fclose (fh);
63                 return (-1);
64         }
65         fclose (fh);
66
67         conntrack = atof (buffer);
68
69         if (conntrack > 0.0)
70                 conntrack_submit (conntrack);
71
72         return (0);
73 } /* static int conntrack_read */
74
75 void module_register (void)
76 {
77         plugin_register_read ("conntrack", conntrack_read);
78 } /* void module_register */