Subversion Repositories sysadmin_scripts

Rev

Rev 88 | Rev 90 | Go to most recent revision | Show entire file | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 88 Rev 89
Line 1... Line 1...
1
#! /usr/bin/env perl
1
#! /usr/bin/env perl
2
 
2
 
3
use strict;
3
use strict;
4
use warnings;
4
use warnings;
5
 
5
 
-
 
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'
-
 
19
 
6
use Data::Dumper;
20
use Data::Dumper;
-
 
21
use FindBin;
-
 
22
use File::Spec;
-
 
23
 
-
 
24
# name of script directory, no trailing slash
-
 
25
my $MY_DIR = $FindBin::Bin;
-
 
26
# name of currently executing file
-
 
27
my $MY_NAME = $FindBin::Script;
7
 
28
 
8
my $DEFAULT_TIMEOUT = 60;
29
my $configName = $MY_DIR . '/' . $MY_NAME . '.yaml';
9
 
30
 
10
# hashref which holds targets
31
# hashref which holds configuration
11
my $sitesToCheck;
32
my $config;
12
     
33
 
-
 
34
# simply read the contents of YAML file $filename into
-
 
35
# hashref and return the hashref
13
sub loadConf {
36
sub loadConf {
-
 
37
   use YAML::Tiny;
-
 
38
   my $filename = shift;
-
 
39
   my $conf = YAML::Tiny->read( $filename );
-
 
40
   return $conf->[0];
14
}
41
}
15
 
42
 
-
 
43
# not used. May use it later, who knows.
-
 
44
sub saveConf {
-
 
45
   use YAML::Tiny;
-
 
46
   my ( $filename, $config ) = @_;
-
 
47
   my $yaml = YAML::Tiny->new( $config );
-
 
48
   $yaml->write( $filename );
-
 
49
}
16
 
50
 
-
 
51
# check using netcat
17
sub checkNC {
52
sub checkNetCat {
-
 
53
   my ($url, $port, $timeout ) = @_;
-
 
54
   my $netcat = `which nc`;
18
   use Net::Netcat;
55
   chomp $netcat;
-
 
56
   unless ( $netcat ) {
-
 
57
      warn "Netcat not found, not testing\n";
-
 
58
      return 0;
-
 
59
   }
-
 
60
   $netcat = join( ' ', ($netcat, "-z -w $timeout", $url, $port ) );
-
 
61
   `$netcat`;
-
 
62
   return not $? >> 8;
19
} 
63
} 
20
 
64
 
21
 
-
 
-
 
65
# this has only been tested on http and https, but with a little work
-
 
66
# could do echo, ftp
-
 
67
# will NOT work with some WordPress sites since it goes into
-
 
68
# infinite redirect loop
22
sub checkHTTP {
69
sub checkHTTP {
23
   use LWP::UserAgent;
70
   use LWP::UserAgent; # probably should eval this in case not loaded
24
   my ($url, $timeout) = @_;
71
   my ($url, $timeout) = @_;
25
   #print "URL = $url\nTimeout = $timeout\n\n";
-
 
26
   return -1 unless $url && $timeout;
72
   # just getting HEAD, so small amount of data
27
   my $r = HTTP::Request->new( 'HEAD', $url );
73
   my $r = HTTP::Request->new( 'HEAD', $url );
-
 
74
   # new agent, make the request, return success
28
   my $ua = LWP::UserAgent->new();
75
   my $ua = LWP::UserAgent->new();
29
   my $res = $ua->request($r);
76
   my $res = $ua->request($r);
30
   return $res->is_success;
77
   return $res->is_success;
31
   #die Dumper( $res );
-
 
32
}
78
}
33
 
79
 
-
 
80
# we're only using icmp. Probably could extend to other things, but
-
 
81
# netcat and lwp do them also, I think.
-
 
82
# basically a ping. Must be run as root.
34
sub checkPing {
83
sub checkPing {
35
   my ($url,$type,$timeout) = @_;
84
   my ($url,$type,$timeout) = @_;
36
   use Net::Ping;
85
   use Net::Ping;
-
 
86
   # verify running as root
-
 
87
   my $login = (getpwuid $>);
-
 
88
   if ( $login ne 'root' ) {
-
 
89
      warn "This script must be run as root for ping type\n";
-
 
90
      return 0;
-
 
91
   }
-
 
92
 
37
   my $p = Net::Ping->new('icmp');
93
   my $p = Net::Ping->new('icmp');
38
   return $p->ping( $url ) ? 1 : 0;
94
   return $p->ping( $url ) ? 1 : 0;
39
}   
95
}   
40
 
