Skip to content

Instantly share code, notes, and snippets.

@mi2428
Created August 21, 2016 04:28
Show Gist options
  • Save mi2428/5663dc58791ee6c099efa4ba44a7ba12 to your computer and use it in GitHub Desktop.
Save mi2428/5663dc58791ee6c099efa4ba44a7ba12 to your computer and use it in GitHub Desktop.
--[[
RTX1200でサーバを死活監視する
(InfluxDB連携 & 死んでいたらブザーで警告)
06.26.16 ver1.0
"Ping応答監視スクリプト"
http://jp.yamaha.com/products/network/solution/wp-content/uploads/2013/05/case3.lua
をベースに機能の追加削除を行った
]]
idle_time = 10
dst_tbl = {
"192.168.10.111",
"10.0.10.110",
"10.0.10.102"
}
count = 1
influxdb = "http://192.168.10.10:8086/write?db=sample"
function ping_reach(adr)
local rtn, str, loss
local reach = false
local cmd = "ping " .. adr
local ptn = "(%d+)%.%d+%%"
rtn, str = rt.command(cmd)
if (rtn) and (str) then
loss = str:match(ptn)
if (loss) then
loss = tonumber(loss)
if (loss == 0) then
reach = true
end
end
end
return rtn, reach, str
end
function count_proc(t, reach, th)
local rtn = 0
if (not reach) then
if (not t.flag) then
t.ng = t.ng + 1
if (t.ng == th) then
rtn = 1
t.flag = true
end
else
if (t.ok > 0) then
t.ok = 0
end
end
else
if (t.flag) then
t.ok = t.ok + 1
if (t.ok == th) then
rtn = -1
t.flag = false
t.ng = 0
t.ok = 0
end
else
if (t.ng > 0) then
t.ng = 0
end
end
end
return rtn
end
function post_data(host, value)
local request_tbl = {
url = influxdb,
method = "POST",
content_type = "application/json",
post_text = string.format("%s value=%s",host,value)
}
response_tbl = rt.httprequest(request_tbl)
-- print(request_tbl.post_text)
-- print(response_tbl.body)
end
function init_count_tbl(n, cnt_t)
for i = 1, n do
cnt_t[i] = {ng = 0, ok = 0, flag = false}
end
end
local rtn, reach, str, adr
local cnt_tbl = {}
init_count_tbl(#dst_tbl, cnt_tbl)
while (true) do
for i, adr in ipairs(dst_tbl) do
rtn, reach, str = ping_reach(adr)
if (not reach) then
rt.syslog("info", "[Lua/alivemon] "..adr.." is dead")
post_data(adr, 0)
bz, err = rt.hw.open("buzzer1")
for i = 1, idle_time do
local tone = i % 3 + 2
bz:tone("B"..tone)
rt.sleep(1)
end
bz:off()
bz:close()
else
post_data(adr, 1)
rt.sleep(idle_time)
end
end
end
--[[
CPU使用率/メモリ使用率/パケットバッファ使用率/筐体内温度監視用Lua拡張
"ルーターリソース監視スクリプト"
http://jp.yamaha.com/products/network/solution/wp-content/uploads/2014/12/case5.lua
]]
--[[
監視間隔 (1-864000 秒)
]]
mon_interval = 5
--[[
InfluxDBエンドポイントURI
]]
influxdb = "http://10.0.10.121:8086/write?db="
function get_env_status(tbl)
local rtn, str
rtn, str = rt.command("show environment")
if (rtn) and (str) then
for k, v in pairs(tbl) do
v.val = str:match(v.ptn)
if (v.val) then
v.val = tostring(v.val)
end
end
else
return nil, nil
end
return rtn, str
end
function post_influxdb(db, field, value)
local request_tbl = {
url = influxdb .. db,
method = "POST",
content_type = "application/json",
post_text = string.format("%s value=%s",field,value)
}
response_tbl = rt.httprequest(request_tbl)
--print(request_tbl.post_text)
--print(response_tbl.body)
end
local env_tbl = {
cpu_5sec = {
ptn = "(%d+)%%%(5sec%)",
val = 0
},
cpu_1min = {
ptn = "(%d+)%%%(1min%)",
val = 0
},
cpu_5min = {
ptn = "(%d+)%%%(5min%)",
val = 0
},
mem = {
ptn = "(%d+)%% used",
val = 0
},
buf_small = {
ptn = "(%d+)%%%(small%)",
val = 0
},
buf_middle = {
ptn = "(%d+)%%%(middle%)",
val = 0
},
buf_large = {
ptn = "(%d+)%%%(large%)",
val = 0
},
buf_huge = {
ptn = "(%d+)%%%(huge%)",
val = 0
},
temp = {
ptn = "Inside Temperature%(C.%): (%d+)",
val = 0
}
}
while (true) do
get_env_status(env_tbl)
post_influxdb("cpu", "5sec", env_tbl.cpu_5sec.val)
post_influxdb("cpu", "1min", env_tbl.cpu_1min.val)
post_influxdb("cpu", "5min", env_tbl.cpu_5min.val)
post_influxdb("memory", "now", env_tbl.mem.val)
post_influxdb("buffer", "small", env_tbl.buf_small.val)
post_influxdb("buffer", "middle", env_tbl.buf_middle.val)
post_influxdb("buffer", "large", env_tbl.buf_large.val)
post_influxdb("buffer", "huge", env_tbl.buf_huge.val)
post_influxdb("temperature", "now", env_tbl.temp.val)
rt.sleep(mon_interval)
end
--[[
NAT/DHCP監視Lua拡張
"ルーターリソース監視スクリプト"
http://jp.yamaha.com/products/network/solution/wp-content/uploads/2014/12/case5.lua
"IP マスカレードテーブル監視スクリプト"
http://jp.yamaha.com/products/network/solution/wp-content/uploads/2014/12/case6.lua
]]
--[[
監視間隔 (1-864000 秒)
]]
mon_interval = 10
--[[
InfluxDBエンドポイントURI
]]
influxdb = "http://10.0.10.121:8086/write?db="
function get_dhcp_status()
local rtn, str, i
local l = {}
local u = {}
rtn, str = rt.command("show status dhcp")
if (rtn) and (str) then
i = 0
for w in string.gmatch(str, "Leased%:%s+(%d+)") do
l[i] = w
i = i + 1
end
i = 0
for w in string.gmatch(str, "Usable%:%s+(%d+)") do
u[i] = w
i = i + 1
end
end
for i = 0, 1 do
if (not l[i]) then
l[i] = "-1"
end
if (not u[i]) then
u[i] = "-1"
end
end
return l[0], u[0], l[1], u[1]
end
function get_nat_status()
local rtn, str, num, ip
rtn, str = rt.command("show nat descriptor address ")
if (rtn) and (str) then
--ip = str:match("ipcp/(%a+)")
num = str:match("(%d+) used.")
end
--if (not ip) then
-- ip = "0.0.0.0"
--end
if (not num) then
num = "-1"
end
--return ip, num
return num
end
function post_influxdb(db, field, value)
local request_tbl = {
url = influxdb .. db,
method = "POST",
content_type = "application/json",
post_text = string.format("%s value=%s",field,value)
}
response_tbl = rt.httprequest(request_tbl)
--print(request_tbl.post_text)
--print(response_tbl.body)
end
while (true) do
local l1, l2, w1, w2, nat
l1, u1, l2, u2 = get_dhcp_status()
post_influxdb("wireddhcp", "leased", l1)
post_influxdb("wireddhcp", "usable", u1)
post_influxdb("wirelessdhcp", "leased", l2)
post_influxdb("wirelessdhcp", "usable", u2)
nat = get_nat_status()
post_influxdb("nat", "entry", nat)
rt.sleep(mon_interval)
end
--[[
PP/LANインターフェイス送受信負荷率監視用Lua拡張
"PP 側回線使用率監視スクリプト"
http://jp.yamaha.com/products/network/solution/wp-content/uploads/2011/02/case8.lua
"LAN 側回線使用率監視スクリプト"
http://jp.yamaha.com/products/network/solution/wp-content/uploads/2014/12/case9.lua
]]
--[[
監視間隔 (1-864000 秒)
これにインターフェイス計測間隔が加算される
]]
mon_interval = 5
--[[
インターフェイス計測間隔 (1-864000 秒)
]]
avr_sec = 10
--[[
監視する相手先情報番号 (1-30)
]]
peer_num = 1
--[[
監視する LAN インターフェイスの番号
]]
lan_num = 1
--[[
InfluxDBエンドポイントURI
]]
influxdb = "http://10.0.10.121:8086/write?db="
function pp_load_info(num, sec)
local rtn, str1, str2, rcv, rcv_per, snd, n
local t = {}
local cmd = "show status pp " .. tostring(num)
local ptn1 = "Load%:%s+(%d+)%.%d+%%"
local ptn2 = "%[(%d+)%s+"
rtn, str1 = rt.command(cmd)
if (rtn) and (str1) then
n = 1
for w in string.gmatch(str1, ptn1) do
t[n] = w
n = n + 1
end
if (t[1]) then
rcv_per = tonumber(t[1])
end
if (t[2]) then
snd_per = tonumber(t[2])
end
end
if (rtn) and (str1) then
rt.sleep(sec)
rtn, str2 = rt.command(cmd)
if (rtn) and (str2) then
str1 = str1 .. str2
n = 1
for w in string.gmatch(str1, ptn2) do
t[n] = w
n = n + 1
end
if (t[1]) and (t[3]) then
rcv = ((tonumber(t[3]) - tonumber(t[1])) * 8) / sec
else
rtn = false
end
if (t[2]) and (t[4]) then
snd = ((tonumber(t[4]) - tonumber(t[2])) * 8) / sec
else
rtn = false
end
end
end
return rtn, rcv, rcv_per, snd, snd_per
end
function lan_interface_speed(num)
local rtn, str, val, rt_name
local cmd = "show config"
local ptn = "speed lan" .. tostring(num) .. " (%d+%a)"
rtn, str = rt.command(cmd)
if (rtn) and (str) then
str = str:match(ptn)
if (str) then
val = unitstr2num(str)
end
end
if (not val) or (val == 0) then
val = unitstr2num("1000m")
end
return val
end
function unitstr2num(str)
local val, unit
val = tonumber(str:match("%d+"))
unit = str:sub(-1):lower()
if (unit == "k") then
val = val * 1024
elseif (unit == "m") then
val = val * 1024 * 1024
else
val = 0
end
return val
end
function lan_load_info(num, sec, max)
local rtn, str1, str2, rcv, snd
local t = {}
local cmd = "show status lan" .. tostring(num)
local ptn = "%((%d+)%s+octets%)"
rtn, str1 = rt.command(cmd)
if (rtn) and (str1) then
rt.sleep(sec)
rtn, str2 = rt.command(cmd)
if (rtn) and (str2) then
str1 = str1 .. str2
n = 1
for w in string.gmatch(str1, ptn) do
t[n] = w
n = n + 1
end
if (t[1]) and (t[3]) then
snd = ((tonumber(t[3]) - tonumber(t[1])) * 8) / sec
snd_per = 100 * snd / max
else
rtn = false
end
if (t[2]) and (t[4]) then
rcv = ((tonumber(t[4]) - tonumber(t[2])) * 8) / sec
rcv_per = 100 * rcv / max
else
rtn = false
end
end
end
return rtn, rcv, rcv_per, snd, snd_per
end
function post_influxdb(db, field, value)
local request_tbl = {
url = influxdb .. db,
method = "POST",
content_type = "application/json",
post_text = string.format("%s value=%s",field,value)
}
response_tbl = rt.httprequest(request_tbl)
--print(request_tbl.post_text)
--print(response_tbl.body)
end
local rtn, rcv, rcv_per, snd, snd_per, str, max
max = lan_interface_speed(lan_num)
while (true) do
rtn, rcv, rcv_per, snd, snd_per = pp_load_info(peer_num, avr_sec)
if (rtn) then
post_influxdb("ppload","receive",rcv_per)
post_influxdb("ppload","transmit",snd_per)
post_influxdb("ppload","receiveByte",rcv)
post_influxdb("ppload","transmitByte",snd)
end
rtn, rcv, rcv_per, snd, snd_per = lan_load_info(lan_num,avr_sec,max)
if (rtn) then
post_influxdb("lanload","receive",rcv_per)
post_influxdb("lanload","transmit",snd_per)
post_influxdb("lanload","receiveByte",rcv)
post_influxdb("lanload","transmitByte",snd)
end
rt.sleep(mon_interval)
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment