Subversion Repositories sysadmin_scripts

Rev

Rev 141 | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
141 rodolico 1
#! /usr/bin/env perl
2
 
142 rodolico 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
 
141 rodolico 20
use strict;
21
use warnings;
22
 
142 rodolico 23
my $configFileSuffix = '.cfg';
24
my @servers;
25
 
26
use File::Spec::Functions qw(rel2abs);
27
use File::Basename;
28
 
29
# loads the config file into $servers, which is a reference to an array
30
sub loadConfig {
31
   my ($confName, $servers ) = @_;
32
   if ( -f $confName ) {
33
      open CONF, "<$confName" or die "Could not read $confName: $!\n";
34
      @$servers = grep { ! m/^#/ } <CONF>;
35
      close CONF;
36
      chomp @$servers;
37
   }
38
}
39
 
40
# Figure out full path and name of script so we can find 
41
# config file
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;
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
141 rodolico 57
my $virsh = '/usr/bin/virsh start ';
142 rodolico 58
# find all running virtuals
141 rodolico 59
my $output = `virsh list`;
60
 
142 rodolico 61
# go through each virtual in @servers and make sure it is running
141 rodolico 62
foreach my $server ( @servers ) {
142 rodolico 63
   if ( $output =~ m/$server/ ) { # its running, so unlink the flag file if it exists
141 rodolico 64
      unlink "/tmp/$server.down" if  -e "/tmp/$server.down";
142 rodolico 65
   } else { # not running
66
      if ( -e "/tmp/$server.down" ) { # second iteration it has been down, so start it
141 rodolico 67
         print "$server has been down for a while, starting back up\n";
68
         `$virsh $server`; 
69
         unlink "/tmp/$server.down";
142 rodolico 70
      } else { # first time we've seen it, so just flag it
141 rodolico 71
         `touch /tmp/$server.down`;
72
      }
73
   }
74
}
75
 
76
1;