Subversion Repositories sysadmin_scripts

Rev

Rev 141 | Show entire file | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 141 Rev 142
Line 1... Line 1...
1
#! /usr/bin/env perl
1
#! /usr/bin/env perl
2
 
2
 
-
 
3
# Script to start any virtuals which are down. Designed to be run
-
 
4
# from a cron job. The first pass, it will determine a virtual down
-
 
5
# and create a flag file file into /tmp/virtname.down
-
 
6
# on the next pass, it will see the virtual is still down and
-
 
7
# the flag file exists in /tmp. At that point, it will start the virtual
-
 
8
# and remove the file.
-
 
9
# on any pass, if it sees a virtual is up, it will remove the flag file
-
 
10
# if it exists.
-
 
11
 
-
 
12
# thus, length of time a virtual can be off is 2x periodicity of cron entry.
-
 
13
# we run it every 5 minutes, meaning the average down time is 7.5 min, and max
-
 
14
# is 10 min
-
 
15
 
-
 
16
# Looks for checkVirtuals.cfg in the same directory as the script.
-
 
17
# that file contains a list of virtuals found with virsh list
-
 
18
# any line with a pound sign in the first column will be ignored.
-
 
19
 
3
use strict;
20
use strict;
4
use warnings;
21
use warnings;
5
 
22
 
-
 
23
my $configFileSuffix = '.cfg';
6
my @servers = ( 
24
my @servers;
-
 
25
 
7
   'enfocus.primarycolors.local',
26
use File::Spec::Functions qw(rel2abs);
8
   'remote0',
27
use File::Basename;
-
 
28
 
-
 
29
# loads the config file into $servers, which is a reference to an array
9
   'remote1',
30
sub loadConfig {
10
   'remote2',
31
   my ($confName, $servers ) = @_;
11
   'remote3',
32
   if ( -f $confName ) {
12
   'zabbix.primarycolors.local',
33
      open CONF, "<$confName" or die "Could not read $confName: $!\n";
13
   'netserver.primarycolorinc.com'   
34
      @$servers = grep { ! m/^#/ } <CONF>;
-
 
35
      close CONF;
-
 
36
      chomp @$servers;
14
   );
37
   }
15
   
38
}
-
 
39
 
-
 
40
# Figure out full path and name of script so we can find 
-
 
41
# config file
16
my $virsh = '/usr/bin/virsh start ';
42
my ( $scriptname, $path ) = fileparse( $0 );
-
 
43
$path = rel2abs($path);
-
 
44
# remove any suffix, if it exists
-
 
45
if ( $scriptname =~ m/^(.*)\.[^.]+$/ ) { 
-
 
46
   $scriptname = $1;
-
 
47
}
-
 
48
 
-
 
49
my $configFile = $path . '/' . $scriptname . $configFileSuffix;
17
 
50
 
-
 
51
&loadConfig( $configFile , \@servers );
-
 
52
die "No configuration file found at $configFile\n" unless @servers;
-
 
53
 
-
 
54
#die "$configFile\n" . join( "\n", @servers ) . "\n";
-
 
55
 
-
 
56
# command used to start a virtual
-
 
57
my $virsh = '/usr/bin/virsh start ';
-
 
58
# find all running virtuals
18
my $output = `virsh list`;
59
my $output = `virsh list`;
19
 
60
 
-
 
61
# go through each virtual in @servers and make sure it is running
20
foreach my $server ( @servers ) {
62
foreach my $server ( @servers ) {
21
   if ( $output =~ m/$server/ ) {
63
   if ( $output =~ m/$server/ ) { # its running, so unlink the flag file if it exists
22
      unlink "/tmp/$server.down" if  -e "/tmp/$server.down";
64
      unlink "/tmp/$server.down" if  -e "/tmp/$server.down";
23
   } else {
65
   } else { # not running
24
      if ( -e "/tmp/$server.down" ) {
66
      if ( -e "/tmp/$server.down" ) { # second iteration it has been down, so start it
25
         print "$server has been down for a while, starting back up\n";
67
         print "$server has been down for a while, starting back up\n";
26
         `$virsh $server`; 
68
         `$virsh $server`; 
27
         unlink "/tmp/$server.down";
69
         unlink "/tmp/$server.down";
28
      } else {
70
      } else { # first time we've seen it, so just flag it
29
         `touch /tmp/$server.down`;
71
         `touch /tmp/$server.down`;
30
      }
72
      }
31
   }
73
   }
32
}
74
}
33
 
75