summaryrefslogtreecommitdiffstats
path: root/plugins-scripts/check_netdns.pl
diff options
context:
space:
mode:
Diffstat (limited to 'plugins-scripts/check_netdns.pl')
-rwxr-xr-xplugins-scripts/check_netdns.pl129
1 files changed, 129 insertions, 0 deletions
diff --git a/plugins-scripts/check_netdns.pl b/plugins-scripts/check_netdns.pl
new file mode 100755
index 00000000..4bf7bd76
--- /dev/null
+++ b/plugins-scripts/check_netdns.pl
@@ -0,0 +1,129 @@
1#!/usr/bin/perl -w
2
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#
21
22use Getopt::Long;
23use Net::DNS;
24
25 Getopt::Long::Configure(`bundling`);
26GetOptions("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
33if($opt_h){ print_help(); exit 0; }
34
35# -V means display version number
36if ($opt_V) { print_version(); exit 0; }
37
38# -H means host name
39$opt_H = shift unless ($opt_H);
40unless ($opt_H) { print_usage(); exit -1; }
41if ($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}
49
50# -s means server name
51$opt_s = shift unless ($opt_s);
52if ($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}
61
62# -t means timeout
63my $timeout = 10 unless ($opt_t);
64
65my $res = new Net::DNS::Resolver;
66#$res->debug(1);
67if ($server) {
68 $res->nameservers($server);
69}
70
71$res->tcp_timeout($timeout);
72$SIG{ALRM} = &catch_alarm;
73alarm($timeout);
74
75$query = $res->query($host);
76if ($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}
88else {
89 print "query failed: ", $res->errorstring, "";
90 exit 2;
91}
92
93sub catch_alarm {
94 print "query timed out";
95 exit 2;
96}
97
98sub print_version () {
99 my $arg0 = $0;
100 chomp $arg0;
101 print "$arg0 version 0.1";
102}
103sub 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";
120}
121
122sub 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]";
128}
129