8dc6d83b638783d799afdcdd3f163d1a152a8d80
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 */
18 #include "collectd.h"
19 #include "plugin.h"
21 #include <sys/ioctl.h>
22 #include <sys/socket.h>
23 #include <net/if.h>
24 #include <net/pfvar.h>
25 #include <paths.h>
26 #include <err.h>
27 #include <pwd.h>
29 static char const *pf_reasons[PFRES_MAX+1] = PFRES_NAMES;
30 static char const *pf_lcounters[LCNT_MAX+1] = LCNT_NAMES;
31 static char const *pf_fcounters[FCNT_MAX+1] = FCNT_NAMES;
32 static char const *pf_scounters[FCNT_MAX+1] = FCNT_NAMES;
34 static char const *pf_device = "/dev/pf";
36 static void pf_submit (char const *type, char const *type_instance,
37 uint64_t val, _Bool is_gauge)
38 {
39 value_t values[1];
40 value_list_t vl = VALUE_LIST_INIT;
42 if (is_gauge)
43 values[0].gauge = (gauge_t) val;
44 else
45 values[0].derive = (derive_t) val;
47 vl.values = values;
48 vl.values_len = 1;
49 sstrncpy (vl.host, hostname_g, sizeof (vl.host));
50 sstrncpy (vl.plugin, "pf", sizeof (vl.plugin));
51 sstrncpy (vl.type, type, sizeof(vl.type));
52 sstrncpy (vl.type_instance, type_instance, sizeof(vl.type_instance));
54 plugin_dispatch_values(&vl);
55 } /* void pf_submit */
57 static int pf_read (void)
58 {
59 struct pf_status state;
60 int fd;
61 int status;
62 int i;
64 fd = open (pf_device, O_RDONLY);
65 if (fd < 0)
66 {
67 char errbuf[1024];
68 ERROR("pf plugin: Unable to open %s: %s",
69 pf_device,
70 sstrerror (errno, errbuf, sizeof (errbuf)));
71 return (-1);
72 }
74 memset (&state, 0, sizeof (state));
75 status = ioctl (fd, DIOCGETSTATUS, &state);
76 if (status != 0)
77 {
78 char errbuf[1024];
79 ERROR("pf plugin: ioctl(DIOCGETSTATUS) failed: %s",
80 sstrerror (errno, errbuf, sizeof (errbuf)));
81 close(fd);
82 return (-1);
83 }
85 close (fd);
86 fd = -1;
88 if (!status.running)
89 {
90 WARNING ("pf plugin: PF is not running.");
91 return (-1);
92 }
94 for (i = 0; i < PFRES_MAX; i++)
95 pf_submit ("pf_counters", pf_reasons[i], state.counters[i],
96 /* is gauge = */ 0);
97 for (i = 0; i < LCNT_MAX; i++)
98 pf_submit ("pf_limits", pf_lcounters[i], state.lcounters[i],
99 /* is gauge = */ 0);
100 for (i = 0; i < FCNT_MAX; i++)
101 pf_submit ("pf_state", pf_fcounters[i], state.fcounters[i],
102 /* is gauge = */ 0);
103 for (i = 0; i < SCNT_MAX; i++)
104 pf_submit ("pf_source", pf_scounters[i], state.scounters[i],
105 /* is gauge = */ 0);
107 pf_submit ("pf_states", "current", (uint32_t) state.states,
108 /* is gauge = */ 1);
110 return (0);
111 } /* int pf_read */
113 void module_register (void)
114 {
115 plugin_register_read ("pf", pf_read);
116 }