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.
156 # Need to reset bandwidth limits here
158 syslog("info", "pl_mom: Beginning bandwidth monitoring for $now");
161 # Get baseline counts
163 open (BASE, "+<$daily_log") ||
164 print "Cannot open $daily_log; $!\n";
166 my ($sliceid, $bytecount) = split(/ /);
167 $Start{$sliceid} = $bytecount;
170 $status = `tc -s -d qdisc show`;
171 @lines = split(/\n/, $status);
172 for ($i = 0; $i < @lines; $i++) {
173 if ($lines[$i] =~ /qdisc pfifo/) {
174 $lines[$i] =~ s/^ +//;
175 @fields = split(/ /, $lines[$i]);
179 if ($slice != 9999) {
180 $lines[$i+1] =~ s/^ +//;
181 @fields = split(/ /, $lines[$i+1]);
183 #if ($bytes) {print "Slice $slice sent $bytes bytes\n";}
185 if (! defined($Start{$slice})) {
186 print BASE "$slice $bytes\n";
187 $Start{$slice} = $bytes;
189 $Now{$slice} = $bytes;
195 foreach $slice ( sort (keys %Start) ) {
196 if (defined $Now{$slice}) {
197 $today = $Now{$slice} - $Start{$slice};
198 if ($today >= $byte_cutoff && ! defined($Cap{$slice})) {
199 $Cap{$slice} = "sent";
200 $slicename = get_slice_name($slice);
202 bw_cap_mail($slicename);
204 syslog("warning", "pl_mom: Could not find slice name".
205 " for slice ID $slice");
207 # Add bandwidth cap here
210 # The /proc/virtual/<id> directory is gone...
219 # Arg 0: recipient addresses, comma-separated string
220 # Arg 1: subject line
221 # Arg 2: body of message
222 my $to = "To: $_[0]\n";
223 my $from = "From: $from_addr\n";
224 my $subject = "Subject: $_[1]\n";
227 open(SENDMAIL, "|$sendmail") or die "Cannot open $sendmail: $!";
229 print SENDMAIL $from;
230 print SENDMAIL $subject;
231 print SENDMAIL "Content-type: text/plain\n\n";
237 my ($sec, $min, $hour, $mday, $mon, $year, $wday, $yday, $isdat)
239 my $date = sprintf("%4d-%02d-%02d %02d:%02d:%02d",
240 $year+1900, $mon+1, $mday, $hour, $min, $sec);
247 # Need to map slice id to slice name; is there a sensor?
248 # For now, get it from /etc/passwd
250 open (PASSWD, "</etc/passwd") ||
251 print "Cannot open /etc/passwd; $!\n";
253 my ($slicename, $passwd, $sliceid) = split(/:/);
254 if ($sliceid == $_[0]) {
263 my $hostname = hostname();
264 my $date = get_date();
265 my $sent = int($byte_cutoff/1000000000);
267 # Put this here because this is where we have the
268 syslog("warning", "pl_mom: Capping bandwidth of slice ".
269 "$slicename at $bwcap until midnight GMT.");
271 send_mail("acb\@planet-lab.org",
272 "$proc capped bandwidth of slice $slicename on $hostname",
273 "Slice $slicename has transmitted more than ${sent}GB today".
275 "Its bandwidth will be capped at $bwcap until midnight GMT.".
276 "\n\n$date $hostname bwcap $slicename\n");
280 my $hostname = hostname();
281 my $date = get_date();
282 send_mail($alias_addr,
283 "$proc rebooted $hostname",
284 "Swap space was exhausted on $hostname and so $proc rebooted ".
285 "it.\n\nAs of $date, the node has successfully come back ".
286 "online.\n\n$date $hostname reboot\n");
289 sub slice_reset_mail {
292 my $hog_mem = sprintf ("%0.f", $Slice{$hog}{pmem}/1000);
293 my $hog_pct = $Slice{$hog}{mem_pct};
294 my $hostname = hostname();
295 my $date = get_date();
296 send_mail("$alias_addr, $hog\@slices.planet-lab.org",
297 "$proc reset slice $hog on $hostname",
298 "As of $date, swap space is nearly exhausted on $hostname.\n\n".
299 "Slice $hog is being reset since it is the largest consumer ".
300 "of physical memory at ${hog_mem}MB ($hog_pct%).\n\n".
301 "Please reply to this message explaining the nature of your ".
302 "experiment, and what you are doing to address the problem.\n".
303 "\nOutput of 'top -b -n 1' in your slice prior to reset:\n".
304 "$top\n\n$date $hostname reset $hog\n");
307 sub slice_warning_mail {
309 my $hog_mem = sprintf ("%0.f", $Slice{$hog}{pmem}/1000);
310 my $hog_pct = $Slice{$hog}{mem_pct};
311 my $hostname = hostname();
312 my $date = get_date();
314 if ($hog =~ /^root$/) {
317 $to = "$alias_addr, $hog\@slices.planet-lab.org";
321 "$proc may reset slice $hog on $hostname",
322 "As of $date, swap space is over $log_thresh% full on ".
323 "$hostname.\n\nSlice $hog is the largest consumer ".
324 "of physical memory at ${hog_mem}MB ($hog_pct%).\n".
325 "Please check the memory usage of your slice to avoid a ".
326 "reset.\n\n$date $hostname warning $hog\n");
329 sub unkillable_alarm_mail {
331 my $hog_mem = sprintf ("%0.f", $Slice{$hog}{pmem}/1000);
332 my $hog_pct = $Slice{$hog}{mem_pct};
333 my $hostname = hostname();
334 my $date = get_date();
336 if ($hog =~ /^root$/) {
339 $to = "$alias_addr, $hog\@slices.planet-lab.org";
343 "$proc: alarm for slice $hog on $hostname",
344 "As of $date, swap space is over $log_thresh% full on ".
345 "$hostname.\n\nSlice $hog is the largest consumer ".
346 "of physical memory at ${hog_mem}MB ($hog_pct%).\n".
347 "The slice will not be reset, but please verify its behavior.\n".
348 "\n$date $hostname alarm $hog\n");
353 my $sliceid = $Slice{$slice}{ctx};
354 system("chcontext --ctx $sliceid sudo kill -9 -1");
355 system("/etc/init.d/vserver-init start $slice");
359 open (SWAP, "</proc/swaps") ||
360 die "Cannot open /proc/swaps; $!\n";
364 $line =~ s/[\t ]+/ /g;
366 my ($filename, $type, $size, $used, $priority) = split(/ /, $line);
369 return 100*($used/$size);
374 #$content = get "http://127.0.0.1:3100/slicestat";
375 $content = `curl -s http://127.0.0.1:3100/slicestat`;
377 $content = `cat ../pl_mom-deploy/slicestat`
379 my @lines = split(/\n/, $content);
381 foreach $line (@lines) {
382 my ($slice, $ctx, $cpu_pct, $mem_pct, $pmem, $vmem, $ntasks)
384 $Slice{$slice}{ctx} = $ctx;
385 $Slice{$slice}{cpu_pct} = $cpu_pct;
386 $Slice{$slice}{mem_pct} = $mem_pct;
387 $Slice{$slice}{pmem} = $pmem;
388 $Slice{$slice}{vmem} = $vmem;
389 $Slice{$slice}{ntasks} = $ntasks;
394 @keys = sort { $Slice{$b}{mem_pct} <=> $Slice{$a}{mem_pct} } (keys %Slice);
395 foreach $key (@keys) {
396 if ($Slice{$key}{mem_pct} >= $min_thresh) {
397 if ($key =~ /^root$/ || $key =~ /slicestat/ || $key =~ /netflow/) {
398 if (! defined ($Warning{$key})) {
399 $Warning{$key} = "sent";
400 syslog ("warning", "pl_mom: Sending alarm mail to ".
401 "unkillable slice $key, using ".
402 "$Slice{$key}{mem_pct}%% of memory");
403 unkillable_alarm_mail($key);
409 #syslog ("info", "pl_mom: No killable slice using > ".
410 # "$min_thresh%% memory");
417 chdir '/' or die "Can't chdir to /: $!";
418 open STDIN, '/dev/null' or die "Can't read /dev/null: $!";
419 open STDOUT, '>>/dev/null' or die "Can't write to /dev/null: $!";
420 open STDERR, '>>/dev/null' or die "Can't write to /dev/null: $!";
421 defined(my $pid = fork) or die "Can't fork: $!";
423 setsid or die "Can't start a new session: $!";