check_netdns.pl 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  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 utils ;
  31. my $PROGNAME = "check_netdns";
  32. $ENV{'PATH'}='@TRUSTED_PATH@';
  33. $ENV{'BASH_ENV'}='';
  34. $ENV{'ENV'}='';
  35. Getopt::Long::Configure(`bundling`);
  36. GetOptions("V" => $opt_V, "version" => $opt_V,
  37. "h" => $opt_h, "help" => $opt_h,
  38. "t=i" => $opt_t, "timeout=i" => $opt_t,
  39. "s=s" => $opt_s, "server=s" => $opt_s,
  40. "H=s" => $opt_H, "hostname=s" => $opt_H);
  41. # -h means display verbose help screen
  42. if($opt_h){ print_help(); exit 0; }
  43. # -V means display version number
  44. if ($opt_V) { print_version(); exit 0; }
  45. # -H means host name
  46. $opt_H = shift unless ($opt_H);
  47. unless ($opt_H) { print_usage(); exit -1; }
  48. if ($opt_H &&
  49. $opt_H =~ m/^([0-9]+.[0-9]+.[0-9]+.[0-9]+|[a-zA-Z][-a-zA-Z0]+(.[a-zA-Z][-a-zA-Z0]+)*)$/)
  50. {
  51. $host = $1;
  52. } else {
  53. print "$opt_H is not a valid host name";
  54. exit -1;
  55. }
  56. # -s means server name
  57. $opt_s = shift unless ($opt_s);
  58. if ($opt_s) {
  59. if ($opt_s =~ m/^([0-9]+.[0-9]+.[0-9]+.[0-9]+|[a-zA-Z][-a-zA-Z0]+(.[a-zA-Z][-a-zA-Z0]+)*)$/)
  60. {
  61. $server = $1;
  62. } else {
  63. print "$opt_s is not a valid host name";
  64. exit -1;
  65. }
  66. }
  67. # -t means timeout
  68. my $timeout = 10 unless ($opt_t);
  69. my $res = new Net::DNS::Resolver;
  70. #$res->debug(1);
  71. if ($server) {
  72. $res->nameservers($server);
  73. }
  74. $res->tcp_timeout($timeout);
  75. $SIG{ALRM} = &catch_alarm;
  76. alarm($timeout);
  77. $query = $res->query($host);
  78. if ($query) {
  79. my @answer = $query->answer;
  80. if (@answer) {
  81. print join(`/`, map {
  82. $_->type . ` ` . $_->rdatastr;
  83. } @answer);
  84. exit 0;
  85. } else {
  86. print "empty answer";
  87. exit 2;
  88. }
  89. }
  90. else {
  91. print "query failed: ", $res->errorstring, "";
  92. exit 2;
  93. }
  94. sub catch_alarm {
  95. print "query timed out";
  96. exit 2;
  97. }
  98. sub print_version () {
  99. my $arg0 = $0;
  100. chomp $arg0;
  101. print "$arg0 version 0.1";
  102. }
  103. sub print_help() {
  104. print_version();
  105. print "";
  106. print "Check if a nameserver can resolve a given hostname.";
  107. print "";
  108. print_usage();
  109. print "";
  110. print "-H, --hostname=HOST";
  111. print " The name or address you want to query";
  112. print "-s, --server=HOST";
  113. print " Optional DNS server you want to use for the lookup";
  114. print "-t, --timeout=INTEGER";
  115. print " Seconds before connection times out (default: 10)";
  116. print "-h, --help";
  117. print " Print detailed help";
  118. print "-V, --version";
  119. print " Print version numbers and license information";
  120. }
  121. sub print_usage () {
  122. my $arg0 = $0;
  123. chomp $arg0;
  124. print "$arg0 check_dns -H host [-s server] [-t timeout]";
  125. print "$arg0 [-h | --help]";
  126. print "$arg0 [-V | --version]";
  127. }