pf plugin: Remove the init() callback.
[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 "collectd.h"
19 #include "plugin.h"
20
21 static char *pf_device = "/dev/pf";
22
23 static void submit_counter (char const *type, char const *inst,
24                 counter_t val, _Bool usegauge)
25 {
26         value_t         values[1];
27         value_list_t    vl = VALUE_LIST_INIT;
28
29         if (usegauge)
30                 values[0].gauge = val;
31         else
32                 values[0].counter = val;
33
34         vl.values = values;
35         vl.values_len = 1;
36         sstrncpy (vl.host, hostname_g, sizeof (vl.host));
37         sstrncpy (vl.plugin, "pf", sizeof (vl.plugin));
38         sstrncpy (vl.type, type, sizeof(vl.type));
39         sstrncpy (vl.type_instance, inst, sizeof(vl.type_instance));
40         plugin_dispatch_values(&vl);
41 }
42
43 static int pf_read (void)
44 {
45         struct pf_status        status;
46         int                     pfdev = -1;
47         int                     i;
48
49         char            *cnames[] = PFRES_NAMES;
50         char            *lnames[] = LCNT_NAMES;
51         char            *names[] = { "searches", "inserts", "removals" };
52
53         if ((pfdev = open(pf_device, O_RDONLY)) == -1) {
54                 ERROR("unable to open %s", pf_device);
55                 return (-1);
56         }
57
58         if (ioctl(pfdev, DIOCGETSTATUS, &status) == -1) {
59                 ERROR("DIOCGETSTATUS: %i", pfdev);
60                 close(pfdev);
61                 return (-1);
62         }
63
64         close(pfdev);
65
66         if (!status.running)
67                 return (-1);
68
69         for (i = 0; i < PFRES_MAX; i++)
70                 submit_counter("pf_counters", cnames[i], status.counters[i], 0);
71         for (i = 0; i < LCNT_MAX; i++)
72                 submit_counter("pf_limits", lnames[i], status.lcounters[i], 0);
73         for (i = 0; i < FCNT_MAX; i++)
74                 submit_counter("pf_state", names[i], status.fcounters[i], 0);
75         for (i = 0; i < SCNT_MAX; i++)
76                 submit_counter("pf_source", names[i], status.scounters[i], 0);
77
78         submit_counter("pf_states", "current", status.states, 1);
79
80         return (0);
81 }
82
83 void module_register (void)
84 {
85         plugin_register_read("pf", pf_read);
86 }