08e57c0823e2ec304c50c283fd3f1707c2ebec7e
[clearscm.git] / bin / checkdns
1 #!/usr/bin/perl
2
3 =pod
4
5 =head1 NAME $RCSfile: checkdns,v $
6
7 Check DNS by attempting to call gethostbyname of a well known host.
8
9 =head1 VERSION
10
11 =over
12
13 =item Author
14
15 Andrew DeFaria <Andrew@ClearSCM.com>
16
17 =item Revision
18
19 $Revision: 1.6 $
20
21 =item Created:
22
23 Wed Aug 30 21:03:14 CDT 2006
24
25 =item Modified:
26
27 $Date: 2011/04/15 15:05:16 $
28
29 =back
30
31 =head1 SYNOPSIS
32
33  Usage: checkdns [-u|sage] [-v|erbose] [-d|ebug]
34                  [-s|leep <n>] [-l|ogpath <path>] 
35  
36  Where:
37    -u|sage     Print this usage
38    -v|erbose:  Verbose mode 
39    -d|ebug:    Emit debug information
40    
41    -s|leep <n>:      Set sleep period to <n> minutes (Default: 15 minutes)
42    -l|ogpath <path>: Put the log file in <path> (Default: /var/log)
43
44 =head1 DESCRIPTION
45
46 This script will look at the security logfile for attempted breakins and then 
47 use whois to report them to the upstream provider.
48
49 =cut
50
51 use strict;
52 use warnings;
53
54 use Getopt::Long;
55
56 use FindBin;
57
58 use lib "$FindBin::Bin/../lib";
59
60 use Logger;
61 use Utils;
62 use Display;
63
64 my $VERSION  = '$Revision: 1.6 $';
65   ($VERSION) = ($VERSION =~ /\$Revision: (.*) /);
66
67 $0 = $FindBin::Script;
68
69 my $host          = 'google.com';
70 my $sleep         = 15;
71 my $initial_sleep = $sleep;
72 my $logpath       = '/var/log';
73 my $log;
74
75 sub CheckDNS {
76   my ($host) = @_;
77
78   $? = 0;
79
80   my @ipaddrs = gethostbyname $host;
81
82   if (!@ipaddrs) {
83     debug "Host: $host (ipaddrs empty)";
84
85     # Cut down sleep time to monitor this outage more closely but do not go 
86     # below once a minute.
87     if ($sleep > 1) {
88       $sleep -= $sleep / 2;
89     } else {
90       $sleep = 1;
91     } # if
92
93     return 1;
94   } # if
95
96   # Successful lookup - set $sleep to $initial_sleep
97   $sleep = $initial_sleep;
98
99   return;
100 } # CheckDNS
101
102 sub Shutdown {
103   my $msg;
104
105   my $errors = $log->errors;
106   
107   $log->msg ("$errors errors encountered since starting")
108     if $errors;
109
110   $log->msg ('Caught interrupt - shutting down');
111
112   exit $errors;
113 } # Interrupt
114
115 # Main
116 GetOptions (
117   usage       => sub { Usage },
118   verbose     => sub { set_verbose },
119   debug       => sub { set_debug },
120   'sleep=i'   => \$sleep,
121   'logpath=s' => \$logpath,
122 ) or Usage 'Invalid parameter';
123
124 $SIG {INT}  =
125 $SIG {TERM} = \&Shutdown;
126
127 # Call sethostent so that gethostbyname is fresh everytime
128 sethostent (0);
129
130 $log = Logger->new (
131   path          => $logpath,
132   timestamped   => 'yes',
133   append        => 'yes',
134 );
135
136 $log->msg (
137   "Started $FindBin::Script $VERSION logging to $logpath/$FindBin::Script.log"
138 );
139
140 $log->msg ("Polling DNS on host $host every $sleep minutes");  
141
142 EnterDaemonMode 
143   unless get_debug;
144
145 while () {
146   my $status = CheckDNS $host;
147
148   if ($status) {
149     $log->err ("Unable to resolve IP address for $host");
150   } else {
151     $log->msg ("Successfully resolved $host");
152   } # if
153
154   sleep ($sleep * 60);
155 } # while
156
157 =pod
158
159 =head1 CONFIGURATION AND ENVIRONMENT
160
161 DEBUG: If set then $debug is set to this level.
162
163 VERBOSE: If set then $verbose is set to this level.
164
165 TRACE: If set then $trace is set to this level.
166
167 =head1 DEPENDENCIES
168
169 =head2 Perl Modules
170
171 L<FindBin>
172
173 L<Getopt::Long|Getopt::Long>
174
175 =head2 ClearSCM Perl Modules
176
177 =begin man 
178
179  Display
180  Logger
181  Utils
182
183 =end man
184
185 =begin html
186
187 <blockquote>
188 <a href="http://clearscm.com/php/cvs_man.php?file=lib/Display.pm">Display</a><br>
189 <a href="http://clearscm.com/php/cvs_man.php?file=lib/Logger.pm">Logger</a><br>
190 <a href="http://clearscm.com/php/cvs_man.php?file=lib/Utils.pm">Utils</a><br>
191 </blockquote>
192
193 =end html
194
195 =head1 BUGS AND LIMITATIONS
196
197 There are no known bugs in this script
198
199 Please report problems to Andrew DeFaria <Andrew@ClearSCM.com>.
200
201 =head1 LICENSE AND COPYRIGHT
202
203 Copyright (c) 2004, ClearSCM, Inc. All rights reserved.
204
205 =cut