#!/usr/bin/perl -w # # Michal Ingeli , 2009-04-08 # # CHANGELOG: # # 0.3 2009-04-16 # + Human readable tables # - Added changelog :) # # 0.2 2009-04-08 # - System virtual memory and resource informations # # 0.1 2009-04-07 # - Initial release use strict; use File::Basename; my $help = <<"HELP"; -a All defined domains -d Domain name (with -a for not running domains) -l List view -H Human readable -m Reserved memory -c Current memory -v Virtual memory -r Resource size -h help HELP my %cfg = (); #CHROCHT @ARGV = grep {not ( $_ eq "-d" ... (($cfg{domain} = $_), 1) )} @ARGV; grep { ($_ eq "-a" and (($cfg{all} = "--all", 1))) or ($_ eq "-c" and (($cfg{mem} = "memory", 1) and not defined $cfg{mem})) or ($_ eq "-m" and (($cfg{mem} = "currentMemory", 1) and not defined $cfg{mem})) or ($_ eq "-v" and (($cfg{mem} = "vsz", 1) and not defined $cfg{mem})) or ($_ eq "-r" and (($cfg{mem} = "rss", 1) and not defined $cfg{mem})) or ($_ eq "-h" and (($cfg{help} = 1, 1))) or ($_ eq "-l" and (($cfg{list} = 1, 1))) or ($_ eq "-H" and (($cfg{human} = 1, 1))) } @ARGV; if ($cfg{help}) { print $help; exit 1; } $cfg{all} = "" if (not defined $cfg{all}); $cfg{mem} = "memory" if (not defined $cfg{mem}); $cfg{list} = 0 if (not defined $cfg{list} or $cfg{human}); $cfg{human} = 0 if (not defined $cfg{human} or $cfg{list}); $cfg{domain} = undef if (not defined $cfg{domain}); my $mem = 0; my $inList = 0; my $_domain = undef; my $_memory = undef; my @stats = (); if ($cfg{human}) { format STDOUT_TOP = +-----------------------+---------------------+ |@||||||||||||||||||||| | @|||||||||||||||||| | "Domain", "Memory (kB)" +-----------------------+---------------------+ . format STDOUT = | @<<<<<<<<<<<<<<<<<... | @>>>>>>>>>>>>>>>>>> | $_domain, $_memory . } open(my $list, "virsh list $cfg{all} 2>/dev/null |"); while (<$list>) { if (/^-+/) { $inList = 1; next; } chomp; if (/^\s+\S*\s*(\S*)/ and $inList) { my $virt = $1; next if (defined $cfg{domain} && $virt ne $cfg{domain}); open(my $virtxml, "virsh dumpxml $virt 2>/dev/null |"); while (<$virtxml>) { if ($cfg{mem} eq "vsz" or $cfg{mem} eq "rss") { if (/(\S*)<\/emulator>/) { my $emulator = basename($1); my $emulator_bin = $1; open(my $ps, "ps -C $emulator -o $cfg{mem}=,args= 2>/dev/null |"); while (<$ps>) { if (/^(\d+)\s+($emulator_bin.*?$virt.*)$/) { print "$virt:$1\n" if ($cfg{list}); $_domain = $virt; $_memory = $1; write if ($cfg{human}); $mem += $1; } } } } elsif (/<$cfg{mem}>(\d*)<\/$cfg{mem}>/) { print "$virt:$1\n" if ($cfg{list}); $_domain = $virt; $_memory = $1; write if ($cfg{human}); $mem += $1; } } } } $_domain = "(SUM)"; $_memory = $mem; write if ($cfg{human}); if ($cfg{list}) { print "-----\nSum: " if ($cfg{list}); print "$mem\n"; }