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