96
 
-
 
97
# determines which test to run, runs it and returns results
41
sub runTest {
98
sub runTest {
42
   my $parameters = shift;
99
   my $parameters = shift;
-
 
100
   #die Dumper( $parameters );
43
   my $result;
101
   my $result;
44
   if ( $parameters->{'type'} eq 'lwp' ) {
102
   if ( $parameters->{'type'} eq 'lwp' ) {
45
      $result = &checkHTTP( $parameters->{'url'}, $parameters->{'timeout'} ? $parameters->{'timeout'} : $DEFAULT_TIMEOUT );
103
      $result = &checkHTTP( $parameters->{'url'}, $parameters->{'timeout'} );
46
   } elsif ( $parameters->{'type'} eq 'ping' ) {
104
   } elsif ( $parameters->{'type'} eq 'ping' ) {
-
 
105
      $result = checkPing( $parameters->{'url'}, $parameters->{'timeout'} );
-
 
106
   } elsif ( $parameters->{'type'} eq 'netcat' ) {
47
      $result = checkPing( $parameters->{'url'}, $parameters->{'timeout'} ? $parameters->{'timeout'} : $DEFAULT_TIMEOUT );
107
      $result = checkNetCat( $parameters->{'url'}, $parameters->{'port'}, $parameters->{'timeout'} );
-
 
108
   } else {
-
 
109
      $result = 0;
48
   }
110
   }
49
   return $result;
111
   return $result;
50
}
112
}
51
 
113
 
-
 
114
# just make up the flag file name in one place so we don't
-
 
115
# forget to do it everyplace it is needed
-
 
116
sub flagFileName {
-
 
117
   my $name = shift;
52
my $login = (getpwuid $>);
118
   return "/tmp/$name.down";
-
 
119
}
-
 
120
   
-
 
121
 
53
die "This script must be run as root\n" if $login ne 'root';
122
# this will put a flag file in /tmp/monitorNetwork which will control
-
 
123
# countdown in case of failure
-
 
124
# if the countdown is exceeded, it will report the status 
-
 
125
sub processFailure {
-
 
126
   my ( $name, $parameters ) = @_;
-
 
127
   print "Failed test for $name\n";
-
 
128
}
54
 
129
 
-
 
130
# we are up, so check for any flag file and unlink if found
-
 
131
sub cleanUp {
55
my $result;
132
   my $name = shift;
-
 
133
   $name = &flagFileName( $name );
-
 
134
   unlink $name if -f $name;
-
 
135
}
-
 
136
 
-
 
137
#######################################################################
-
 
138
#                     Main Code
-
 
139
#######################################################################
-
 
140
 
-
 
141
# get the config file
-
 
142
$config = &loadConf( $configName );
-
 
143
 
-
 
144
# loop through each site to check
56
foreach my $testToRun( keys %$sitesToCheck ) {
145
foreach my $testToRun( keys %{$config->{'sites to check'}} ) {
-
 
146
   # there is a faster way of doing this, but I don't remember it
-
 
147
   # you can merge two hashes, overwriting one with values if not existing in the other
-
 
148
   # I'll look it up, but for now
-
 
149
   foreach my $key ( keys %{$config->{'defaults'}} ) {
-
 
150
      $config->{'sites to check'}->{$testToRun}->{$key} = $config->{'defaults'}->{$key} unless defined $config->{'sites to check'}->{$testToRun}->{$key};
-
 
151
   }
-
 
152
   # run the test
57
   $result = &runTest( $sitesToCheck->{$testToRun} );
153
   if ( &runTest( $config->{'sites to check'}->{$testToRun} ) ) {
58
   print "Results for $testToRun " . ( $result ? "is" : "is not" ) . " ok\n";
154
      &cleanUp( $testToRun ); # success, clean up any old stuff
-
 
155
   } else { # failure, this is what we're here for
-
 
156
      &processFailure( $testToRun, $config->{'sites to check'}->{$testToRun} );
-
 
157
   }
59
}
158
}