it运维开发教程,运维管理,因为微不足道,所以值得关注!域名来源:sos 救援 idc 数据中心 所以sos+idc=sosidc.com

nagios教程21-nagios自定义php脚本报警

发布:小小编辑2016-3-4 15:44分类: nagios教程 标签: nagios教程

使用shell调用系统smtp发送邮件,很容易被当成垃圾邮件

改用php脚本发送邮件效果会好很多。

vim commands.cfg

define command{

        command_name    notify-service-by-email
        command_line    /usr/bin/printf "%b" "***** Nagios *****\n\nNotification Type: $NOTIFICATIONTYPE$\n\nService:
$SERVICEDESC$\nHost: $HOSTALIAS$\nAddress: $HOSTADDRESS$\nState: $SERVICESTATE$\n\nDate/Time: $LONGDATETIME$\n\nAdditi
onal Info:\n\n$SERVICEOUTPUT$" | /opt/php/bin/php mail.php "*| $LONGDATETIME$| $HOSTALIAS$| $SERVICEDESC
$ is $SERVICESTATE$*" $CONTACTEMAIL$  -- -f jtest@test.com

}


调用mail.php脚本发送邮件


jd@db2:~$ more mail.php
    <?php  
      
    // Pear Mail ??  
require_once('Mail.php');  
require_once('Mail/mime.php');  
require_once('Net/SMTP.php');  
 
      
    $smtpinfo = array();  
    $smtpinfo["host"] = "smtp.exmail.qq.com";//SMTP???  
    $smtpinfo["port"] = "25"; //SMTP?????  
    $smtpinfo["username"] = "jiankong@test.com"; //?????  
    $smtpinfo["password"] = "Jiankong";//???????  
    $smtpinfo["timeout"] = 10;//??????,?  
    $smtpinfo["auth"] = true;//????  
    $smtpinfo["debug"] = true;//????  
      
    $mailAddr = array('18500255555@wo.cn,13810319888@139.com,13671033333@139.com');  
      
    $from = "Name <jiankong@test.com>";  
      
    $to = implode(',',$mailAddr);  
     $c=$_SERVER["argv"][1];
    $subject = "$c";  
      
    $content = "$c";  
      
    $contentType = "text; charset=utf-8";  
      
    //???? Linux: \n  Windows: \r\n  
    $crlf = "\n";  
    $mime = new Mail_mime($crlf);  
    $mime->setHTMLBody($content);  
      
    $param['text_charset'] = 'utf-8';  
    $param['html_charset'] = 'utf-8';  
    $param['head_charset'] = 'utf-8';  
    $body = $mime->get($param);  
      
    $headers = array();  
    $headers["From"] = $from;  
    $headers["To"] = $to;  
    $headers["Subject"] = $subject;  
    $headers["Content-Type"] = $contentType;  
    $headers = $mime->headers($headers);  
      
    $smtp =& Mail::factory("smtp", $smtpinfo);  
      
      
    $mail = $smtp->send($mailAddr, $headers, $body);  
    $smtp->disconnect();  
      
    if (PEAR::isError($mail)) {  
        echo 'Email sending failed: ' . $mail->getMessage()."\n";  
    }  
    else{  
        echo "success!\n";  
    } 


温馨提示如有转载或引用以上内容之必要,敬请将本文链接作为出处标注,谢谢合作!

已有 0/17508 人参与

发表评论:

欢迎使用手机扫描访问本站,还可以关注微信哦~