linux perl——发送邮件及监控内存

#!perluse warnings;use strict;use Net::SMTP_auth;#&sendMail;while(1){ my ($sumMem, $freeMem, $rateMem); open MEM, "cat /proc/meminfo |" or die $!; while() { $sumMem = $1 if(/MemTotal:\s+
摘要由CSDN通过智能技术生成
#!perl
use 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();
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值