Merge branch 'collectd-5.1' into collectd-5.2
[collectd.git] / src / pf.c
1 /*
2  * Copyright (c) 2010 Pierre-Yves Ritschard <pyr@openbsd.org>
3  * Copyright (c) 2011 Stefan Rinkes <stefan.rinkes@gmail.org>
4  *
5  * Permission to use, copy, modify, and distribute this software for any
6  * purpose with or without fee is hereby granted, provided that the above
7  * copyright notice and this permission notice appear in all copies.
8  *
9  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
10  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
11  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
12  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
13  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
14  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
15  * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
16  */
17
18 #include "collectd.h"
19 #include "plugin.h"
20 #include "common.h"
21
22 #include <sys/ioctl.h>
23 #include <sys/socket.h>
24 #include <net/if.h>
25 #include <net/pfvar.h>
26 #include <paths.h>
27 #include <err.h>
28 #include <pwd.h>
29
30 static char const *pf_reasons[PFRES_MAX+1] = PFRES_NAMES;
31 static char const *pf_lcounters[LCNT_MAX+1] = LCNT_NAMES;
32 static char const *pf_fcounters[FCNT_MAX+1] = FCNT_NAMES;
33 static char const *pf_scounters[FCNT_MAX+1] = FCNT_NAMES;
34
35 static char const *pf_device = "/dev/pf";
36
37 static void pf_submit (char const *type, char const *type_instance,
38                 uint64_t val, _Bool is_gauge)
39 {
40         value_t         values[1];
41         value_list_t    vl = VALUE_LIST_INIT;
42
43         if (is_gauge)
44                 values[0].gauge = (gauge_t) val;
45         else
46                 values[0].derive = (derive_t) val;
47
48         vl.values = values;
49         vl.values_len = 1;
50         sstrncpy (vl.host, hostname_g, sizeof (vl.host));
51         sstrncpy (vl.plugin, "pf", sizeof (vl.plugin));
52         sstrncpy (vl.type, type, sizeof(vl.type));
53         sstrncpy (vl.type_instance, type_instance, sizeof(vl.type_instance));
54
55         plugin_dispatch_values(&vl);
56 } /* void pf_submit */
57
58 static int pf_read (void)
59 {
60         struct pf_status state;
61         int fd;
62         int status;
63         int i;
64
65         fd = open (pf_device, O_RDONLY);
66         if (fd < 0)
67         {
68                 char errbuf[1024];
69                 ERROR("pf plugin: Unable to open %s: %s",
70                                 pf_device,
71                                 sstrerror (errno, errbuf, sizeof (errbuf)));
72                 return (-1);
73         }
74
75         memset (&state, 0, sizeof (state));
76         status = ioctl (fd, DIOCGETSTATUS, &state);
77         if (status != 0)
78         {
79                 char errbuf[1024];
80                 ERROR("pf plugin: ioctl(DIOCGETSTATUS) failed: %s",
81                                 sstrerror (errno, errbuf, sizeof (errbuf)));
82                 close(fd);
83                 return (-1);
84         }
85
86         close (fd);
87         fd = -1;
88
89         if (!status.running)
90         {
91                 WARNING ("pf plugin: PF is not running.");
92                 return (-1);
93         }
94
95         for (i = 0; i < PFRES_MAX; i++)
96                 pf_submit ("pf_counters", pf_reasons[i], state.counters[i],
97                                 /* is gauge = */ 0);
98         for (i = 0; i < LCNT_MAX; i++)
99                 pf_submit ("pf_limits", pf_lcounters[i], state.lcounters[i],
100                                 /* is gauge = */ 0);
101         for (i = 0; i < FCNT_MAX; i++)
102                 pf_submit ("pf_state", pf_fcounters[i], state.fcounters[i],
103                                 /* is gauge = */ 0);
104         for (i = 0; i < SCNT_MAX; i++)
105                 pf_submit ("pf_source", pf_scounters[i], state.scounters[i],
106                                 /* is gauge = */ 0);
107
108         pf_submit ("pf_states", "current", (uint32_t) state.states,
109                         /* is gauge = */ 1);
110
111         return (0);
112 } /* int pf_read */
113
114 void module_register (void)
115 {
116         plugin_register_read ("pf", pf_read);
117 }