Code

Fix for regex input of '|', being output causing problems with Nagios' parsing of
[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 # Revision 1.3  2002/05/07 05:35:49  sghosh
9 # 2nd fix for ePN
10 #
11 # Revision 1.2  2002/05/02 16:43:29  sghosh
12 # fix for embedded perl
13 #
14 # Revision 1.1.1.1  2002/02/28 06:43:00  egalstad
15 # Initial import of existing plugin code
16 #
17 # Revision 1.1  2000/08/03 20:41:12  karldebisschop
18 # rename to avoid conflict when installing
19 #
20 # Revision 1.1  2000/08/03 19:27:08  karldebisschop
21 # use Net::DNS to check name server
22 #
23 # Revision 1.1  2000/07/20 19:09:13  cwg
24 # All the pieces needed to use my version of check_dns.
25 #
26
28 use Getopt::Long;
29 use Net::DNS;
30 use lib utils.pm;
31 use utils ;
33 my $PROGNAME = "check_netdns";
35 Getopt::Long::Configure(`bundling`);
36 GetOptions("V" => $opt_V,         "version" => $opt_V,
37                                          "h" => $opt_h,         "help" => $opt_h,
38                                          "t=i" => $opt_t,       "timeout=i" => $opt_t,
39                                          "s=s" => $opt_s,       "server=s" => $opt_s,
40                                          "H=s" => $opt_H,       "hostname=s" => $opt_H);
41                            
42 # -h means display verbose help screen
43 if($opt_h){ print_help(); exit 0; }
45 # -V means display version number
46 if ($opt_V) { print_version(); exit 0; }
48 # -H means host name
49 $opt_H = shift unless ($opt_H);
50 unless ($opt_H) { print_usage(); exit -1; }
51 if ($opt_H && 
52                 $opt_H =~ m/^([0-9]+.[0-9]+.[0-9]+.[0-9]+|[a-zA-Z][-a-zA-Z0]+(.[a-zA-Z][-a-zA-Z0]+)*)$/)
53 {
54         $host = $1;
55 } else {
56         print "$opt_H is not a valid host name";
57         exit -1;
58 }
60 # -s means server name
61 $opt_s = shift unless ($opt_s);
62 if ($opt_s) {
63         if ($opt_s =~ m/^([0-9]+.[0-9]+.[0-9]+.[0-9]+|[a-zA-Z][-a-zA-Z0]+(.[a-zA-Z][-a-zA-Z0]+)*)$/)
64         {
65                 $server = $1;
66         } else {
67                 print "$opt_s is not a valid host name";
68                 exit -1;
69         }
70 }
72 # -t means timeout
73 my $timeout = 10 unless ($opt_t);
75 my $res = new Net::DNS::Resolver;
76 #$res->debug(1);
77 if ($server) {
78         $res->nameservers($server);
79 }
81 $res->tcp_timeout($timeout);
82 $SIG{ALRM} = &catch_alarm;
83 alarm($timeout);
85 $query = $res->query($host);
86 if ($query) {
87         my @answer = $query->answer;
88         if (@answer) {
89                 print join(`/`, map {
90                         $_->type . ` ` . $_->rdatastr;
91                 } @answer);
92                 exit 0;
93         } else {
94                 print "empty answer";
95                 exit 2;
96         }
97 }
98 else {
99         print "query failed: ", $res->errorstring, "";
100         exit 2;
103 sub catch_alarm {
104         print "query timed out";
105         exit 2;
108 sub print_version () {
109         my $arg0 =  $0;
110         chomp $arg0;
111         print "$arg0                        version 0.1";
113 sub print_help() {
114         print_version();
115         print "";
116         print "Check if a nameserver can resolve a given hostname.";
117         print "";
118         print_usage();
119         print "";
120         print "-H, --hostname=HOST";
121         print "   The name or address you want to query";
122         print "-s, --server=HOST";
123         print "   Optional DNS server you want to use for the lookup";
124         print "-t, --timeout=INTEGER";
125         print "   Seconds before connection times out (default: 10)";
126         print "-h, --help";
127         print "   Print detailed help";
128         print "-V, --version";
129         print "   Print version numbers and license information";
132 sub print_usage () {
133         my $arg0 = $0;
134         chomp $arg0;
135         print "$arg0 check_dns -H host [-s server] [-t timeout]";
136         print "$arg0 [-h | --help]";
137         print "$arg0 [-V | --version]";