2 * Copyright (c) 2010 Pierre-Yves Ritschard
3 * Copyright (c) 2011 Stefan Rinkes
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.
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.
18 * Pierre-Yves Ritschard <pyr at openbsd.org>
19 * Stefan Rinkes <stefan.rinkes at gmail.org>
28 #include <sys/ioctl.h>
34 #include <netinet/in.h>
37 #include <net/pfvar.h>
41 #error "Unexpected value for FCNT_MAX"
43 #define FCNT_NAMES {"search", "insert", "removals", NULL};
48 #error "Unexpected value for SCNT_MAX"
50 #define SCNT_NAMES {"search", "insert", "removals", NULL};
53 static char const *pf_reasons[PFRES_MAX + 1] = PFRES_NAMES;
54 static char const *pf_lcounters[LCNT_MAX + 1] = LCNT_NAMES;
55 static char const *pf_fcounters[FCNT_MAX + 1] = FCNT_NAMES;
56 static char const *pf_scounters[SCNT_MAX + 1] = SCNT_NAMES;
58 static char const *pf_device = "/dev/pf";
60 static void pf_submit(char const *type, char const *type_instance, uint64_t val,
63 value_list_t vl = VALUE_LIST_INIT;
66 values[0].gauge = (gauge_t)val;
68 values[0].derive = (derive_t)val;
72 sstrncpy(vl.plugin, "pf", sizeof(vl.plugin));
73 sstrncpy(vl.type, type, sizeof(vl.type));
74 sstrncpy(vl.type_instance, type_instance, sizeof(vl.type_instance));
76 plugin_dispatch_values(&vl);
77 } /* void pf_submit */
79 static int pf_read(void) {
80 struct pf_status state;
84 fd = open(pf_device, O_RDONLY);
86 ERROR("pf plugin: Unable to open %s: %s", pf_device, STRERRNO);
90 status = ioctl(fd, DIOCGETSTATUS, &state);
92 ERROR("pf plugin: ioctl(DIOCGETSTATUS) failed: %s", STRERRNO);
100 WARNING("pf plugin: PF is not running.");
104 for (int i = 0; i < PFRES_MAX; i++)
105 pf_submit("pf_counters", pf_reasons[i], state.counters[i],
107 for (int i = 0; i < LCNT_MAX; i++)
108 pf_submit("pf_limits", pf_lcounters[i], state.lcounters[i],
110 for (int i = 0; i < FCNT_MAX; i++)
111 pf_submit("pf_state", pf_fcounters[i], state.fcounters[i],
113 for (int i = 0; i < SCNT_MAX; i++)
114 pf_submit("pf_source", pf_scounters[i], state.scounters[i],
117 pf_submit("pf_states", "current", (uint32_t)state.states,
123 void module_register(void) { plugin_register_read("pf", pf_read); }