X-Git-Url: https://git.octo.it/?p=collectd.git;a=blobdiff_plain;f=src%2Fpf.c;h=82adc53b275d2b10f0a194e586d220121f9fe4a5;hp=d7a5a15cff11b4cd3d2baf07297161fcc56867f3;hb=da11ce02eb202b3e01d3e2d1b40f248a84430973;hpb=ed985cb9d3d89aec3490b17e0fda3ee97908f935 diff --git a/src/pf.c b/src/pf.c index d7a5a15c..82adc53b 100644 --- a/src/pf.c +++ b/src/pf.c @@ -21,117 +21,107 @@ #include "collectd.h" -#include "plugin.h" #include "common.h" +#include "plugin.h" #if HAVE_SYS_IOCTL_H -# include +#include #endif #if HAVE_NET_IF_H -# include +#include #endif #if HAVE_NETINET_IN_H -# include +#include #endif #include #ifndef FCNT_NAMES -# if FCNT_MAX != 3 -# error "Unexpected value for FCNT_MAX" -# endif -# define FCNT_NAMES {"search", "insert", "removals", NULL}; +#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}; +#if SCNT_MAX != 3 +#error "Unexpected value for SCNT_MAX" +#endif +#define SCNT_NAMES {"search", "insert", "removals", NULL}; #endif -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 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 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); +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.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; - - 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); - } - - 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); - - if (!state.running) - { - WARNING ("pf plugin: PF is not running."); - return (-1); - } - - for (int i = 0; i < PFRES_MAX; i++) - pf_submit ("pf_counters", pf_reasons[i], state.counters[i], - /* is gauge = */ 0); - for (int i = 0; i < LCNT_MAX; i++) - pf_submit ("pf_limits", pf_lcounters[i], state.lcounters[i], - /* is gauge = */ 0); - for (int i = 0; i < FCNT_MAX; i++) - pf_submit ("pf_state", pf_fcounters[i], state.fcounters[i], - /* is gauge = */ 0); - for (int 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); +static int pf_read(void) { + struct pf_status state; + int fd; + int status; + + 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; + } + + 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); + + if (!state.running) { + WARNING("pf plugin: PF is not running."); + return -1; + } + + for (int i = 0; i < PFRES_MAX; i++) + pf_submit("pf_counters", pf_reasons[i], state.counters[i], + /* is gauge = */ 0); + for (int i = 0; i < LCNT_MAX; i++) + pf_submit("pf_limits", pf_lcounters[i], state.lcounters[i], + /* is gauge = */ 0); + for (int i = 0; i < FCNT_MAX; i++) + pf_submit("pf_state", pf_fcounters[i], state.fcounters[i], + /* is gauge = */ 0); + for (int 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); -} +void module_register(void) { plugin_register_read("pf", pf_read); }