fastd traffic monitoring through /lib/gluon/announce/statistics.d/traffic_fastd

This commit is contained in:
Daniel Frank 2015-10-10 20:50:21 +02:00
parent 5d6b96cd2d
commit d4e12c100e
5 changed files with 137 additions and 0 deletions

View file

@ -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))

View file

@ -0,0 +1,26 @@
Copyright (c) 2012-2015, Matthias Schiffer <mschiffer@universe-factory.net>
All rights reserved.
Contributors:
Copyright (c) 2014-2015, Haofeng "Rick" Lei <ricklei@gmail.com>
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.

View file

@ -0,0 +1,6 @@
all: fastd-status
CFLAGS += -Wall
fastd-status:
$(CC) $(CPPFLAGS) $(CFLAGS) $(LDFLAGS) -o $@ fastd-status.c $(LDLIBS)

View file

@ -0,0 +1,51 @@
#include <errno.h>
#include <stddef.h>
#include <stdio.h>
#include <string.h>
#include <sys/socket.h>
#include <sys/un.h>
int main(int argc, char *argv[]) {
if (argc != 2) {
fprintf(stderr, "Usage: %s <socket>\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);
}
}

View file

@ -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 }