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