Subversion Repositories sysadmin_scripts

Rev

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

Rev Author Line No. Line
111 rodolico 1
#! /usr/bin/perl -w
2
 
3
my @directoriesToSearch = ( '/etc/xen', '/etc/xen/iscsi_configs' );
4
my %output;
5
 
6
 
7
sub getConfigurationFiles {
8
   my $directory = shift;
9
   my @files;
10
   if ( opendir(my $dh, $directory) ) {
11
      @files = map{ "$directory/$_" } grep { /((\.cfg)|(\.hvm))$/ } readdir($dh);
12
      closedir $dh;
13
   } else {
14
      warn "can't opendir $directory: $!";
15
   }
16
   return @files;
17
}
18
 
19
 
20
sub parseConfigFile {
21
   my ($content,$hostname) = @_;
22
   if ( $content =~ m/vif\s*=\s*\[([^\]]+)/gi ) {
23
      my $vifs = $1;
24
      while ( $vifs =~ m/mac\s*=\s*([a-z0-9:-]+)/gi ) {
25
         my $mac = $1;
26
         $mac .= '-Dup' if  $output{'mac'}{$mac};
27
         $output{'mac'}{$mac} = $hostname;
28
      }
29
   }
30
}
31
 
32
sub processConfigFile {
33
   my $filename = shift;
34
   open CONF, "<$filename" or die "Could not read $filename: $!\n";
35
   my $contents = join( '', <CONF>); # slurp the file contents in
36
   close CONF;
37
   &parseConfigFile( $contents, $filename );
38
}
39
 
40
my @files;
41
foreach my $dir ( @directoriesToSearch ) {
42
   push @files, getConfigurationFiles( $dir );
43
}
44
 
45
foreach my $file ( @files ) {
46
   &processConfigFile( $file );
47
}
48
 
49
foreach my $key ( keys %output ) {
50
   print "$key\n";
51
   foreach my $item ( keys %{$output{$key}} ) {
52
      print "$item\t$output{$key}{$item}\n";
53
   }
54
}
55
 
56
1;