X-Git-Url: https://git.octo.it/?a=blobdiff_plain;f=src%2Fpf.c;h=33e2d4b964c0ef8dc5d45058c383a5a7889dfd55;hb=22d0859f5c5c43adc8c58ff0dd40be79528681eb;hp=449567afb77b61ee1a19ee2dbb748cf7fab61276;hpb=223535fabc55e3ef8932e9b429177dcfd2b6b62f;p=collectd.git diff --git a/src/pf.c b/src/pf.c index 449567af..33e2d4b9 100644 --- a/src/pf.c +++ b/src/pf.c @@ -1,5 +1,6 @@ /* * Copyright (c) 2010 Pierre-Yves Ritschard + * Copyright (c) 2011 Stefan Rinkes * * Permission to use, copy, modify, and distribute this software for any * purpose with or without fee is hereby granted, provided that the above @@ -14,103 +15,49 @@ * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. */ -#include -#include -#include - -#include -#include - -#include -#include -#include -#include -#include -#include -#include -#include - -#ifndef TEST -#include "collectd.h" -#include "common.h" -#include "plugin.h" -#include "configfile.h" -#else -#include -typedef u_int64_t counter_t; -#endif - -#define PF_SOCKET "/dev/pf" - -struct pfdata { - int pd_dev; -}; - -static struct pfdata pd; - -static int pf_init(void); -static int pf_read(void); -static void submit_counter(const char *, const char *, counter_t); - -void -submit_counter(const char *type, const char *inst, counter_t val) -{ -#ifndef TEST - value_t values[1]; - value_list_t vl = VALUE_LIST_INIT; - - values[0].counter = 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, inst, sizeof(vl.type_instance)); - plugin_dispatch_values(&vl); -#else - printf("%s.%s: %lld\n", type, inst, val); -#endif -} +#include "pfcommon.h" +static int pf_init(void); +static int pf_read(void); +static void submit_counter(const char *, const char *, counter_t); int pf_init(void) { struct pf_status status; - memset(&pd, '\0', sizeof(pd)); - - if ((pd.pd_dev = open(PF_SOCKET, O_RDWR)) == -1) { + if ((dev = open(PF_SOCKET, O_RDONLY)) == -1) { return (-1); } - if (ioctl(pd.pd_dev, DIOCGETSTATUS, &status) == -1) { + if (ioctl(dev, DIOCGETSTATUS, &status) == -1) { return (-1); } - close(pd.pd_dev); + + close(dev); if (!status.running) return (-1); - + return (0); } int pf_read(void) { - int i; - struct pf_status status; + int i; + struct pf_status status; - char *cnames[] = PFRES_NAMES; - char *lnames[] = LCNT_NAMES; - char *names[] = { "searches", "inserts", "removals" }; + char *cnames[] = PFRES_NAMES; + char *lnames[] = LCNT_NAMES; + char *names[] = { "searches", "inserts", "removals" }; - if ((pd.pd_dev = open(PF_SOCKET, O_RDWR)) == -1) { + if ((dev = open(PF_SOCKET, O_RDONLY)) == -1) { return (-1); } - if (ioctl(pd.pd_dev, DIOCGETSTATUS, &status) == -1) { + if (ioctl(dev, DIOCGETSTATUS, &status) == -1) { return (-1); } - close(pd.pd_dev); + + close(dev); for (i = 0; i < PFRES_MAX; i++) submit_counter("pf_counters", cnames[i], status.counters[i]); for (i = 0; i < LCNT_MAX; i++) @@ -122,6 +69,27 @@ pf_read(void) return (0); } +void +submit_counter(const char *type, const char *inst, counter_t val) +{ +#ifndef TEST + value_t values[1]; + value_list_t vl = VALUE_LIST_INIT; + + values[0].counter = 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, inst, sizeof(vl.type_instance)); + plugin_dispatch_values(&vl); +#else + printf("%s.%s: %lld\n", type, inst, val); +#endif +} + #ifdef TEST int main(int argc, char *argv[]) @@ -138,4 +106,3 @@ void module_register(void) { plugin_register_read("pf", pf_read); } #endif -