10 $alias_addr = "pl-mom\@planet-lab.org";
11 $from_addr = "support\@planet-lab.org";
21 $byte_cutoff = 16000000000; # 16GB
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";
29 $configfile = "/etc/planetlab/pl_mom.conf";
40 $pidfile = "./$proc.pid";
41 $rebootfile = "./pl_mom.reboot";
42 $daily_log = "./pl_mom.daily";
43 $daily_stamp = "./pl_mom.stamp";
44 $configfile = "./pl_mom.conf";
49 # daemonize the program
54 system("echo $$ > $pidfile");
58 # Check to see whether pl_mom rebooted the node
61 syslog ("warning", "pl_mom: Sending shutdown mail");
67 syslog ("info", "pl_mom: Launching reboot kicker");
73 syslog ("info", "pl_mom: Launching bandwidth monitor");
74 if ($bwcap =~ /off/) {
75 syslog("info", "pl_mom: Bandwidth capping is off");
82 $used = int(swap_used());
84 if (defined($old_used)) {
85 if ($used >= $old_used + $change_thresh) {
86 syslog ("info", "pl_mom: %d%% swap consumed in last %d seconds",
87 $used - $old_used, $sleep);
91 if ($used >= $log_thresh) {
92 if (! defined($old_used) || $used != $old_used) {
93 syslog ("info", "pl_mom: Swap used: %d%%", $used);
96 my $hog = memory_hog();
98 if ($used < $kill_thresh) {
99 if (! defined($Warning{$hog})) {
100 $Warning{$hog} = "sent";
101 syslog ("warning", "pl_mom: Slice $hog is ".
102 "using $Slice{$hog}{mem_pct}%% of memory");
103 #slice_warning_mail($hog);
106 my $id = `id -u $hog`;
108 my $top = `/usr/sbin/chcontext --ctx $id /usr/bin/top -b -n 1`;
109 syslog ("warning", "pl_mom: Resetting slice $hog");
113 syslog ("warning", "pl_mom: Sending mail to slice $hog");
114 slice_reset_mail($hog, $top);
128 if ($used >= $reboot_thresh) {
129 syslog ("warning", "pl_mom: Rebooting node");
131 system("touch $rebootfile");
133 system("shutdown -r now");
142 sub bandwidth_monitor {
144 # See if a new day has started for bandwidth monitoring
145 chomp($now = `date -u +%D`);
146 if (-e $daily_stamp) {
147 chomp($stamp = `cat $daily_stamp`);
149 if (! defined($stamp) || !($stamp =~ $now)) {
150 open (STAMP, ">$daily_stamp") ||
151 die "Can't open file $daily_stamp for writing: $!\n";
152 print STAMP "$now\n";
156 # Could save the list of capped slices in a file in order to
157 # avoid re-sending mails if the daemon restarts.
158 # Also may want a list of slices that are exempt from capping.
159 if (defined(%Start)) { undef %Start; }
160 if (defined(%Now)) { undef %Now; }
163 # Reset bandwidth limits here
164 chomp($cap = `cat /etc/planetlab/bwcap`);
165 foreach $sliceid ( sort ( keys %Cap ) ) {
166 $slice = get_slice_name($sliceid);
167 syslog("info", "pl_mom: Uncapping bandwidth of $slice");
168 cap_bandwidth ($slice, $cap);
173 syslog("info", "pl_mom: Beginning bandwidth monitoring for $now");
176 # Get baseline counts
178 open (BASE, "+<$daily_log") ||
179 print "Cannot open $daily_log; $!\n";
181 my ($sliceid, $bytecount) = split(/ /);
182 $Start{$sliceid} = $bytecount;
185 $status = `tc -s -d qdisc show`;
186 @lines = split(/\n/, $status);
187 for ($i = 0; $i < @lines; $i++) {
188 if ($lines[$i] =~ /qdisc pfifo/) {
189 $lines[$i] =~ s/^ +//;
190 @fields = split(/ /, $lines[$i]);
194 if ($slice != 9999) {
195 $lines[$i+1] =~ s/^ +//;
196 @fields = split(/ /, $lines[$i+1]);
198 #if ($bytes) {print "Slice $slice sent $bytes bytes\n";}
200 if (! defined($Start{$slice})) {
201 print BASE "$slice $bytes\n";
202 $Start{$slice} = $bytes;
204 $Now{$slice} = $bytes;
210 if (!($bwcap =~ /off/)) {
211 foreach $slice ( sort (keys %Start) ) {
212 if (defined $Now{$slice}) {
213 $today = $Now{$slice} - $Start{$slice};
214 if ($today >= $byte_cutoff && ! defined($Cap{$slice})) {
215 $Cap{$slice} = "sent";
216 $slicename = get_slice_name($slice);
218 bw_cap_mail($slicename);
220 cap_bandwidth($slicename, $bwcap);
222 syslog("warning", "pl_mom: Could not find slice ".
223 "name for slice ID $slice");
227 # Token bucket for this slice is gone!
236 sub read_config_file {
237 if (-e $configfile) {
238 open (CONFIG, "<$configfile") ||
239 print "Cannot open $configfile; $!\n";
241 if (m/^(.*)=(.*)$/) {
244 print "read_config_file: $1 = ${$1}\n";
253 # Arg 0: recipient addresses, comma-separated string
254 # Arg 1: subject line
255 # Arg 2: body of message
256 my $to = "To: $_[0]\n";
257 my $from = "From: $from_addr\n";
258 my $subject = "Subject: $_[1]\n";
261 open(SENDMAIL, "|$sendmail") or die "Cannot open $sendmail: $!";
263 print SENDMAIL $from;
264 print SENDMAIL $subject;
265 print SENDMAIL "Content-type: text/plain\n\n";
272 # Arg 1: bandwidth cap for 'tc'
273 `bwlimit setcap $_[0] $_[1]`;
278 my ($sec, $min, $hour, $mday, $mon, $year, $wday, $yday, $isdat)
280 my $date = sprintf("%4d-%02d-%02d %02d:%02d:%02d",
281 $year+1900, $mon+1, $mday, $hour, $min, $sec);
288 # Need to map slice id to slice name; is there a sensor?
289 # For now, get it from /etc/passwd
291 open (PASSWD, "</etc/passwd") ||
292 print "Cannot open /etc/passwd; $!\n";
294 my ($slicename, $passwd, $sliceid) = split(/:/);
295 if ($sliceid == $_[0]) {
304 my $hostname = hostname();
305 my $date = get_date();
306 my $sent = int($byte_cutoff/1000000000);
308 # Put this here because this is where we have the
309 syslog("warning", "pl_mom: Capping bandwidth of slice ".
310 "$slicename at $bwcap until midnight GMT.");
312 send_mail($alias_addr,
313 "$proc capped bandwidth of slice $slicename on $hostname",
314 "Slice $slicename has transmitted more than ${sent}GB today".
316 "Its bandwidth will be capped at $bwcap until midnight GMT.".
317 "\n\n$date $hostname bwcap $slicename\n");
321 my $hostname = hostname();
322 my $date = get_date();
323 send_mail($alias_addr,
324 "$proc rebooted $hostname",
325 "Swap space was exhausted on $hostname and so $proc rebooted ".
326 "it.\n\nAs of $date, the node has successfully come back ".
327 "online.\n\n$date $hostname reboot\n");
330 sub slice_reset_mail {
333 my $hog_mem = sprintf ("%0.f", $Slice{$hog}{pmem}/1000);
334 my $hog_pct = $Slice{$hog}{mem_pct};
335 my $hostname = hostname();
336 my $date = get_date();
337 send_mail("$alias_addr, $hog\@slices.planet-lab.org",
338 "$proc reset slice $hog on $hostname",
339 "As of $date, swap space is nearly exhausted on $hostname.\n\n".
340 "Slice $hog is being reset since it is the largest consumer ".
341 "of physical memory at ${hog_mem}MB ($hog_pct%).\n\n".
342 "Please reply to this message explaining the nature of your ".
343 "experiment, and what you are doing to address the problem.\n".
344 "\nOutput of 'top -b -n 1' in your slice prior to reset:\n".
345 "$top\n\n$date $hostname reset $hog\n");
348 sub slice_warning_mail {
350 my $hog_mem = sprintf ("%0.f", $Slice{$hog}{pmem}/1000);
351 my $hog_pct = $Slice{$hog}{mem_pct};
352 my $hostname = hostname();
353 my $date = get_date();
355 if ($hog =~ /^root$/) {
358 $to = "$alias_addr, $hog\@slices.planet-lab.org";
362 "$proc may reset slice $hog on $hostname",
363 "As of $date, swap space is over $log_thresh% full on ".
364 "$hostname.\n\nSlice $hog is the largest consumer ".
365 "of physical memory at ${hog_mem}MB ($hog_pct%).\n".
366 "Please check the memory usage of your slice to avoid a ".
367 "reset.\n\n$date $hostname warning $hog\n");
370 sub unkillable_alarm_mail {
372 my $hog_mem = sprintf ("%0.f", $Slice{$hog}{pmem}/1000);
373 my $hog_pct = $Slice{$hog}{mem_pct};
374 my $hostname = hostname();
375 my $date = get_date();
377 if ($hog =~ /^root$/) {
380 $to = "$alias_addr, $hog\@slices.planet-lab.org";
384 "$proc: alarm for slice $hog on $hostname",
385 "As of $date, swap space is over $log_thresh% full on ".
386 "$hostname.\n\nSlice $hog is the largest consumer ".
387 "of physical memory at ${hog_mem}MB ($hog_pct%).\n".
388 "The slice will not be reset, but please verify its behavior.\n".
389 "\n$date $hostname alarm $hog\n");
394 my $sliceid = $Slice{$slice}{ctx};
395 system("chcontext --ctx $sliceid sudo kill -9 -1");
396 system("/etc/init.d/vserver-init start $slice");
400 open (SWAP, "</proc/swaps") ||
401 die "Cannot open /proc/swaps; $!\n";
405 $line =~ s/[\t ]+/ /g;
407 my ($filename, $type, $size, $used, $priority) = split(/ /, $line);
410 return 100*($used/$size);
415 $content = `curl -s http://127.0.0.1:3100/slicestat`;
417 #$content = `cat ../pl_mom-deploy/slicestat`
418 $content = `curl -s http://127.0.0.1:3100/slicestat`;
420 my @lines = split(/\n/, $content);
422 foreach $line (@lines) {
423 my ($slice, $ctx, $cpu_pct, $mem_pct, $pmem, $vmem, $ntasks)
425 $Slice{$slice}{ctx} = $ctx;
426 $Slice{$slice}{cpu_pct} = $cpu_pct;
427 $Slice{$slice}{mem_pct} = $mem_pct;
428 $Slice{$slice}{pmem} = $pmem;
429 $Slice{$slice}{vmem} = $vmem;
430 $Slice{$slice}{ntasks} = $ntasks;
435 @keys = sort { $Slice{$b}{mem_pct} <=> $Slice{$a}{mem_pct} } (keys %Slice);
436 foreach $key (@keys) {
437 if ($Slice{$key}{mem_pct} >= $min_thresh) {
438 if ($key =~ /^root$/ || $key =~ /slicestat/ || $key =~ /netflow/) {
439 if (! defined ($Warning{$key})) {
440 $Warning{$key} = "sent";
441 syslog ("warning", "pl_mom: Sending alarm mail to ".
442 "unkillable slice $key, using ".
443 "$Slice{$key}{mem_pct}%% of memory");
444 unkillable_alarm_mail($key);
450 #syslog ("info", "pl_mom: No killable slice using > ".
451 # "$min_thresh%% memory");
458 chdir '/' or die "Can't chdir to /: $!";
459 open STDIN, '/dev/null' or die "Can't read /dev/null: $!";
460 open STDOUT, '>>/dev/null' or die "Can't write to /dev/null: $!";
461 open STDERR, '>>/dev/null' or die "Can't write to /dev/null: $!";
462 defined(my $pid = fork) or die "Can't fork: $!";
464 setsid or die "Can't start a new session: $!";