513634c0d0f84a997f596b2423fd4e616732e961
1 /*
2 * Copyright (c) 2010 Pierre-Yves Ritschard <pyr@openbsd.org>
3 *
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.
7 *
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.
15 */
17 #include <sys/types.h>
18 #include <sys/ioctl.h>
19 #include <sys/socket.h>
21 #include <net/if.h>
22 #include <net/pfvar.h>
24 #include <limits.h>
25 #include <fcntl.h>
26 #include <paths.h>
27 #include <string.h>
28 #include <stdio.h>
29 #include <stdlib.h>
30 #include <errno.h>
32 #ifndef TEST
33 #include "collectd.h"
34 #include "plugin.h"
35 #else
36 typedef u_int64_t counter_t;
37 #endif
39 #define PF_SOCKET "/dev/pf"
41 struct pfdata {
42 int pd_dev;
43 };
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);
51 void
52 submit_counter(const char *type, const char *inst, counter_t val)
53 {
54 #ifndef TEST
55 value_t values[1];
56 value_list_t vl = VALUE_LIST_INIT;
58 values[0].counter = val;
59 vl.values = values;
60 vl.values_len = 1;
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);
67 #else
68 printf("%s.%s: %lld\n", type, inst, val);
69 #endif
70 }
73 int
74 pf_init(void)
75 {
76 struct pf_status status;
78 memset(&pd, '\0', sizeof(pd));
80 if ((pd.pd_dev = open(PF_SOCKET, O_RDWR)) == -1) {
81 return (-1);
82 }
83 if (ioctl(pd.pd_dev, DIOCGETSTATUS, &status) == -1) {
84 return (-1);
85 }
86 close(pd.pd_dev);
87 if (!status.running)
88 return (-1);
90 return (0);
91 }
93 int
94 pf_read(void)
95 {
96 int i;
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) {
104 return (-1);
105 }
106 if (ioctl(pd.pd_dev, DIOCGETSTATUS, &status) == -1) {
107 return (-1);
108 }
109 close(pd.pd_dev);
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]);
118 return (0);
119 }
121 #ifdef TEST
122 int
123 main(int argc, char *argv[])
124 {
125 if (pf_init())
126 err(1, "pf_init");
127 if (pf_read())
128 err(1, "pf_read");
129 return (0);
130 }
131 #else
132 void module_register(void) {
133 plugin_register_init("pf", pf_init);
134 plugin_register_read("pf", pf_read);
135 }
136 #endif