Zabbix3.0 自动邮件报障

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Zabbix3.0 自动邮件报障相关的知识,希望对你有一定的参考价值。

Zabbix3.0以后,自带的邮件报警支持SSL验证了,

但是仍然没有发送复数个邮箱以及CC,BCC的功能,

技术分享

因此,我们还是得用别的方法来实现邮件报障。

实现方法有很多种,我用的是phpmailer。

http://files.cnblogs.com/files/justbio/PHPMailer-master.zip

这个是比较新的PHPmailer,下载后解压到文件夹。

cd /usr/lib/zabbix/alertscripts/
unzip PHPMailer-master.zip

编写一个shell文件,是用来给zabbix调用的。

cd /usr/lib/zabbix/alertscripts/
vi sendmail.sh  
技术分享
#!/bin/sh
nohup /usr/lib/zabbix/alertscripts/sendmessage_php.sh "$1" "$2" "$3" >/dev/null 2>&1 &
View Code

 

编写一个php文件,就是上面的sendmessage_php.sh

vi sendmessage_php.sh
技术分享
#!/usr/bin/php

<?php

mb_language("uni");

mb_internal_encoding("UTF-8");

require(‘/usr/lib/zabbix/alertscripts/PHPMailer-master/PHPMailerAutoload.php‘);

 

$MAIL_LOG_PATH = ‘/var/log/zabbix-phpmailer‘; //日志

$MAIL_FROM = "[email protected]"; //邮件的from信息

$MAIL_FROMNAME = "Zabbixtest 障害通知"; //邮件的from信息

$MAIL_SMTP_HOST = ‘smtp.partner.outlook.cn:587‘; //邮箱的host信息(ssl)

$MAIL_SMTP_USER = ‘ [email protected] ‘; //用户名

$MAIL_SMTP_PASS = ‘xxxxxxx‘; //密码

$retry_count = 3; //重试3次

 

//邮件的TO,CC从文件中取得,文件名为第一个参数

$MAIL_LIST_NM = $argv[1];

$MAIL_LIST = ‘/usr/lib/zabbix/alertscripts/addresslist/‘.$MAIL_LIST_NM;

$MAIL_ADRESS = file_get_contents($MAIL_LIST);

$ADRESS_ARRAY = array();

