pf plugin: Remove the init() callback.
[collectd.git] / src / pf.c
index 02eb5ae..4b5154d 100644 (file)
--- a/src/pf.c
+++ b/src/pf.c
  * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
  */
 
-#include "pfcommon.h"
+#include "collectd.h"
+#include "plugin.h"
 
-static int     pf_init(void);
-static int     pf_read(void);
-static void    submit_counter(const char *, const char *, counter_t);
+static char *pf_device = "/dev/pf";
 
-void
-submit_counter(const char *type, const char *inst, counter_t val)
+static void submit_counter (char const *type, char const *inst,
+               counter_t val, _Bool usegauge)
 {
-#ifndef TEST
        value_t         values[1];
        value_list_t    vl = VALUE_LIST_INIT;
 
-       values[0].gauge = val;
+       if (usegauge)
+               values[0].gauge = val;
+       else
+               values[0].counter = val;
 
        vl.values = values;
        vl.values_len = 1;
@@ -37,74 +38,49 @@ submit_counter(const char *type, const char *inst, counter_t val)
        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
 }
 
-
-int
-pf_init(void)
+static int pf_read (void)
 {
        struct pf_status        status;
-
-       if ((dev = open(PF_SOCKET, O_RDWR)) == -1) {
-               return (-1);
-       }
-       if (ioctl(dev, DIOCGETSTATUS, &status) == -1) {
-               return (-1);
-       }
-
-       close(dev);
-       if (!status.running)
-               return (-1);
-
-       return (0);
-}
-
-int
-pf_read(void)
-{
+       int                     pfdev = -1;
        int                     i;
-       struct pf_status        status;
 
        char            *cnames[] = PFRES_NAMES;
        char            *lnames[] = LCNT_NAMES;
        char            *names[] = { "searches", "inserts", "removals" };
 
-       if ((dev = open(PF_SOCKET, O_RDWR)) == -1) {
+       if ((pfdev = open(pf_device, O_RDONLY)) == -1) {
+               ERROR("unable to open %s", pf_device);
                return (-1);
        }
-       if (ioctl(dev, DIOCGETSTATUS, &status) == -1) {
+
+       if (ioctl(pfdev, DIOCGETSTATUS, &status) == -1) {
+               ERROR("DIOCGETSTATUS: %i", pfdev);
+               close(pfdev);
                return (-1);
        }
 
-       close(dev);
+       close(pfdev);
+
+       if (!status.running)
+               return (-1);
+
        for (i = 0; i < PFRES_MAX; i++)
-               submit_counter("pf_counters", cnames[i], status.counters[i]);
+               submit_counter("pf_counters", cnames[i], status.counters[i], 0);
        for (i = 0; i < LCNT_MAX; i++)
-               submit_counter("pf_limits", lnames[i], status.lcounters[i]);
+               submit_counter("pf_limits", lnames[i], status.lcounters[i], 0);
        for (i = 0; i < FCNT_MAX; i++)
-               submit_counter("pf_state", names[i], status.fcounters[i]);
+               submit_counter("pf_state", names[i], status.fcounters[i], 0);
        for (i = 0; i < SCNT_MAX; i++)
-               submit_counter("pf_source", names[i], status.scounters[i]);
+               submit_counter("pf_source", names[i], status.scounters[i], 0);
+
+       submit_counter("pf_states", "current", status.states, 1);
+
        return (0);
 }
 
-#ifdef TEST
-int
-main(int argc, char *argv[])
+void module_register (void)
 {
-       if (pf_init())
-               err(1, "pf_init");
-       if (pf_read())
-               err(1, "pf_read");
-       return (0);
-}
-#else
-void module_register(void) {
-       plugin_register_init("pf", pf_init);
        plugin_register_read("pf", pf_read);
 }
-#endif
-