zabbix版本:2.2.5
开发语言:perl
系统环境:redhat 6.7
一.主要实现功能:1.邮件报警内容个性化定制
2.收件人同时显示多个联系人
3.邮件里增加报警项的监控图(1小时)
二,需要注意的两个地方:
1.zabbix_server.conf开启自己告警脚本路径
AlertScriptsPath=/wls/zabbix/alertscripts
2.权限问题
赋予AlertScriptsPath=/wls/zabbix/alertscripts目录下的文件777权限
chmod 777 -R AlertScriptsPath=/wls/zabbix/alertscripts
三,开发所使用到的模块:
1.DBI 数据连接模块
2.MIME::Lite 邮件模块
3.MIME::Base64 编码模块
4.get_html.pm 自定义模块用来处理邮件内容为HTML格式
5.graphid.pm 自定义模块用来获取itemid号
6.get_graph.pm 自定义模块用来下载监控图
7.zabbix_mail.pm 自定义模块用来发送邮件
四,自定义模块程序代码
#####get_html.pm####
#!/usr/local/perl5/bin/perl
package get_html;
use strict;
use warnings;
require Exporter;
our @ISA = qw(Exporter);
our @EXPORT = qw(fetch_html);
=pod 处理邮件报警内容为html文件 主要接收两个参数 1st.file读取报警文件的内容 2th.image引用一个数组(存储图上的名称) =cut
sub fetch_html {
my($file,$imge) = @_;
open(my $HTM,'>/wls/zabbix/zabbix_alert_mail/progarm/log/mesg.html') or die "Cant open mesg.html:$!\n";
print $HTM <<'HTML';
<html>
<head><meta http-equiv="Content-Type" content="text/html; charset=utf-8"/></haed> <body> <style>table{font-size:14px;}</style>
<div align="center">
<font color="#525252">
<table border='0' style="border:5px solid #F2F2F2;" cellspacing="2" cellpadding="2" width="900" >
<tr bgcolor="#D1D1D1"><th align="left" style="font-size:23px;">告警信息</marquee></th></tr> HTML my (@htmls,@string); open(my $FILE,'<',"$file") or die "Can't open '$file':$!\n"; while(<$FILE>) { push @htmls,"$1" if(/(告警项名.*)/);
push @htmls,"$1" if(/(告警主机.*)/);
push @htmls,"$1" if(/(告警IP.*)/);
push @htmls,"$1" if(/(告警项Key值.*)/);
push @htmls,"$1" if(/(告警触发规则.*)/);
push @htmls,"$1" if(/(告警时间.*)/);
push @htmls,"$1" if(/(告警等级.*)/);
if(/(问题描述)/ .. eof)
{
chomp;
push @string, "$_ <br \/>\n";
}
push @htmls,"$1" if(/(告警值.*)/);
push @htmls,"$1" if(/(最新采集值.*)/);
}
my $arr = "@string";
push @htmls,"$arr";
for my $val (@htmls)
{
my @te = split(/\#\^/,$val);
my $new = "<b>$te[0]<\/b>:\t $te[1]";
print $HTM "<tr><td align=\"left\" style=\"font-family:'微软雅黑'; size=5\"> $new <\/td> <\/tr>\n";
}
print $HTM "<\/table> <br \/>\n";
print $HTM <<'HTML2';
<table style="border:5px solid #F2F2F2;" width="900">
HTML2
if($imge)
{
print $HTM "<tr><th>1小时内监控图<\/th><\/tr>\n";
foreach my $val (@$imge)
{
print $HTM "<tr><td><img src=\"$val\" width=\"890\" ><\/td><\/tr><br \/>\n";
}
}
else
{
print $HTM "<tr><td align=\"center\" style=\"font-family:'微软雅黑'; size=5\"> 没有配置监控图<\/td><\/tr><br \/>\n";
}
print $HTM <<'HTML3';
</table> </br><p>此邮件为运营 zabbix监控平台自动发送,请勿回复!</p> </div>
</body> </html>
HTML3
close($HTM);
close($FILE);
my @mfile = qx(cat /wls/zabbix/zabbix_alert_mail/progarm/log/mesg.html);
return @mfile;
}
1;
####get_graph.pm####
#!/usr/local/perl5/bin/perl
=pod 用于下载一小内曲线图 $gid #graphid $curr #当前时间 $stri #当前时间-360 =cut
package get_graph;
use strict;
use warnings;
use POSIX qw(strftime);
require Exporter;
use File::Find;
use Encode qw(encode decode);
our @ISA = qw(Exporter);
our @EXPORT = qw(get_graphs);
=pod 下载graph曲线图1小内的报表 声明声明两个变量用于存储graphid and graph图名称 登录zabbix生成会话和cookie用于下载graph验证信息 将图片保存为jpeg格式 需要传三个参数: 参数1.传入itemid或graphid 参数2.传入当前时间 参数3.传入1时以的前时间 图的命名是以当前时间+itemid或graphid.jpeg的格式 =cut
sub get_graphs {
#接收相关参数
#graphid,当前时间,当前时间-3600,图片名
my($gid,$curr,$stri,$pcn) = @_;
my $path = '/wls/zabbix/zabbix_alert_mail/progarm/image'; #图存储路径
my $period = 3600; # 1 hours #一个小时范围
my $login = 'xxxxxx'; # Zabbix Web User
my $pass = '123456'; # Zabbix Web User Password
my $cook = "/wls/zabbix/zabbix_alert_mail/progarm/log/cookie";
eval
{
#清空昨天的图片
my $dir='/wls/zabbix/zabbix_alert_mail/progarm/image/';
sub wanted {
my $time = strftime("%Y%m%d",localtime(time - 86400));
unlink $File::Find::name if( $File::Find::name =~ /$time/);
}
find(\&wanted,$dir);
};
#Get all graphs by using curl
my $p = "$path/$pcn.jpeg";
#zabbix用户登录及SESSIONID
my $strcomm = `curl -c $cook -b $cook -d "request=&name=$login&password=$pass&autologin=1&enter=Sign+in" http://127.0.0.1/zabbix/index.php`;
#GRAPH图下载
$strcomm = `curl -b $cook -F "itemid=$gid" -F "period=$period" -F "stime=$stri" -F "updateProfile=1" -F "profileIdx=web.item.graph" -F "profileIdx2=$gid" -F "width=850" -F "curtime=$curr" http://127.0.0.1/zabbix/chart.php > $p`;
}
1;
####zabbix_mail.pm###
#!/usr/local/perl5/bin/perl
package zabbix_mail;
use strict;
use warnings;
use MIME::Base64;
use MIME::Lite;
use Encode;
require Exporter;
our @ISA = qw(Exporter);
our @EXPORT = qw(sendmail);
sub sendmail {
#发件人邮箱地址,邮件主题,邮件内容,图片位置
my($to_contacts,$str,$me,$image_id) = @_;
open(STDERR,'>>','/wls/zabbix/zabbix_alert_mail/progarm/log/message.log');
#open(my $test, '>>','/wls/zabbix/zabbix_alert_mail/progarm/log/test.log');
my $mail = MIME::Lite->new(
From => '[email protected]', #发件人邮箱地址
To => "@$to_contacts", #收件人邮箱地址
Subject => "$str", #邮件主题
Type => 'multipart/mixed',
);
#邮件正以HTML形式展示
$mail->attach(
HTMLCharset => 'utf-8',
Type => 'text/html',
Data => \@$me, #内容
);
if($image_id)
{
foreach my $val (sort keys %$image_id)
{
#print $test " key: $val\n";
$mail->attach(
HTMLCharset => 'utf-8',
HTMLEncoding => 'base64',
Type => 'image/jpeg',
Id => "$val",
Path => $image_id->{$val},
);
#print $test "value: ", $image_id->{$val},"\n";
}
}
$mail->print(\*STDERR);
$mail->send('smtp','xxx.xxxx.com.cn',Timeout=>60,Debug=>0);
close(STDERR);
}
1;
五,程序代码
####mail_alert.pl####
#!/usr/local/perl5/bin/perl5.22.1
#author@laomeng
#date@2016-04-2 修定
#version:2.0
#email:[email protected]
use strict;
use warnings;
use Data::Dumper;
use autodie;
use Encode;
use POSIX qw(strftime);
unshift @INC,"/usr/local/perl5/lib/";
=pod 1.接收zabbix传入的三个参数:联系人,邮件主题,报警内容 2.get_html模块用于处理报警内容转换成HMTL代码 3.get_graph模块用于接接收itemdid根据ID下载grap监控图 4.zabbix_mail模块主要功能是发送邮件 =cut
#联系人,主题,报警内容
my($conacts,$subject,$messges) = @_;
open (my $fl,'+>','/wls/zabbix/zabbix_alert_mail/progarm/log/msg.txt');
my($id,$host,$key,$con,$dengji,@itemid);
if ($messges)
{
print $fl "$messges\n";
my $content = "$messges";
push @itemid,"$1" if($content =~ /Itemid:(\d+)/);
$dengji = "$1" if($content =~ /dengji:(\w+)/);
=pod 留以扩展 $key = "$1" if($content =~ /\[kt\]\w+\:(.*)\[.*\]\[kn\]/); $host = "$1" if($content =~ /\[hs\]host:(.*)\[hn\]/); =cut
$con = "$1" if($content =~/CN_ST\[(.*)\]EN_CN/);
}
close($fl);
my $log='/wls/zabbix/zabbix_alert_mail/progarm/log';
my $runlog='/wls/zabbix/zabbix_alert_mail/progarm/log/runningerr.log';
open(my $rg,'>>',"$runlog") or die "cant open $runlog:$!\n";
chomp(my $time = `date`);
#主程序
sub zabbix_main {
my($conts,$sub,$sub,$newcn,$itemid,$warn) = @_;
print $rg "debug---> 001 $time $conts $sub\n"; #打印时间,联系人,邮件主题信息
my $file='/wls/zabbix/zabbix_alert_mail/progarm/log/alert_info.txt';
my @conacts; #存储收件人
push @conacts,"$conts;";
push @conacts,"$newcn";
if($warn eq "High" or $warn eq "Disaster")
{ #邮件报警等级判断,如果报警等级为High" or "Disaster"将邮件发送给监控岗
push @conacts,'[email protected];[email protected]';
}
=pod 扩展 #根据主机别名和key项目名来判断取出关联itemid #use graphid qw(get_graphid); #my @itemids = get_graphid("$hosts","$keys"); =cut
if(@$itemid)
{
my ($graphid,$host,$item,$key,$img_src);
my $curr_time = strftime("%Y%m%d%H%M%S",localtime(time)); #当前时间
my $str_time = strftime("%Y%m%d%H%M%S",localtime(time - 3600));
my (@ids,@srcs,%img_path);
eval
{
use get_graph qw(get_graphs);
foreach my $num (@$itemid)
{
get_graphs("$num","$curr_time","$str_time","$curr_time$num");
push @ids,"$curr_time$num.jpeg";
push @srcs,"/wls/zabbix/zabbix_alert_mail/progarm/image/$curr_time$num.jpeg";
$img_path{"$curr_time$num.jpeg"} = "/wls/zabbix/zabbix_alert_mail/progarm/image/$curr_time$num.jpeg";
}
};
my @test = %img_path;
print $rg "$time : get_graphs method: $@\n" if $@; #捕获错误信息
eval
{
use get_html qw(fetch_html);
fetch_html("/wls/zabbix/zabbix_alert_mail/progarm/log/msg.txt",\@ids);
my @message = qx(cat /wls/zabbix/zabbix_alert_mail/progarm/log/mesg.html);
use zabbix_mail qw(sendmail);
sendmail(\@conacts,"$sub",\@message,\%img_path);#传参
};
print $rg "$time : sendmail method: $@\n" if $@; #捕获错误
}
else
{
print $rg "\@itemids is null no config graph info\n";
fetch_html("/wls/zabbix/zabbix_alert_mail/progarm/log/msg.txt");
my @messages = qx(cat /wls/zabbix/zabbix_alert_mail/progarm/log/mesg.html);
sendmail(\@conacts,"$sub",\@messages);#传参
}
}
if($subject && $con && @itemid)
{
eval
{
&zabbix_main("$conacts","$subject","$con",\@itemid,"$dengji");
};
}
else
{
print $rg "$time zabbix_main()参数为空或参数不全,请检查参数\n";
}
print $rg "$time : zabbix_main method: $@\n" if $@; #捕获错误信息
close($rg)
六,监控模板内容:
Itemid:{ITEM.ID1}
dengji:{TRIGGER.SEVERITY}
[kt]Key:{ITEM.KEY1}[kn]
[hs]host:{HOST.NAME1}[hn]
CN_ST[xxxx@xxxan.com.cn;[email protected];]EN_CN
告警项名#^{ITEM.NAME1}
告警主机#^{HOST.NAME1}
告警项Key值#^{ITEM.KEY1}
告警触发规则#^{TRIGGER.EXPRESSION}
告警时间#^{EVENT.DATE} {EVENT.TIME}
告警等级#^{TRIGGER.SEVERITY}
告警值#^{ITEM.VALUE1}
最新采集值#^{{HOSTNAME}:{TRIGGER.KEY}.last(0)}
问题描述#^{TRIGGER.DESCRIPTION}