For 1.4.9 release
[monitoring-plugins.git] / plugins-scripts / check_netdns.pl
blob6872b9320a813215f1d541b9bf3d0c94652a616f
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)
6 # Copyright 2000, virCIO, LLP
8 # $Log$
9 # Revision 1.3 2002/05/07 05:35:49 sghosh
10 # 2nd fix for ePN
12 # Revision 1.2 2002/05/02 16:43:29 sghosh
13 # fix for embedded perl
15 # Revision 1.1.1.1 2002/02/28 06:43:00 egalstad
16 # Initial import of existing plugin code
18 # Revision 1.1 2000/08/03 20:41:12 karldebisschop
19 # rename to avoid conflict when installing
21 # Revision 1.1 2000/08/03 19:27:08 karldebisschop
22 # use Net::DNS to check name server
24 # Revision 1.1 2000/07/20 19:09:13 cwg
25 # All the pieces needed to use my version of check_dns.
29 use Getopt::Long;
30 use Net::DNS;
31 use lib utils.pm;
32 use utils ;
34 my $PROGNAME = "check_netdns";
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);
43 # -h means display verbose help screen
44 if($opt_h){ print_help(); exit 0; }
46 # -V means display version number
47 if ($opt_V) { print_version(); exit 0; }
49 # -H means host name
50 $opt_H = shift unless ($opt_H);
51 unless ($opt_H) { print_usage(); exit -1; }
52 if ($opt_H &&
53 $opt_H =~ m/^([0-9]+.[0-9]+.[0-9]+.[0-9]+|[a-zA-Z][-a-zA-Z0]+(.[a-zA-Z][-a-zA-Z0]+)*)$/)
55 $host = $1;
56 } else {
57 print "$opt_H is not a valid host name";
58 exit -1;
61 # -s means server name
62 $opt_s = shift unless ($opt_s);
63 if ($opt_s) {
64 if ($opt_s =~ m/^([0-9]+.[0-9]+.[0-9]+.[0-9]+|[a-zA-Z][-a-zA-Z0]+(.[a-zA-Z][-a-zA-Z0]+)*)$/)
66 $server = $1;
67 } else {
68 print "$opt_s is not a valid host name";
69 exit -1;
73 # -t means timeout
74 my $timeout = 10 unless ($opt_t);
76 my $res = new Net::DNS::Resolver;
77 #$res->debug(1);
78 if ($server) {
79 $res->nameservers($server);
82 $res->tcp_timeout($timeout);
83 $SIG{ALRM} = &catch_alarm;
84 alarm($timeout);
86 $query = $res->query($host);
87 if ($query) {
88 my @answer = $query->answer;
89 if (@answer) {
90 print join(`/`, map {
91 $_->type . ` ` . $_->rdatastr;
92 } @answer);
93 exit 0;
94 } else {
95 print "empty answer";
96 exit 2;
99 else {
100 print "query failed: ", $res->errorstring, "";
101 exit 2;
104 sub catch_alarm {
105 print "query timed out";
106 exit 2;
109 sub print_version () {
110 my $arg0 = $0;
111 chomp $arg0;
112 print "$arg0 version 0.1";
114 sub print_help() {
115 print_version();
116 print "";
117 print "Check if a nameserver can resolve a given hostname.";
118 print "";
119 print_usage();
120 print "";
121 print "-H, --hostname=HOST";
122 print " The name or address you want to query";
123 print "-s, --server=HOST";
124 print " Optional DNS server you want to use for the lookup";
125 print "-t, --timeout=INTEGER";
126 print " Seconds before connection times out (default: 10)";
127 print "-h, --help";
128 print " Print detailed help";
129 print "-V, --version";
130 print " Print version numbers and license information";
133 sub print_usage () {
134 my $arg0 = $0;
135 chomp $arg0;
136 print "$arg0 check_dns -H host [-s server] [-t timeout]";
137 print "$arg0 [-h | --help]";
138 print "$arg0 [-V | --version]";