Subversion Repositories sysadmin_scripts

Rev

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

Rev 143 Rev 144
Line 25... Line 25...
25
my $scriptDir = $FindBin::RealBin;
25
my $scriptDir = $FindBin::RealBin;
26
my $scriptName = $FindBin::Script;
26
my $scriptName = $FindBin::Script;
27
my $confDir = "$scriptDir/conf";
27
my $confDir = "$scriptDir/conf";
28
my $dbDir = "$scriptDir/var";
28
my $dbDir = "$scriptDir/var";
29
my $nodeDBName = "$dbDir/node.yaml";
29
my $nodeDBName = "$dbDir/node.yaml";
30
my $virtualDBName = "$dbDir/virtuals.yaml";
30
my $domainDBName = "$dbDir/domains.yaml";
31
my $nodePopulationDBName = "$dbDir/node_population.yaml";
31
my $nodePopulationDBName = "$dbDir/node_population.yaml";
32
 
32
 
33
# these contain the values from the databases
33
# these contain the values from the databases
34
# loaded on demand
34
# loaded on demand
35
my $nodeDB;
35
my $nodeDB;
Line 43... Line 43...
43
sub help {
43
sub help {
44
   print "$0 command [argument]\n";
44
   print "$0 command [argument]\n";
45
   print "where command is one of\n";
45
   print "where command is one of\n";
46
   print "\tnode update [node] [node]... # update a given node (or ALL)\n";
46
   print "\tnode update [node] [node]... # update a given node (or ALL)\n";
47
   print "\tnode list # display tab delimited list of node specs\n";
47
   print "\tnode list # display tab delimited list of node specs\n";
48
   print "\tnode scan # find virtuals on all nodes\n ";
48
   print "\tnode scan # find domains on all nodes\n ";
49
   print "\tvirtual update ALL|RUNNING|[virtual] [virtual]... # update virtuals\n";
49
   print "\tdomain update ALL|RUNNING|[domain] [domain]... # update domains\n";
-
 
50
   print "\tdomain list ALL|RUNNING|[domain] [domain]... # display tab delimited list of domain specs\n";
50
}
51
}
51
 
52
 
52
sub readDB {
53
sub readDB {
53
   my ($filename) = @_;
54
   my ($filename) = @_;
54
   my $yaml = YAML::Tiny->new( {} );
55
   my $yaml = YAML::Tiny->new( {} );
Line 64... Line 65...
64
   $yaml->write( $filename );
65
   $yaml->write( $filename );
65
}
66
}
66
 
67
 
67
sub loadVirtDB {
68
sub loadVirtDB {
68
   return if $virtDB;
69
   return if $virtDB;
69
   $virtDB = &readDB( $virtualDBName );
70
   $virtDB = &readDB( $domainDBName );
70
}
71
}
71
 
72
 
72
sub loadNodePopulations {
73
sub loadNodePopulations {
73
   return if $nodePopulations;
74
   return if $nodePopulations;
74
   $nodePopulations = &readDB( $nodePopulationDBName );
75
   $nodePopulations = &readDB( $nodePopulationDBName );
Line 77... Line 78...
77
sub loadNodeDB {
78
sub loadNodeDB {
78
   return if $nodeDB;
79
   return if $nodeDB;
79
   $nodeDB = &readDB( $nodeDBName );
80
   $nodeDB = &readDB( $nodeDBName );
80
}
81
}
81
 
82
 
82
sub virtual {
83
sub domain {
83
   my $action = lc shift;
84
   my $action = lc shift;
84
   my $return = '';
85
   my $return = '';
85
   &loadVirtDB();
86
   &loadVirtDB();
86
   &loadNodePopulations();
87
   &loadNodePopulations();
87
   @_ = keys( %$virtDB ) if ( $_[0] && $_[0] eq 'ALL' );
88
   @_ = keys( %$virtDB ) if ( $_[0] && $_[0] eq 'ALL' );
Line 92... Line 93...
92
      }
93
      }
93
      @_ = @running;
94
      @_ = @running;
94
   }
95
   }
95
   if ( $action eq 'update' ) { # download xml to var and update database
96
   if ( $action eq 'update' ) { # download xml to var and update database
96
      while ( my $virt = shift ) {
97
      while ( my $virt = shift ) {
97
         &parseVirtual( $virt );
98
         &parseDomain( $virt );
98
      } # while
99
      } # while
99
      &writeDB( $virtualDBName, $virtDB );
100
      &writeDB( $domainDBName, $virtDB );
100
   } elsif ( $action eq 'list' ) { # dump virtual as a tab separated data file
101
   } elsif ( $action eq 'list' ) { # dump domain as a tab separated data file
101
      my @return;
102
      my @return;
102
      foreach my $node ( keys %$nodePopulations ) {
103
      foreach my $node ( keys %$nodePopulations ) {
103
         foreach my $virt (keys %{$nodePopulations->{$node}->{'running'}} ) {
104
         foreach my $virt (keys %{$nodePopulations->{$node}->{'running'}} ) {
104
            push @return, &listVirtual( $virt, $node );
105
            push @return, &listDomain( $virt, $node );
105
         }
106
         }
106
      }
107
      }
107
      $return = join( "\n", sort @return ) . "\n";;
108
      $return = join( "\n", sort @return ) . "\n";;
108
   }
109
   }
