111 |
rodolico |
1 |
#! /usr/bin/perl -w
|
|
|
2 |
|
|
|
3 |
use YAML::XS qw(DumpFile LoadFile); # libyaml-libyaml-perl
|
|
|
4 |
use Getopt::Std;
|
|
|
5 |
|
|
|
6 |
$main::VERSION = '0.1';
|
|
|
7 |
|
|
|
8 |
my $dbFile = 'dom0_db.yaml';
|
|
|
9 |
my $lockFile = 'dom0_db.lock';
|
|
|
10 |
my $command = 'virsh'; $command = 'xl' unless `which $command`;
|
|
|
11 |
|
|
|
12 |
#
|
|
|
13 |
# find where the script is actually located as cfg should be there
|
|
|
14 |
#
|
|
|
15 |
sub getScriptLocation {
|
|
|
16 |
use strict;
|
|
|
17 |
use File::Spec::Functions qw(rel2abs);
|
|
|
18 |
use File::Basename;
|
|
|
19 |
return dirname(rel2abs($0));
|
|
|
20 |
}
|
|
|
21 |
|
|
|
22 |
sub main::HELP_MESSAGE {
|
|
|
23 |
print "\nScript to read output of xl list, parse it and store results in database\n\n";
|
|
|
24 |
print "-s Show database\n";
|
|
|
25 |
print "-c filename Use alternative database\n";
|
|
|
26 |
print "-n Don't update the database\n";
|
|
|
27 |
print "-v Be Verbose (show some stats)\n";
|
|
|
28 |
print "-r Report - only display if something changed\n";
|
|
|
29 |
}
|
|
|
30 |
|
|
|
31 |
sub main::VERSION {
|
|
|
32 |
&HELP_MESSAGE();
|
|
|
33 |
}
|
|
|
34 |
|
|
|
35 |
sub readDB {
|
|
|
36 |
my $lock = shift;
|
|
|
37 |
my $db;
|
|
|
38 |
sleep(5) if -e $lockFile;
|
|
|
39 |
die "Could not get lock on $dbFile: Aborting\n" if -e $lockFile;
|
|
|
40 |
`touch $lockFile` unless $lock;
|
|
|
41 |
$db = LoadFile( $dbFile ) if ( -e $dbFile );
|
|
|
42 |
return $db;
|
|
|
43 |
}
|
|
|
44 |
|
|
|
45 |
sub saveDB {
|
|
|
46 |
my ($db,$dirtyCount, $verbose) = @_;
|
|
|
47 |
if ( $dirtyCount ) {
|
|
|
48 |
print "Updating database since $dirtyCount changes found\n" if $verbose;
|
|
|
49 |
DumpFile( $dbFile, $db );
|
|
|
50 |
}
|
|
|
51 |
`rm $lockFile` if -e $lockFile;
|
|
|
52 |
}
|
|
|
53 |
|
|
|
54 |
sub showDOMUs {
|
|
|
55 |
my $db = shift;
|
|
|
56 |
foreach my $domu ( sort keys %$db ) {
|
|
|
57 |
print "$domu\t$$db{$domu}->{'server'}\n";
|
|
|
58 |
}
|
|
|
59 |
}
|
|
|
60 |
|
|
|
61 |
sub trim {
|
|
|
62 |
my $string = shift;
|
|
|
63 |
$string =~ s/^\s+|\s+$//;
|
|
|
64 |
return $string;
|
|
|
65 |
}
|
|
|
66 |
|
|
|
67 |
sub getHeader {
|
|
|
68 |
my ($line) = @_;
|
|
|
69 |
my @return = split( '\s+', trim( $line ) );
|
|
|
70 |
return @return;
|
|
|
71 |
}
|
|
|
72 |
|
|
|
73 |
sub parseALine {
|
|
|
74 |
my ($line, @headers ) = @_;
|
|
|
75 |
my %return;
|
|
|
76 |
# next line see http://www.perlmonks.org/?node_id=4402
|
|
|
77 |
@return{@headers} = split( '\s+', trim( $line ) );
|
|
|
78 |
return %return;
|
|
|
79 |
}
|
|
|
80 |
|
|
|
81 |
sub updateDB {
|
|
|
82 |
my ( $db, $self, $verbose ) = @_;
|
|
|
83 |
my $new = 0;
|
|
|
84 |
my $moved = 0;
|
|
|
85 |
my $removed = 0;
|
|
|
86 |
use Data::Dumper;
|
|
|
87 |
|
|
|
88 |
my @domains = `$command list`;
|
|
|
89 |
chomp @domains;
|
|
|
90 |
my @fieldnames = &getHeader( $domains[0] );
|
|
|
91 |
|
|
|
92 |
my %found;
|
|
|
93 |
|
|
|
94 |
for ($i = 1; $i < @domains; $i++ ) {
|
|
|
95 |
next if $domains[$i] =~ m/^[=-]*$/;
|
|
|
96 |
my %thisLine = &parseALine( $domains[$i], @fieldnames );
|
|
|
97 |
next if $thisLine{'Name'} eq 'Domain-0';
|
|
|
98 |
$found{ $thisLine{'Name'} } = 1;
|
|
|
99 |
unless ( exists $$db{$thisLine{'Name'}} ) {
|
|
|
100 |
$$db{$thisLine{'Name'}} = \%thisLine;
|
|
|
101 |
print "adding $thisLine{'Name'}\n" if $verbose;
|
|
|
102 |
$new++;
|
|
|
103 |
}
|
|
|
104 |
unless ( exists $$db{$thisLine{'Name'}}->{'server'} && $$db{$thisLine{'Name'}}->{'server'} eq $self ) {
|
|
|
105 |
$$db{$thisLine{'Name'}}->{'server'} = $self;
|
|
|
106 |
print "$thisLine{'Name'} has moved to me ($self)\n" if $verbose;
|
|
|
107 |
$moved++;
|
|
|
108 |
}
|
|
|
109 |
}
|
|
|
110 |
# clean up any entries which were here, but are not any longer
|
|
|
111 |
foreach $entry ( keys %$db ) {
|
|
|
112 |
if ( $db->{$entry}->{'server'} eq $self && ! defined ( $found{$entry} ) ) {
|
|
|
113 |
# it used to be here, but now it is gone
|
|
|
114 |
$db->{$entry}->{'server'} = '';
|
|
|
115 |
$removed++;
|
|
|
116 |
} # if
|
|
|
117 |
} # foreach
|
|
|
118 |
print "$new new virtuals found, $removed removed and $moved virtuals moved\n" if ( $verbose );
|
|
|
119 |
return ($new+$moved+$removed,$db);
|
|
|
120 |
}
|
|
|
121 |
|
|
|
122 |
my $path = &getScriptLocation() . '/';
|
|
|
123 |
$dbFile = $path . $dbFile;
|
|
|
124 |
$lockFile = $path . $lockFile;
|
|
|
125 |
|
|
|
126 |
|
|
|
127 |
$Getopt::Std::STANDARD_HELP_VERSION = 1;
|
|
|
128 |
my %options = ();
|
|
|
129 |
getopts( 'vosrc:n', \%options );
|
|
|
130 |
|
|
|
131 |
if ( $options{'o'} ) {
|
|
|
132 |
print "Dumping cli parameters\n";
|
|
|
133 |
foreach my $option ( keys %options ) {
|
|
|
134 |
print "$option\t[$options{$option}]\n";
|
|
|
135 |
}
|
|
|
136 |
}
|
|
|
137 |
|
|
|
138 |
|
|
|
139 |
my $db = &readDB( $options{'n'} );
|
|
|
140 |
|
|
|
141 |
unless ( $options{ 'n' } ) {
|
|
|
142 |
my $dirtyCount;
|
|
|
143 |
print STDERR "Updating the database\n" if $options{'v'};
|
|
|
144 |
my $self = `hostname`;
|
|
|
145 |
chomp $self;
|
|
|
146 |
($dirtyCount,$db) = &updateDB( $db,$self, $options{'v'} );
|
|
|
147 |
print "Virtuals changed on $self\n" if ($options{'r'} && $dirtyCount);
|
|
|
148 |
&saveDB( $db, $dirtyCount, $options{'v'} );
|
|
|
149 |
}
|
|
|
150 |
|
|
|
151 |
&showDOMUs( $db ) if $options{'s'};
|
|
|
152 |
|
|
|
153 |
1;
|
|
|
154 |
|