#!/usr/bin/perl
=pod
Author@Laomeng
E_mail:18682093512@163.com
运维工作自动化:
由于这种工作是一种重复性的操作,在人工产生的报表过程有可能出现
数据的错或格式不正确通过(难免不认真),所以为减少工作的重复性和避
免生产过程中数据错误和格式错误而开发了自动邮件程序(这只是主程序一部分)。
=cut
use MIME::Base64;
use Encode;
use MIME::Lite;
use IO::Handle;
use POSIX qw(strftime);
my $ydate= strftime("%Y-%m-%d", localtime(time - 24*3600));#yestarday
my $file='/home/mamc_report/report_file/mamc_core_report$ydate.xls';
my $str="example调用报表[($ydate)]接口";
use DBI;
use Encode;
use File::Find;
use Data::Dumper;
#数据账户信息
my %mysql = (
'dbname' => "example", #数据库名
'host' => "127.0.0.1",
'port' => 3306,
'user' => 'example', #用户
'pass' => 'example', #密码
);
#删除之前的数据文件
unlink './mamc.html' if -e './mamc.html';
#创建新的数据文件
open(FL,'>','/home/mamc_report/mamc.html') or die "Can't write file:$!\n";
my @data;
my $database ="DBI:mysql:$mysql{'dbname'}";
my $dbh = DBI->connect($database,$mysql{'user'},$mysql{'pass'}) or die("Could not make connection to database:$DBI::errstr");
$dbh->do("SET character_set_client='utf8'"); #连接客户端编码
$dbh->do("SET character_set_connection='utf8'"); #连接编程
$dbh->do("SET character_set_results='utf8'"); #查询结果编码
#作为查询条件和小标签
my @api=("TEST1","TEST2","TEST3","TEST4","TEST5","TEST5");
print FL "<html>\n";
print FL '<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>',"\n";
print FL "<body>\n";
print FL '<p>各位领导、同事好:</p></br>',"\n";
print FL '<p>  以下是example接口调用报表,A&B的TOP5统计图见附件</p></br>',"\n";
print FL "<table border='1' style=\"font-family:'宋体'; font-size:12px;\">\n";
my $num;
foreach my $name (@api)
{
$num++;
print FL "<td>$num => $name<\/td>\n";
print FL "<tr>\n";
print FL "<th style=\"width:20%\" align=\"left\">接口功能说明<\/th>\n";
print FL "<th style=\"width:auto\" align=\"left\" >接口<\/th>\n";
print FL "<th style=\"width:160px\" align=\"left\">调用总次数<\/th>\n";
print FL "<th style=\"width:260px\" align=\"left\">平均耗时(ms)<\/th>\n";
print FL "<th style=\"width:auto\" align=\"left\">调用方<\/th>\n";
print FL "</tr>\n";
my $sth = $dbh->prepare("SELECT apimap AS \"接口功能说明\",sapiname AS \"接口\",scount AS \"调用总次数\",stime AS \"平均耗时(ms)\",jcallname AS \"调用方\" FROM v_join_result WHERE sdate=\"$ydate\" AND sname=\"$name\" ORDER BY scount DESC; ");
$sth->execute() or die "无法执行SQL语句:$dbh->errstr";
while (@data = $sth->fetchrow_array())
{
print FL"<tr>\n";
print FL "<td>" . "$data[0]" . "<\/td>\n";
print FL "<td>" . "$data[1]" . "<\/td>\n";
print FL "<td>" . "$data[2]" . "<\/td>\n";
print FL "<td>" . "$data[3]" . "<\/td>\n";
print FL "<td>" . "$data[4]" . "<\/td>\n";
print FL "<\/tr>\n";
}
}
print FL "</table>\n";
print FL "</head>\n";
print FL "</html>\n";
=pod
读取邮件报表内容,由于perl5.10.1版本的bug
导致open函数或其模块(读取文件)输出的结果
不完整。由此改用linux带的cat命令打开文件
内容赋值给@str数组。建议在perl新版本5.20
以后的版本
#use Tie::File;
#my @str = Slurp::to_array("./mamc.html");
#tie @str, 'Tie::File',"./mamc.html";
=cut
#打开报表数据内容
my @str = qx(cat /home/mamc_report/mamc.html);
my $mail = MIME::Lite->new(
From => 'example@example.com.cn', #发邮件人地址
To => 'exampleli@example.com.cn', #收件人地址
Cc => 'example1@example.cn;example2@example.com.cn', #抄送邮件人地址
Subject => "$str", #邮件主题
Type => 'multipart/mixed',
);
#添加邮件附件
$mail->attach(
Type => 'image/svg', #文件类型
Id => 'line.svg',
Path => './line.svg', #文件名
);
#报表数据内容
$mail->attach(
HTMLCharset => 'utf-8', #编码设置
Type => 'text/html', #文件类型
Data => \@str #邮件正文
);
#添邮件附件
$mail->attach(
HTMLCharset => 'utf-8',
TextCharset => 'utf-8',
TextEncoding => 'base64',
HTMLEncoding => 'base64',
Type => 'auto',
Path => "$file", #附件文件
Disposition => 'attachment'
);
#SMTP服务器地址或域名,Timeout设置超时时间, Debug参数0关闭,1开启
$mail->send('smtp','example.example.com.cn',Timeout=>60,Debug=>1);
自动邮件程序(perl)
最新推荐文章于 2020-04-08 21:16:00 发布