监控WAN的线路使用率(FTTH/ADSL)

図 ルーターの動作状況を報告する

定期的监控WAN线路(FTTH/ADSL)的使用率,如果超过了设置的阀值时,就将结果通过邮件发送给管理员。

LAN的
接口的设置
(使用LAN1接口)

ip lan1 address 192.168.0.1/24

WAN的
接口的设置
(使用LAN2接口)

pp select 1
pp always-on on
pppoe use lan2
pp auth accept pap chap
pp auth myname(连接ISP的ID) (连接ISP的密码)
ppp lcp mru on 1454
ppp ipcp ipaddress on
ppp ipcp msext on
ip pp mtu 1454
ip pp nat descriptor 1
pp enable 1
ip route default gateway pp 1

NAT的设置

nat descriptor type 1 masquerade

DHCP的设置

dhcp service server
dhcp scope 1 192.168.0.2-192.168.0.100/24

DNS的设置

dns server(ISP指定的DNS服务器地址)
dns private address spoof on

过滤的设置

ip filter source-route on
ip filter directed-broadcast on
ip filter 1010 reject * * udp,tcp 135 *
ip filter 1011 reject * * udp,tcp * 135
ip filter 1012 reject * * udp,tcp netbios_ns-netbios_ssn *
ip filter 1013 reject * * udp,tcp * netbios_ns-netbios_ssn
ip filter 1014 reject * * udp,tcp 445 *
ip filter 1015 reject * * udp,tcp * 445
ip filter 1020 reject 192.168.0.0/24 *
ip filter 1030 pass * 192.168.0.0/24 icmp
ip filter 2000 reject * *
ip filter 3000 pass * *
ip filter dynamic 100 * * ftp
ip filter dynamic 101 * * www
ip filter dynamic 102 * * domain
ip filter dynamic 103 * * smtp
ip filter dynamic 104 * * pop3
ip filter dynamic 105 * * netmeeting
ip filter dynamic 106 * * tcp
ip filter dynamic 107 * * udp
pp select 1
ip pp secure filter in 1020 1030 2000
ip pp secure filter out 1010 1011 1012 1013 1014 1015 3000 dynamic 100 101 102 103 104 105 106 107 # 注释1
pp enable 1

Lua脚本的启动设置

schedule at 1 startup * lua(Lua脚本文件名)

设置值

-- 监控的间隔 (1-864000 秒)
idle_time =(监控间隔)

-- 监控的PP接口号码(1 - 30)
peer_num =(接口号码)

-- 发送数据的阀值(1 - 99 %)
snd_th =(负载率)

-- 接收数据的阀值(1 - 99 %)
rcv_th =(负载率)

-- 连续多少次超过、或恢复才判断为异常或正常(1, 2 ..)
count =(次数)

-- 恢复正常时是否发送邮件通知(发送:true / 不发送:false)
down_mail =(true / false)

-- 邮件的设置
mail_tbl = {
   smtp_address = "(SMTP服务器地址)",
   smtp_auth_name = "(邮箱用户名)",
   smtp_auth_password = "(邮箱密码)",
   smtp_auth_protocol = "(验证方式)",     #验证方式一般为plain
   from = "(发件人邮箱)",
   to = "(收件人邮箱)"
     }

-- 邮件发送失败时,输出的SYSLOG的等级(info, debug, notice)
log_level = "(SYSLOG等级)"

计算PP接口的负载率的函数

function pp_load_info(num)
 local rtn, snd, rcv, str, n
 local t = {}
 local cmd = "show status pp " .. tostring(num)
 local ptn = "Load%:%s+(%d+)%.%d+%%"

 rtn, str = rt.command(cmd)
 if (rtn) and (str) then
  n = 1
  for w in string.gmatch(str, ptn) do
   t[n] = w
   n = n + 1
  end

  if (t[1]) then
   rcv = tonumber(t[1])
  end
  if (t[2]) then
   snd = tonumber(t[2])
  end
 end

 return rtn, rcv, snd, str
end

线路使用率超过阀值时,
或者恢复正常时
返回信息的函数

function make_ppmsg(tbl, val, th, down)
 local rtn
 local str = ""

 if (val) then
  rtn = count_proc(tbl, val, th)
  if (rtn < 0) then
   if (down) then
    str = tbl.title .. "Load is less than the threshold value.\r\n\r\n"
   end
  elseif (rtn > 0) then
   str = tbl.title .. "Load is more than the threshold value.\r\n"
   str = str .. string.format(" %sLoad: %d%%\r\n threshold: %d%%\r\n\r\n",
      tbl.title, val, th)
  end
 end

 return str
end

超过阀值(或恢复)
的连续次数的计数函数

function count_proc(t, val, th)
 local rtn = 0

 if (val > th) then
  if (not t.flag) then
   t.over = t.over + 1
   if (t.over == count) then
    rtn = 1
    t.flag = true
   end
  else
   if (t.down > 0) then
    t.down = 0
   end
  end
 else
  if (t.flag) then
   t.down = t.down + 1
   if (t.down == count) then
    rtn = -1
    t.flag = false
    t.over = 0
    t.down = 0
   end
  else
   if (t.over > 0) then
    t.over = 0
   end
  end
 end

 return rtn
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 rtn, rcv, snd, str
local snd_tbl = {over = 0, down = 0, flag = false, title = "Transmitted"}
local rcv_tbl = {over = 0, down = 0, flag = false, title = "Received"}

while (true) do
 mail_tbl.text = ""

 rtn, rcv, snd, str = pp_load_info(peer_num)
 if (rtn) then
  mail_tbl.text = mail_tbl.text .. make_ppmsg(rcv_tbl, rcv, rcv_th, down_mail)
  mail_tbl.text = mail_tbl.text .. make_ppmsg(snd_tbl, snd, snd_th, down_mail)
 else
  mail_tbl.text = str
 end

 if (mail_tbl.text:len() > 0) then
  mail_tbl.subject = string.format("[PP %d] transmit/receive load watch (%s)", peer_num, 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

返回顶部