diff options
-rw-r--r-- | lib/VServer.pm | 37 |
1 files changed, 36 insertions, 1 deletions
diff --git a/lib/VServer.pm b/lib/VServer.pm index 1ef31aa..20c8cc1 100644 --- a/lib/VServer.pm +++ b/lib/VServer.pm @@ -1,7 +1,7 @@ #!/usr/bin/perl -T package VServer; -our $VERSION = '1.5'; +our $VERSION = '1.6'; use strict; use warnings; @@ -95,6 +95,31 @@ sub get_loadavg($) { return @load; } +sub get_mem($) { + my $context = shift; + my $dir = get_proc_dir($context) || return undef; + my $ps = POSIX::sysconf(&POSIX::_SC_PAGESIZE) || return undef; + my @mem = undef; + + open(my $limit, "<", "$dir/limit") || return undef; + while (<$limit>) { + if ($_ =~ m/^VM:\s*([0-9]+)\s/) { + $mem[0] = $1 * $ps; + } + elsif ($_ =~ m/RSS:\s*([0-9]+)\s/) { + $mem[1] = $1 * $ps; + } + elsif ($_ =~ m/ANON:\s*([0-9]+)\s/) { + $mem[2] = $1 * $ps; + } + elsif ($_ =~ m/VML:\s*([0-9]+)\s/) { + $mem[3] = $1 * $ps; + } + } + close($limit); + + return @mem; +} 1; __END__ @@ -163,6 +188,16 @@ file inside the I</proc> directory of the vserver and parses the loadavg values. The function returns the three values or B<undef> if any error occurs. +=item B<get_mem($context)> + +Return memory information for the vserver specified by the given +context id. The function read the B<limit> file inside the I</proc> +directory of the vserver, parses the memory information and +multiplies it with the pagesize. The function returns four values +in the following order I<virtual memory>, I<resident set size>, +I<anonymous memory> and I<virtual locked memory> or B<undef> if +any error occurs. + =back =head1 AUTHORS |