a52bc9e4ac2d8ed4dbc99c5d0783bcc722cf2f15
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 */
18 #include <sys/types.h>
19 #include <sys/ioctl.h>
20 #include <sys/socket.h>
22 #include <net/if.h>
23 #include <net/pfvar.h>
25 #include <limits.h>
26 #include <fcntl.h>
27 #include <paths.h>
28 #include <string.h>
29 #include <stdio.h>
30 #include <stdlib.h>
31 #include <errno.h>
32 #include <unistd.h>
34 #ifndef TEST
35 #include "collectd.h"
36 #include "common.h"
37 #include "plugin.h"
38 #include "configfile.h"
39 #else
40 #include <err.h>
41 typedef u_int64_t counter_t;
42 #endif
44 #define PF_SOCKET "/dev/pf"
46 struct pfdata {
47 int pd_dev;
48 };
50 static struct pfdata pd;
52 static int pf_init(void);
53 static int pf_read(void);
54 static void submit_counter(const char *, const char *, counter_t);
56 void
57 submit_counter(const char *type, const char *inst, counter_t val)
58 {
59 #ifndef TEST
60 value_t values[1];
61 value_list_t vl = VALUE_LIST_INIT;
63 values[0].gauge = val;
65 vl.values = values;
66 vl.values_len = 1;
67 sstrncpy (vl.host, hostname_g, sizeof (vl.host));
68 sstrncpy (vl.plugin, "pf", sizeof (vl.plugin));
69 sstrncpy (vl.type, type, sizeof(vl.type));
70 sstrncpy (vl.type_instance, inst, sizeof(vl.type_instance));
71 plugin_dispatch_values(&vl);
72 #else
73 printf("%s.%s: %lld\n", type, inst, val);
74 #endif
75 }
78 int
79 pf_init(void)
80 {
81 struct pf_status status;
83 memset(&pd, '\0', sizeof(pd));
85 if ((pd.pd_dev = open(PF_SOCKET, O_RDWR)) == -1) {
86 return (-1);
87 }
88 if (ioctl(pd.pd_dev, DIOCGETSTATUS, &status) == -1) {
89 return (-1);
90 }
91 close(pd.pd_dev);
92 if (!status.running)
93 return (-1);
95 return (0);
96 }
98 int
99 pf_read(void)
100 {
101 int i;
102 struct pf_status status;
104 char *cnames[] = PFRES_NAMES;
105 char *lnames[] = LCNT_NAMES;
106 char *names[] = { "searches", "inserts", "removals" };
108 if ((pd.pd_dev = open(PF_SOCKET, O_RDWR)) == -1) {
109 return (-1);
110 }
111 if (ioctl(pd.pd_dev, DIOCGETSTATUS, &status) == -1) {
112 return (-1);
113 }
114 close(pd.pd_dev);
115 for (i = 0; i < PFRES_MAX; i++)
116 submit_counter("pf_counters", cnames[i], status.counters[i]);
117 for (i = 0; i < LCNT_MAX; i++)
118 submit_counter("pf_limits", lnames[i], status.lcounters[i]);
119 for (i = 0; i < FCNT_MAX; i++)
120 submit_counter("pf_state", names[i], status.fcounters[i]);
121 for (i = 0; i < SCNT_MAX; i++)
122 submit_counter("pf_source", names[i], status.scounters[i]);
123 return (0);
124 }
126 #ifdef TEST
127 int
128 main(int argc, char *argv[])
129 {
130 if (pf_init())
131 err(1, "pf_init");
132 if (pf_read())
133 err(1, "pf_read");
134 return (0);
135 }
136 #else
137 void module_register(void) {
138 plugin_register_init("pf", pf_init);
139 plugin_register_read("pf", pf_read);
140 }
141 #endif