linux perl——發送郵件及監控記憶體

來源:互聯網
上載者:User

標籤:

#!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 = $1 if(/MemTotal:\s+(\d+)\s+kB/);$freeMem += $1 if(/MemFree:\s+(\d+)\s+kB/);$freeMem += $1 if(/Buffers:\s+(\d+)\s+kB/);$freeMem += $1 if(/Cached:\s+(\d+)\s+kB/);}$rateMem = sprintf("%d", $freeMem / $sumMem * 100);if($rateMem <= 10){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 if(/^$/);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 = '25'; my $username = '***@genedenovo.com';my $passowrd = '***'; my @to = ('***', '***');my $suffix = "\@genedenovo.com";my $subject = "請注意19伺服器記憶體使用量已超過90%!";my $message = "Details:$content來自$username的監控程式";my $smtp = Net::SMTP_auth->new($smtpHost, Timeout => 30) or die "Error:串連到$smtpHost失敗!";$smtp->auth('LOGIN', $username, $passowrd) or die("Error:認證失敗!");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——發送郵件及監控記憶體

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.