X-Git-Url: http://git.onelab.eu/?a=blobdiff_plain;f=pl_mom.pl;h=ecab7bfc8fa42b8d7d191b5bdfd320b764550147;hb=56223fd521ba496f961baf743698ef2cfaebf76e;hp=1e16c3c2a231171fe1841bac4b39c7e1a4f952a8;hpb=1beb79232d2015f6c4eb40c4fab2b1ccda9fc85a;p=mom.git diff --git a/pl_mom.pl b/pl_mom.pl index 1e16c3c..ecab7bf 100755 --- a/pl_mom.pl +++ b/pl_mom.pl @@ -5,7 +5,7 @@ use Sys::Syslog; 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"; @@ -27,6 +27,7 @@ if (! $debug) { $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; @@ -42,6 +43,7 @@ if (! $debug) { $daily_log = "./pl_mom.daily"; $daily_stamp = "./pl_mom.stamp"; $configfile = "./pl_mom.conf"; + $capfile = "./pl_mom.oldcaps"; } $sleep = 30; @@ -74,6 +76,7 @@ if (! $pid) { if ($bwcap =~ /off/) { syslog("info", "pl_mom: Bandwidth capping is off"); } + reset_bandwidth_caps(); bandwidth_monitor(); die (0); } @@ -105,7 +108,7 @@ while (1) { } 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); @@ -130,7 +133,8 @@ sub reboot_kicker { system("touch $rebootfile"); if (! $debug) { - system("shutdown -r now"); + #system("shutdown -r now"); + system("/bin/sync; /sbin/reboot -f"); } die (0); } @@ -158,17 +162,9 @@ sub bandwidth_monitor { # 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 $sliceid ( sort ( keys %Cap ) ) { - $slice = get_slice_name($sliceid); - syslog("info", "pl_mom: Uncapping bandwidth of $slice"); - cap_bandwidth ($slice, $cap); - } - undef %Cap; - } + reset_bandwidth_caps(); syslog("info", "pl_mom: Beginning bandwidth monitoring for $now"); } @@ -216,7 +212,7 @@ sub bandwidth_monitor { $slicename = get_slice_name($slice); if ($slicename) { bw_cap_mail($slicename); - # Cap bandwidth here + log_bandwidth_cap($slicename, $bwcap); cap_bandwidth($slicename, $bwcap); } else { syslog("warning", "pl_mom: Could not find slice ". @@ -249,6 +245,29 @@ sub read_config_file { } } +sub reset_bandwidth_caps { + if (-e $capfile) { + open(CAP, "<$capfile") or die "Cannot open $capfile: $!"; + while () { + 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 @@ -268,10 +287,9 @@ sub send_mail { } 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 { @@ -305,11 +323,7 @@ sub bw_cap_mail { 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. ". @@ -412,10 +426,10 @@ sub swap_used { 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 = ();