pf plugin: Define {F,S}CNT_NAMES.
[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 #ifndef FCNT_NAMES
31 # if FCNT_MAX != 3
32 #  error "Unexpected value for FCNT_MAX"
33 # endif
34 # define FCNT_NAMES {"search", "insert", "removals", NULL};
35 #endif
36
37 #ifndef SCNT_NAMES
38 # if SCNT_MAX != 3
39 #  error "Unexpected value for SCNT_MAX"
40 # endif
41 # define SCNT_NAMES {"search", "insert", "removals", NULL};
42 #endif
43
44 static char const *pf_reasons[PFRES_MAX+1] = PFRES_NAMES;
45 static char const *pf_lcounters[LCNT_MAX+1] = LCNT_NAMES;
46 static char const *pf_fcounters[FCNT_MAX+1] = FCNT_NAMES;
47 static char const *pf_scounters[SCNT_MAX+1] = SCNT_NAMES;
48
49 static char const *pf_device = "/dev/pf";
50
51 static void pf_submit (char const *type, char const *type_instance,
52                 uint64_t val, _Bool is_gauge)
53 {
54         value_t         values[1];
55         value_list_t    vl = VALUE_LIST_INIT;
56
57         if (is_gauge)
58                 values[0].gauge = (gauge_t) val;
59         else
60                 values[0].derive = (derive_t) val;
61
62         vl.values = values;
63         vl.values_len = 1;
64         sstrncpy (vl.host, hostname_g, sizeof (vl.host));
65         sstrncpy (vl.plugin, "pf", sizeof (vl.plugin));
66         sstrncpy (vl.type, type, sizeof(vl.type));
67         sstrncpy (vl.type_instance, type_instance, sizeof(vl.type_instance));
68
69         plugin_dispatch_values(&vl);
70 } /* void pf_submit */
71
72 static int pf_read (void)
73 {
74         struct pf_status state;
75         int fd;
76         int status;
77         int i;
78
79         fd = open (pf_device, O_RDONLY);
80         if (fd < 0)
81         {
82                 char errbuf[1024];
83                 ERROR("pf plugin: Unable to open %s: %s",
84                                 pf_device,
85                                 sstrerror (errno, errbuf, sizeof (errbuf)));
86                 return (-1);
87         }
88
89         memset (&state, 0, sizeof (state));
90         status = ioctl (fd, DIOCGETSTATUS, &state);
91         if (status != 0)
92         {
93                 char errbuf[1024];
94                 ERROR("pf plugin: ioctl(DIOCGETSTATUS) failed: %s",
95                                 sstrerror (errno, errbuf, sizeof (errbuf)));
96                 close(fd);
97                 return (-1);
98         }
99
100         close (fd);
101         fd = -1;
102
103         if (!state.running)
104         {
105                 WARNING ("pf plugin: PF is not running.");
106                 return (-1);
107         }
108
109         for (i = 0; i < PFRES_MAX; i++)
110                 pf_submit ("pf_counters", pf_reasons[i], state.counters[i],
111                                 /* is gauge = */ 0);
112         for (i = 0; i < LCNT_MAX; i++)
113                 pf_submit ("pf_limits", pf_lcounters[i], state.lcounters[i],
114                                 /* is gauge = */ 0);
115         for (i = 0; i < FCNT_MAX; i++)
116                 pf_submit ("pf_state", pf_fcounters[i], state.fcounters[i],
117                                 /* is gauge = */ 0);
118         for (i = 0; i < SCNT_MAX; i++)
119                 pf_submit ("pf_source", pf_scounters[i], state.scounters[i],
120                                 /* is gauge = */ 0);
121
122         pf_submit ("pf_states", "current", (uint32_t) state.states,
123                         /* is gauge = */ 1);
124
125         return (0);
126 } /* int pf_read */
127
128 void module_register (void)
129 {
130         plugin_register_read ("pf", pf_read);
131 }