Subversion Repositories sysadmin_scripts

Rev

Go to most recent revision | Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
88 rodolico 1
#! /usr/bin/env perl
2
 
3
use strict;
4
use warnings;
5
 
89 rodolico 6
# Currently three types of tests
7
# type: lwp
8
# url: site to test
9
# timeout: optional integer, number of seconds. defaults to 'default timeout'
10
#
11
# type: ping
12
# url: site to test
13
# timeout: optional integer, number of seconds. defaults to 'default timeout'
14
#
15
# type: netcat
16
# url: site to test
17
# port: port to test on
18
# timeout: optional integer, number of seconds. defaults to 'default timeout'
93 rodolico 19
# additional parameters: any additional cli parameters to add.
89 rodolico 20
 
93 rodolico 21
 
88 rodolico 22
use Data::Dumper;
89 rodolico 23
use FindBin;
24
use File::Spec;
88 rodolico 25
 
93 rodolico 26
my $TESTING = 0;
27
 
89 rodolico 28
# name of script directory, no trailing slash
29
my $MY_DIR = $FindBin::Bin;
30
# name of currently executing file
31
my $MY_NAME = $FindBin::Script;
88 rodolico 32
 
89 rodolico 33
my $configName = $MY_DIR . '/' . $MY_NAME . '.yaml';
34
 
35
# hashref which holds configuration
36
my $config;
37
 
38
# simply read the contents of YAML file $filename into
39
# hashref and return the hashref
88 rodolico 40
sub loadConf {
89 rodolico 41
   use YAML::Tiny;
42
   my $filename = shift;
43
   my $conf = YAML::Tiny->read( $filename );
44
   return $conf->[0];
88 rodolico 45
}
46
 
89 rodolico 47
# not used. May use it later, who knows.
48
sub saveConf {
49
   use YAML::Tiny;
50
   my ( $filename, $config ) = @_;
51
   my $yaml = YAML::Tiny->new( $config );
52
   $yaml->write( $filename );
53
}
88 rodolico 54
 
89 rodolico 55
# check using netcat
56
sub checkNetCat {
93 rodolico 57
   my ($url, $port, $timeout, $additionalParameters ) = @_;
89 rodolico 58
   my $netcat = `which nc`;
59
   chomp $netcat;
60
   unless ( $netcat ) {
61
      warn "Netcat not found, not testing\n";
62
      return 0;
63
   }
93 rodolico 64
   $netcat = join( ' ', ($netcat, "-z -w $timeout", $additionalParameters, $url, $port ) );
65
   print "\t$netcat\n" if $TESTING;
89 rodolico 66
   `$netcat`;
67
   return not $? >> 8;
88 rodolico 68
} 
69
 
89 rodolico 70
# this has only been tested on http and https, but with a little work
71
# could do echo, ftp
72
# will NOT work with some WordPress sites since it goes into
73
# infinite redirect loop
88 rodolico 74
sub checkHTTP {
89 rodolico 75
   use LWP::UserAgent; # probably should eval this in case not loaded
88 rodolico 76
   my ($url, $timeout) = @_;
89 rodolico 77
   # just getting HEAD, so small amount of data
88 rodolico 78
   my $r = HTTP::Request->new( 'HEAD', $url );
89 rodolico 79
   # new agent, make the request, return success
88 rodolico 80
   my $ua = LWP::UserAgent->new();
81
   my $res = $ua->request($r);
82
   return $res->is_success;
83
}
84
 
89 rodolico 85
# we're only using icmp. Probably could extend to other things, but
86
# netcat and lwp do them also, I think.
87
# basically a ping. Must be run as root.
88 rodolico 88
sub checkPing {
89
   my ($url,$type,$timeout) = @_;
90
   use Net::Ping;
89 rodolico 91
   # verify running as root
92
   my $login = (getpwuid $>);
93
   if ( $login ne 'root' ) {
94
      warn "This script must be run as root for ping type\n";
95
      return 0;
96
   }
97
 
88 rodolico 98
   my $p = Net::Ping->new('icmp');
99
   return $p->ping( $url ) ? 1 : 0;
100
}   
101
 
