--- trunk/lib/VZ.pm 2008/02/08 23:16:29 41 +++ trunk/lib/VZ.pm 2008/02/08 23:53:40 42 @@ -2,19 +2,26 @@ use Exporter 'import'; our @EXPORT = qw( check_root + ip2hostname hostname2ip -vzlist_configs + +conf_veids + vzctl +vzlist $vz_root $vz_conf + +dump ); use warnings; use strict; use Socket; +use Data::Dump qw/dump/; our $vz_root = '/vz'; our $vz_conf = '/etc/vz/conf'; @@ -64,17 +71,18 @@ sub ip2hostname { my $ip = shift || return; - return gethostbyaddr(inet_aton($ip), AF_INET) + my $hostname = gethostbyaddr(inet_aton($ip), AF_INET) or die "Can't look up $ip: $!\n"; + return $hostname; } -=head2 vzlist_configs +=head2 conf_veids - my @VEIDs = vzlist_configs; + my @VEIDs = conf_veids; =cut -sub vzlist_configs { +sub conf_veids { my @c; open( my $cfgs, 'find /etc/vz/conf -maxdepth 1 -name "*.conf" |' ) || die "can't run find: $!"; while(<$cfgs>) { @@ -104,4 +112,44 @@ system('vzctl',@args) == 0 or die "vzctl @args failed: $?" } +=head2 vzlist + + my $output = vzlist(); + my $hash = vzlist( hash => 1 ); + +=cut + +my @vzlist_fields; + +sub vzlist { + die "need hash as argument" unless $#_ % 2 == 1; + my $args = {@_}; + + + my $output; + + if ( ! @vzlist_fields ) { + open(my $vzlist, 'vzlist -L |') || die "can't start vzlist -L: $!"; + while(<$vzlist>) { + push @vzlist_fields, (split(/\s+/, $_, 2 ))[0]; + } + } + + open(my $vzlist, 'vzlist -a -H -o ' . join(',', @vzlist_fields) . ' |') || die "can't start vzlist: $!"; + while(<$vzlist>) { + s/^\s+//; + my @data = split(/\s+/, $_); + + if ( defined $args->{hash} ) { + my $hash; + $hash->{ $vzlist_fields[$_] } = $data[$_] foreach 1 .. $#data; + $output->{ $data[0] } = $hash; + } else { + $output .= $_; + } + } + + return $output; +} + 1;