监控syslog

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

发现了指定的SYSLOG后,运行指定的命令,并将结果通过邮件发送给管理员。

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脚本文件名)

设置值

-- 想要指定的SYSLOG的字符串类型
ptn = "(Log类型)"

-- 检测出ptn的字符串类型后,通过逗号分隔show系列的命令,并将结果发送邮件
-- 例如:想要知道路由器当前设置时,用"show config"
-- 想要知道当前所有的log时,用 "show log"
cmd_tbl = {
 "(show系列命令1)",
 "(show系列命令2)"
}

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

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

输出show命令的结果的函数

function exec_command(cmd)
 local rtn, str

 rtn, str = rt.command(cmd)
 if (not rtn) or (not str) then
  str = "exec failed.\r\n"
 end

 return rtn, string.format("# %s\r\n%s\r\n", cmd, 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 rtn, str

while (true) do
 rtn, str = rt.syslogwatch(ptn)
 if (rtn) and (str) then
  mail_tbl.text = string.format("pattern string detected.\r\n pattern: \"%s\"\r\n\r\n", ptn)

  for i, cmd in ipairs(cmd_tbl) do
   rtn, str = exec_command(cmd)
   mail_tbl.text = mail_tbl.text .. str
  end

  mail_tbl.subject = string.format("pattern string detected (%s)", time_stamp())
  rtn = rt.mail(mail_tbl)
  if (not rtn) then
   rt.syslog(log_level, "failed to send mail.(Lua脚本文件名)")
  end
 end
end

返回顶部