- start pl_conf in case it's dead
[mom.git] / pl_mom.pl
index 86a56c8..c6ac506 100755 (executable)
--- a/pl_mom.pl
+++ b/pl_mom.pl
@@ -196,10 +196,8 @@ sub bandwidth_monitor {
                    ! defined($Cap{$slice})) {
                    $Cap{$slice} = "sent";
                    bw_cap_mail($slice);
-                   if (! $debug) {
-                       log_bandwidth_cap($slice, $bwcap_default);
-                       cap_bandwidth($slice, $bwcap_default);
-                   } 
+                   log_bandwidth_cap($slice, $Maxrate{$slice});
+                   cap_bandwidth($slice, $Maxrate{$slice});
                }
            } else {
                # Token bucket for this slice is gone!
@@ -248,15 +246,25 @@ sub get_baseline_counts {
     }
 
     my $status = `tc -s -d qdisc show`;
-    my $sliceid = "9999";
+    my $sliceid = 0xffff;
     @Lines = split(/\n/, $status);
     foreach $line ( @Lines ) {
        if ($line =~ /qdisc pfifo (.*): dev/) {
-           $sliceid = $1;
+           $sliceid = hex($1);
+           # "Capped" buckets all begin with 0x1000. Ignore the root
+           # (0x1000) and default (0x1fff) buckets, as well as
+           # "exempt" buckets that begin with 0x2000 (or anything
+           # other than 0x1000).
+           if (($sliceid & 0xf000) == 0x1000 &&
+               $sliceid != 0x1000 && $sliceid != 0x1fff) {
+               $sliceid = $sliceid & 0x0fff;
+           } else {
+               $sliceid = 0xffff;
+           }
        } else {
            if ($line =~ /Sent (.*) bytes/) {
                my $bytes = $1;
-               if ($sliceid != 9999) {
+               if ($sliceid != 0xffff) {
                    my $slice = $Name{$sliceid};
                    if ($debug && $bytes) {
                        print "Slice: $slice ($sliceid), bytes $bytes\n";
@@ -296,10 +304,6 @@ sub get_slice_limits {
            }
            $Maxrate{$slice} = $limit;
            $Cutoff{$slice} = $cutoff;
-           if ($debug) {
-               print "Slice $slice, maxrate $Maxrate{$slice}, ".
-                   "cutoff $Cutoff{$slice}\n";
-           }
        }
     }
 }
@@ -336,13 +340,18 @@ sub send_mail {
     my $subject = "Subject: $_[1]\n";
     my $msg = $_[2];
 
-    open(SENDMAIL, "|$sendmail") or die "Cannot open $sendmail: $!";
-    print SENDMAIL $to;
-    print SENDMAIL $from;
-    print SENDMAIL $subject;
-    print SENDMAIL "Content-type: text/plain\n\n";
-    print SENDMAIL $msg;
-    close(SENDMAIL);
+    if ($debug) {
+       print $to;
+       print $subject;
+    } else {
+       open(SENDMAIL, "|$sendmail") or die "Cannot open $sendmail: $!";
+       print SENDMAIL $to;
+       print SENDMAIL $from;
+       print SENDMAIL $subject;
+       print SENDMAIL "Content-type: text/plain\n\n";
+       print SENDMAIL $msg;
+       close(SENDMAIL);
+    }
 }
 
 sub cap_bandwidth {
@@ -363,12 +372,12 @@ sub bw_cap_mail {
     my ($slicename) = @_;
     my $hostname = hostname();
     my $date = get_date();
-    my $sent = int($Cutoff{$slicename}/1000000000);
+    my $sent = int($Cutoff{$slicename}/(1024*1024));
     my $bwcap = $Maxrate{$slicename};
 
     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".
+             "Slice $slicename has transmitted more than ${sent}MB today".
              " on $hostname.  ".
              "Its bandwidth will be capped at $bwcap until midnight GMT.".
              "\n\n$date $hostname bwcap $slicename\n");