89 rodolico 102
# determines which test to run, runs it and returns results
88 rodolico 103
sub runTest {
104
   my $parameters = shift;
89 rodolico 105
   #die Dumper( $parameters );
88 rodolico 106
   my $result;
107
   if ( $parameters->{'type'} eq 'lwp' ) {
89 rodolico 108
      $result = &checkHTTP( $parameters->{'url'}, $parameters->{'timeout'} );
88 rodolico 109
   } elsif ( $parameters->{'type'} eq 'ping' ) {
89 rodolico 110
      $result = checkPing( $parameters->{'url'}, $parameters->{'timeout'} );
111
   } elsif ( $parameters->{'type'} eq 'netcat' ) {
93 rodolico 112
      $result = checkNetCat( 
113
               $parameters->{'url'},
114
               $parameters->{'port'},
115
               $parameters->{'timeout'}, 
116
               defined $parameters->{'additional parameters'} ? $parameters->{'additional parameters'} : '' 
117
               );
89 rodolico 118
   } else {
91 rodolico 119
      warn "Unknown test type $parameters->{type}, aborting test\n";
89 rodolico 120
      $result = 0;
88 rodolico 121
   }
122
   return $result;
123
}
124
 
89 rodolico 125
# just make up the flag file name in one place so we don't
126
# forget to do it everyplace it is needed
127
sub flagFileName {
128
   my $name = shift;
129
   return "/tmp/$name.down";
130
}
88 rodolico 131
 
91 rodolico 132
# Report outage via system mail
133
# your system must be able to send e-mail from 'report from'
134
# otherwise, you'll need to use something like sendemail.pl
92 rodolico 135
# apt install libemail-sender-perl libemail-mime-perl
91 rodolico 136
sub reportViaMail {
137
   my ( $name,$parameters ) = @_;
138
   use Email::MIME;
139
   my $message = Email::MIME->create(
140
     header_str => [
141
       From    => $parameters->{'report from'},
142
       To      => $parameters->{'report to'},
143
       Subject => "Outage on $name",
144
     ],
145
     attributes => {
146
       encoding => 'quoted-printable',
147
       charset  => 'ISO-8859-1',
148
     },
149
     body_str => "$name has been reported as current state down\n",
150
   );
151
 
152
   # send the message
153
   use Email::Sender::Simple qw(sendmail);
154
   sendmail($message);   
155
}   
156
 
90 rodolico 157
# report the outage
158
sub reportOutage {
159
   my ( $name,$parameters ) = @_;
91 rodolico 160
   if ( $parameters->{'report type'} eq 'mail' ) {
161
      &reportViaMail( $name,$parameters );
162
   } else {
163
      warn "Do not have a way of notifying outage for $name\n";
164
   }
90 rodolico 165
}
166
 
89 rodolico 167
# this will put a flag file in /tmp/monitorNetwork which will control
168
# countdown in case of failure
169
# if the countdown is exceeded, it will report the status 
170
sub processFailure {
171
   my ( $name, $parameters ) = @_;
90 rodolico 172
   # temporary for testing
92 rodolico 173
   my $flagFileName = &flagFileName( $name );
90 rodolico 174
   my $remainingCounts = $parameters->{'failure count'};
93 rodolico 175
   print "$name => Failed\n" if $TESTING;
90 rodolico 176
   if ( -f $flagFileName ) {
177
      # open flag file, read the value into $remaingCounts
178
      return if $remainingCounts == -1;
179
   }
180
   if ( $remainingCounts-- == 0 ) {
92 rodolico 181
      &reportOutage( $name, $parameters );
90 rodolico 182
   }
183
   # open flag file, write new $remainingCounts to it
88 rodolico 184
}
89 rodolico 185
 
186
# we are up, so check for any flag file and unlink if found
187
sub cleanUp {
188
   my $name = shift;
93 rodolico 189
   print "$name => Success\n" if $TESTING;
89 rodolico 190
   $name = &flagFileName( $name );
191
   unlink $name if -f $name;
192
}
193
 
194
#######################################################################
195
#                     Main Code
196
#######################################################################
197
 
198
# get the config file
199
$config = &loadConf( $configName );
200
 
201
# loop through each site to check
202
foreach my $testToRun( keys %{$config->{'sites to check'}} ) {
203
   # there is a faster way of doing this, but I don't remember it
204
   # you can merge two hashes, overwriting one with values if not existing in the other
205
   # I'll look it up, but for now
206
   foreach my $key ( keys %{$config->{'defaults'}} ) {
207
      $config->{'sites to check'}->{$testToRun}->{$key} = $config->{'defaults'}->{$key} unless defined $config->{'sites to check'}->{$testToRun}->{$key};
208
   }
209
   # run the test
210
   if ( &runTest( $config->{'sites to check'}->{$testToRun} ) ) {
211
      &cleanUp( $testToRun ); # success, clean up any old stuff
212
   } else { # failure, this is what we're here for
213
      &processFailure( $testToRun, $config->{'sites to check'}->{$testToRun} );
214
   }
215
}