Code

Merge branch 'sr/pf'
authorFlorian Forster <octo@collectd.org>
Sat, 17 Nov 2012 08:42:55 +0000 (09:42 +0100)
committerFlorian Forster <octo@collectd.org>
Sat, 17 Nov 2012 08:42:55 +0000 (09:42 +0100)
configure.in
src/Makefile.am
src/pf.c [new file with mode: 0644]
src/types.db

index 7c4ec3a2da07f0ab5d091e32e99e2fced01d0cae..cd24f536c0328fbf0c28307fb9af5e0b76ad7d41 100644 (file)
@@ -535,6 +535,9 @@ AC_CHECK_HEADERS(netinet/if_ether.h, [], [],
 
 AC_CHECK_HEADERS(netinet/ip_compat.h)
 
+have_net_pfvar_h="no"
+AC_CHECK_HEADERS(net/pfvar.h, [have_net_pfvar_h="yes"])
+
 # For the multimeter plugin
 have_termios_h="no"
 AC_CHECK_HEADERS(termios.h, [have_termios_h="yes"])
@@ -4919,6 +4922,7 @@ AC_PLUGIN([onewire],     [$with_libowcapi],    [OneWire sensor statistics])
 AC_PLUGIN([openvpn],     [yes],                [OpenVPN client statistics])
 AC_PLUGIN([oracle],      [$with_oracle],       [Oracle plugin])
 AC_PLUGIN([perl],        [$plugin_perl],       [Embed a Perl interpreter])
+AC_PLUGIN([pf],          [$have_net_pfvar_h],  [BSD packet filter (PF) statistics])
 # FIXME: Check for libevent, too.
 AC_PLUGIN([pinba],       [$have_protoc_c],     [Pinba statistics])
 AC_PLUGIN([ping],        [$with_liboping],     [Network latency statistics])
@@ -5252,6 +5256,7 @@ Configuration:
     openvpn . . . . . . . $enable_openvpn
     oracle  . . . . . . . $enable_oracle
     perl  . . . . . . . . $enable_perl
+    pf  . . . . . . . . . $enable_pf
     pinba . . . . . . . . $enable_pinba
     ping  . . . . . . . . $enable_ping
     postgresql  . . . . . $enable_postgresql
index 3532e7b2d63875f21babd0d4bc309b234d40c8c7..26c2355e50a0aead892ddc9fb3bc10156dc05a54 100644 (file)
@@ -888,6 +888,14 @@ collectd_LDADD += "-dlopen" perl.la
 collectd_DEPENDENCIES += perl.la
 endif
 
+if BUILD_PLUGIN_PF
+pkglib_LTLIBRARIES += pf.la
+pf_la_SOURCES = pf.c
+pf_la_LDFLAGS = -module -avoid-version
+collectd_LDADD += "-dlopen" pf.la
+collectd_DEPENDENCIES += pf.la
+endif
+
 if BUILD_PLUGIN_PINBA
 BUILT_SOURCES += pinba.pb-c.c pinba.pb-c.h
 CLEANFILES += pinba.pb-c.c pinba.pb-c.h
diff --git a/src/pf.c b/src/pf.c
new file mode 100644 (file)
index 0000000..da539a2
--- /dev/null
+++ b/src/pf.c
@@ -0,0 +1,117 @@
+/*
+ * Copyright (c) 2010 Pierre-Yves Ritschard <pyr@openbsd.org>
+ * Copyright (c) 2011 Stefan Rinkes <stefan.rinkes@gmail.org>
+ *
+ * Permission to use, copy, modify, and distribute this software for any
+ * purpose with or without fee is hereby granted, provided that the above
+ * copyright notice and this permission notice appear in all copies.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
+ * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
+ * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
+ * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
+ * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
+ * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
+ * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
+ */
+
+#include "collectd.h"
+#include "plugin.h"
+#include "common.h"
+
+#include <sys/ioctl.h>
+#include <sys/socket.h>
+#include <net/if.h>
+#include <net/pfvar.h>
+#include <paths.h>
+#include <err.h>
+#include <pwd.h>
+
+static char const *pf_reasons[PFRES_MAX+1] = PFRES_NAMES;
+static char const *pf_lcounters[LCNT_MAX+1] = LCNT_NAMES;
+static char const *pf_fcounters[FCNT_MAX+1] = FCNT_NAMES;
+static char const *pf_scounters[FCNT_MAX+1] = FCNT_NAMES;
+
+static char const *pf_device = "/dev/pf";
+
+static void pf_submit (char const *type, char const *type_instance,
+               uint64_t val, _Bool is_gauge)
+{
+       value_t         values[1];
+       value_list_t    vl = VALUE_LIST_INIT;
+
+       if (is_gauge)
+               values[0].gauge = (gauge_t) val;
+       else
+               values[0].derive = (derive_t) val;
+
+       vl.values = values;
+       vl.values_len = 1;
+       sstrncpy (vl.host, hostname_g, sizeof (vl.host));
+       sstrncpy (vl.plugin, "pf", sizeof (vl.plugin));
+       sstrncpy (vl.type, type, sizeof(vl.type));
+       sstrncpy (vl.type_instance, type_instance, sizeof(vl.type_instance));
+
+       plugin_dispatch_values(&vl);
+} /* void pf_submit */
+
+static int pf_read (void)
+{
+       struct pf_status state;
+       int fd;
+       int status;
+       int i;
+
+       fd = open (pf_device, O_RDONLY);
+       if (fd < 0)
+       {
+               char errbuf[1024];
+               ERROR("pf plugin: Unable to open %s: %s",
+                               pf_device,
+                               sstrerror (errno, errbuf, sizeof (errbuf)));
+               return (-1);
+       }
+
+       memset (&state, 0, sizeof (state));
+       status = ioctl (fd, DIOCGETSTATUS, &state);
+       if (status != 0)
+       {
+               char errbuf[1024];
+               ERROR("pf plugin: ioctl(DIOCGETSTATUS) failed: %s",
+                               sstrerror (errno, errbuf, sizeof (errbuf)));
+               close(fd);
+               return (-1);
+       }
+
+       close (fd);
+       fd = -1;
+
+       if (!status.running)
+       {
+               WARNING ("pf plugin: PF is not running.");
+               return (-1);
+       }
+
+       for (i = 0; i < PFRES_MAX; i++)
+               pf_submit ("pf_counters", pf_reasons[i], state.counters[i],
+                               /* is gauge = */ 0);
+       for (i = 0; i < LCNT_MAX; i++)
+               pf_submit ("pf_limits", pf_lcounters[i], state.lcounters[i],
+                               /* is gauge = */ 0);
+       for (i = 0; i < FCNT_MAX; i++)
+               pf_submit ("pf_state", pf_fcounters[i], state.fcounters[i],
+                               /* is gauge = */ 0);
+       for (i = 0; i < SCNT_MAX; i++)
+               pf_submit ("pf_source", pf_scounters[i], state.scounters[i],
+                               /* is gauge = */ 0);
+
+       pf_submit ("pf_states", "current", (uint32_t) state.states,
+                       /* is gauge = */ 1);
+
+       return (0);
+} /* int pf_read */
+
+void module_register (void)
+{
+       plugin_register_read ("pf", pf_read);
+}
index ed27b7854fb568a3a347fc2f9330dd23e6cc766b..25f4d7ca534dcadb1627124d6a83b9c4e847cf17 100644 (file)
@@ -60,8 +60,8 @@ fanspeed              value:GAUGE:0:U
 file_size              value:GAUGE:0:U
 files                  value:GAUGE:0:U
 fork_rate              value:DERIVE:0:U
-frequency              value:GAUGE:0:U
 frequency_offset       value:GAUGE:-1000000:1000000
+frequency              value:GAUGE:0:U
 fscache_stat           value:DERIVE:0:U
 gauge                  value:GAUGE:U:U
 hash_collisions                value:DERIVE:0:U
@@ -109,6 +109,11 @@ node_stat          value:DERIVE:0:U
 node_tx_rate           value:GAUGE:0:127
 operations             value:DERIVE:0:U
 percent                        value:GAUGE:0:100.1
+pf_counters            value:DERIVE:0:U
+pf_limits              value:DERIVE:0:U
+pf_source              value:DERIVE:0:U
+pf_states              value:GAUGE:0:U
+pf_state               value:DERIVE:0:U
 pg_blks                        value:DERIVE:0:U
 pg_db_size             value:GAUGE:0:U
 pg_n_tup_c             value:DERIVE:0:U
@@ -117,8 +122,8 @@ pg_numbackends              value:GAUGE:0:U
 pg_scan                        value:DERIVE:0:U
 pg_xact                        value:DERIVE:0:U
 ping_droprate          value:GAUGE:0:100
-ping                   value:GAUGE:0:65535
 ping_stddev            value:GAUGE:0:65535
+ping                   value:GAUGE:0:65535
 players                        value:GAUGE:0:1000000
 power                  value:GAUGE:0:U
 protocol_counter       value:DERIVE:0:U