Those scripts don't take into consideration the amount of memory allocated to other types of resources like buffers for example. See this post for more information
Please remember that the total of the resource allocation variables tcpsndbuf, tcprcvbuf, othersockbuf, dgramrcvbuf, kmemsize, and privvmpages is the total memory usage of your VPS, and so the total of the limits of all of those values is your memory limit.
Contents |
This tries to match the formatting of free
#!/usr/bin/perl
# Made by Max Vohra <maxv@spry.com>
# For use with VPSLink.com or Spry.com VPSs
use Switch;
sub fetch_ubc {
open( UBC, '<', '/proc/user_beancounters' );
my %_BEANS;
while (<UBC>) {
m/([a-z]+)\s+(\d+)\s+(\d+)\s+(\d+)\s+(\d+)\s+(\d+)/;
$_BEANS{$1} = { 'held' => $2,
'maxheld' => $3,
'barrier' => $4,
'limit' => $5,
'failcnt' => $6
};
}
close UBC;
return \%_BEANS;
}
sub show_help {
print "usage: free [-b|-k|-m|-g] [-l] [-o] [-t] [-s delay] [-c count] [-V]
-b,-k,-m,-g show output in bytes, KB, MB, or GB
-o use old format (no -/+buffers/cache line)
-s update every [delay] seconds
-c update [count] times
-V display version information and exit\n";
exit 0;
};
my $scale = 1;
my $SLEEP = 0;
my $COUNT = -1;
my $O_FLAG = 0;
my $skip_next_flag = 0;
for(0 .. $#ARGV){
if($skip_next_flag){
$skip_next_flag = 0;
next;
}
switch($ARGV[$_]) {
case '-m' { $scale = 1024 }
case '-k' { $scale = 1 }
case '-b' { $scale = 1/1024 }
case '-o' { $O_FLAG = 1 }
case '-s' {
if( $ARGV[$_+1] =~ m/^\d+$/ ) {
$SLEEP = $ARGV[$_+1];
$skip_next_flag = 1;
} else {
die "-s must be an integer\n";
}
}
case '-c' {
if( $ARGV[$_+1] =~ m/^\d+$/ ) {
$COUNT = ($ARGV[$_+1]-1);
$skip_next_flag = 1;
} else {
die "-c must be an integer\n";
}
}
case '-V' { print "Version 0.00000000000000000003"; exit 0 }
else { show_help(); }
};
};
$i = 0;
while(1){
my %BEANS = %{ fetch_ubc() };
my $param = 'limit';
$buffer_limit = ( ( $BEANS{'tcpsndbuf'}{$param}+
$BEANS{'tcprcvbuf'}{$param}+
$BEANS{'othersockbuf'}{$param}+
$BEANS{'dgramrcvbuf'}{$param}
)/1024 );
$memory_limit = ( $BEANS{'privvmpages'}{$param}*4 );
$limit = $buffer_limit + $memory_limit;
$param = 'held';
$buffer_held = ( ( $BEANS{'tcpsndbuf'}{$param}+
$BEANS{'tcprcvbuf'}{$param}+
$BEANS{'othersockbuf'}{$param}+
$BEANS{'dgramrcvbuf'}{$param}
)/1024 );
$memory_held = ( $BEANS{'privvmpages'}{$param}*4 );
$held = $buffer_held + $memory_held;
$memory_free = $memory_limit - $memory_held;
$free = $limit-$held;
$shared = $BEANS{'shmpages'}{'held'} * 4;
print "\t\t", join("\t",qw( total used free shared buffers cached)),"\n";
print "Mem:\t\t", join("\t", int($limit/$scale),
int($held/$scale),
int($free/$scale),
int($shared/$scale),
int($buffer_held/$scale), "0"), "\n";
unless ( $O_FLAG ) {
print join("\t", "-/+ buffers/cache:",
int($memory_held/$scale),
int($memory_free/$scale) ), "\n";
}
if ( 0 < $SLEEP && ( $i < $COUNT || -1 == $COUNT ) ) {
sleep $SLEEP;
if($COUNT > 0){$i++};
} else {
last;
}
}
Have you had any instances where you're running out of memory at certain times, but you can't ever make it there in time to actually see what caused it? This script is for you. It will save a "ps faux" output to a directory that you choose every few seconds (configurable).
#!/usr/bin/perl
# Made by Max Vohra <maxv@spry.com>
# To change what it does when it reaches the threshold
# modify the function "panic_time"
$SLEEP=3; # Time between checks
$THRESHOLD=(80/100); # fraction of memory used
$SAVE_DIR='/root/mem-monitor/'; # Where to save the output
my @fields = ( "kmemsize", "privvmpages" ) ; # watch these feilds
sub fetch_ubc {
open( UBC, '<', '/proc/user_beancounters' );
my %_BEANS;
while (<UBC>) {
m/([a-z]+)\s+(\d+)\s+(\d+)\s+(\d+)\s+(\d+)\s+(\d+)/;
$_BEANS{$1} = { 'held' => $2,
'maxheld' => $3,
'barrier' => $4,
'limit' => $5,
'failcnt' => $6
};
}
close UBC;
return \%_BEANS;
}
sub panic_time {
open( PS_OUT, '>', $SAVE_DIR.time().'.ps_faux');
print PS_OUT `ps faux`;
close( PS_OUT );
}
while(1) {
my $beans = fetch_ubc();
for my $field ( @fields ) {
if ($beans->{$field}{"held"}>($THRESHOLD*$beans->{$field}{"limit"})){
panic_time();
};
}
sleep( $SLEEP );
};
A simple Memory Utilization script was contributed by sleddog from forums.vpslink.com and modified by jwthompson2.
#!/bin/bash
bean=`cat /proc/user_beancounters`
guar=`echo "$bean" | grep vmguar | awk '{ print $4;}'`
priv=`echo "$bean" | grep privvm | awk '{ print $2;}'`
let totl=guar/256
let used=priv/256
let free=totl-used
let per=used*100/totl
echo "VPS Memory:"
echo " total: $totl mb used: $used mb ($per%) free: $free mb"
As root:
- Open a text editor and save the above code as /usr/local/bin/Free (with a capital 'F'). - chmod 700 (or 750 or 755) /usr/local/bin/Free
[root@vps] Free VPS Memory: total: 232 mb used: 8 mb (3%) free: 224 mb
That's on a VPSLink-3.
Another, similar script, also suggested on forums.vpslink.com
#!/bin/bash
beans=`cat /proc/user_beancounters | grep priv`
max=`echo $beans | awk '{ print $4;}'`
use=`echo $beans | awk '{ print $2;}'`
let "per=$use*100/$max"
let "mb=$use/256"
echo "privvmpages usage: $mb MB ($per%)"
Save and edit as above. When executed, it returns
[root@vps ~]# Free2 privvmpages usage: 48 MB (40%) [root@vps ~]#
Labrador Data has written a PHP script for displaying current system utilization under Virtuozzo: vpsinfo. You will also need the beanc helper application to avoid permission problems with OpenVZ and newer versions of Virtuozzo. Labrador Data is not affiliated with VPSLink.
Here is a great one liner from Ted, see forum for discussion
grep oomguarpages /proc/user_beancounters \
| awk '{s=$2;t=$3;u=$4; {print "VPS Memory Usage\nCurrent Held: " s/204.8 \
"MB\nMax Held: " t/204.8 "MB\nBarrier: "u/204.8"MB" }}'
Run that and you should get a result like this, VPS Memory Usage Current Held: 18.3545MB Max Held: 67.0068MB Barrier: 255.996MB