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"
20 #include "common.h"
22 #if HAVE_SYS_IOCTL_H
23 # include <sys/ioctl.h>
24 #endif
25 #if HAVE_SYS_SOCKET_H
26 # include <sys/socket.h>
27 #endif
28 #if HAVE_NET_IF_H
29 # include <net/if.h>
30 #endif
32 #include <net/pfvar.h>
34 #ifndef FCNT_NAMES
35 # if FCNT_MAX != 3
36 # error "Unexpected value for FCNT_MAX"
37 # endif
38 # define FCNT_NAMES {"search", "insert", "removals", NULL};
39 #endif
41 #ifndef SCNT_NAMES
42 # if SCNT_MAX != 3
43 # error "Unexpected value for SCNT_MAX"
44 # endif
45 # define SCNT_NAMES {"search", "insert", "removals", NULL};
46 #endif
48 static char const *pf_reasons[PFRES_MAX+1] = PFRES_NAMES;
49 static char const *pf_lcounters[LCNT_MAX+1] = LCNT_NAMES;
50 static char const *pf_fcounters[FCNT_MAX+1] = FCNT_NAMES;
51 static char const *pf_scounters[SCNT_MAX+1] = SCNT_NAMES;
53 static char const *pf_device = "/dev/pf";
55 static void pf_submit (char const *type, char const *type_instance,
56 uint64_t val, _Bool is_gauge)
57 {
58 value_t values[1];
59 value_list_t vl = VALUE_LIST_INIT;
61 if (is_gauge)
62 values[0].gauge = (gauge_t) val;
63 else
64 values[0].derive = (derive_t) val;
66 vl.values = values;
67 vl.values_len = 1;
68 sstrncpy (vl.host, hostname_g, sizeof (vl.host));
69 sstrncpy (vl.plugin, "pf", sizeof (vl.plugin));
70 sstrncpy (vl.type, type, sizeof(vl.type));
71 sstrncpy (vl.type_instance, type_instance, sizeof(vl.type_instance));
73 plugin_dispatch_values(&vl);
74 } /* void pf_submit */
76 static int pf_read (void)
77 {
78 struct pf_status state;
79 int fd;
80 int status;
81 int i;
83 fd = open (pf_device, O_RDONLY);
84 if (fd < 0)
85 {
86 char errbuf[1024];
87 ERROR("pf plugin: Unable to open %s: %s",
88 pf_device,
89 sstrerror (errno, errbuf, sizeof (errbuf)));
90 return (-1);
91 }
93 memset (&state, 0, sizeof (state));
94 status = ioctl (fd, DIOCGETSTATUS, &state);
95 if (status != 0)
96 {
97 char errbuf[1024];
98 ERROR("pf plugin: ioctl(DIOCGETSTATUS) failed: %s",
99 sstrerror (errno, errbuf, sizeof (errbuf)));
100 close(fd);
101 return (-1);
102 }
104 close (fd);
105 fd = -1;
107 if (!state.running)
108 {
109 WARNING ("pf plugin: PF is not running.");
110 return (-1);
111 }
113 for (i = 0; i < PFRES_MAX; i++)
114 pf_submit ("pf_counters", pf_reasons[i], state.counters[i],
115 /* is gauge = */ 0);
116 for (i = 0; i < LCNT_MAX; i++)
117 pf_submit ("pf_limits", pf_lcounters[i], state.lcounters[i],
118 /* is gauge = */ 0);
119 for (i = 0; i < FCNT_MAX; i++)
120 pf_submit ("pf_state", pf_fcounters[i], state.fcounters[i],
121 /* is gauge = */ 0);
122 for (i = 0; i < SCNT_MAX; i++)
123 pf_submit ("pf_source", pf_scounters[i], state.scounters[i],
124 /* is gauge = */ 0);
126 pf_submit ("pf_states", "current", (uint32_t) state.states,
127 /* is gauge = */ 1);
129 return (0);
130 } /* int pf_read */
132 void module_register (void)
133 {
134 plugin_register_read ("pf", pf_read);
135 }