1 /*
2 * Copyright (c) 2010 Pierre-Yves Ritschard
3 * Copyright (c) 2011 Stefan Rinkes
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 * Authors:
18 * Pierre-Yves Ritschard <pyr at openbsd.org>
19 * Stefan Rinkes <stefan.rinkes at gmail.org>
20 */
22 #include "collectd.h"
24 #include "plugin.h"
25 #include "common.h"
27 #if HAVE_SYS_IOCTL_H
28 # include <sys/ioctl.h>
29 #endif
30 #if HAVE_NET_IF_H
31 # include <net/if.h>
32 #endif
33 #if HAVE_NETINET_IN_H
34 # include <netinet/in.h>
35 #endif
37 #include <net/pfvar.h>
39 #ifndef FCNT_NAMES
40 # if FCNT_MAX != 3
41 # error "Unexpected value for FCNT_MAX"
42 # endif
43 # define FCNT_NAMES {"search", "insert", "removals", NULL};
44 #endif
46 #ifndef SCNT_NAMES
47 # if SCNT_MAX != 3
48 # error "Unexpected value for SCNT_MAX"
49 # endif
50 # define SCNT_NAMES {"search", "insert", "removals", NULL};
51 #endif
53 static char const *pf_reasons[PFRES_MAX+1] = PFRES_NAMES;
54 static char const *pf_lcounters[LCNT_MAX+1] = LCNT_NAMES;
55 static char const *pf_fcounters[FCNT_MAX+1] = FCNT_NAMES;
56 static char const *pf_scounters[SCNT_MAX+1] = SCNT_NAMES;
58 static char const *pf_device = "/dev/pf";
60 static void pf_submit (char const *type, char const *type_instance,
61 uint64_t val, _Bool is_gauge)
62 {
63 value_t values[1];
64 value_list_t vl = VALUE_LIST_INIT;
66 if (is_gauge)
67 values[0].gauge = (gauge_t) val;
68 else
69 values[0].derive = (derive_t) val;
71 vl.values = values;
72 vl.values_len = 1;
73 sstrncpy (vl.plugin, "pf", sizeof (vl.plugin));
74 sstrncpy (vl.type, type, sizeof(vl.type));
75 sstrncpy (vl.type_instance, type_instance, sizeof(vl.type_instance));
77 plugin_dispatch_values(&vl);
78 } /* void pf_submit */
80 static int pf_read (void)
81 {
82 struct pf_status state;
83 int fd;
84 int status;
86 fd = open (pf_device, O_RDONLY);
87 if (fd < 0)
88 {
89 char errbuf[1024];
90 ERROR("pf plugin: Unable to open %s: %s",
91 pf_device,
92 sstrerror (errno, errbuf, sizeof (errbuf)));
93 return (-1);
94 }
96 status = ioctl (fd, DIOCGETSTATUS, &state);
97 if (status != 0)
98 {
99 char errbuf[1024];
100 ERROR("pf plugin: ioctl(DIOCGETSTATUS) failed: %s",
101 sstrerror (errno, errbuf, sizeof (errbuf)));
102 close(fd);
103 return (-1);
104 }
106 close (fd);
108 if (!state.running)
109 {
110 WARNING ("pf plugin: PF is not running.");
111 return (-1);
112 }
114 for (int i = 0; i < PFRES_MAX; i++)
115 pf_submit ("pf_counters", pf_reasons[i], state.counters[i],
116 /* is gauge = */ 0);
117 for (int i = 0; i < LCNT_MAX; i++)
118 pf_submit ("pf_limits", pf_lcounters[i], state.lcounters[i],
119 /* is gauge = */ 0);
120 for (int i = 0; i < FCNT_MAX; i++)
121 pf_submit ("pf_state", pf_fcounters[i], state.fcounters[i],
122 /* is gauge = */ 0);
123 for (int i = 0; i < SCNT_MAX; i++)
124 pf_submit ("pf_source", pf_scounters[i], state.scounters[i],
125 /* is gauge = */ 0);
127 pf_submit ("pf_states", "current", (uint32_t) state.states,
128 /* is gauge = */ 1);
130 return (0);
131 } /* int pf_read */
133 void module_register (void)
134 {
135 plugin_register_read ("pf", pf_read);
136 }