-
-
Save j3tm0t0/a3a47b48befee1e64df9b69a4b16b015 to your computer and use it in GitHub Desktop.
lua script to gather system information of RTX-1200 and submit to SORACOM Harvest
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
--[[ | |
]] | |
-- Internet 接続用の LAN インターフェース | |
wan_if = "lan2" | |
-- traffic 監視 interval & count | |
interval = 1 | |
count = 60 | |
-- Harvest へ UDP でデータ送信 | |
function harvest(payload) | |
udp = rt.socket.udp() | |
udp:setpeername("harvest.soracom.io", 8514) | |
rhost, rport = udp:getpeername() | |
debug("remote host:" .. rhost .. " port:" .. rport) | |
-- データを送信 | |
res, err = udp:send(payload) | |
if (err) then | |
debug("send error(" .. err .. ")") | |
udp:close() | |
os.exit(1) | |
end | |
udp:settimeout(10) -- 受信タイムアウトを10秒に設定 | |
-- 相手からのレスポンスを受信 | |
dgram, err = udp:receive() | |
if (not dgram) and err then | |
debug("receive error("..err..")") | |
udp:close() | |
os.exit(1) | |
end | |
-- 受信データを処理する(例として、ここではコンソールに出力している) | |
debug(dgram) | |
udp:close() | |
end | |
function debug(message) | |
-- print(message) | |
rt.syslog("debug", message) | |
end | |
function get_interface_metric(interface) | |
local cmd = "show status " .. interface | |
local ptn = "%p(%d+)%s+octets" | |
local t={} | |
ret, str = rt.command(cmd) | |
if(ret) and (str) then | |
n = 1 | |
for w in string.gmatch(str, ptn) do | |
t[n] = w | |
n = n + 1 | |
end | |
return ret, tonumber(t[1]), tonumber(t[2]) | |
else | |
return ret | |
end | |
end | |
function get_traffic(interface, interval, count) | |
local s0, r0, s1, s2, r1, r2, s_max, r_max, s_avg, r_avg | |
ret, s0, r0 = get_interface_metric(interface) | |
s_max=0 | |
r_max=0 | |
s1=s0 | |
r1=r0 | |
for i=1, count do | |
rt.sleep(interval) | |
ret, s2, r2 = get_interface_metric(interface) | |
if s_max < (s2-s1) then | |
s_max = s2-s1 | |
end | |
if r_max < (r2-r1) then | |
r_max = r2-r1 | |
end | |
s1=s2 | |
r1=r2 | |
end | |
s_avg = s2-s0 | |
r_avg = r2-r0 | |
s_max_mbps=string.gsub(string.gsub(tostring((80*s_max)/(1024*1024*interval)),"(%d)$",".%1"),"^%.","0.") | |
r_max_mbps=string.gsub(string.gsub(tostring((80*r_max)/(1024*1024*interval)),"(%d)$",".%1"),"^%.","0.") | |
s_avg_mbps=string.gsub(string.gsub(tostring((80*s_avg)/(1024*1024*interval*count)),"(%d)$",".%1"),"^%.","0.") | |
r_avg_mbps=string.gsub(string.gsub(tostring((80*r_avg)/(1024*1024*interval*count)),"(%d)$",".%1"),"^%.","0.") | |
return s_max_mbps, r_max_mbps, s_avg_mbps, r_avg_mbps | |
end | |
function rt_res_status(table) | |
local rtn, str | |
local cmd = "show environment" | |
rtn, str = rt.command(cmd) | |
if (rtn) and (str) then | |
for k, v in pairs(table) do | |
v.val = str:match(v.ptn) | |
if (v.val) then | |
v.val = tostring(v.val) | |
end | |
end | |
else | |
str = cmd .. "コマンド実行失敗\r\n\r\n" | |
end | |
return rtn, str | |
end | |
function make_post_text(table1, table2) | |
local a = ""; | |
for k, v in pairs(table1) do | |
a = a .. string.format("\"%s\":%s", k, v.val) .. "," | |
end | |
for k, v in pairs(table2) do | |
a = a .. string.format("\"%s\":%s", k, v.val) .. "," | |
end | |
return "{" .. string.sub(a,1,-2) .. "}" | |
end | |
function count_hosts(interface) | |
local cmd = "show arp " .. interface | |
local ptn = "Count:%s+(%d+)" | |
rtn, str = rt.command(cmd) | |
if (rtn) and (str) then | |
c = string.match(str,ptn,1) | |
return c | |
end | |
end | |
local rtn, str, rt_res_tbl, up_max, down_max, up_avg, down_avg | |
rt_res_tbl = { | |
cpu_5sec = { ptn = "(%d+)%%%(5sec%)", val = 0 , name = "custom.rtx.cpu.5sec"}, | |
cpu_1min = { ptn = "(%d+)%%%(1min%)", val = 0 , name = "custom.rtx.cpu.1min"}, | |
cpu_5min = { ptn = "(%d+)%%%(5min%)", val = 0 , name = "custom.rtx.cpu.5min"}, | |
memory = { ptn = "(%d+)%% used", val = 0 , name = "custom.rtx.memory.usage"}, | |
packet_small = { ptn = "(%d+)%%%(small%)", val = 0 , name = "custom.rtx.packetbuff.small"}, | |
packet_middle = { ptn = "(%d+)%%%(middle%)", val = 0 , name = "custom.rtx.packetbuff.middle"}, | |
packet_large = { ptn = "(%d+)%%%(large%)", val = 0 , name = "custom.rtx.packetbuff.large"}, | |
packet_huge = { ptn = "(%d+)%%%(huge%)", val = 0 , name = "custom.rtx.packetbuff.huge"}, | |
temp = { ptn = "Inside Temperature.+: (%d+)", val = 0 , name = "custom.rtx.temperature.body"} | |
} | |
up_max, down_max, up_avg, down_avg = get_traffic(wan_if, interval, count) | |
local traffic_tbl = { | |
up_max = {val=up_max}, | |
down_max = {val = down_max }, | |
up_avg = {val=up_avg}, | |
down_avg = {val = down_avg } | |
} | |
rtn, str = rt_res_status(rt_res_tbl) | |
if (rtn) then | |
payload = make_post_text(rt_res_tbl, traffic_tbl) | |
harvest(payload) | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment