Merge branch 'collectd-5.4' into collectd-5.5
[collectd.git] / src / pf.c
1 /*
2  * Copyright (c) 2010 Pierre-Yves Ritschard
3  * Copyright (c) 2011 Stefan Rinkes
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  * Authors:
18  *   Pierre-Yves Ritschard <pyr at openbsd.org>
19  *   Stefan Rinkes <stefan.rinkes at gmail.org>
20  */
21
22 #include "collectd.h"
23 #include "plugin.h"
24 #include "common.h"
25
26 #if HAVE_SYS_IOCTL_H
27 # include <sys/ioctl.h>
28 #endif
29 #if HAVE_SYS_SOCKET_H
30 # include <sys/socket.h>
31 #endif
32 #if HAVE_NET_IF_H
33 # include <net/if.h>
34 #endif
35 #if HAVE_NETINET_IN_H
36 # include <netinet/in.h>
37 #endif
38
39 #include <net/pfvar.h>
40
41 #ifndef FCNT_NAMES
42 # if FCNT_MAX != 3
43 #  error "Unexpected value for FCNT_MAX"
44 # endif
45 # define FCNT_NAMES {"search", "insert", "removals", NULL};
46 #endif
47
48 #ifndef SCNT_NAMES
49 # if SCNT_MAX != 3
50 #  error "Unexpected value for SCNT_MAX"
51 # endif
52 # define SCNT_NAMES {"search", "insert", "removals", NULL};
53 #endif
54
55 static char const *pf_reasons[PFRES_MAX+1] = PFRES_NAMES;
56 static char const *pf_lcounters[LCNT_MAX+1] = LCNT_NAMES;
57 static char const *pf_fcounters[FCNT_MAX+1] = FCNT_NAMES;
58 static char const *pf_scounters[SCNT_MAX+1] = SCNT_NAMES;
59
60 static char const *pf_device = "/dev/pf";
61
62 static void pf_submit (char const *type, char const *type_instance,
63                 uint64_t val, _Bool is_gauge)
64 {
65         value_t         values[1];
66         value_list_t    vl = VALUE_LIST_INIT;
67
68         if (is_gauge)
69                 values[0].gauge = (gauge_t) val;
70         else
71                 values[0].derive = (derive_t) val;
72
73         vl.values = values;
74         vl.values_len = 1;
75         sstrncpy (vl.host, hostname_g, sizeof (vl.host));
76         sstrncpy (vl.plugin, "pf", sizeof (vl.plugin));
77         sstrncpy (vl.type, type, sizeof(vl.type));
78         sstrncpy (vl.type_instance, type_instance, sizeof(vl.type_instance));
79
80         plugin_dispatch_values(&vl);
81 } /* void pf_submit */
82
83 static int pf_read (void)
84 {
85         struct pf_status state;
86         int fd;
87         int status;
88         int i;
89
90         fd = open (pf_device, O_RDONLY);
91         if (fd < 0)
92         {
93                 char errbuf[1024];
94                 ERROR("pf plugin: Unable to open %s: %s",
95                                 pf_device,
96                                 sstrerror (errno, errbuf, sizeof (errbuf)));
97                 return (-1);
98         }
99
100         memset (&state, 0, sizeof (state));
101         status = ioctl (fd, DIOCGETSTATUS, &state);
102         if (status != 0)
103         {
104                 char errbuf[1024];
105                 ERROR("pf plugin: ioctl(DIOCGETSTATUS) failed: %s",
106                                 sstrerror (errno, errbuf, sizeof (errbuf)));
107                 close(fd);
108                 return (-1);
109         }
110
111         close (fd);
112         fd = -1;
113
114         if (!state.running)
115         {
116                 WARNING ("pf plugin: PF is not running.");
117                 return (-1);
118         }
119
120         for (i = 0; i < PFRES_MAX; i++)
121                 pf_submit ("pf_counters", pf_reasons[i], state.counters[i],
122                                 /* is gauge = */ 0);
123         for (i = 0; i < LCNT_MAX; i++)
124                 pf_submit ("pf_limits", pf_lcounters[i], state.lcounters[i],
125                                 /* is gauge = */ 0);
126         for (i = 0; i < FCNT_MAX; i++)
127                 pf_submit ("pf_state", pf_fcounters[i], state.fcounters[i],
128                                 /* is gauge = */ 0);
129         for (i = 0; i < SCNT_MAX; i++)
130                 pf_submit ("pf_source", pf_scounters[i], state.scounters[i],
131                                 /* is gauge = */ 0);
132
133         pf_submit ("pf_states", "current", (uint32_t) state.states,
134                         /* is gauge = */ 1);
135
136         return (0);
137 } /* int pf_read */
138
139 void module_register (void)
140 {
141         plugin_register_read ("pf", pf_read);
142 }