check_netdns.pl 3.2 KB

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