10 $alias_addr = "pl-mom\@planet-lab.org";
11 $from_addr = "support\@planet-lab.org";
20 $byte_cutoff = 16000000000; # 16GB
21 #$byte_cutoff = 5000000000; # 5GB
24 $sendmail = "/usr/sbin/sendmail -t -f$from_addr";
25 $pidfile = "/var/run/$proc.pid";
26 $rebootfile = "/var/lib/misc/pl_mom.reboot";
27 $daily_log = "/var/lib/misc/pl_mom.daily";
28 $daily_stamp = "/var/lib/misc/pl_mom.stamp";
39 $pidfile = "./$proc.pid";
40 $rebootfile = "./pl_mom.reboot";
41 $daily_log = "./pl_mom.daily";
42 $daily_stamp = "./pl_mom.stamp";
47 # daemonize the program
52 system("echo $$ > $pidfile");
54 # Check to see whether pl_mom rebooted the node
57 syslog ("warning", "pl_mom: Sending shutdown mail");
63 syslog ("info", "pl_mom: Launching reboot kicker");
69 syslog ("info", "pl_mom: Launching bandwidth monitor");
75 $used = int(swap_used());
77 if (defined($old_used)) {
78 if ($used >= $old_used + $change_thresh) {
79 syslog ("info", "pl_mom: %d%% swap consumed in last %d seconds",
80 $used - $old_used, $sleep);
84 if ($used >= $log_thresh) {
85 if (! defined($old_used) || $used != $old_used) {
86 syslog ("info", "pl_mom: Swap used: %d%%", $used);
89 my $hog = memory_hog();
91 if ($used < $kill_thresh) {
92 if (! defined($Warning{$hog})) {
93 $Warning{$hog} = "sent";
94 syslog ("warning", "pl_mom: Slice $hog is ".
95 "using $Slice{$hog}{mem_pct}%% of memory");
96 #slice_warning_mail($hog);
99 my $id = `id -u $hog`;
101 my $top = `chcontext --ctx $id top -b -n 1`;
102 syslog ("warning", "pl_mom: Resetting slice $hog");
106 syslog ("warning", "pl_mom: Sending mail to slice $hog");
107 slice_reset_mail($hog, $top);
121 if ($used >= $reboot_thresh) {
122 syslog ("warning", "pl_mom: Rebooting node");
124 system("touch $rebootfile");
126 system("shutdown -r now");
135 sub bandwidth_monitor {
137 # See if a new day has started for bandwidth monitoring
138 chomp($now = `date -u +%D`);
139 if (-e $daily_stamp) {
140 chomp($stamp = `cat $daily_stamp`);
142 if (! defined($stamp) || !($stamp =~ $now)) {
143 open (STAMP, ">$daily_stamp") ||
144 die "Can't open file $daily_stamp for writing: $!\n";
145 print STAMP "$now\n";
149 # Could save the list of capped slices in a file in order to
150 # avoid re-sending mails if the daemon restarts.
151 # Also may want a list of slices that are exempt from capping.
152 if (defined(%Start)) { undef %Start; }
153 if (defined(%Now)) { undef %Now; }
156 # Reset bandwidth limits here
157 chomp($cap = `cat /etc/planetlab/bwcap`);
158 foreach $slice ( sort ( keys %Cap ) ) {
159 cap_bandwidth ($slice, $cap);
164 syslog("info", "pl_mom: Beginning bandwidth monitoring for $now");
167 # Get baseline counts
169 open (BASE, "+<$daily_log") ||
170 print "Cannot open $daily_log; $!\n";
172 my ($sliceid, $bytecount) = split(/ /);
173 $Start{$sliceid} = $bytecount;
176 $status = `tc -s -d qdisc show`;
177 @lines = split(/\n/, $status);
178 for ($i = 0; $i < @lines; $i++) {
179 if ($lines[$i] =~ /qdisc pfifo/) {
180 $lines[$i] =~ s/^ +//;
181 @fields = split(/ /, $lines[$i]);
185 if ($slice != 9999) {
186 $lines[$i+1] =~ s/^ +//;
187 @fields = split(/ /, $lines[$i+1]);
189 #if ($bytes) {print "Slice $slice sent $bytes bytes\n";}
191 if (! defined($Start{$slice})) {
192 print BASE "$slice $bytes\n";
193 $Start{$slice} = $bytes;
195 $Now{$slice} = $bytes;
201 foreach $slice ( sort (keys %Start) ) {
202 if (defined $Now{$slice}) {
203 $today = $Now{$slice} - $Start{$slice};
204 if ($today >= $byte_cutoff && ! defined($Cap{$slice})) {
205 $Cap{$slice} = "sent";
206 $slicename = get_slice_name($slice);
208 bw_cap_mail($slicename);
210 cap_bandwidth($slicename, $bwcap);
212 syslog("warning", "pl_mom: Could not find slice name".
213 " for slice ID $slice");
217 # The /proc/virtual/<id> directory is gone...
226 # Arg 0: recipient addresses, comma-separated string
227 # Arg 1: subject line
228 # Arg 2: body of message
229 my $to = "To: $_[0]\n";
230 my $from = "From: $from_addr\n";
231 my $subject = "Subject: $_[1]\n";
234 open(SENDMAIL, "|$sendmail") or die "Cannot open $sendmail: $!";
236 print SENDMAIL $from;
237 print SENDMAIL $subject;
238 print SENDMAIL "Content-type: text/plain\n\n";
245 # Arg 1: bandwidth cap for 'tc'
246 `bwlimit setcap $_[0] $_[1]`;
251 my ($sec, $min, $hour, $mday, $mon, $year, $wday, $yday, $isdat)
253 my $date = sprintf("%4d-%02d-%02d %02d:%02d:%02d",
254 $year+1900, $mon+1, $mday, $hour, $min, $sec);
261 # Need to map slice id to slice name; is there a sensor?
262 # For now, get it from /etc/passwd
264 open (PASSWD, "</etc/passwd") ||
265 print "Cannot open /etc/passwd; $!\n";
267 my ($slicename, $passwd, $sliceid) = split(/:/);
268 if ($sliceid == $_[0]) {
277 my $hostname = hostname();
278 my $date = get_date();
279 my $sent = int($byte_cutoff/1000000000);
281 # Put this here because this is where we have the
282 syslog("warning", "pl_mom: Capping bandwidth of slice ".
283 "$slicename at $bwcap until midnight GMT.");
285 send_mail($alias_addr,
286 "$proc capped bandwidth of slice $slicename on $hostname",
287 "Slice $slicename has transmitted more than ${sent}GB today".
289 "Its bandwidth will be capped at $bwcap until midnight GMT.".
290 "\n\n$date $hostname bwcap $slicename\n");
294 my $hostname = hostname();
295 my $date = get_date();
296 send_mail($alias_addr,
297 "$proc rebooted $hostname",
298 "Swap space was exhausted on $hostname and so $proc rebooted ".
299 "it.\n\nAs of $date, the node has successfully come back ".
300 "online.\n\n$date $hostname reboot\n");
303 sub slice_reset_mail {
306 my $hog_mem = sprintf ("%0.f", $Slice{$hog}{pmem}/1000);
307 my $hog_pct = $Slice{$hog}{mem_pct};
308 my $hostname = hostname();
309 my $date = get_date();
310 send_mail("$alias_addr, $hog\@slices.planet-lab.org",
311 "$proc reset slice $hog on $hostname",
312 "As of $date, swap space is nearly exhausted on $hostname.\n\n".
313 "Slice $hog is being reset since it is the largest consumer ".
314 "of physical memory at ${hog_mem}MB ($hog_pct%).\n\n".
315 "Please reply to this message explaining the nature of your ".
316 "experiment, and what you are doing to address the problem.\n".
317 "\nOutput of 'top -b -n 1' in your slice prior to reset:\n".
318 "$top\n\n$date $hostname reset $hog\n");
321 sub slice_warning_mail {
323 my $hog_mem = sprintf ("%0.f", $Slice{$hog}{pmem}/1000);
324 my $hog_pct = $Slice{$hog}{mem_pct};
325 my $hostname = hostname();
326 my $date = get_date();
328 if ($hog =~ /^root$/) {
331 $to = "$alias_addr, $hog\@slices.planet-lab.org";
335 "$proc may reset slice $hog on $hostname",
336 "As of $date, swap space is over $log_thresh% full on ".
337 "$hostname.\n\nSlice $hog is the largest consumer ".
338 "of physical memory at ${hog_mem}MB ($hog_pct%).\n".
339 "Please check the memory usage of your slice to avoid a ".
340 "reset.\n\n$date $hostname warning $hog\n");
343 sub unkillable_alarm_mail {
345 my $hog_mem = sprintf ("%0.f", $Slice{$hog}{pmem}/1000);
346 my $hog_pct = $Slice{$hog}{mem_pct};
347 my $hostname = hostname();
348 my $date = get_date();
350 if ($hog =~ /^root$/) {
353 $to = "$alias_addr, $hog\@slices.planet-lab.org";
357 "$proc: alarm for slice $hog on $hostname",
358 "As of $date, swap space is over $log_thresh% full on ".
359 "$hostname.\n\nSlice $hog is the largest consumer ".
360 "of physical memory at ${hog_mem}MB ($hog_pct%).\n".
361 "The slice will not be reset, but please verify its behavior.\n".
362 "\n$date $hostname alarm $hog\n");
367 my $sliceid = $Slice{$slice}{ctx};
368 system("chcontext --ctx $sliceid sudo kill -9 -1");
369 system("/etc/init.d/vserver-init start $slice");
373 open (SWAP, "</proc/swaps") ||
374 die "Cannot open /proc/swaps; $!\n";
378 $line =~ s/[\t ]+/ /g;
380 my ($filename, $type, $size, $used, $priority) = split(/ /, $line);
383 return 100*($used/$size);
388 #$content = get "http://127.0.0.1:3100/slicestat";
389 $content = `curl -s http://127.0.0.1:3100/slicestat`;
391 $content = `cat ../pl_mom-deploy/slicestat`
393 my @lines = split(/\n/, $content);
395 foreach $line (@lines) {
396 my ($slice, $ctx, $cpu_pct, $mem_pct, $pmem, $vmem, $ntasks)
398 $Slice{$slice}{ctx} = $ctx;
399 $Slice{$slice}{cpu_pct} = $cpu_pct;
400 $Slice{$slice}{mem_pct} = $mem_pct;
401 $Slice{$slice}{pmem} = $pmem;
402 $Slice{$slice}{vmem} = $vmem;
403 $Slice{$slice}{ntasks} = $ntasks;
408 @keys = sort { $Slice{$b}{mem_pct} <=> $Slice{$a}{mem_pct} } (keys %Slice);
409 foreach $key (@keys) {
410 if ($Slice{$key}{mem_pct} >= $min_thresh) {
411 if ($key =~ /^root$/ || $key =~ /slicestat/ || $key =~ /netflow/) {
412 if (! defined ($Warning{$key})) {
413 $Warning{$key} = "sent";
414 syslog ("warning", "pl_mom: Sending alarm mail to ".
415 "unkillable slice $key, using ".
416 "$Slice{$key}{mem_pct}%% of memory");
417 unkillable_alarm_mail($key);
423 #syslog ("info", "pl_mom: No killable slice using > ".
424 # "$min_thresh%% memory");
431 chdir '/' or die "Can't chdir to /: $!";
432 open STDIN, '/dev/null' or die "Can't read /dev/null: $!";
433 open STDOUT, '>>/dev/null' or die "Can't write to /dev/null: $!";
434 open STDERR, '>>/dev/null' or die "Can't write to /dev/null: $!";
435 defined(my $pid = fork) or die "Can't fork: $!";
437 setsid or die "Can't start a new session: $!";