From a1052aea4c790463f400d30c1fc5f45650d95bbc Mon Sep 17 00:00:00 2001 From: Alexander Sulfrian Date: Wed, 12 Feb 2014 04:02:54 +0100 Subject: get_mem: add function --- lib/VServer.pm | 37 ++++++++++++++++++++++++++++++++++++- 1 file changed, 36 insertions(+), 1 deletion(-) 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 directory of the vserver and parses the loadavg values. The function returns the three values or B if any error occurs. +=item B + +Return memory information for the vserver specified by the given +context id. The function read the B file inside the I +directory of the vserver, parses the memory information and +multiplies it with the pagesize. The function returns four values +in the following order I, I, +I and I or B if +any error occurs. + =back =head1 AUTHORS -- cgit v1.2.3-1-g7c22