pf plugin: whitespace cleanup
[collectd.git] / src / pf.c
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  */
17
18 #include <sys/types.h>
19 #include <sys/ioctl.h>
20 #include <sys/socket.h>
21
22 #include <net/if.h>
23 #include <net/pfvar.h>
24
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>
33
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
43
44 #define PF_SOCKET "/dev/pf"
45
46 struct pfdata {
47         int     pd_dev;
48 };
49
50 static struct pfdata    pd;
51
52 static int      pf_init(void);
53 static int      pf_read(void);
54 static void     submit_counter(const char *, const char *, counter_t);
55
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;
62
63         values[0].gauge = val;
64
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 }
76
77
78 int
79 pf_init(void)
80 {
81         struct pf_status        status;
82
83         memset(&pd, '\0', sizeof(pd));
84
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
92         close(pd.pd_dev);
93         if (!status.running)
94                 return (-1);
95
96         return (0);
97 }
98
99 int
100 pf_read(void)
101 {
102         int                     i;
103         struct pf_status        status;
104
105         char            *cnames[] = PFRES_NAMES;
106         char            *lnames[] = LCNT_NAMES;
107         char            *names[] = { "searches", "inserts", "removals" };
108
109         if ((pd.pd_dev = open(PF_SOCKET, O_RDWR)) == -1) {
110                 return (-1);
111         }
112         if (ioctl(pd.pd_dev, DIOCGETSTATUS, &status) == -1) {
113                 return (-1);
114         }
115
116         close(pd.pd_dev);
117         for (i = 0; i < PFRES_MAX; i++)
118                 submit_counter("pf_counters", cnames[i], status.counters[i]);
119         for (i = 0; i < LCNT_MAX; i++)
120                 submit_counter("pf_limits", lnames[i], status.lcounters[i]);
121         for (i = 0; i < FCNT_MAX; i++)
122                 submit_counter("pf_state", names[i], status.fcounters[i]);
123         for (i = 0; i < SCNT_MAX; i++)
124                 submit_counter("pf_source", names[i], status.scounters[i]);
125         return (0);
126 }
127
128 #ifdef TEST
129 int
130 main(int argc, char *argv[])
131 {
132         if (pf_init())
133                 err(1, "pf_init");
134         if (pf_read())
135                 err(1, "pf_read");
136         return (0);
137 }
138 #else
139 void module_register(void) {
140         plugin_register_init("pf", pf_init);
141         plugin_register_read("pf", pf_read);
142 }
143 #endif
144