Merge pull request #1578 from sni/tests_fix_snmp_test
[monitoring-plugins.git] / plugins-scripts / check_netdns.pl
blob38538e560f07cd0f7d744e55262cecec09a1db17
1 #!@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 # Revision 1.3 2002/05/07 05:35:49 sghosh
9 # 2nd fix for ePN
11 # Revision 1.2 2002/05/02 16:43:29 sghosh
12 # fix for embedded perl
14 # Revision 1.1.1.1 2002/02/28 06:43:00 egalstad
15 # Initial import of existing plugin code
17 # Revision 1.1 2000/08/03 20:41:12 karldebisschop
18 # rename to avoid conflict when installing
20 # Revision 1.1 2000/08/03 19:27:08 karldebisschop
21 # use Net::DNS to check name server
23 # Revision 1.1 2000/07/20 19:09:13 cwg
24 # All the pieces needed to use my version of check_dns.
28 use Getopt::Long;
29 use Net::DNS;
30 use FindBin;
31 use lib "$FindBin::Bin";
32 use utils;
34 my $PROGNAME = "check_netdns";
35 sub print_help ();
36 sub print_version();
37 sub print_usage ();
39 $ENV{'PATH'}='@TRUSTED_PATH@';
40 $ENV{'BASH_ENV'}='';
41 $ENV{'ENV'}='';
43 Getopt::Long::Configure(`bundling`);
44 GetOptions("V" => $opt_V, "version" => $opt_V,
45 "h" => $opt_h, "help" => $opt_h,
46 "t=i" => $opt_t, "timeout=i" => $opt_t,
47 "s=s" => $opt_s, "server=s" => $opt_s,
48 "H=s" => $opt_H, "hostname=s" => $opt_H);
50 # -h means display verbose help screen
51 if($opt_h){ print_help(); exit 3; }
53 # -V means display version number
54 if ($opt_V) { print_version(); exit 3; }
56 # -H means host name
57 $opt_H = shift unless ($opt_H);
58 unless ($opt_H) { print_usage(); exit -1; }
59 if ($opt_H &&
60 $opt_H =~ m/^([0-9]+.[0-9]+.[0-9]+.[0-9]+|[a-zA-Z][-a-zA-Z0]+(.[a-zA-Z][-a-zA-Z0]+)*)$/)
62 $host = $1;
63 } else {
64 print "$opt_H is not a valid host name";
65 exit -1;
68 # -s means server name
69 $opt_s = shift unless ($opt_s);
70 if ($opt_s) {
71 if ($opt_s =~ m/^([0-9]+.[0-9]+.[0-9]+.[0-9]+|[a-zA-Z][-a-zA-Z0]+(.[a-zA-Z][-a-zA-Z0]+)*)$/)
73 $server = $1;
74 } else {
75 print "$opt_s is not a valid host name";
76 exit 3;
80 # -t means timeout
81 my $timeout = 10 unless ($opt_t);
83 my $res = new Net::DNS::Resolver;
84 #$res->debug(1);
85 if ($server) {
86 $res->nameservers($server);
89 $res->tcp_timeout($timeout);
90 $SIG{ALRM} = &catch_alarm;
91 alarm($timeout);
93 $query = $res->query($host);
94 if ($query) {
95 my @answer = $query->answer;
96 if (@answer) {
97 print join(`/`, map {
98 $_->type . ` ` . $_->rdatastr;
99 } @answer);
100 exit 0;
101 } else {
102 print "empty answer";
103 exit 2;
106 else {
107 print "query failed: ", $res->errorstring, "";
108 exit 2;
111 sub catch_alarm {
112 print "query timed out";
113 exit 2;
116 sub print_version () {
117 my $arg0 = $0;
118 chomp $arg0;
119 print "$arg0 version 0.1";
121 sub print_help() {
122 print_version();
123 print "";
124 print "Check if a nameserver can resolve a given hostname.";
125 print "";
126 print_usage();
127 print "";
128 print "-H, --hostname=HOST";
129 print " The name or address you want to query";
130 print "-s, --server=HOST";
131 print " Optional DNS server you want to use for the lookup";
132 print "-t, --timeout=INTEGER";
133 print " Seconds before connection times out (default: 10)";
134 print "-h, --help";
135 print " Print detailed help";
136 print "-V, --version";
137 print " Print version numbers and license information";
140 sub print_usage () {
141 my $arg0 = $0;
142 chomp $arg0;
143 print "$arg0 check_dns -H host [-s server] [-t timeout]";
144 print "$arg0 [-h | --help]";
145 print "$arg0 [-V | --version]";