#!perluse warnings;use strict;use net::smtp_auth;#&sendmail;while (1) {My ($sumMem, $freeMem, $rateMem); open MEM, " Cat/proc/meminfo | "or Die $!; while (<MEM>) {$sumMem = $ if (/memtotal:\s+ (\d+) \s+kb/), $freeMem + = if (/memfree:\s+ (\d+) \s+kb/); $freeMem + = $ if (/buffers:\s+ (\d+) \s+kb/); $freeMem + = (/cached:\s+ (\d+) \s+kb/);} $rateMem = sprintf ("%d", $freeMem/$sumMem *), if ($rateMem <=) {My $content = &detail;&sendmail ($content) ; Sleep (3600);} Sleep (60);} Sub detail{' Top-bn 1 > log '; open log, "head-100 log |" or Die $!; My $content, while (<LOG>) {next/^$/), my @top = Split/\s+/;if ($top [5] =~/g|t/and $top [0] =~/\d+/) {$content. = " $top [1]\t$top[5]\t$top[11]\n ";}} return $content;} Sub Sendmail{my $content = shift @_;my $smtpHost = ' smtp.exmail.qq.com '; my $smtpPort = ' + '; my $username = ' * * * @genedenov O.com '; my $passowrd = ' * * * '; My @to = (' * * * ', ' * * * '); my $suffix = "\ @genedenovo. com"; my $subject = "Please note that 19 server memory usage has exceeded 90%! "; My $message =" Details: $contentMonitoring program from $username "; my $smtp = Net::smtp_auth->new ($smtpHost, Timeout = +) or die" Error: Connection to $smtphost failed! "; $smtp->auth (' LOGIN ', $username, $passowrd) or Die (" Error: Authentication failed! My @address = map {My $i = $_. $suffix; $i} @to, my $x = join ",", @address; $smtp->mail ($username); $smtp->to (@address); $smtp->data (); $smtp Datasend ("to: $x \ n"); # Strict Format$smtp->datasend ("from: $username \ n"); # strict Format$smtp->datasend ("Subject: $subject \ n"); # strict Format$smtp->datasend ("content-type:text/plain;charset=utf-8\n"); # strict Format$smtp->datasend ("content-trensfer-encoding:7bit\n\n"); # strict Format$smtp->datasend ($message); $smtp->dataend (); $smtp->quit ();}
Linux perl--sending mail and monitoring memory