use Sys::Hostname;
#use LWP::Simple;
-$debug = 0;
+$debug = 1;
$proc = "pl_mom";
$alias_addr = "pl-mom\@planet-lab.org";
$from_addr = "support\@planet-lab.org";
-$bwcap = "1.5Mbit";
+#$bwcap = "1.5Mbit";
+$bwcap = "off";
if (! $debug) {
$kill_thresh = 90;
$change_thresh = 5;
$min_thresh = 10;
$byte_cutoff = 16000000000; # 16GB
- #$byte_cutoff = 5000000000; # 5GB
$bwmon_sleep = 900;
$sendmail = "/usr/sbin/sendmail -t -f$from_addr";
$rebootfile = "/var/lib/misc/pl_mom.reboot";
$daily_log = "/var/lib/misc/pl_mom.daily";
$daily_stamp = "/var/lib/misc/pl_mom.stamp";
+ $configfile = "/etc/planetlab/pl_mom.conf";
+ $capfile = "/var/lib/misc/pl_mom.oldcaps";
} else {
$kill_thresh = 2;
$reboot_thresh = 20;
$rebootfile = "./pl_mom.reboot";
$daily_log = "./pl_mom.daily";
$daily_stamp = "./pl_mom.stamp";
+ $configfile = "./pl_mom.conf";
+ $capfile = "./pl_mom.oldcaps";
}
$sleep = 30;
system("echo $$ > $pidfile");
+read_config_file();
+
# Check to see whether pl_mom rebooted the node
if (-e $rebootfile) {
unlink($rebootfile);
$pid = fork();
if (! $pid) {
syslog ("info", "pl_mom: Launching bandwidth monitor");
+ if ($bwcap =~ /off/) {
+ syslog("info", "pl_mom: Bandwidth capping is off");
+ }
+ reset_bandwidth_caps();
bandwidth_monitor();
die (0);
}
} else {
my $id = `id -u $hog`;
chomp($id);
- my $top = `chcontext --ctx $id top -b -n 1`;
+ my $top = `/usr/sbin/chcontext --ctx $id /usr/bin/top -b -n 1`;
syslog ("warning", "pl_mom: Resetting slice $hog");
if (! $debug) {
slice_reset($hog);
system("touch $rebootfile");
if (! $debug) {
- system("shutdown -r now");
+ #system("shutdown -r now");
+ system("/bin/sync; /sbin/reboot -f");
}
die (0);
}
# Also may want a list of slices that are exempt from capping.
if (defined(%Start)) { undef %Start; }
if (defined(%Now)) { undef %Now; }
+ if (defined(%Cap)) { undef %Cap; }
- if (defined(%Cap)) {
- # Reset bandwidth limits here
- chomp($cap = `cat /etc/planetlab/bwcap`);
- foreach $slice ( sort ( keys %Cap ) ) {
- cap_bandwidth ($slice, $cap);
- }
- undef %Cap;
- }
+ reset_bandwidth_caps();
syslog("info", "pl_mom: Beginning bandwidth monitoring for $now");
}
}
}
close (BASE);
-
- foreach $slice ( sort (keys %Start) ) {
- if (defined $Now{$slice}) {
- $today = $Now{$slice} - $Start{$slice};
- if ($today >= $byte_cutoff && ! defined($Cap{$slice})) {
- $Cap{$slice} = "sent";
- $slicename = get_slice_name($slice);
- if ($slicename) {
- bw_cap_mail($slicename);
- # Cap bandwidth here
- cap_bandwidth($slicename, $bwcap);
- } else {
- syslog("warning", "pl_mom: Could not find slice name".
- " for slice ID $slice");
+
+ if (!($bwcap =~ /off/)) {
+ foreach $slice ( sort (keys %Start) ) {
+ if (defined $Now{$slice}) {
+ $today = $Now{$slice} - $Start{$slice};
+ if ($today >= $byte_cutoff && ! defined($Cap{$slice})) {
+ $Cap{$slice} = "sent";
+ $slicename = get_slice_name($slice);
+ if ($slicename) {
+ bw_cap_mail($slicename);
+ log_bandwidth_cap($slicename, $bwcap);
+ cap_bandwidth($slicename, $bwcap);
+ } else {
+ syslog("warning", "pl_mom: Could not find slice ".
+ "name for slice ID $slice");
+ }
}
+ } else {
+ # Token bucket for this slice is gone!
}
- } else {
- # The /proc/virtual/<id> directory is gone...
}
}
}
}
+sub read_config_file {
+ if (-e $configfile) {
+ open (CONFIG, "<$configfile") ||
+ print "Cannot open $configfile; $!\n";
+ while (<CONFIG>) {
+ if (m/^(.*)=(.*)$/) {
+ ${$1} = $2;
+ if ($debug) {
+ print "read_config_file: $1 = ${$1}\n";
+ }
+ }
+ }
+ close CONFIG;
+ }
+}
+
+sub reset_bandwidth_caps {
+ if (-e $capfile) {
+ open(CAP, "<$capfile") or die "Cannot open $capfile: $!";
+ while (<CAP>) {
+ chomp();
+ ($slicename, $oldcap) = split(/ /);
+ syslog("info", "pl_mom: Restoring bandwidth cap of $oldcap ".
+ "to $slicename");
+ cap_bandwidth ($slicename, $oldcap);
+ }
+ close CAP;
+ unlink($capfile);
+ }
+}
+
+sub log_bandwidth_cap {
+ ($slicename, $cap) = @_;
+ syslog("warning", "pl_mom: Capping bandwidth of slice ".
+ "$slicename at $cap until midnight GMT.");
+ # Save current cap to $capfile
+ system("echo $slicename `bwlimit getcap $slicename` >> $capfile");
+}
+
sub send_mail {
# Arg 0: recipient addresses, comma-separated string
# Arg 1: subject line
}
sub cap_bandwidth {
- # Arg 0: slice name
- # Arg 1: bandwidth cap for 'tc'
- `bwlimit setcap $_[0] $_[1]`;
- `bwlimit on $_[0]`;
+ ($slicename, $cap) = @_;
+ system("bwlimit setcap $slicename $cap");
+ system("bwlimit on $slicename");
}
sub get_date {
my $date = get_date();
my $sent = int($byte_cutoff/1000000000);
- # Put this here because this is where we have the
- syslog("warning", "pl_mom: Capping bandwidth of slice ".
- "$slicename at $bwcap until midnight GMT.");
-
- send_mail($alias_addr,
+ send_mail("$alias_addr, $slicename\@slices.planet-lab.org",
"$proc capped bandwidth of slice $slicename on $hostname",
"Slice $slicename has transmitted more than ${sent}GB today".
" on $hostname. ".
sub get_slice_info {
if (! $debug) {
- #$content = get "http://127.0.0.1:3100/slicestat";
$content = `curl -s http://127.0.0.1:3100/slicestat`;
} else {
- $content = `cat ../pl_mom-deploy/slicestat`
+ #$content = `cat ../pl_mom-deploy/slicestat`
+ $content = `curl -s http://127.0.0.1:3100/slicestat`;
}
my @lines = split(/\n/, $content);
%Slice = ();