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