上一篇提到这几天公共DNS服务器总有问题,故写一个简单的Perl脚本用于监控公共DNS解析情况。这个脚本可直接用于Nagios插件。
先在域名系统里配置一个泛域名,比如*.livemonitor.game.yy.com,该泛域名有一个固定的A记录值,比如8.8.8.8. 然后每次构造一个随机字串作为起始label,去特定的DNS服务器(如广东电信)查询这个泛域名,比如abcd.livemonitor.game.yy.com,如果查询失败则触发告警。
脚本如下:
#!/usr/bin/perl use strict; use Net::DNS; my @x = ("a".."z"); my $str = join "", map { $x[int rand @x] } 1..8; my $domain_name = $str . ".livemonitor.game.yy.com"; my $domain_value = "8.8.8.8"; my @nameservers = qw(202.96.128.86 202.96.128.166); my $res = Net::DNS::Resolver->new(nameservers => [@nameservers]); $res->tcp_timeout(5); $res->udp_timeout(5); my $answer = $res->query($domain_name); if (defined $answer) { my @rr = $answer->answer; if ($rr[0]->address ne $domain_value) { print "DNS查询记录值不匹配", "\n"; exit 2; } } else { print "DNS查询失败","\n"; exit 2; } print "OK\n"; exit 0;