From d4e12c100e62cae126e12db0fc28c7a685569023 Mon Sep 17 00:00:00 2001 From: Daniel Frank Date: Sat, 10 Oct 2015 20:50:21 +0200 Subject: [PATCH 1/2] fastd traffic monitoring through /lib/gluon/announce/statistics.d/traffic_fastd --- fastd-traffic-status/Makefile | 36 +++++++++++++++++ fastd-traffic-status/src/COPYRIGHT | 26 +++++++++++++ fastd-traffic-status/src/Makefile | 6 +++ fastd-traffic-status/src/fastd-status.c | 51 +++++++++++++++++++++++++ fastd-traffic-status/src/traffic_fastd | 18 +++++++++ 5 files changed, 137 insertions(+) create mode 100644 fastd-traffic-status/Makefile create mode 100644 fastd-traffic-status/src/COPYRIGHT create mode 100644 fastd-traffic-status/src/Makefile create mode 100644 fastd-traffic-status/src/fastd-status.c create mode 100644 fastd-traffic-status/src/traffic_fastd diff --git a/fastd-traffic-status/Makefile b/fastd-traffic-status/Makefile new file mode 100644 index 0000000..257715f --- /dev/null +++ b/fastd-traffic-status/Makefile @@ -0,0 +1,36 @@ +include $(TOPDIR)/rules.mk + +PKG_NAME:=fastd-traffic-status +PKG_VERSION:=1 + +PKG_BUILD_DIR := $(BUILD_DIR)/$(PKG_NAME) + +include $(INCLUDE_DIR)/package.mk + +define Package/fastd-traffic-status + SECTION:=net + CATEGORY:=Libraries + TITLE:=Adds fastd traffic statistics to alfred + DEPENDS:=+lua +fastd +endef + +define Build/Prepare + mkdir -p $(PKG_BUILD_DIR) + $(CP) ./src/* $(PKG_BUILD_DIR)/ +endef + +define Build/Configure +endef + +define Build/Compile + CFLAGS="$(TARGET_CFLAGS)" CPPFLAGS="$(TARGET_CPPFLAGS)" $(MAKE) -C $(PKG_BUILD_DIR) $(TARGET_CONFIGURE_OPTS) +endef + +define Package/fastd-traffic-status/install + $(INSTALL_DIR) $(1)/lib/gluon/announce/statistics.d + $(CP) $(PKG_BUILD_DIR)/traffic_fastd $(1)/lib/gluon/announce/statistics.d/traffic_fastd + $(INSTALL_DIR) $(1)/usr/bin + $(CP) $(PKG_BUILD_DIR)/fastd-status $(1)/usr/bin/fastd-status +endef + +$(eval $(call BuildPackage,fastd-traffic-status)) diff --git a/fastd-traffic-status/src/COPYRIGHT b/fastd-traffic-status/src/COPYRIGHT new file mode 100644 index 0000000..9af3cd6 --- /dev/null +++ b/fastd-traffic-status/src/COPYRIGHT @@ -0,0 +1,26 @@ +Copyright (c) 2012-2015, Matthias Schiffer +All rights reserved. + +Contributors: +Copyright (c) 2014-2015, Haofeng "Rick" Lei +All rights reserved. + +Redistribution and use in source and binary forms, with or without +modification, are permitted provided that the following conditions are met: + + 1. Redistributions of source code must retain the above copyright notice, + this list of conditions and the following disclaimer. + 2. Redistributions in binary form must reproduce the above copyright notice, + this list of conditions and the following disclaimer in the documentation + and/or other materials provided with the distribution. + +THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" +AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE +IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE +DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE +FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL +DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR +SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER +CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, +OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE +OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. diff --git a/fastd-traffic-status/src/Makefile b/fastd-traffic-status/src/Makefile new file mode 100644 index 0000000..9b5c633 --- /dev/null +++ b/fastd-traffic-status/src/Makefile @@ -0,0 +1,6 @@ +all: fastd-status + +CFLAGS += -Wall + +fastd-status: + $(CC) $(CPPFLAGS) $(CFLAGS) $(LDFLAGS) -o $@ fastd-status.c $(LDLIBS) diff --git a/fastd-traffic-status/src/fastd-status.c b/fastd-traffic-status/src/fastd-status.c new file mode 100644 index 0000000..7f0e46e --- /dev/null +++ b/fastd-traffic-status/src/fastd-status.c @@ -0,0 +1,51 @@ +#include +#include +#include +#include + +#include +#include + + +int main(int argc, char *argv[]) { + if (argc != 2) { + fprintf(stderr, "Usage: %s \n", argv[0]); + return 1; + } + + size_t addrlen = strlen(argv[1]); + + /* Allocate enough space for arbitrary-length paths */ + char addrbuf[offsetof(struct sockaddr_un, sun_path) + addrlen + 1]; + memset(addrbuf, 0, sizeof(addrbuf)); + + struct sockaddr_un *addr = (struct sockaddr_un *)addrbuf; + addr->sun_family = AF_UNIX; + memcpy(addr->sun_path, argv[1], addrlen+1); + + int fd = socket(AF_UNIX, SOCK_STREAM, 0); + if (fd < 0) { + fprintf(stderr, "Failed to create socket: %s\n", strerror(errno)); + return 1; + } + + if (connect(fd, (struct sockaddr*)addr, sizeof(addrbuf)) < 0) { + fprintf(stderr, "Can't connect to `%s': %s\n", argv[1], strerror(errno)); + return 1; + } + + char buf[1024]; + ssize_t r; + while (1) { + r = recv(fd, buf, sizeof(buf), 0); + if (r < 0) { + fprintf(stderr, "read: %s\n", strerror(errno)); + return 1; + } + + if (r == 0) + return 0; + + fwrite(buf, r, 1, stdout); + } +} diff --git a/fastd-traffic-status/src/traffic_fastd b/fastd-traffic-status/src/traffic_fastd new file mode 100644 index 0000000..2621379 --- /dev/null +++ b/fastd-traffic-status/src/traffic_fastd @@ -0,0 +1,18 @@ +local stats_bin = '/usr/bin/fastd-status' +local stats_socket = '/var/run/fastd.mesh_vpn.socket' +local stats_io = io.popen(stats_bin .. " " .. stats_socket) +local stats_json = stats_io:read("*a") +stats_io:close() +local stats = string.match(stats_json, 'statistics.-rx.-packets.- %d+, .-bytes.- %d+ .-rx_reordered.-packets.- %d+, .-bytes.- %d+ .-tx.-packets.- %d+, .bytes.: %d+') +local stats_rx_text = string.match(stats, 'rx.. .- %d+ }') +local stats_rx_packets = string.match(stats_rx_text, '.-%d+') +local stats_rx_packets = tonumber(string.match(stats_rx_packets, '%d+')) +local stats_rx_bytes = string.match(stats_rx_text, '.+%d+') +local stats_rx_bytes = tonumber(string.match(stats_rx_bytes, '%d+$')) +local stats_tx_text = string.match(stats, 'tx.. .+ %d+') +local stats_tx_packets = string.match(stats_tx_text, '.-%d+') +local stats_tx_packets = tonumber(string.match(stats_tx_packets, '%d+')) +local stats_tx_bytes = string.match(stats_tx_text, '.+%d+') +local stats_tx_bytes = tonumber(string.match(stats_tx_bytes, '%d+$')) + +return { rx_bytes = stats_rx_bytes, rx_packets = stats_rx_packets, tx_bytes = stats_tx_bytes, tx_packets = stats_tx_packets } From bba4bd7899d629c8d346bf9ee9a282a56d07a47f Mon Sep 17 00:00:00 2001 From: Daniel Frank Date: Sun, 11 Oct 2015 11:25:26 +0200 Subject: [PATCH 2/2] install_bin statt cp --- fastd-traffic-status/Makefile | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/fastd-traffic-status/Makefile b/fastd-traffic-status/Makefile index 257715f..1ed49f8 100644 --- a/fastd-traffic-status/Makefile +++ b/fastd-traffic-status/Makefile @@ -30,7 +30,7 @@ define Package/fastd-traffic-status/install $(INSTALL_DIR) $(1)/lib/gluon/announce/statistics.d $(CP) $(PKG_BUILD_DIR)/traffic_fastd $(1)/lib/gluon/announce/statistics.d/traffic_fastd $(INSTALL_DIR) $(1)/usr/bin - $(CP) $(PKG_BUILD_DIR)/fastd-status $(1)/usr/bin/fastd-status + $(INSTALL_BIN) $(PKG_BUILD_DIR)/fastd-status $(1)/usr/bin/fastd-status endef $(eval $(call BuildPackage,fastd-traffic-status))