Subversion Repositories havirt

Rev

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

Rev 9 Rev 10
Line 111... Line 111...
111
   print "\t--target|-t NODE # the action use NODE for the target of actions\n";
111
   print "\t--target|-t NODE # the action use NODE for the target of actions\n";
112
   print "\t--dryrun|-n # does not perform the actions, simply shows what commands would be executed\n";
112
   print "\t--dryrun|-n # does not perform the actions, simply shows what commands would be executed\n";
113
   print "\t--debug|d # increases verbosity, with -ddd, totally outragious\n";
113
   print "\t--debug|d # increases verbosity, with -ddd, totally outragious\n";
114
}
114
}
115
 
115
 
116
sub loadVirtDB {
-
 
117
   return if $virtDB;
-
 
118
   $virtDB = &readDB( $domainDBName );
-
 
119
}
-
 
120
 
-
 
121
sub domain {
-
 
122
   my $action = lc shift;
-
 
123
   my $return = '';
-
 
124
   &loadVirtDB();
-
 
125
   &loadNodePopulations();
-
 
126
   @_ = keys( %$virtDB ) if ( $_[0] && $_[0] eq 'ALL' );
-
 
127
   if ( $_[0] && $_[0] eq 'RUNNING' ) {
-
 
128
      my @running;
-
 
129
      foreach my $node ( keys %$nodePopulations ) {
-
 
130
         push @running, keys %{ $nodePopulations->{$node}->{'running'} };
-
 
131
      }
-
 
132
      @_ = @running;
-
 
133
   }
-
 
134
   if ( $action eq 'update' ) { # download xml to var and update database
-
 
135
      while ( my $virt = shift ) {
-
 
136
         &parseDomain( $virt );
-
 
137
      } # while
-
 
138
      &writeDB( $domainDBName, $virtDB );
-
 
139
   } elsif ( $action eq 'list' ) { # dump domain as a tab separated data file
-
 
140
      my @return;
-
 
141
      foreach my $node ( keys %$nodePopulations ) {
-
 
142
         foreach my $virt (keys %{$nodePopulations->{$node}->{'running'}} ) {
-
 
143
            push @return, &listDomain( $virt, $node );
-
 
144
         }
-
 
145
      }
-
 
146
      $return = join( "\n", sort @return ) . "\n";;
-
 
147
   }
-
 
148
   return $return;;
-
 
149
} # sub domain
-
 
150
 
-
 
151
sub listDomain {
-
 
152
   my ($virt,$node) = @_;
-
 
153
   my @return;
-
 
154
   push @return, $virt;
-
 
155
   push @return, $node;
-
 
156
   foreach my $column ( sort keys %{ $virtDB->{$virt} } ) {
-
 
157
      push @return, $virtDB->{$virt}->{$column};
-
 
158
   }
-
 
159
   return join( "\t", @return);
-
 
160
}
-
 
161
   
-
 
162
 
-
 
163
 
-
 
164
# get the XML definition file of a running domain off of whatever
-
 
165
# node it is running on, and save it to disk
-
 
166
sub getVirtConfig {
-
 
167
   my ($virt,$filename) = @_;
-
 
168
   my $return;
-
 
169
   print "In getVirtConfig looking for $virt with file $filename\n" if $DEBUG;
-
 
170
   if ( -f $filename ) {
-
 
171
      open XML, "<$filename" or die "Could not read from $filename: $!\n";
-
 
172
      $return = join( '', <XML> );
-
 
173
      close XML;
-
 
174
   } else {
-
 
175
      &loadNodePopulations();
-
 
176
      #die Dumper( $nodePopulations );
-
 
177
      foreach my $node ( keys %$nodePopulations ) {
-
 
178
         print "getVirtConfig Looking on $node for $virt\n";
-
 
179
         if ( exists( $nodePopulations->{$node}->{'running'}->{$virt} ) ) { # we found it
-
 
180
            print "Found $virt on node $node\n";
-
 
181
            $return = `ssh $node 'virsh dumpxml $virt'`;
-
 
182
            open XML,">$filename" or die "Could not write to $filename: $!\n";
-
 
183
            print XML $return;
-
 
184
            close XML;
-
 
185
         } # if
-
 
186
      } # foreach
-
 
187
   } # if..else
-
 
188
   return $return;
-
 
189
} # sub getVirtConfig
-
 
