/[vz-tools]/trunk/lib/VZ.pm
This is repository of my old source code which isn't updated any more. Go to git.rot13.org for current projects!
ViewVC logotype

Diff of /trunk/lib/VZ.pm

Parent Directory Parent Directory | Revision Log Revision Log | View Patch Patch

revision 40 by dpavlin, Fri Feb 8 23:02:25 2008 UTC revision 42 by dpavlin, Fri Feb 8 23:53:40 2008 UTC
# Line 2  package VZ; Line 2  package VZ;
2  use Exporter 'import';  use Exporter 'import';
3  our @EXPORT = qw(  our @EXPORT = qw(
4  check_root  check_root
5    
6  ip2hostname  ip2hostname
7  hostname2ip  hostname2ip
8  vzlist_configs  
9    conf_veids
10    
11  vzctl  vzctl
12    vzlist
13    
14  $vz_root  $vz_root
15  $vz_conf  $vz_conf
16    
17    dump
18  );  );
19    
20  use warnings;  use warnings;
21  use strict;  use strict;
22    
23  use Socket;  use Socket;
24    use Data::Dump qw/dump/;
25    
26  our $vz_root = '/vz';  our $vz_root = '/vz';
27  our $vz_conf = '/etc/vz/conf';  our $vz_conf = '/etc/vz/conf';
# Line 64  sub hostname2ip { Line 71  sub hostname2ip {
71  sub ip2hostname {  sub ip2hostname {
72          my $ip = shift || return;          my $ip = shift || return;
73    
74          return gethostbyaddr(inet_aton($ip), AF_INET)          my $hostname = gethostbyaddr(inet_aton($ip), AF_INET)
75                  or die "Can't look up $ip: $!\n";                  or die "Can't look up $ip: $!\n";
76            return $hostname;
77  }  }
78    
79  =head2 vzlist_configs  =head2 conf_veids
80    
81    my @VEIDs = vzlist_configs;    my @VEIDs = conf_veids;
82    
83  =cut  =cut
84    
85  sub vzlist_configs {  sub conf_veids {
86          my @c;          my @c;
87          open( my $cfgs, 'find /etc/vz/conf -maxdepth 1 -name "*.conf" |' ) || die "can't run find: $!";          open( my $cfgs, 'find /etc/vz/conf -maxdepth 1 -name "*.conf" |' ) || die "can't run find: $!";
88          while(<$cfgs>) {          while(<$cfgs>) {
# Line 104  sub vzctl { Line 112  sub vzctl {
112          system('vzctl',@args) == 0 or die "vzctl @args failed: $?"          system('vzctl',@args) == 0 or die "vzctl @args failed: $?"
113  }  }
114    
115    =head2 vzlist
116    
117      my $output = vzlist();
118      my $hash = vzlist( hash => 1 );
119    
120    =cut
121    
122    my @vzlist_fields;
123    
124    sub vzlist {
125            die "need hash as argument" unless $#_ % 2 == 1;
126            my $args = {@_};
127    
128    
129            my $output;
130    
131            if ( ! @vzlist_fields ) {
132                    open(my $vzlist, 'vzlist -L |') || die "can't start vzlist -L: $!";
133                    while(<$vzlist>) {
134                            push @vzlist_fields, (split(/\s+/, $_, 2 ))[0];
135                    }
136            }
137    
138            open(my $vzlist, 'vzlist -a -H -o ' . join(',', @vzlist_fields) . ' |') || die "can't start vzlist: $!";
139            while(<$vzlist>) {
140                    s/^\s+//;
141                    my @data = split(/\s+/, $_);
142                    
143                    if ( defined $args->{hash} ) {
144                            my $hash;
145                            $hash->{ $vzlist_fields[$_] } = $data[$_] foreach 1 .. $#data;
146                            $output->{ $data[0] } = $hash;
147                    } else {
148                            $output .= $_;
149                    }
150            }
151    
152            return $output;
153    }
154    
155  1;  1;

Legend:
Removed from v.40  
changed lines
  Added in v.42

  ViewVC Help
Powered by ViewVC 1.1.26