效果图查看,Action模板级一些其他介绍请查看链接http://blog.csdn.net/jackm1688/article/details/50907607
本次代码属于更新
配置报警媒介目录:AlertScriptsPath=/wls/zabbix/alertscripts (at zabbix_server.conf confiuration file)
脚本位置:/wls/zabbix/alertscripts/zabbixAlertMail.pl
模块目录:/usr/local/perl5/lib/
模块名称:alertTools.pm
权限:所有相关目录及脚本赋777权限
模块名:alertTools.pm
#!/usr/local/perl5/bin/perl5.22.1
package alertTools;
use strict;
use warnings;
#####################################################
# #
# @作者:老孟 #
# @功能:监控邮件报警工具 #
# @日期:2016-06-20 #
# @Copyright (c) 2016 All rights reserved. #
# #
#####################################################
###############加载相关模块##########################
use Encode;
use MIME::Base64;
use MIME::Lite;
use File::Find;
use POSIX qw(strftime);
use File::Temp qw/ tempfile tempdir/;
#####################################################
#构造函数
sub new
{
my $class = shift;
my($logdir,$imgdir) = @_;
my $data = {
'logdir'=> $logdir,
'imgdir' => $imgdir
};
bless($data,$class);
return $data;
}
#告警邮件内容转换成html代码
sub contentToHtml
{
my $this = shift;
my($mailContent,$imge) = @_;
my @htmlContent;
push @htmlContent,"
告警信息
";
my @htmls;
my @string;
foreach (@$mailContent)
{
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, "$_
\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 = "$te[0]<\/b>\:\t $te[1]";
push @htmlContent, "$new<\/td> <\/tr>\n";
}
push @htmlContent, "<\/table>
\n";
push @htmlContent, ' ';
if($imge)
{
push @htmlContent, "1小时内监控图<\/th><\/tr>\n";
foreach my $val (@$imge)
{
push @htmlContent, "$val\" width=\"890\" ><\/td><\/tr>
\n";
}
}
else
{
push @htmlContent, " 没有配置监控图<\/td><\/tr>
\n";
}
push @htmlContent,'
此邮件为运营 zabbix监控平台自动发送,请勿回复!
';
return @htmlContent;
}
#下载相关监控项目的可视图
sub downloadImage
{
my $this = shift;
#用户名,密码,graphitemid,当前时间,当前时间-3600,图片名
my($user,$pass,$itemId,$currentTime,$endTime,$imageName) = @_;
my $path = $this->{'imgdir'}; #图存储路径
my $period = 3600; #1 hours
my $cook = "$this->{'logdir'}/cookie"; #会话缓存
eval
{
#清空昨天的图片
my $dir=$this->{'imgdir'};
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/$imageName.jpeg";
#zabbix用户登录及SESSIONID
my $strcomm = qx(curl -c $cook -b $cook -d "request=&name=$user&password=$pass&autologin=1&enter=Sign+in" http://127.0.0.1/zabbix/index.php);
#GRAPH图下载
$strcomm = qx(curl -b $cook -F "itemid=$itemId" -F "period=$period" -F "stime=$endTime" -F "updateProfile=1" -F "profileIdx=web.item.graph" -F "profileIdx2=$itemId" -F "width=850" -F "curtime=$currentTime" http://127.0.0.1/zabbix/chart.php > $p);
}
#发送邮件功能
sub sendMail
{
my $this = shift;
#收件人邮箱地址,邮件主题,邮件内容,图片位置
my($toConacts,$mailSubJect,$mailContent,$imageId) = @_;
#邮件日志记录
open(STDERR,'>>','./log/message.log');
my $mail = MIME::Lite->new(
From => '[email protected]', #发件人邮箱地址
To => "@$toConacts",
Subject => "$mailSubJect",
Type => 'multipart/mixed',
);
#邮件正以HTML形式展示
$mail->attach(
HTMLCharset => 'utf-8',
Type => 'text/html',
Data => \@$mailContent,
);
if($imageId)
{
foreach my $key (sort keys %$imageId)
{
$mail->attach(
HTMLCharset => 'utf-8',
HTMLEncoding => 'base64',
Type => 'image/jpeg',
Id => "$key",
Path => $imageId->{$key}, #图的位置所在
);
}
}
#格式化输出邮件头部信息
print STDERR '-' x 120,"\n";
$mail->print_header(\*STDERR);
print STDERR '-' x 120,"\n";
$mail->send('smtp','test.test.com.cn',Timeout=>60,Debug=>0); #邮件服务器地址
close(STDERR);
}
1;
#!/usr/local/perl5/bin/perl5.22.1
##################################################
# #
# @作者:老 孟 #
# @功能:zabbix自定义告警邮件脚本, #
# 支持多同时显示多个收件人, #
# 告警邮件自带监控项的可视图。 #
# @时间: 2016-03-08 #
# @更新: 2016-06-16 #
# @Copyright (c) 2016 All rights reserved. #
# #
# #
##################################################
################加载相关模块信息##################
use strict;
use warnings;
use Data::Dumper;
use autodie;
use Encode;
use POSIX qw(strftime);
use File::Temp qw/ tempfile tempdir /;
use lib '/usr/local/perl5/lib/';
use alertTools;
##################################################
=pod
1.用于获取zabbix脚本传入的参数
2.从参数3获取事件ID
3.从参数3获取主机别
4.从参数3获取item名
5.调用告警脚本
=cut
#联系人
my $conacts = shift;
#主题内容
my $subject = shift;
#报警内容
my $messges = shift;
our $dir='/wls/zabbix/zabbix_alert_program';
#创建临时文件
my ($FH, $filename) = tempfile(my $template, SUFFIX => '.txt');
my($id,$host,$key,$con,$dengji,@itemid);
if ($messges)
{
print $FH "$messges\n";
close($FH);
my $content = "$messges";
if($content =~ /Itemid:(\d+)/)
{
push @itemid,"$1";
}
if($content =~ /dengji:(\w+)/)
{
$dengji = "$1";
}
=pod
留以扩展
if($content =~ /\[kt\]\w+\:(.*)\[.*\]\[kn\]/)
{
$key = "$1";
}
if($content =~ /\[hs\]host:(.*)\[hn\]/)
{
$host = "$1";
}
=cut
if($content =~/CN_ST\[(.*)\]EN_CN/)
{
$con = "$1";
}
}
#将邮件内容文本存存储变量中
my @mailContent;
open ($FH,"$filename") or die "Cant open $filename:$!\n";
while(<$FH>)
{
push @mailContent,split /\s+/;
}
my $log="$dir/log";
my $runlog="$dir/log/runningerr.log";
open(my $rg,'>>',"$runlog") or die "cant open $runlog:$!\n";
chomp(my $time = `date`);
#主程序
sub zabbix_main
{
my($conts,$sub,$newcn,$itemid,$warn,$mailContent) = @_;
my $logdir='/wls/zabbix/zabbix_alert_program/log';
my $imgdir='/wls/zabbix/zabbix_alert_program/image';
#实例化对象
my $alertMail = alertTools->new($logdir,$imgdir);
my @messages;
print $rg "Info---> 001 $time $conts $sub\n";
my $file="$dir/log/alert_info.txt";
my @conacts; #存储收件人
push @conacts,"$conts;";
push @conacts,"$newcn";
if($warn eq "High" or $warn eq "Disaster")
{
push @conacts,'[email protected];[email protected]';
}
#留以扩展
#根据主机别名和key项目名来判断取出关联itemid
#use graphid qw(get_graphid);
#my @itemids = get_graphid("$hosts","$keys");
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
{
foreach my $num (@$itemid)
{
#下载相关监控项目的itemId
$alertMail->downloadImage("admin","1234","$num","$curr_time","$str_time","$curr_time$num");
push @ids,"$curr_time$num.jpeg";
push @srcs,"$imgdir/$curr_time$num.jpeg";
$img_path{"$curr_time$num.jpeg"} = "$imgdir/$curr_time$num.jpeg";
}
};
my @test = %img_path;
print $rg "$time : get_graphs method: $@\n" if $@; #捕获错误信息
eval
{
#转换报警内容为html格式
@messages = $alertMail->contentToHtml(\@$mailContent,\@ids);
$alertMail->sendMail(\@conacts,"$sub",\@messages,\%img_path);#传参
};
print $rg "$time : sendmail method: $@\n" if $@; #捕获错误
}
else
{
print $rg "\@itemids is null no config graph info\n";
@messages = $alertMail->contentToHtml("$filename");
$alertMail->sendMail(\@conacts,"$sub",\@messages);#传参
}
}
eval
{
&zabbix_main("$conacts","$subject","$con",\@itemid,"$dengji",\@mailContent);
};
print $rg "$time : zabbix_main method: $@\n" if $@; #捕获错误信息
close($rg)
zabbix web界面上的操作
Itemid:{ITEM.ID1} //监控模板ID,是用来获取graph监控图表的内容
dengji:{TRIGGER.SEVERITY} //告警等级
[kt]Key:{ITEM.KEY1}[kn] //告警KEY值
[hs]host:{HOST.NAME1}[hn] //告警主机别名
CN_ST[test@test.com.cn;]EN_CN //告警邮件收件人邮箱地,
填写多个收件人邮箱的格式为:
CN_ST[test1@test.com.cn;[email protected];[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}
备注:如果你想在监控模板里增加新的采集项目需要在”最新采集值#^”项之前增加采集项目名及宏值。模板里增加了相应的项目名那后台的脚本要去增加相应的匹配规则
你可能感兴趣的:(Perl系统管理,系统运维)