This repository has been archived by the owner on Feb 12, 2022. It is now read-only.
/
conntrack.c
85 lines (71 loc) · 2.16 KB
/
conntrack.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
/**
* collectd - src/conntrack.c
* Copyright (C) 2009 Tomasz Pala
*
* This program is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License as published by the
* Free Software Foundation; only version 2 of the License is applicable.
*
* This program is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* General Public License for more details.
*
* You should have received a copy of the GNU General Public License along
* with this program; if not, write to the Free Software Foundation, Inc.,
* 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*
* Authors:
* Tomasz Pala <gotar at pld-linux.org>
* based on entropy.c by:
* Florian octo Forster <octo at verplant.org>
**/
#include "collectd.h"
#include "common.h"
#include "plugin.h"
#if !KERNEL_LINUX
# error "No applicable input method."
#endif
#define CONNTRACK_FILE "/proc/sys/net/netfilter/nf_conntrack_count"
static void conntrack_submit (value_t conntrack)
{
value_list_t vl = VALUE_LIST_INIT;
vl.values = &conntrack;
vl.values_len = 1;
sstrncpy (vl.host, hostname_g, sizeof (vl.host));
sstrncpy (vl.plugin, "conntrack", sizeof (vl.plugin));
sstrncpy (vl.type, "conntrack", sizeof (vl.type));
plugin_dispatch_values (&vl);
} /* static void conntrack_submit */
static int conntrack_read (void)
{
value_t conntrack;
FILE *fh;
char buffer[64];
size_t buffer_len;
fh = fopen (CONNTRACK_FILE, "r");
if (fh == NULL)
return (-1);
memset (buffer, 0, sizeof (buffer));
if (fgets (buffer, sizeof (buffer), fh) == NULL)
{
fclose (fh);
return (-1);
}
fclose (fh);
/* strip trailing newline. */
buffer_len = strlen (buffer);
while ((buffer_len > 0) && isspace ((int) buffer[buffer_len - 1]))
{
buffer[buffer_len - 1] = 0;
buffer_len--;
}
if (parse_value (buffer, &conntrack, DS_TYPE_GAUGE) != 0)
return (-1);
conntrack_submit (conntrack);
return (0);
} /* static int conntrack_read */
void module_register (void)
{
plugin_register_read ("conntrack", conntrack_read);
} /* void module_register */