pf plugin: some code 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 "pfcommon.h"
19
20 static int      pf_init(void);
21 static int      pf_read(void);
22 static void     submit_counter(const char *, const char *, counter_t);
23
24 int
25 pf_init(void)
26 {
27         struct pf_status        status;
28
29         if ((dev = open(PF_SOCKET, O_RDONLY)) == -1) {
30                 return (-1);
31         }
32         if (ioctl(dev, DIOCGETSTATUS, &status) == -1) {
33                 return (-1);
34         }
35
36         close(dev);
37         if (!status.running)
38                 return (-1);
39
40         return (0);
41 }
42
43 int
44 pf_read(void)
45 {
46         int                     i;
47         struct pf_status        status;
48
49         char            *cnames[] = PFRES_NAMES;
50         char            *lnames[] = LCNT_NAMES;
51         char            *names[] = { "searches", "inserts", "removals" };
52
53         if ((dev = open(PF_SOCKET, O_RDONLY)) == -1) {
54                 return (-1);
55         }
56         if (ioctl(dev, DIOCGETSTATUS, &status) == -1) {
57                 return (-1);
58         }
59
60         close(dev);
61         for (i = 0; i < PFRES_MAX; i++)
62                 submit_counter("pf_counters", cnames[i], status.counters[i]);
63         for (i = 0; i < LCNT_MAX; i++)
64                 submit_counter("pf_limits", lnames[i], status.lcounters[i]);
65         for (i = 0; i < FCNT_MAX; i++)
66                 submit_counter("pf_state", names[i], status.fcounters[i]);
67         for (i = 0; i < SCNT_MAX; i++)
68                 submit_counter("pf_source", names[i], status.scounters[i]);
69         return (0);
70 }
71
72 void
73 submit_counter(const char *type, const char *inst, counter_t val)
74 {
75 #ifndef TEST
76         value_t         values[1];
77         value_list_t    vl = VALUE_LIST_INIT;
78
79         values[0].counter = val;
80
81         vl.values = values;
82         vl.values_len = 1;
83         sstrncpy (vl.host, hostname_g, sizeof (vl.host));
84         sstrncpy (vl.plugin, "pf", sizeof (vl.plugin));
85         sstrncpy (vl.type, type, sizeof(vl.type));
86         sstrncpy (vl.type_instance, inst, sizeof(vl.type_instance));
87         plugin_dispatch_values(&vl);
88 #else
89         printf("%s.%s: %lld\n", type, inst, val);
90 #endif
91 }
92
93 #ifdef TEST
94 int
95 main(int argc, char *argv[])
96 {
97         if (pf_init())
98                 err(1, "pf_init");
99         if (pf_read())
100                 err(1, "pf_read");
101         return (0);
102 }
103 #else
104 void module_register(void) {
105         plugin_register_init("pf", pf_init);
106         plugin_register_read("pf", pf_read);
107 }
108 #endif