$ADRESS_ARRAY = explode(‘##‘,$MAIL_ADRESS);

$MAIL_TO = $ADRESS_ARRAY[0];

$MAIL_CC = $ADRESS_ARRAY[1];

 

//邮件的标题和内容为第二,第三个参数

$MAIL_SUBJECT = $argv[2];

$MAIL_MESSAGE = $argv[3];

 

$mailer = new PHPMailer();

$mailer->CharSet = ‘UTF-8‘;

$mailer->Encoding = ‘base64‘;

$mailer->IsSMTP();

$mailer->SMTPSecure=‘tls‘;

$mailer->Host = $MAIL_SMTP_HOST;

$mailer->SMTPAuth = true;

$mailer->Username = $MAIL_SMTP_USER;

$mailer->Password = $MAIL_SMTP_PASS;

$mailer->From = $MAIL_FROM;

 

//to,cc,bcc edit

$TO_COUNT = array();

$CC_COUNT = array();

$TO_COUNT = explode(‘,‘,$MAIL_TO);

$CC_COUNT = explode(‘,‘,$MAIL_CC);

$key;

 

while ($key = current($TO_COUNT)) {

$mailer->AddAddress($key);

next($TO_COUNT);

}

 

while ($key = current($CC_COUNT)) {

$mailer->AddCC($key);

next($CC_COUNT);

}

 

$mailer->FromName = mb_convert_encoding($MAIL_FROMNAME,"UTF-8","UTF-8");

$mailer->Subject = $MAIL_SUBJECT;

$mailer->Body = mb_convert_encoding($MAIL_MESSAGE,"UTF-8","UTF-8");

 

/* logging data set */

$queuing_time = explode(‘ ‘, microtime());

$queuing_id = date(‘YmdHis‘, $queuing_time[1]).‘ ‘.$queuing_time[0];

$log_sub_dir = $MAIL_LOG_PATH;

$log_filename = $log_sub_dir .‘/‘.substr($queuing_id, 0, 8).‘.txt‘;

$log_message = "From: $MAIL_FROM\\n";

$log_message .= "To: $MAIL_TO\\n";

$log_message .= "Subject: $MAIL_SUBJECT\\n";

$log_message .= "Message: $MAIL_MESSAGE\\n";

$retry = 0;

 

/* mail info */

$mail_info = ‘ to send mail to ‘;

$mail_info .= $MAIL_LIST_NM;

$mail_info .=‘(‘;

$mail_info .= ‘Subject:‘.$MAIL_SUBJECT;

$mail_info .= ‘ To:‘.str_replace("\\n","",$MAIL_TO);

$mail_info .= ‘ Cc:‘.str_replace("\\n","",$MAIL_CC);

$mail_info .= ‘ Message:‘.str_replace("\\n","←",$MAIL_MESSAGE);

$mail_info .=‘)‘;

 

//如果失败,重试3次

do{

 

try {

$send_result=$mailer->Send();

} catch (Exception $e) {

Logging($log_sub_dir, $log_filename, $queuing_id.‘[‘.$retry.‘]‘, ‘Exception:‘.$e->getMessage()."\\n");

$send_result=false;

}

 

if(!$send_result){

Logging($log_sub_dir, $log_filename, $queuing_id.‘[‘.$retry.‘]‘, ‘Result: failed ‘.$mailer->ErrorInfo."\\n".$log_message);

print ‘failed: ‘ . $mailer->ErrorInfo . "\\n";

}else{

Logging($log_sub_dir, $log_filename, $queuing_id.‘[‘.$retry.‘]‘, "Result: success\\n".$log_message);

print ‘success‘ . "\\n";

break;

}

$retry++;

sleep(10);

$log_message = ‘‘;

}while ($retry < $retry_count);

 

if($retry==$retry_count){

OutputResult($log_sub_dir,$log_sub_dir.‘/mailresult.log‘, ‘outlook failed‘.$mail_info);

}else{

OutputResult($log_sub_dir,$log_sub_dir.‘/mailresult.log‘, ‘outlook success‘.$mail_info);

}

 

/* logging */

function Logging($dir, $filename, $id, $message) {

if (!is_dir($dir)) { mkdir($dir, 0755); }

$logging_time = explode(‘ ‘, microtime());

$logging_date = date(‘YmdHis‘, $logging_time[1]).‘ ‘.$logging_time[0];

$log = ‘----- Logging Date: ‘.$logging_date."\\n";

$log.= ‘Queuing ID: ‘.$id."\\n";

$log.= $message."\\n";

$fp = fopen($filename, ‘a‘);

fwrite($fp, $log);

fclose($fp);

}

 

function OutputResult($dir, $filename, $info) {

if (!is_dir($dir)) { mkdir($dir, 0755); }

$logging_time = explode(‘ ‘, microtime());

$logging_date = date(‘YmdHis‘, $logging_time[1]);

$log = $logging_date.‘:‘.$info."\\n";

$fp = fopen($filename, ‘a‘);

fwrite($fp, $log);

fclose($fp);

}

?>
View Code

 

编辑地址簿文件,不同的地址用逗号隔开,to和cc用##隔开

mkdir /usr/lib/zabbix/alertscripts/addresslist
vi test
[email protected]##[email protected],[email protected]

 

回到zabbix网页

Administrator -> media type -> create media type

如下设置

技术分享

*在zabbix3.0以前,PARAMETER是固定的,不用设置也不能更改。3.0默认值变成空值,需要自己设置。

 

Administrator -> user -> create user

名字我这里取testuser,在media标签中点add,如下填写

技术分享

Send to 填的是刚才建的地址簿的文件名

Pemmision标签当然是选择zabbixadministrators

 

Configuration -> Action ->create action

名字我这里取testaction,在action和conditions标签按实际需求填写,

Operations中点New,如下填写

技术分享

*user我这里涂掉了,应该填的是刚才新建的testuser

 

这样,自动邮件就配置完成了,让我们测试一下脚本

./sendmail.sh test testsubject testmessage

第一个参数是地址簿的文件名,第二个参数是标题,第三个参数是正文

地址簿test里的所有人都会收到标题为testsubject,正文为testmessage的邮件。

以上是关于Zabbix3.0 自动邮件报障的主要内容,如果未能解决你的问题,请参考以下文章

zabbix3.0.9邮件告警

zabbix3.0.4发邮件小坑一个

Zabbix2.0和Zabbix3.0设置邮件报警教程

zabbix3.0.4设置邮件告警

zabbix3.0微信邮件报警

zabbix3.0.4安装部署文档--邮件报警