Code

4bf7bd769a923a0002e48e88f52d7b1b7b1780ab
[nagiosplug.git] / plugins-scripts / check_netdns.pl
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.1  2002/02/28 06:43:00  egalstad
10 # Initial revision
11 #
12 # Revision 1.1  2000/08/03 20:41:12  karldebisschop
13 # rename to avoid conflict when installing
14 #
15 # Revision 1.1  2000/08/03 19:27:08  karldebisschop
16 # use Net::DNS to check name server
17 #
18 # Revision 1.1  2000/07/20 19:09:13  cwg
19 # All the pieces needed to use my version of check_dns.
20 #
22 use Getopt::Long;
23 use Net::DNS;
25  Getopt::Long::Configure(`bundling`);
26 GetOptions("V" => $opt_V,         "version" => $opt_V,
27                                          "h" => $opt_h,         "help" => $opt_h,
28                                          "t=i" => $opt_t,       "timeout=i" => $opt_t,
29                                          "s=s" => $opt_s,       "server=s" => $opt_s,
30                                          "H=s" => $opt_H,       "hostname=s" => $opt_H);
31                            
32 # -h means display verbose help screen
33 if($opt_h){ print_help(); exit 0; }
35 # -V means display version number
36 if ($opt_V) { print_version(); exit 0; }
38 # -H means host name
39 $opt_H = shift unless ($opt_H);
40 unless ($opt_H) { print_usage(); exit -1; }
41 if ($opt_H && 
42                 $opt_H =~ m/^([0-9]+.[0-9]+.[0-9]+.[0-9]+|[a-zA-Z][-a-zA-Z0]+(.[a-zA-Z][-a-zA-Z0]+)*)$/)
43 {
44         $host = $1;
45 } else {
46         print "$opt_H is not a valid host name";
47         exit -1;
48 }
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 }
62 # -t means timeout
63 my $timeout = 10 unless ($opt_t);
65 my $res = new Net::DNS::Resolver;
66 #$res->debug(1);
67 if ($server) {
68         $res->nameservers($server);
69 }
71 $res->tcp_timeout($timeout);
72 $SIG{ALRM} = &catch_alarm;
73 alarm($timeout);
75 $query = $res->query($host);
76 if ($query) {
77         my @answer = $query->answer;
78         if (@answer) {
79                 print join(`/`, map {
80                         $_->type . ` ` . $_->rdatastr;
81                 } @answer);
82                 exit 0;
83         } else {
84                 print "empty answer";
85                 exit 2;
86         }
87 }
88 else {
89         print "query failed: ", $res->errorstring, "";
90         exit 2;
91 }
93 sub catch_alarm {
94         print "query timed out";
95         exit 2;
96 }
98 sub print_version () {
99         my $arg0 =  $0;
100         chomp $arg0;
101         print "$arg0                        version 0.1";
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";
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]";