From 0a66a7a0c2ef27e93902f9521607c614cbc62afa Mon Sep 17 00:00:00 2001 From: Gui Iribarren Date: Thu, 12 Sep 2013 07:30:41 -0300 Subject: [PATCH 1/8] Add a lightweight framework for running facter scripts that feed data into alfred every 5 minutes --- alfred/Makefile | 2 +- alfred/files/alfred.config | 1 + alfred/files/alfred.init | 25 ++++++++++++++++++------- 3 files changed, 20 insertions(+), 8 deletions(-) diff --git a/alfred/Makefile b/alfred/Makefile index db9c71f..3d2bd97 100644 --- a/alfred/Makefile +++ b/alfred/Makefile @@ -12,7 +12,7 @@ include $(TOPDIR)/rules.mk # PKG_NAME:=alfred PKG_VERSION:=2013.3.0 -PKG_RELEASE:=0 +PKG_RELEASE:=1 PKG_MD5SUM:=018ef6262cdd11e900af31d71a864b13 PKG_SOURCE:=$(PKG_NAME)-$(PKG_VERSION).tar.gz diff --git a/alfred/files/alfred.config b/alfred/files/alfred.config index b8fa9b2..9d3fff6 100644 --- a/alfred/files/alfred.config +++ b/alfred/files/alfred.config @@ -3,5 +3,6 @@ config 'alfred' 'alfred' option mode 'master' option batmanif 'bat0' option start_vis '1' + option run_facters '1' # REMOVE THIS LINE TO ENABLE ALFRED option disabled '1' diff --git a/alfred/files/alfred.init b/alfred/files/alfred.init index 56818d8..31357fa 100755 --- a/alfred/files/alfred.init +++ b/alfred/files/alfred.init @@ -11,6 +11,7 @@ START=99 STOP=99 alfred_args="" vis_args="" +facters_dir="/etc/alfred" pid_file_alfred="/var/run/alfred.pid" pid_file_vis="/var/run/vis.pid" enable=0 @@ -48,6 +49,8 @@ alfred_start() append vis_args "-i $batmanif -s" fi + config_get_bool run_facters "$section" run_facters 0 + return 0 } @@ -63,12 +66,19 @@ start() SERVICE_PID_FILE="$pid_file_alfred" service_start /usr/sbin/alfred ${alfred_args} - if [ "$vis_enable" = "0" ]; then - exit 0 - fi - echo "${initscript}: starting vis" - SERVICE_PID_FILE="$pid_file_vis" - service_start /usr/sbin/vis ${vis_args} + if [ "$vis_enable" = "1" ]; then + echo "${initscript}: starting vis" + SERVICE_PID_FILE="$pid_file_vis" + service_start /usr/sbin/vis ${vis_args} + fi + + if [ "$run_facters" = "1" ]; then + if ! ( grep -q "for file in $facters_dir/\* ; do " /etc/crontabs/root 2>/dev/null ) ; then + echo "*/5 * * * * ( for file in $facters_dir/* ; do [ -x \$file ] && \$file ; done )" >> /etc/crontabs/root + /etc/init.d/cron enable + /etc/init.d/cron restart + fi + fi } stop() @@ -77,5 +87,6 @@ stop() service_stop /usr/sbin/alfred SERVICE_PID_FILE="$pid_file_vis" [ -x /usr/sbin/vis ] && service_stop /usr/sbin/vis - + sed "\|for file in $facters_dir/\* ; do |d" -i /etc/crontabs/root + /etc/init.d/cron restart } From 393bc88a13b41218b397f119b8e5837c3401976b Mon Sep 17 00:00:00 2001 From: Gui Iribarren Date: Thu, 12 Sep 2013 08:25:24 -0300 Subject: [PATCH 2/8] Bundle a lua script for generating a bat-hosts --- alfred/Makefile | 4 +- alfred/files/bat-hosts.lua | 80 ++++++++++++++++++++++++++++++++++++++ 2 files changed, 83 insertions(+), 1 deletion(-) create mode 100644 alfred/files/bat-hosts.lua diff --git a/alfred/Makefile b/alfred/Makefile index 3d2bd97..89950be 100644 --- a/alfred/Makefile +++ b/alfred/Makefile @@ -12,7 +12,7 @@ include $(TOPDIR)/rules.mk # PKG_NAME:=alfred PKG_VERSION:=2013.3.0 -PKG_RELEASE:=1 +PKG_RELEASE:=2 PKG_MD5SUM:=018ef6262cdd11e900af31d71a864b13 PKG_SOURCE:=$(PKG_NAME)-$(PKG_VERSION).tar.gz @@ -74,6 +74,8 @@ define Package/alfred/install $(INSTALL_BIN) ./files/alfred.init $(1)/etc/init.d/alfred $(INSTALL_DIR) $(1)/etc/config $(INSTALL_DATA) ./files/alfred.config $(1)/etc/config/alfred + $(INSTALL_DIR) $(1)/etc/alfred + $(INSTALL_BIN) ./files/bat-hosts.lua $(1)/etc/alfred/bat-hosts.lua endef $(eval $(call BuildPackage,alfred)) diff --git a/alfred/files/bat-hosts.lua b/alfred/files/bat-hosts.lua new file mode 100644 index 0000000..0d4333a --- /dev/null +++ b/alfred/files/bat-hosts.lua @@ -0,0 +1,80 @@ +#!/usr/bin/lua + +local nxo = require "nixio" + +local type_id = 64 -- bat-hosts + +local function generate_bat_hosts() +-- get hostname and interface macs/names +-- then return a table containing valid bat-hosts lines + local n, i + local ifaces, ret = {}, {} + + local hostname = nxo.uname().nodename + + -- skip loopback ("lo") mac (00:00:00:00:00:00) + for n, i in ipairs(nxo.getifaddrs()) do + if i.addr:match("%x%x:%x%x:%x%x:%x%x:%x%x:%x%x") + and not i.addr:match("00:00:00:00:00:00") then + ifaces[i.addr] = i.name:match("[^:]+") + end + end + + for mac, iname in pairs(ifaces) do + table.insert(ret, mac.." "..hostname.."_"..iname.."\n") + end + + return ret +end + +local function publish_bat_hosts() +-- pass a raw chunk of data to alfred + local fd = io.popen("alfred -s " .. type_id, "w") + if fd then + local ret = generate_bat_hosts() + if ret then + fd:write(table.concat(ret)) + end + fd:close() + end +end + +local function write_bat_hosts(rows) + local content = { "### File generated by alfred-mod-bat-hosts\n" } + + -- merge the chunks from all nodes, de-escaping newlines + for _, row in ipairs(rows) do + local node, value = unpack(row) + table.insert(content, "# Node ".. node .. "\n") + table.insert(content, value:gsub("\x0a", "\n") .. "\n") + end + + -- write parsed content down to disk + local fd = io.open("/tmp/bat-hosts", "w") + if fd then + fd:write(table.concat(content)) + fd:close() + end +end + +local function receive_bat_hosts() +-- read raw chunks from alfred, convert them to a nested table and call write_bat_hosts + + local fd = io.popen("alfred -r " .. type_id) + --[[ this command returns something like + { "54:e6:fc:b9:cb:37", "00:11:22:33:44:55 ham_wlan0\x0a00:22:33:22:33:22 ham_eth0\x0a" }, + { "90:f6:52:bb:ec:57", "00:22:33:22:33:23 spam\x0a" }, + ]]-- + + if fd then + local output = fd:read("*a") + if output then + assert(loadstring("rows = {" .. output .. "}"))() + write_bat_hosts(rows) + end + fd:close() + end +end + +publish_bat_hosts() +receive_bat_hosts() From fe76518e48b5aab93886fedcd5993d384238f645 Mon Sep 17 00:00:00 2001 From: jahead Date: Sun, 22 Sep 2013 18:17:02 -0300 Subject: [PATCH 3/8] drop nixio dependency from bat-hosts.lua --- alfred/files/bat-hosts.lua | 43 +++++++++++++++++++++++++++++++------- 1 file changed, 35 insertions(+), 8 deletions(-) diff --git a/alfred/files/bat-hosts.lua b/alfred/files/bat-hosts.lua index 0d4333a..0d5d0b6 100644 --- a/alfred/files/bat-hosts.lua +++ b/alfred/files/bat-hosts.lua @@ -1,23 +1,50 @@ #!/usr/bin/lua -local nxo = require "nixio" - local type_id = 64 -- bat-hosts +function get_hostname() + local hostfile = io.open("/proc/sys/kernel/hostname", "r") + local ret_string = hostfile:read("*a") + ret_string = string.gsub(ret_string, "\n", "") + hostfile:close() + return ret_string +end + +function get_interfaces_names() + local i, ret + i = 0 + ret = {} + for name in io.popen("ls -1 /sys/class/net/"):lines() do + if name ~= "lo" then + i = i + 1 + ret[i] = name + end + end + + return ret +end + +function get_interface_address(name) + local addressfile = io.open("/sys/class/net/"..name.."/address", "r") + local ret_string = addressfile:read("*a") + ret_string = string.gsub(ret_string, "\n", "") + addressfile:close() + return ret_string +end + + local function generate_bat_hosts() -- get hostname and interface macs/names -- then return a table containing valid bat-hosts lines local n, i local ifaces, ret = {}, {} - local hostname = nxo.uname().nodename + local hostname = get_hostname() -- skip loopback ("lo") mac (00:00:00:00:00:00) - for n, i in ipairs(nxo.getifaddrs()) do - if i.addr:match("%x%x:%x%x:%x%x:%x%x:%x%x:%x%x") - and not i.addr:match("00:00:00:00:00:00") then - ifaces[i.addr] = i.name:match("[^:]+") - end + for n, i in ipairs(get_interfaces_names()) do + local address = get_interface_address(i) + ifaces[address] = i end for mac, iname in pairs(ifaces) do From b44df15f0fabd20b49fdb850cbec1893d7544e7b Mon Sep 17 00:00:00 2001 From: Gui Iribarren Date: Sun, 22 Sep 2013 18:23:44 -0300 Subject: [PATCH 4/8] refactor get_interface_names: use table.insert --- alfred/files/bat-hosts.lua | 10 ++++------ 1 file changed, 4 insertions(+), 6 deletions(-) diff --git a/alfred/files/bat-hosts.lua b/alfred/files/bat-hosts.lua index 0d5d0b6..86a522a 100644 --- a/alfred/files/bat-hosts.lua +++ b/alfred/files/bat-hosts.lua @@ -11,13 +11,12 @@ function get_hostname() end function get_interfaces_names() - local i, ret - i = 0 - ret = {} + local ret = {} + for name in io.popen("ls -1 /sys/class/net/"):lines() do + -- skip loopback ("lo") mac (00:00:00:00:00:00) if name ~= "lo" then - i = i + 1 - ret[i] = name + table.insert(ret, name) end end @@ -41,7 +40,6 @@ local function generate_bat_hosts() local hostname = get_hostname() - -- skip loopback ("lo") mac (00:00:00:00:00:00) for n, i in ipairs(get_interfaces_names()) do local address = get_interface_address(i) ifaces[address] = i From 9ef0c0caa02b592d3c68334788c2a7e460d64b92 Mon Sep 17 00:00:00 2001 From: Gui Iribarren Date: Sun, 22 Sep 2013 18:50:24 -0300 Subject: [PATCH 5/8] if there's no /etc/bat-hosts, make a symlink pointing to autogenerated tmp file --- alfred/Makefile | 2 +- alfred/files/bat-hosts.lua | 6 +++++- 2 files changed, 6 insertions(+), 2 deletions(-) diff --git a/alfred/Makefile b/alfred/Makefile index 89950be..7589322 100644 --- a/alfred/Makefile +++ b/alfred/Makefile @@ -12,7 +12,7 @@ include $(TOPDIR)/rules.mk # PKG_NAME:=alfred PKG_VERSION:=2013.3.0 -PKG_RELEASE:=2 +PKG_RELEASE:=3 PKG_MD5SUM:=018ef6262cdd11e900af31d71a864b13 PKG_SOURCE:=$(PKG_NAME)-$(PKG_VERSION).tar.gz diff --git a/alfred/files/bat-hosts.lua b/alfred/files/bat-hosts.lua index 86a522a..c92357e 100644 --- a/alfred/files/bat-hosts.lua +++ b/alfred/files/bat-hosts.lua @@ -65,7 +65,7 @@ local function publish_bat_hosts() end local function write_bat_hosts(rows) - local content = { "### File generated by alfred-mod-bat-hosts\n" } + local content = { "### File generated by alfred-bat-hosts\n" } -- merge the chunks from all nodes, de-escaping newlines for _, row in ipairs(rows) do @@ -80,6 +80,10 @@ local function write_bat_hosts(rows) fd:write(table.concat(content)) fd:close() end + + -- try to make a symlink in /etc pointing to /tmp, + -- if it exists, ln will do nothing. + os.execute("ln -ns /tmp/bat-hosts /etc/bat-hosts") end local function receive_bat_hosts() From 6240662804bfabf85c4124a992aa7008fcd930b4 Mon Sep 17 00:00:00 2001 From: Gui Iribarren Date: Sun, 22 Sep 2013 19:59:36 -0300 Subject: [PATCH 6/8] run facters when starting init.d script --- alfred/Makefile | 2 +- alfred/files/alfred.init | 1 + 2 files changed, 2 insertions(+), 1 deletion(-) diff --git a/alfred/Makefile b/alfred/Makefile index 7589322..b088e45 100644 --- a/alfred/Makefile +++ b/alfred/Makefile @@ -12,7 +12,7 @@ include $(TOPDIR)/rules.mk # PKG_NAME:=alfred PKG_VERSION:=2013.3.0 -PKG_RELEASE:=3 +PKG_RELEASE:=4 PKG_MD5SUM:=018ef6262cdd11e900af31d71a864b13 PKG_SOURCE:=$(PKG_NAME)-$(PKG_VERSION).tar.gz diff --git a/alfred/files/alfred.init b/alfred/files/alfred.init index 31357fa..e52a7bd 100755 --- a/alfred/files/alfred.init +++ b/alfred/files/alfred.init @@ -73,6 +73,7 @@ start() fi if [ "$run_facters" = "1" ]; then + ( for file in $facters_dir/* ; do [ -x $file ] && $file ; done ) if ! ( grep -q "for file in $facters_dir/\* ; do " /etc/crontabs/root 2>/dev/null ) ; then echo "*/5 * * * * ( for file in $facters_dir/* ; do [ -x \$file ] && \$file ; done )" >> /etc/crontabs/root /etc/init.d/cron enable From 65f291d478688cad1abfeede6a29c540ea1079dc Mon Sep 17 00:00:00 2001 From: Gui Iribarren Date: Mon, 30 Sep 2013 18:49:04 +0200 Subject: [PATCH 7/8] better heuristics for filtering bat-hosts candidate entries: only accept 6-bytes MACs, and not all-zeroes like lo --- alfred/files/bat-hosts.lua | 9 ++++----- 1 file changed, 4 insertions(+), 5 deletions(-) diff --git a/alfred/files/bat-hosts.lua b/alfred/files/bat-hosts.lua index c92357e..67d2523 100644 --- a/alfred/files/bat-hosts.lua +++ b/alfred/files/bat-hosts.lua @@ -14,10 +14,7 @@ function get_interfaces_names() local ret = {} for name in io.popen("ls -1 /sys/class/net/"):lines() do - -- skip loopback ("lo") mac (00:00:00:00:00:00) - if name ~= "lo" then - table.insert(ret, name) - end + table.insert(ret, name) end return ret @@ -46,7 +43,9 @@ local function generate_bat_hosts() end for mac, iname in pairs(ifaces) do - table.insert(ret, mac.." "..hostname.."_"..iname.."\n") + if mac:match("^%x%x:%x%x:%x%x:%x%x:%x%x:%x%x$") and not mac:match("00:00:00:00:00:00") then + table.insert(ret, mac.." "..hostname.."_"..iname.."\n") + end end return ret From f62b16766372e0425370cf7031c6c9dbf92e11ee Mon Sep 17 00:00:00 2001 From: Gui Iribarren Date: Mon, 30 Sep 2013 18:49:55 +0200 Subject: [PATCH 8/8] discard spurious error 'file exists' from ln -ns syscall --- alfred/files/bat-hosts.lua | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/alfred/files/bat-hosts.lua b/alfred/files/bat-hosts.lua index 67d2523..7347fac 100644 --- a/alfred/files/bat-hosts.lua +++ b/alfred/files/bat-hosts.lua @@ -82,7 +82,7 @@ local function write_bat_hosts(rows) -- try to make a symlink in /etc pointing to /tmp, -- if it exists, ln will do nothing. - os.execute("ln -ns /tmp/bat-hosts /etc/bat-hosts") + os.execute("ln -ns /tmp/bat-hosts /etc/bat-hosts 2>/dev/null") end local function receive_bat_hosts()