check_netdns.pl 3.2 KB

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