From be69e1c337ad466ab6ab26cf21c3398f97323326 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Z=CC=8Cilvinas=20Valinskas?= Date: Wed, 30 Apr 2014 22:54:15 +0300 Subject: osx: use instead of MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Žilvinas Valinskas Signed-off-by: Thomas Graf --- include/bmon/config.h | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/include/bmon/config.h b/include/bmon/config.h index e717245..ad6a367 100644 --- a/include/bmon/config.h +++ b/include/bmon/config.h @@ -46,7 +46,11 @@ #include #include #include -#include +#ifdef SYS_BSD +# include +#else +# include +#endif #if TIME_WITH_SYS_TIME # include -- cgit v1.2.3 From c62eb9de44361e621f542b79f201f67421ca2a43 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Z=CC=8Cilvinas=20Valinskas?= Date: Wed, 30 Apr 2014 23:11:48 +0300 Subject: osx: bmon v3.2 compiles and links MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Žilvinas Valinskas Signed-off-by: Thomas Graf --- configure.ac | 8 ++++++-- src/in_netlink.c | 3 +++ src/input.c | 2 ++ 3 files changed, 11 insertions(+), 2 deletions(-) diff --git a/configure.ac b/configure.ac index 6e7cc8c..bd1724f 100644 --- a/configure.ac +++ b/configure.ac @@ -69,8 +69,12 @@ fi PKG_CHECK_MODULES([CONFUSE], [libconfuse], [], AC_MSG_ERROR([requires libconfuse])) -PKG_CHECK_MODULES([LIBNL], [libnl-3.0], [], AC_MSG_ERROR([requires libnl3-dev])) -PKG_CHECK_MODULES([LIBNL_ROUTE], [libnl-route-3.0], [], AC_MSG_ERROR([requires libnl3-route])) +case ${target_os} in + linux*) + PKG_CHECK_MODULES([LIBNL], [libnl-3.0], [], AC_MSG_ERROR([requires libnl3-dev])) + PKG_CHECK_MODULES([LIBNL_ROUTE], [libnl-route-3.0], [], AC_MSG_ERROR([requires libnl3-route])) + ;; +esac AC_CHECK_LIB(m, pow, [], AC_MSG_ERROR([requires libm])) diff --git a/src/in_netlink.c b/src/in_netlink.c index 67f4a83..64021f5 100644 --- a/src/in_netlink.c +++ b/src/in_netlink.c @@ -31,6 +31,8 @@ #include #include +#ifndef SYS_BSD + static int c_notc = 0; static struct element_group *grp; static struct bmon_module netlink_ops; @@ -866,3 +868,4 @@ static void __init netlink_init(void) { input_register(&netlink_ops); } +#endif diff --git a/src/input.c b/src/input.c index f4afac7..2c02bbd 100644 --- a/src/input.c +++ b/src/input.c @@ -44,8 +44,10 @@ static void activate_default(void) if (!input_subsys.s_nmod) { struct bmon_module *m; +#ifdef SYS_LINUX if (!input_set("netlink")) return; +#endif if (!input_set("proc")) return; -- cgit v1.2.3 From 22b13d250608b10b694f2756ebd2f18bd3ce2f6c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Z=CC=8Cilvinas=20Valinskas?= Date: Thu, 1 May 2014 00:05:42 +0300 Subject: osx: Import src/in_sysctl.c from bmon v2.0.1. MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Žilvinas Valinskas Signed-off-by: Thomas Graf --- src/Makefile.am | 1 + src/in_sysctl.c | 168 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 169 insertions(+) create mode 100644 src/in_sysctl.c diff --git a/src/Makefile.am b/src/Makefile.am index dcc7bfd..c773410 100644 --- a/src/Makefile.am +++ b/src/Makefile.am @@ -36,6 +36,7 @@ bmon_SOURCES = \ in_null.c \ in_dummy.c \ in_proc.c \ + in_sysctl.c \ out_null.c \ out_format.c \ out_ascii.c \ diff --git a/src/in_sysctl.c b/src/in_sysctl.c new file mode 100644 index 0000000..79c03fa --- /dev/null +++ b/src/in_sysctl.c @@ -0,0 +1,168 @@ +/* + * in_sysctl.c sysctl (BSD) + * + * $Id: in_sysctl.c 20 2004-10-30 22:46:16Z tgr $ + * + * Copyright (c) 2001-2004 Thomas Graf + * + * Permission is hereby granted, free of charge, to any person obtaining a + * copy of this software and associated documentation files (the "Software"), + * to deal in the Software without restriction, including without limitation + * the rights to use, copy, modify, merge, publish, distribute, sublicense, + * and/or sell copies of the Software, and to permit persons to whom the + * Software is furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included + * in all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS + * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL + * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING + * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER + * DEALINGS IN THE SOFTWARE. + */ + +#include +#include +#include +#include +#include +#include + +#if defined SYS_BSD +#include +#include +#include +#include +#include +#include + +static int c_debug = 0; + +static void +sysctl_read(void) +{ + int mib[] = {CTL_NET, PF_ROUTE, 0, 0, NET_RT_IFLIST, 0}; + size_t n; + char *buf, *next, *lim; + + if (sysctl(mib, 6, NULL, &n, NULL, 0) < 0) + quit("sysctl() failed"); + + if (c_debug) + fprintf(stderr, "sysctl 1-pass n=%d\n", n); + + buf = xcalloc(1, n); + + if (sysctl(mib, 6, buf, &n, NULL, 0) < 0) + quit("sysctl() failed"); + + if (c_debug) + fprintf(stderr, "sysctl 2-pass n=%d\n", n); + + lim = (buf + n); + + for (next = buf; next < lim; ) { + struct if_msghdr *ifm, *nextifm; + struct sockaddr_dl *sdl; + intf_t *i; + + ifm = (struct if_msghdr *) next; + if (ifm->ifm_type != RTM_IFINFO) + break; + + next += ifm->ifm_msglen; + + while (next < lim) { + nextifm = (struct if_msghdr *) next; + if (nextifm->ifm_type != RTM_NEWADDR) + break; + next += nextifm->ifm_msglen; + } + + sdl = (struct sockaddr_dl *) (ifm + 1); + + if (get_show_only_running() && !(ifm->ifm_flags & IFF_UP)) + continue; + + if(sdl->sdl_family != AF_LINK) + continue; + + if (c_debug) + fprintf(stderr, "Processing %s\n", sdl->sdl_data); + + i = lookup_intf(get_local_node(), sdl->sdl_data, 0, 0); + + if (NULL == i) + continue; + + i->i_rx_bytes.r_total = ifm->ifm_data.ifi_ibytes; + i->i_tx_bytes.r_total = ifm->ifm_data.ifi_obytes; + i->i_rx_packets.r_total = ifm->ifm_data.ifi_ipackets; + i->i_tx_packets.r_total = ifm->ifm_data.ifi_opackets; + + update_attr(i, ERRORS, ifm->ifm_data.ifi_ierrors, + ifm->ifm_data.ifi_oerrors, RX_PROVIDED | TX_PROVIDED); + update_attr(i, COLLISIONS, 0, ifm->ifm_data.ifi_collisions, + TX_PROVIDED); + update_attr(i, MULTICAST, ifm->ifm_data.ifi_imcasts, 0, RX_PROVIDED); + update_attr(i, DROP, 0, ifm->ifm_data.ifi_iqdrops, TX_PROVIDED); + + notify_update(i); + increase_lifetime(i, 1); + } + + xfree(buf); +} + +static void +print_help(void) +{ + printf( + "sysctl - sysctl statistic collector for BSD and Darwin\n" \ + "\n" \ + " BSD and Darwin statistic collector using sysctl()\n" \ + " Author: Thomas Graf \n" \ + "\n"); +} + +static void +sysctl_set_opts(tv_t *attrs) +{ + while (attrs) { + if (!strcasecmp(attrs->type, "debug")) + c_debug = 1; + else if (!strcasecmp(attrs->type, "help")) { + print_help(); + exit(0); + } + attrs = attrs->next; + } +} + +static int +sysctl_probe(void) +{ + size_t n; + int mib[] = {CTL_NET, PF_ROUTE, 0, 0, NET_RT_IFLIST, 0}; + if (sysctl(mib, 6, NULL, &n, NULL, 0) < 0) + return 0; + return 1; +} + +static struct input_module kstat_ops = { + .im_name = "sysctl", + .im_read = sysctl_read, + .im_set_opts = sysctl_set_opts, + .im_probe = sysctl_probe, +}; + +static void __init +sysctl_init(void) +{ + register_input_module(&kstat_ops); +} + +#endif -- cgit v1.2.3 From 852008c4aebfaa4f26efaf41a2c05ffd7e399869 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Z=CC=8Cilvinas=20Valinskas?= Date: Thu, 1 May 2014 23:00:38 +0300 Subject: =?UTF-8?q?osx:=20read=20interfaces,=20counters=20&=C2=A0etc.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Žilvinas Valinskas Signed-off-by: Thomas Graf --- src/in_sysctl.c | 201 +++++++++++++++++++++++++++++++++++++++++++++----------- src/input.c | 7 +- 2 files changed, 169 insertions(+), 39 deletions(-) diff --git a/src/in_sysctl.c b/src/in_sysctl.c index 79c03fa..6583574 100644 --- a/src/in_sysctl.c +++ b/src/in_sysctl.c @@ -4,6 +4,7 @@ * $Id: in_sysctl.c 20 2004-10-30 22:46:16Z tgr $ * * Copyright (c) 2001-2004 Thomas Graf + * Copyright (c) 2014 Žilvinas Valinskas * * Permission is hereby granted, free of charge, to any person obtaining a * copy of this software and associated documentation files (the "Software"), @@ -26,9 +27,9 @@ #include #include -#include #include -#include +#include +#include #include #if defined SYS_BSD @@ -40,6 +41,101 @@ #include static int c_debug = 0; +static struct element_group *grp; + +enum { + SYSCTL_RX_BYTES = 0x100, + SYSCTL_TX_BYTES, + SYSCTL_RX_PACKETS, + SYSCTL_TX_PACKETS, + SYSCTL_RX_ERRORS, + SYSCTL_TX_ERRORS, + SYSCTL_RX_DROPS, + SYSCTL_RX_MCAST, + SYSCTL_TX_MCAST, + SYSCTL_TX_COLLS, +}; +static struct attr_map link_attrs[] = { +{ + .name = "bytes", + .type = ATTR_TYPE_COUNTER, + .unit = UNIT_BYTE, + .rxid = SYSCTL_RX_BYTES, + .txid = SYSCTL_TX_BYTES, + .description = "Bytes", +}, +{ + .name = "packets", + .type = ATTR_TYPE_COUNTER, + .unit = UNIT_NUMBER, + .rxid = SYSCTL_RX_PACKETS, + .txid = SYSCTL_TX_PACKETS, + .description = "Packets", +}, +{ + .name = "errors", + .type = ATTR_TYPE_COUNTER, + .unit = UNIT_NUMBER, + .rxid = SYSCTL_RX_ERRORS, + .txid = SYSCTL_TX_ERRORS, + .description = "Errors", +}, +{ + .name = "drop", + .type = ATTR_TYPE_COUNTER, + .unit = UNIT_NUMBER, + .rxid = SYSCTL_RX_DROPS, + .description = "Dropped", +}, +{ + .name = "coll", + .type = ATTR_TYPE_COUNTER, + .unit = UNIT_NUMBER, + .txid = SYSCTL_TX_COLLS, + .description = "Collisions", +}, +{ + .name = "mcast", + .type = ATTR_TYPE_COUNTER, + .unit = UNIT_NUMBER, + .rxid = SYSCTL_RX_MCAST, + .txid = SYSCTL_TX_MCAST, + .description = "Multicast", +} +}; + +uint64_t sysctl_get_stats(const struct if_msghdr *ifm, int what) +{ + switch(what) { + case SYSCTL_RX_BYTES: + return ifm->ifm_data.ifi_ibytes; + case SYSCTL_TX_BYTES: + return ifm->ifm_data.ifi_obytes; + + case SYSCTL_RX_PACKETS: + return ifm->ifm_data.ifi_ipackets; + case SYSCTL_TX_PACKETS: + return ifm->ifm_data.ifi_opackets; + + case SYSCTL_RX_ERRORS: + return ifm->ifm_data.ifi_ierrors; + case SYSCTL_TX_ERRORS: + return ifm->ifm_data.ifi_oerrors; + + case SYSCTL_RX_DROPS: + return ifm->ifm_data.ifi_iqdrops; + + case SYSCTL_RX_MCAST: + return ifm->ifm_data.ifi_imcasts; + case SYSCTL_TX_MCAST: + return ifm->ifm_data.ifi_omcasts; + case SYSCTL_TX_COLLS: + return ifm->ifm_data.ifi_collisions; + + default: + return 0; + }; +} static void sysctl_read(void) @@ -52,7 +148,7 @@ sysctl_read(void) quit("sysctl() failed"); if (c_debug) - fprintf(stderr, "sysctl 1-pass n=%d\n", n); + fprintf(stderr, "sysctl 1-pass n=%zd\n", n); buf = xcalloc(1, n); @@ -60,14 +156,13 @@ sysctl_read(void) quit("sysctl() failed"); if (c_debug) - fprintf(stderr, "sysctl 2-pass n=%d\n", n); + fprintf(stderr, "sysctl 2-pass n=%zd\n", n); lim = (buf + n); - for (next = buf; next < lim; ) { + struct element *e, *e_parent = NULL; struct if_msghdr *ifm, *nextifm; struct sockaddr_dl *sdl; - intf_t *i; ifm = (struct if_msghdr *) next; if (ifm->ifm_type != RTM_IFINFO) @@ -84,34 +179,54 @@ sysctl_read(void) sdl = (struct sockaddr_dl *) (ifm + 1); - if (get_show_only_running() && !(ifm->ifm_flags & IFF_UP)) + if (!cfg_show_all && !(ifm->ifm_flags & IFF_UP)) continue; - if(sdl->sdl_family != AF_LINK) + if (sdl->sdl_family != AF_LINK) continue; if (c_debug) fprintf(stderr, "Processing %s\n", sdl->sdl_data); - i = lookup_intf(get_local_node(), sdl->sdl_data, 0, 0); - - if (NULL == i) + sdl->sdl_data[sdl->sdl_nlen] = '\0'; + e = element_lookup(grp, + sdl->sdl_data, sdl->sdl_index, + e_parent, ELEMENT_CREAT); + if (!e) continue; - i->i_rx_bytes.r_total = ifm->ifm_data.ifi_ibytes; - i->i_tx_bytes.r_total = ifm->ifm_data.ifi_obytes; - i->i_rx_packets.r_total = ifm->ifm_data.ifi_ipackets; - i->i_tx_packets.r_total = ifm->ifm_data.ifi_opackets; + if (e->e_flags & ELEMENT_FLAG_CREATED) { + if (e->e_parent) + e->e_level = e->e_parent->e_level + 1; + + if (element_set_key_attr(e, "bytes", "packets") || + element_set_usage_attr(e, "bytes")) + BUG(); + + e->e_flags &= ~ELEMENT_FLAG_CREATED; + } + + int i; + for (i = 0; i < ARRAY_SIZE(link_attrs); i++) { + struct attr_map *m = &link_attrs[i]; + uint64_t rx = 0, tx = 0; + int flags = 0; + + if (m->rxid) { + rx = sysctl_get_stats(ifm, m->rxid); + flags |= UPDATE_FLAG_RX; + } - update_attr(i, ERRORS, ifm->ifm_data.ifi_ierrors, - ifm->ifm_data.ifi_oerrors, RX_PROVIDED | TX_PROVIDED); - update_attr(i, COLLISIONS, 0, ifm->ifm_data.ifi_collisions, - TX_PROVIDED); - update_attr(i, MULTICAST, ifm->ifm_data.ifi_imcasts, 0, RX_PROVIDED); - update_attr(i, DROP, 0, ifm->ifm_data.ifi_iqdrops, TX_PROVIDED); + if (m->txid) { + tx = sysctl_get_stats(ifm, m->txid); + flags |= UPDATE_FLAG_TX; + } - notify_update(i); - increase_lifetime(i, 1); + attr_update(e, m->attrid, rx, tx, flags); + } + + element_notify_update(e, NULL); + element_lifesign(e, 1); } xfree(buf); @@ -129,16 +244,13 @@ print_help(void) } static void -sysctl_set_opts(tv_t *attrs) +sysctl_set_opts(const char* type, const char* value) { - while (attrs) { - if (!strcasecmp(attrs->type, "debug")) - c_debug = 1; - else if (!strcasecmp(attrs->type, "help")) { - print_help(); - exit(0); - } - attrs = attrs->next; + if (!strcasecmp(type, "debug")) + c_debug = 1; + else if (!strcasecmp(type, "help")) { + print_help(); + exit(0); } } @@ -152,17 +264,30 @@ sysctl_probe(void) return 1; } -static struct input_module kstat_ops = { - .im_name = "sysctl", - .im_read = sysctl_read, - .im_set_opts = sysctl_set_opts, - .im_probe = sysctl_probe, +static int sysctl_do_init(void) +{ + if (attr_map_load(link_attrs, ARRAY_SIZE(link_attrs))) + BUG(); + + grp = group_lookup(DEFAULT_GROUP, GROUP_CREATE); + if (!grp) + BUG(); + + return 0; +} + +static struct bmon_module kstat_ops = { + .m_name = "sysctl", + .m_do = sysctl_read, + .m_parse_opt = sysctl_set_opts, + .m_probe = sysctl_probe, + .m_init = sysctl_do_init, }; static void __init sysctl_init(void) { - register_input_module(&kstat_ops); + input_register(&kstat_ops); } #endif diff --git a/src/input.c b/src/input.c index 2c02bbd..4a7d4ca 100644 --- a/src/input.c +++ b/src/input.c @@ -47,10 +47,15 @@ static void activate_default(void) #ifdef SYS_LINUX if (!input_set("netlink")) return; -#endif if (!input_set("proc")) return; +#endif + +#ifdef SYS_BSD + if (!input_set("sysctl")) + return; +#endif /* Fall back to anything that could act as default */ list_for_each_entry(m, &input_subsys.s_mod_list, m_list) { -- cgit v1.2.3 From ee98b9ea304744766e22ccc10899dadf71a361b8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Z=CC=8Cilvinas=20Valinskas?= Date: Fri, 2 May 2014 20:02:59 +0300 Subject: osx: fix remaining compiler warnings MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Žilvinas Valinskas Signed-off-by: Thomas Graf --- include/bmon/list.h | 8 ++++++++ src/module.c | 1 + src/out_format.c | 4 ++-- 3 files changed, 11 insertions(+), 2 deletions(-) diff --git a/include/bmon/list.h b/include/bmon/list.h index c7569d7..fff9409 100644 --- a/include/bmon/list.h +++ b/include/bmon/list.h @@ -8,6 +8,14 @@ #ifndef BMON_LIST_H_ #define BMON_LIST_H_ +#ifdef __APPLE__ +/* Apple systems define these macros in system headers, so we undef + * them prior to inclusion of this file */ +#undef LIST_HEAD +#undef LIST_HEAD_INIT +#undef INIT_LIST_HEAD +#endif + struct list_head { struct list_head * next; diff --git a/src/module.c b/src/module.c index 5727e58..b245f72 100644 --- a/src/module.c +++ b/src/module.c @@ -115,6 +115,7 @@ int module_register(struct bmon_subsys *ss, struct bmon_module *m) list_add_tail(&m->m_list, &ss->s_mod_list); m->m_subsys = ss; + return 0; } static void __auto_load(struct bmon_module *m) diff --git a/src/out_format.c b/src/out_format.c index bfe29a5..19c588f 100644 --- a/src/out_format.c +++ b/src/out_format.c @@ -126,10 +126,10 @@ static char *get_token(struct element_group *g, struct element *e, } if (!strncasecmp(type, "rx:", 3)) { - snprintf(buf, len, "%lu", a->a_rx_rate.r_total); + snprintf(buf, len, "%llu", a->a_rx_rate.r_total); return buf; } else if (!strncasecmp(type, "tx:", 3)) { - snprintf(buf, len, "%lu", a->a_tx_rate.r_total); + snprintf(buf, len, "%llu", a->a_tx_rate.r_total); return buf; } else if (!strncasecmp(type, "rxrate:", 7)) { snprintf(buf, len, "%.2f", a->a_rx_rate.r_rate); -- cgit v1.2.3