check_netdns.pl 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. #!/usr/bin/perl -w
  2. # Perl version of check_dns plugin which calls DNS directly instead of
  3. # relying on nslookup (which has bugs)
  4. #
  5. # Copyright 2000, virCIO, LLP
  6. #
  7. # $Log$
  8. # Revision 1.2 2002/05/02 16:43:29 sghosh
  9. # fix for embedded perl
  10. #
  11. # Revision 1.1.1.1 2002/02/28 06:43:00 egalstad
  12. # Initial import of existing plugin code
  13. #
  14. # Revision 1.1 2000/08/03 20:41:12 karldebisschop
  15. # rename to avoid conflict when installing
  16. #
  17. # Revision 1.1 2000/08/03 19:27:08 karldebisschop
  18. # use Net::DNS to check name server
  19. #
  20. # Revision 1.1 2000/07/20 19:09:13 cwg
  21. # All the pieces needed to use my version of check_dns.
  22. #
  23. #
  24. use Getopt::Long;
  25. use Net::DNS;
  26. use Findbin;
  27. use lib "$FindBin::Bin";
  28. use utils ;
  29. Getopt::Long::Configure(`bundling`);
  30. GetOptions("V" => $opt_V, "version" => $opt_V,
  31. "h" => $opt_h, "help" => $opt_h,
  32. "t=i" => $opt_t, "timeout=i" => $opt_t,
  33. "s=s" => $opt_s, "server=s" => $opt_s,
  34. "H=s" => $opt_H, "hostname=s" => $opt_H);
  35. # -h means display verbose help screen
  36. if($opt_h){ print_help(); exit 0; }
  37. # -V means display version number
  38. if ($opt_V) { print_version(); exit 0; }
  39. # -H means host name
  40. $opt_H = shift unless ($opt_H);
  41. unless ($opt_H) { print_usage(); exit -1; }
  42. if ($opt_H &&
  43. $opt_H =~ m/^([0-9]+.[0-9]+.[0-9]+.[0-9]+|[a-zA-Z][-a-zA-Z0]+(.[a-zA-Z][-a-zA-Z0]+)*)$/)
  44. {
  45. $host = $1;
  46. } else {
  47. print "$opt_H is not a valid host name";
  48. exit -1;
  49. }
  50. # -s means server name
  51. $opt_s = shift unless ($opt_s);
  52. if ($opt_s) {
  53. if ($opt_s =~ m/^([0-9]+.[0-9]+.[0-9]+.[0-9]+|[a-zA-Z][-a-zA-Z0]+(.[a-zA-Z][-a-zA-Z0]+)*)$/)
  54. {
  55. $server = $1;
  56. } else {
  57. print "$opt_s is not a valid host name";
  58. exit -1;
  59. }
  60. }
  61. # -t means timeout
  62. my $timeout = 10 unless ($opt_t);
  63. my $res = new Net::DNS::Resolver;
  64. #$res->debug(1);
  65. if ($server) {
  66. $res->nameservers($server);
  67. }
  68. $res->tcp_timeout($timeout);
  69. $SIG{ALRM} = &catch_alarm;
  70. alarm($timeout);
  71. $query = $res->query($host);
  72. if ($query) {
  73. my @answer = $query->answer;
  74. if (@answer) {
  75. print join(`/`, map {
  76. $_->type . ` ` . $_->rdatastr;
  77. } @answer);
  78. exit 0;
  79. } else {
  80. print "empty answer";
  81. exit 2;
  82. }
  83. }
  84. else {
  85. print "query failed: ", $res->errorstring, "";
  86. exit 2;
  87. }
  88. sub catch_alarm {
  89. print "query timed out";
  90. exit 2;
  91. }
  92. sub print_version () {
  93. my $arg0 = $0;
  94. chomp $arg0;
  95. print "$arg0 version 0.1";
  96. }
  97. sub print_help() {
  98. print_version();
  99. print "";
  100. print "Check if a nameserver can resolve a given hostname.";
  101. print "";
  102. print_usage();
  103. print "";
  104. print "-H, --hostname=HOST";
  105. print " The name or address you want to query";
  106. print "-s, --server=HOST";
  107. print " Optional DNS server you want to use for the lookup";
  108. print "-t, --timeout=INTEGER";
  109. print " Seconds before connection times out (default: 10)";
  110. print "-h, --help";
  111. print " Print detailed help";
  112. print "-V, --version";
  113. print " Print version numbers and license information";
  114. }
  115. sub print_usage () {
  116. my $arg0 = $0;
  117. chomp $arg0;
  118. print "$arg0 check_dns -H host [-s server] [-t timeout]";
  119. print "$arg0 [-h | --help]";
  120. print "$arg0 [-V | --version]";
  121. }