#!/usr/bin/perl use warnings; use strict; use Data::Dumper; ####################### my ($zoneid, $zonename); my ($clusid, $clusname); my ($host, $hoststatus); my ($cpu, $cpuusage); my ($mem, $memusage); my ($storage, $storage_used); my %ClusInfo; my @zones; my $clushashref = {}; ######################## sub getZoneInfo { my $file = shift; open my $fh, '<', $file or die "Unable to Open the File $file for reading: $!\n"; my $on; while (my $line = <$fh>) { chomp $line; if ($line =~ /^Zone/i) { $on = 1; } elsif ($on) { last if $line =~ /^$/; $line =~ s/-{2,}//g; ($zonename) = (split /\s+/, $line)[1]; push @zones, $zonename if defined ($zonename); } } close($fh); # print Dumper \@zones; return \@zones; } sub get_Kvm_Clusters_of_Zones { my $file = shift; my $ZoneAR = &getZoneInfo($file); open my $fh, '<', $file or die "Unable to Open the File $file for reading: $!\n"; while (my $line = <$fh> ) { chomp $line; $line =~ s/^\s+|\s+$//g; $line =~ s/^\s.*//g; next if $line =~ /^$/; foreach my $zone (@$ZoneAR) { if ($line =~ /(^$zone)/) { $zonename= $1; } elsif ($line =~ /HVM\s+\w+\.\w+/) { ($clusid, $clusname) = (split /\s+/, $line)[0,1]; $ClusInfo{$zonename}{$clusname} = {}; } elsif ($line =~ /No HVM Clusters/) { $ClusInfo{$zonename}{'No HVM'} = 0; } } } close($fh); #print Dumper \%ClusInfo; return \%ClusInfo; } sub get_HostInfo_of_Clusters { my $file = shift; $clushashref = &get_Kvm_Clusters_of_Zones($file); open my $fh, '<', $file or die "Unable to Open the File $file for reading: $!\n"; while (my $line = <$fh>) { chomp $line; $line =~ s/^\s+|\s+$//g; next if $line =~ /^$/; foreach my $zone (keys %$clushashref) { foreach my $cname (keys %{ $clushashref->{$zone} } ) { next if $cname =~ /No HVM/; if ($line =~ /^($cname)/) { $clusname = $1 } elsif ($line =~ /example\.com/) { ($host, $hoststatus) = (split /\s+/, $line)[0, 2]; chomp $host; $clushashref->{$zone}->{$clusname}->{$host} = $hoststatus; chomp $hoststatus; } elsif ($line =~ /^CPU/) { ($cpu, $cpuusage) = (split /\s+/, $line)[0, -1]; $cpuusage =~ s/%//g; chomp $cpuusage; $clushashref->{$zone}->{$clusname}->{$cpu} = $cpuusage if (defined($cpuusage)); } elsif ($line =~ /^MEMORY/) { ($mem, $memusage) = (split /\s+/, $line)[0, -1]; $memusage =~ s/%//g; chomp $memusage; $clushashref->{$zone}->{$clusname}->{$mem} = $memusage if (defined($memusage)); } elsif ($line =~ /^ALLOCATED\s+STORAGE/) { ($storage, $storage_used) = (split /\s+/, $line)[1, -1]; $storage_used =~ s/%//g; chomp $storage_used; $clushashref->{$zone}->{$clusname}->{$storage} = $storage_used if (defined($storage_used)); } } } } close($fh); print Dumper \%$clushashref; # return $clushashref; } #&getZoneInfo('hvm.txt'); #&get_Kvm_Clusters_of_Zones('hvm.txt'); &get_HostInfo_of_Clusters('hvm.txt');