190
 
-
 
191
sub getXMLValue {
-
 
192
   my ( $key, $string ) = @_;
-
 
193
   my $start = "<$key";
-
 
194
   my $end = "</$key>";
-
 
195
   $string =~ m/$start([^>]*)>([^<]+)$end/;
-
 
196
   return ($1,$2);
-
 
197
}
-
 
198
 
-
 
199
sub parseDomain {
-
 
200
   my ($virt, $nodePopulations ) = @_;
-
 
201
   
-
 
202
   my @keysToSave = ( 'uuid', 'memory', 'vcpu' );
-
 
203
   my $filename = "$confDir/$virt.xml";
-
 
204
   my $xml = &getVirtConfig( $virt, $filename );
-
 
205
   my ($param,$value) = &getXMLValue( 'uuid', $xml );
-
 
206
   $virtDB->{$virt}->{'uuid'} = $value;
-
 
207
   ($param,$value) = &getXMLValue( 'memory', $xml );
-
 
208
   $virtDB->{$virt}->{'memory'} = $value;
-
 
209
   ($param,$value) = &getXMLValue( 'vcpu', $xml );
-
 
210
   $virtDB->{$virt}->{'vcpu'} = $value;
-
 
211
 
-
 
212
   $xml =~ m/type='vnc' port='(\d+)'/;
-
 
213
   $virtDB->{$virt}->{'vnc'} = $1;
-
 
214
}
-
 
215
 
-
 
216
sub cluster {
-
 
217
   my $action = lc shift;
-
 
218
   my $return = '';
-
 
219
   if ( $action eq 'status' ) {
-
 
220
      &loadVirtDB();
-
 
221
      &loadNodePopulations();
-
 
222
      &loadNodeDB();
-
 
223
      print "Node\tThreads\tMemory\tDomains\tvcpu\tmem_used\n";
-
 
224
      my $usedmem = 0;
-
 
225
      my $usedcpu = 0;
-
 
226
      my $availmem = 0;
-
 
227
      my $availcpu = 0;
-
 
228
      my $totalDomains = 0;
-
 
229
      foreach my $node (sort keys %$nodeDB ) {
-
 
230
         my $memory = 0;
-
 
231
         my $vcpus = 0;
-
 
232
         my $count = 0;
-
 
233
         foreach my $domain ( keys %{ $nodePopulations->{$node}->{'running'} } ) {
-
 
234
            $memory += $virtDB->{$domain}->{'memory'};
-
 
235
            $vcpus += $virtDB->{$domain}->{'vcpu'};
-
 
236
            $count++;
-
 
237
         }
-
 
238
         $return .= "$node\t$nodeDB->{$node}->{cpu_count}\t$nodeDB->{$node}->{memory}\t$count\t$vcpus\t$memory\n";
-
 
239
         $usedmem += $memory;
-
 
240
         $usedcpu += $vcpus;
-
 
241
         $totalDomains += $count;
-
 
242
         $availmem += $nodeDB->{$node}->{memory};
-
 
243
         $availcpu += $nodeDB->{$node}->{cpu_count};
-
 
244
      } # outer for
-
 
245
      $return .= "Total\t$availcpu\t$availmem\t$totalDomains\t$usedcpu\t$usedmem\n";
-
 
246
   }
-
 
247
   return $return;
-
 
248
}
-
 
249
 
116
 
250
 
117
 
251
# handle any command line parameters that may have been passed in
118
# handle any command line parameters that may have been passed in
252
 
119
 
253
GetOptions (
120
GetOptions (