projects
/
collectd.git
/ blobdiff
commit
grep
author
committer
pickaxe
?
search:
re
summary
|
shortlog
|
log
|
commit
|
commitdiff
|
tree
raw
|
inline
| side by side
Attempt to fix collectd/collectd#229 by doing user / group lookups before we fork.
[collectd.git]
/
src
/
pf.c
diff --git
a/src/pf.c
b/src/pf.c
index
513634c
..
44f0c7b
100644
(file)
--- a/
src/pf.c
+++ b/
src/pf.c
@@
-1,5
+1,6
@@
/*
* Copyright (c) 2010 Pierre-Yves Ritschard <pyr@openbsd.org>
/*
* 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
*
* Permission to use, copy, modify, and distribute this software for any
* purpose with or without fee is hereby granted, provided that the above
@@
-14,123
+15,121
@@
* OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
*/
* OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
*/
-#include <sys/types.h>
-#include <sys/ioctl.h>
-#include <sys/socket.h>
-
-#include <net/if.h>
-#include <net/pfvar.h>
-
-#include <limits.h>
-#include <fcntl.h>
-#include <paths.h>
-#include <string.h>
-#include <stdio.h>
-#include <stdlib.h>
-#include <errno.h>
-
-#ifndef TEST
#include "collectd.h"
#include "plugin.h"
#include "collectd.h"
#include "plugin.h"
-#else
-typedef u_int64_t counter_t;
+#include "common.h"
+
+#if HAVE_SYS_IOCTL_H
+# include <sys/ioctl.h>
+#endif
+#if HAVE_SYS_SOCKET_H
+# include <sys/socket.h>
+#endif
+#if HAVE_NET_IF_H
+# include <net/if.h>
#endif
#endif
-#
define PF_SOCKET "/dev/pf"
+#
include <net/pfvar.h>
-struct pfdata {
- int pd_dev;
-};
+#ifndef FCNT_NAMES
+# if FCNT_MAX != 3
+# error "Unexpected value for FCNT_MAX"
+# endif
+# define FCNT_NAMES {"search", "insert", "removals", NULL};
+#endif
+
+#ifndef SCNT_NAMES
+# if SCNT_MAX != 3
+# error "Unexpected value for SCNT_MAX"
+# endif
+# define SCNT_NAMES {"search", "insert", "removals", NULL};
+#endif
-static struct pfdata pd;
+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[SCNT_MAX+1] = SCNT_NAMES;
-static int pf_init(void);
-static int pf_read(void);
-static void submit_counter(const char *, const char *, counter_t);
+static char const *pf_device = "/dev/pf";
-void
-
submit_counter(const char *type, const char *inst, counter_t val
)
+static void pf_submit (char const *type, char const *type_instance,
+
uint64_t val, _Bool is_gauge
)
{
{
-#ifndef TEST
value_t values[1];
value_list_t vl = VALUE_LIST_INIT;
value_t values[1];
value_list_t vl = VALUE_LIST_INIT;
- values[0].counter = val;
+ if (is_gauge)
+ values[0].gauge = (gauge_t) val;
+ else
+ values[0].derive = (derive_t) val;
+
vl.values = values;
vl.values_len = 1;
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));
- strlcpy(vl.host, hostname_g, sizeof(vl.host));
- strlcpy(vl.plugin, "pf", sizeof(vl.plugin));
- strlcpy(vl.type, type, sizeof(vl.type));
- strlcpy(vl.type_instance, inst, sizeof(vl.type_instance));
plugin_dispatch_values(&vl);
plugin_dispatch_values(&vl);
-#else
- printf("%s.%s: %lld\n", type, inst, val);
-#endif
-}
-
+} /* void pf_submit */
-int
-pf_init(void)
+static int pf_read (void)
{
{
- struct pf_status status;
-
- memset(&pd, '\0', sizeof(pd));
-
- if ((pd.pd_dev = open(PF_SOCKET, O_RDWR)) == -1) {
+ 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);
}
return (-1);
}
- if (ioctl(pd.pd_dev, DIOCGETSTATUS, &status) == -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);
}
return (-1);
}
- close(pd.pd_dev);
- if (!status.running)
- return (-1);
-
- return (0);
-}
-
-int
-pf_read(void)
-{
- int i;
- struct pf_status status;
- char *cnames[] = PFRES_NAMES;
- char *lnames[] = LCNT_NAMES;
- char *names[] = { "searches", "inserts", "removals" };
+ close (fd);
+ fd = -1;
- if ((pd.pd_dev = open(PF_SOCKET, O_RDWR)) == -1) {
+ if (!state.running)
+ {
+ WARNING ("pf plugin: PF is not running.");
return (-1);
}
return (-1);
}
- if (ioctl(pd.pd_dev, DIOCGETSTATUS, &status) == -1) {
- return (-1);
- }
- close(pd.pd_dev);
+
for (i = 0; i < PFRES_MAX; i++)
for (i = 0; i < PFRES_MAX; i++)
- submit_counter("pf_counters", cnames[i], status.counters[i]);
+ pf_submit ("pf_counters", pf_reasons[i], state.counters[i],
+ /* is gauge = */ 0);
for (i = 0; i < LCNT_MAX; i++)
for (i = 0; i < LCNT_MAX; i++)
- submit_counter("pf_limits", lnames[i], status.lcounters[i]);
+ pf_submit ("pf_limits", pf_lcounters[i], state.lcounters[i],
+ /* is gauge = */ 0);
for (i = 0; i < FCNT_MAX; i++)
for (i = 0; i < FCNT_MAX; i++)
- submit_counter("pf_state", names[i], status.fcounters[i]);
+ pf_submit ("pf_state", pf_fcounters[i], state.fcounters[i],
+ /* is gauge = */ 0);
for (i = 0; i < SCNT_MAX; i++)
for (i = 0; i < SCNT_MAX; i++)
- submit_counter("pf_source", names[i], status.scounters[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);
return (0);
-}
+}
/* int pf_read */
-#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);
+ plugin_register_read ("pf", pf_read);
}
}
-#endif
\ No newline at end of file