--[[ ●ルーターリソース監視スクリプト   定期的にルーターのリソースを監視して、各リソースの使用状況を管理者にメール   で送信するスクリプトです。監視するリソースは以下です。   ・CPU 使用率   ・メモリ使用率   ・筐体内温度(RTX1200 のみ)   ・IP マスカレードの使用ポート数   ・DHCP サーバ機能での割り当て中と利用可能なアドレス数 <説明> ・このファイルを RTFS か外部メモリに保存してください。 ・本項目の config の設定では schedule at コマンドでルーター起動時に Lua スク リプトが実行されるように設定しています。 ・スクリプトを停止するときは terminate lua コマンドを実行してください。 ・再度、Lua スクリプトを実行する場合は lua コマンドで実行してください。 ・★マークの付いた設定値は変更が可能です。 <ノート>  ・メールの送信失敗時に出力する SYSLOG レベルを指定可能です。   SYSLOG のレベルを指定するには、log_level を設定してください。   debug レベル、notice レベルの SYSLOG を出力するためには、それぞれ以下の設定   が必要です。    debug レベル ・・・ syslog debug on    notice レベル・・・ syslog notice on  ・本スクリプトファイルを編集する場合、文字コードは必ず Shift-JIS を使用してく   ださい。 ]] --------------------------## 設定値 ##-------------------------------- -- 監視間隔(1 - 864000 秒) idle_time = (監視間隔) -- ★ -- 使用状況を取得する IP マスカレードの NAT ディスクリプタ番号(1 - 2147483647) nat_descriptor = (NAT ディスクリプタ番号) -- ★ -- メールの設定 mail_tbl = { -- ★ smtp_address = "(SMTP サーバのアドレス)", from = "(送信元メールアドレス)", to = "(宛先メールアドレス)" } -- メールの送信に失敗した時に出力する SYSLOG のレベル(info, debug, notice) log_level = "(SYSLOG レベル)" -- ★ ----------------------## 設定値ここまで ##---------------------------- ------------------------------------------------------------ -- ルーターのハードウェアリソースの使用状況を取得する関数 -- ------------------------------------------------------------ function rt_res_status(t) local rtn, str local cmd = "show environment" rtn, str = rt.command(cmd) if (rtn) and (str) then for k, v in pairs(t) 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 ------------------------------------------------------------ -- IP マスカレードの使用ポート数を返す関数 -- ------------------------------------------------------------ function natmsq_use_status(id) local rtn, str, num local cmd = "show nat descriptor address " .. tostring(id) local ptn = "(%d+)個使用中" local err = "NATディスクリプタは定義されていません" rtn, str = rt.command(cmd) if (rtn) and (str) then num = str:match(ptn) if (num) then num = tostring(num) else if (str ~= err) then str = "マスカレードテーブルは現在使用されていません\r\n" end rtn = false end else str = cmd .. "コマンド実行失敗\r\n" end return rtn, num, str end ------------------------------------------------------------ -- DHCP サーバー機能で割当中、または利用可能な -- -- アドレスを返す関数 -- ------------------------------------------------------------ function dhcp_status(t) local rtn, str, n, id, asn, avl local cmd = "show status dhcp" rtn, str = rt.command(cmd) if (rtn) then if (str) then s = 1 n = 1 while (true) do s, e, id = string.find(str, "番号: (%d+)", s) if (s) then s, e, asn = str:find("割り当て中アドレス数: (%d+)", e) s, e, avl = str:find("利用可能アドレス数: (%d+)", e) t[n] = {id = id, asn = asn, avl = avl} else break end end else str = " 有効なDHCPスコープがありません\r\n\r\n" rtn = false end end return rtn, str end ------------------------------------------------------------ -- 現在の日時を取得する関数 -- ------------------------------------------------------------ function time_stamp() local t t = os.date("*t") return string.format("%d/%02d/%02d %02d:%02d:%02d", t.year, t.month, t.day, t.hour, t.min, t.sec) end ------------------------------------------------------------ -- メインルーチン -- ------------------------------------------------------------ -- ハードウェアリソース情報テーブル local rt_res_tbl = { cpu_5sec = {ptn = "(%d+)%%%(5sec%)", val = 0 }, cpu_1min = {ptn = "(%d+)%%%(1min%)", val = 0 }, cpu_5min = {ptn = "(%d+)%%%(5min%)", val = 0 }, memory = {ptn = "(%d+)%% used", val = 0 }, temp = {ptn = "筐体内温度%(℃%): (%d+)", val = 0 } } local rtn, str, num local scope_tbl = {} local rt_name = string.match(_RT_FIRM_REVISION, "(%w+)") while (true) do mail_tbl.text = "" -- cpu, mem (,temp -- RTX1200 only) rtn, str = rt_res_status(rt_res_tbl) if (rtn) then str = string.format("[CPU負荷率]\r\n %d%%(5sec)\t%d%%(1min)\t%d%%(5min)\r\n[メモリ使用率]\r\n %d%%\r\n", rt_res_tbl.cpu_5sec.val, rt_res_tbl.cpu_1min.val, rt_res_tbl.cpu_5min.val, rt_res_tbl.memory.val) if (rt_name == "RTX1200") then str = str .. string.format("[筐体内温度]\r\n %d℃\r\n", rt_res_tbl.temp.val) end else if (rt_name == "RTX1200") then str = "[CPU使用率]\r\n[メモリ使用率]\r\n[筐体内温度]\r\n " .. str else str = "[CPU使用率]\r\n[メモリ使用率]\r\n " .. str end end mail_tbl.text = mail_tbl.text .. str .. "\r\n" -- nat mail_tbl.text = mail_tbl.text .. string.format("[NATディスクリプタ : %d]\r\n ", nat_descriptor) rtn, num, str = natmsq_use_status(nat_descriptor) if (rtn) then if (num) then str = string.format("使用中のポート数: %d個\r\n", num) end end mail_tbl.text = mail_tbl.text .. str .. "\r\n" -- dhcp mail_tbl.text = mail_tbl.text .. "[DHCP]\r\n" rtn, str = dhcp_status(scope_tbl) if (rtn) then str = "" for i, v in ipairs(scope_tbl) do str = str .. string.format(" DHCPスコープ番号: %d\r\n", v.id) str = str .. string.format(" 割り当て可能アドレス: %d\r\n", v.asn) str = str .. string.format(" 利用可能アドレス: %d\r\n", v.avl) end end mail_tbl.text = mail_tbl.text .. str .. "\r\n" if (mail_tbl.text:len() > 0) then mail_tbl.subject = string.format("watch router resources (%s)", time_stamp()) rtn = rt.mail(mail_tbl) if (not rtn) then rt.syslog(log_level, "failed to send mail. (Lua スクリプトファイル名)") end end rt.sleep(idle_time) end