Changes to cedrtbot scripts
[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    -de|bug:    Emit debug information
40
41    -h|ost:           Host to check (Default: google.com)
42    -s|leep <n>:      Set sleep period to <n> minutes (Default: 15 minutes)
43    -l|ogpath <path>: Put the log file in <path> (Default: /var/log)
44    -da|emon:         Whether to go into daemon most (Default: yes)
45
46 =head1 DESCRIPTION
47
48 This script will look at the security logfile for attempted breakins and then 
49 use whois to report them to the upstream provider.
50
51 =cut
52
53 use strict;
54 use warnings;
55
56 use Getopt::Long;
57
58 use FindBin;
59
60 use lib "$FindBin::Bin/../lib";
61
62 use Logger;
63 use Utils;
64 use Display;
65
66 my $VERSION  = '$Revision: 1.6 $';
67   ($VERSION) = ($VERSION =~ /\$Revision: (.*) /);
68
69 $0 = $FindBin::Script;
70
71 my ($log, $initial_sleep);
72
73 my %opts = (
74   host        => 'google.com',
75   sleep       => 15,
76   logpath     => '/var/local/log',
77   usage       => sub { Usage },
78   verbose     => sub { set_verbose },
79   debug       => sub { set_debug },
80   daemon      => 1,
81 );
82
83 sub CheckDNS {
84   my ($host) = @_;
85
86   $? = 0;
87
88   my @ipaddrs = gethostbyname $host;
89
90   if (!@ipaddrs) {
91     debug "Host: $host (ipaddrs empty)";
92
93     # Cut down sleep time to monitor this outage more closely but do not go 
94     # below once a minute.
95     if ($opts{sleep} > 1) {
96       $opts{sleep} -= $opts{sleep} / 2;
97     } else {
98       $opts{sleep} = 1;
99     } # if
100
101     return 1;
102   } # if
103
104   # Successful lookup - set $sleep to $initial_sleep
105   $opts{sleep} = $initial_sleep;
106
107   return;
108 } # CheckDNS
109
110 sub Shutdown {
111   my $msg;
112
113   my $errors = $log->errors;
114
115   $log->msg("$errors errors encountered since starting") if $errors;
116   $log->msg('Caught interrupt - shutting down');
117
118   exit $errors;
119 } # Interrupt
120
121 # Main
122 GetOptions (
123   \%opts,
124   'usage',
125   'verbose',
126   'debug',
127   'host=s',
128   'sleep=i',
129   'logpath=s',
130   'daemon!',
131 ) or Usage 'Invalid parameter';
132
133 $initial_sleep = $opts{sleep};
134
135 $SIG {INT}  =
136 $SIG {TERM} = \&Shutdown;
137
138 # Call sethostent so that gethostbyname is fresh everytime
139 sethostent (0);
140
141 $log = Logger->new (
142   path        => $opts{logpath},
143   timestamped => 'yes',
144   append      => 'yes',
145 );
146
147 $log->msg (
148   "Started $FindBin::Script $VERSION logging to $opts{logpath}/$FindBin::Script.log"
149 );
150
151 if ($opts{sleep} > 1) {
152   $log->msg ("Polling DNS on host $opts{host} every $opts{sleep} minutes");
153 } else {
154   $log->msg ("Polling DNS on host $opts{host} every minute");  
155 } # if
156
157 $opts{daemon} = 0 if get_debug;
158
159 EnterDaemonMode if $opts{daemon};
160
161 while () {
162   my $status = CheckDNS $opts{host};
163
164   if ($status) {
165     $log->err ("Unable to resolve IP address for $opts{host}");
166   } else {
167     $log->msg ("Successfully resolved $opts{host}");
168   } # if
169
170   sleep $opts{sleep} * 60;
171 } # while
172
173 =pod
174
175 =head1 CONFIGURATION AND ENVIRONMENT
176
177 DEBUG: If set then $debug is set to this level.
178
179 VERBOSE: If set then $verbose is set to this level.
180
181 TRACE: If set then $trace is set to this level.
182
183 =head1 DEPENDENCIES
184
185 =head2 Perl Modules
186
187 L<FindBin>
188
189 L<Getopt::Long|Getopt::Long>
190
191 =head2 ClearSCM Perl Modules
192
193 =begin man 
194
195  Display
196  Logger
197  Utils
198
199 =end man
200
201 =begin html
202
203 <blockquote>
204 <a href="http://clearscm.com/php/scm_man.php?file=lib/Display.pm">Display</a><br>
205 <a href="http://clearscm.com/php/scm_man.php?file=lib/Logger.pm">Logger</a><br>
206 <a href="http://clearscm.com/php/scm_man.php?file=lib/Utils.pm">Utils</a><br>
207 </blockquote>
208
209 =end html
210
211 =head1 BUGS AND LIMITATIONS
212
213 There are no known bugs in this script
214
215 Please report problems to Andrew DeFaria <Andrew@ClearSCM.com>.
216
217 =head1 LICENSE AND COPYRIGHT
218
219 Copyright (c) 2004, ClearSCM, Inc. All rights reserved.
220
221 =cut