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