X-Git-Url: https://git.octo.it/?a=blobdiff_plain;f=src%2Fpf.c;h=0fff6720706966c3ab9c351eedee0a23e60c23f1;hb=f8287addd23543a970453e473829bd3d6c28bcdd;hp=bbac3019f8f128a27a6cde6db54fc9bde7100f79;hpb=92c19723b578f03b12c83a87a7f4fce8c5fc5a20;p=collectd.git diff --git a/src/pf.c b/src/pf.c index bbac3019..0fff6720 100644 --- a/src/pf.c +++ b/src/pf.c @@ -15,108 +15,103 @@ * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. */ -#include "pfcommon.h" +#include "collectd.h" +#include "plugin.h" +#include "common.h" + +#include +#include +#include +#include +#include +#include +#include + +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; -static int pf_init(void); -static int pf_read(void); -static void submit_counter(const char *, const char *, counter_t); + if (is_gauge) + values[0].gauge = (gauge_t) val; + else + values[0].derive = (derive_t) val; -char *pf_device = "/dev/pf"; + 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)); -int -pf_init(void) -{ - struct pf_status status; - int pfdev = -1; + plugin_dispatch_values(&vl); +} /* void pf_submit */ - if ((pfdev = open(pf_device, O_RDONLY)) == -1) { - ERROR("unable to open %s", pf_device); +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); } - if (ioctl(pfdev, DIOCGETSTATUS, &status) == -1) { - ERROR("DIOCGETSTATUS: %i", pfdev); - close(pfdev); + 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(pfdev); - if (!status.running) - return (-1); - - return (0); -} - -int -pf_read(void) -{ - struct pf_status status; - int pfdev = -1; - int i; - - char *cnames[] = PFRES_NAMES; - char *lnames[] = LCNT_NAMES; - char *names[] = { "searches", "inserts", "removals" }; + close (fd); + fd = -1; - if ((pfdev = open(pf_device, O_RDONLY)) == -1) { - ERROR("unable to open %s", pf_device); + if (!state.running) + { + WARNING ("pf plugin: PF is not running."); return (-1); } - if (ioctl(pfdev, DIOCGETSTATUS, &status) == -1) { - ERROR("DIOCGETSTATUS: %i", pfdev); - close(pfdev); - return (-1); - } - - close(pfdev); - 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++) - 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++) - 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++) - submit_counter("pf_source", names[i], status.scounters[i]); - - return (0); -} + pf_submit ("pf_source", pf_scounters[i], state.scounters[i], + /* is gauge = */ 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; + pf_submit ("pf_states", "current", (uint32_t) state.states, + /* is gauge = */ 1); - 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 -} + 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