2 * Copyright (c) 2010 Pierre-Yves Ritschard <pyr@openbsd.org>
4 * Permission to use, copy, modify, and distribute this software for any
5 * purpose with or without fee is hereby granted, provided that the above
6 * copyright notice and this permission notice appear in all copies.
8 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
11 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
13 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
14 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17 #include <sys/types.h>
18 #include <sys/ioctl.h>
19 #include <sys/socket.h>
22 #include <net/pfvar.h>
36 typedef u_int64_t counter_t;
39 #define PF_SOCKET "/dev/pf"
45 static struct pfdata pd;
47 static int pf_init(void);
48 static int pf_read(void);
49 static void submit_counter(const char *, const char *, counter_t);
52 submit_counter(const char *type, const char *inst, counter_t val)
56 value_list_t vl = VALUE_LIST_INIT;
58 values[0].counter = val;
62 strlcpy(vl.host, hostname_g, sizeof(vl.host));
63 strlcpy(vl.plugin, "pf", sizeof(vl.plugin));
64 strlcpy(vl.type, type, sizeof(vl.type));
65 strlcpy(vl.type_instance, inst, sizeof(vl.type_instance));
66 plugin_dispatch_values(&vl);
68 printf("%s.%s: %lld\n", type, inst, val);
76 struct pf_status status;
78 memset(&pd, '\0', sizeof(pd));
80 if ((pd.pd_dev = open(PF_SOCKET, O_RDWR)) == -1) {
83 if (ioctl(pd.pd_dev, DIOCGETSTATUS, &status) == -1) {
97 struct pf_status status;
99 char *cnames[] = PFRES_NAMES;
100 char *lnames[] = LCNT_NAMES;
101 char *names[] = { "searches", "inserts", "removals" };
103 if ((pd.pd_dev = open(PF_SOCKET, O_RDWR)) == -1) {
106 if (ioctl(pd.pd_dev, DIOCGETSTATUS, &status) == -1) {
110 for (i = 0; i < PFRES_MAX; i++)
111 submit_counter("pf_counters", cnames[i], status.counters[i]);
112 for (i = 0; i < LCNT_MAX; i++)
113 submit_counter("pf_limits", lnames[i], status.lcounters[i]);
114 for (i = 0; i < FCNT_MAX; i++)
115 submit_counter("pf_state", names[i], status.fcounters[i]);
116 for (i = 0; i < SCNT_MAX; i++)
117 submit_counter("pf_source", names[i], status.scounters[i]);
123 main(int argc, char *argv[])
132 void module_register(void) {
133 plugin_register_init("pf", pf_init);
134 plugin_register_read("pf", pf_read);