109
   return $return;;
110
   return $return;;
110
} # sub virtual
111
} # sub domain
111
 
112
 
112
sub listVirtual {
113
sub listDomain {
113
   my ($virt,$node) = @_;
114
   my ($virt,$node) = @_;
114
   my @return;
115
   my @return;
115
   push @return, $virt;
116
   push @return, $virt;
116
   push @return, $node;
117
   push @return, $node;
117
   foreach my $column ( sort keys %{ $virtDB->{$virt} } ) {
118
   foreach my $column ( sort keys %{ $virtDB->{$virt} } ) {
Line 120... Line 121...
120
   return join( "\t", @return);
121
   return join( "\t", @return);
121
}
122
}
122
   
123
   
123
 
124
 
124
 
125
 
125
# get the XML definition file of a running virtual off of whatever
126
# get the XML definition file of a running domain off of whatever
126
# node it is running on, and save it to disk
127
# node it is running on, and save it to disk
127
sub getVirtConfig {
128
sub getVirtConfig {
128
   my ($virt,$filename) = @_;
129
   my ($virt,$filename) = @_;
129
   my $return;
130
   my $return;
130
   print "In getVirtConfig looking for $virt with file $filename\n" if $DEBUG;
131
   print "In getVirtConfig looking for $virt with file $filename\n" if $DEBUG;
Line 155... Line 156...
155
   my $end = "</$key>";
156
   my $end = "</$key>";
156
   $string =~ m/$start([^>]*)>([^<]+)$end/;
157
   $string =~ m/$start([^>]*)>([^<]+)$end/;
157
   return ($1,$2);
158
   return ($1,$2);
158
}
159
}
159
 
160
 
160
sub parseVirtual {
161
sub parseDomain {
161
   my ($virt, $nodePopulations ) = @_;
162
   my ($virt, $nodePopulations ) = @_;
162
   
163
   
163
   my @keysToSave = ( 'uuid', 'memory', 'vcpu' );
164
   my @keysToSave = ( 'uuid', 'memory', 'vcpu' );
164
   my $filename = "$confDir/$virt.xml";
165
   my $filename = "$confDir/$virt.xml";
165
   my $xml = &getVirtConfig( $virt, $filename );
166
   my $xml = &getVirtConfig( $virt, $filename );
Line 172... Line 173...
172
 
173
 
173
   $xml =~ m/type='vnc' port='(\d+)'/;
174
   $xml =~ m/type='vnc' port='(\d+)'/;
174
   $virtDB->{$virt}->{'vnc'} = $1;
175
   $virtDB->{$virt}->{'vnc'} = $1;
175
}
176
}
176
 
177
 
177
sub getVirtualsOnNode {
178
sub getDomainsOnNode {
178
   my $node = shift;
179
   my $node = shift;
179
   my @nodeList = grep { /^\s*\d/ } `ssh $node 'virsh list'`;
180
   my @nodeList = grep { /^\s*\d/ } `ssh $node 'virsh list'`;
180
   for ( my $i = 0; $i < @nodeList; $i++ ) {
181
   for ( my $i = 0; $i < @nodeList; $i++ ) {
181
      if ( $nodeList[$i] =~ m/\s*\d+\s*([^ ]+)/ ) {
182
      if ( $nodeList[$i] =~ m/\s*\d+\s*([^ ]+)/ ) {
182
         $nodeList[$i] = $1;
183
         $nodeList[$i] = $1;
Line 234... Line 235...
234
         push @return, join( "\t", @line );
235
         push @return, join( "\t", @line );
235
      }
236
      }
236
      $return = join( "\n", @return ) . "\n";
237
      $return = join( "\n", @return ) . "\n";
237
   } elsif ( $action eq 'scan' ) {
238
   } elsif ( $action eq 'scan' ) {
238
      foreach my $node ( keys %$nodeDB ) {
239
      foreach my $node ( keys %$nodeDB ) {
239
         $nodePopulations->{$node}->{'running'} = &getVirtualsOnNode( $node );
240
         $nodePopulations->{$node}->{'running'} = &getDomainsOnNode( $node );
240
         $nodePopulations->{$node}->{'lastchecked'} = time;
241
         $nodePopulations->{$node}->{'lastchecked'} = time;
241
      }
242
      }
242
      &writeDB( $nodePopulationDBName,$nodePopulations );
243
      &writeDB( $nodePopulationDBName,$nodePopulations );
243
   } # if..elsif
244
   } # if..elsif
244
   return $return;
245
   return $return;
Line 249... Line 250...
249
my $command = shift; # the first one is the actual subsection
250
my $command = shift; # the first one is the actual subsection
250
my $action = shift; # second is action to run
251
my $action = shift; # second is action to run
251
 
252
 
252
if ( $command eq 'node' ) {
253
if ( $command eq 'node' ) {
253
   print &node( $action, @ARGV );
254
   print &node( $action, @ARGV );
254
} elsif ( $command eq 'virtual' ) {
255
} elsif ( $command eq 'domain' ) {
255
   print &virtual( $action, @ARGV );
256
   print &domain( $action, @ARGV );
256
} else {
257
} else {
257
   &help();
258
   &help();
258
}
259
}
259
 
260
 
260
 
261