监控多个终端的运行状态

网络示意图:监控多个终端的运行状态

定期的通过ping命令来监控指定的IP地址,如果没有收到应答,则发送邮件给管理员。
可以确认多台PC或服务器是否正常运作。

RTX1200的设置例

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

ip lan1 address 192.168.100.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脚本文件名)

Lua脚本例

设置值

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

-- 需要ping的目标IP地址
dst_tbl = {
 "(目标地址1)",
 "(目标地址2)"
}

-- 判断为ping没有应答,或者是判断为恢复应答的连续的次数(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等级)"

判断是否ping通
的函数

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

计算连续多少次没有ping通
的函数

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 make_pingmsg(tbl, reach, adr, cnt, sec, down)
 local rtn
 local str = ""

 rtn = count_proc(tbl, reach, cnt)
 if (rtn < 0) then
  if (down) then
   str = "ping success.\r\n"
   str = str .. string.format(" destination: %s\r\n time interval: %d(S)\r\n\r\n",adr, sec)
  end
 elseif (rtn > 0) then
  str = "ping failed.\r\n"
  str = str .. string.format(" destination: %s\r\n Failed times: %d(C)\r\n time interval: %d(S)\r\n\r\n",
     adr, cnt, sec)
 end

 return str
end

重置记录了连续无响应次数
的表单的函数

function init_count_tbl(n, cnt_t)
 for i = 1, n do
  cnt_t[i] = {ng = 0, ok = 0, flag = false}
 end
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, reach, str, adr
local cnt_tbl = {}

init_count_tbl(#dst_tbl, cnt_tbl)

while (true) do
 mail_tbl.text = ""

 for i, adr in ipairs(dst_tbl) do
  rtn, reach, str = ping_reach(adr)
  if (rtn) then
   mail_tbl.text = mail_tbl.text .. make_pingmsg(cnt_tbl[i], reach, adr,
         count, idle_time, down_mail)
  else
   mail_tbl.text = string.format("%s (destination: %s\r\n\r\n)", str, dst_tbl[i])
  end
 end

 if (mail_tbl.text:len() > 0) then
  mail_tbl.subject = string.format("watch ping : multi destination (%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

返回顶部