如果nagios 發現異常要發送警告信給管理者
本機必須要有postfix或sendmail 之類的MTA

如果因為資安考量或其他因素不打算安裝
或是跑在docker 上,預設也沒有MTA,就無法發送信件

幸好nagios 非常地有彈性
可以改觸發其他scripts

以下範例改用 phpmailer 發送


編輯 /phpmailer/example/mail.php
#!/usr/bin/php

<?php

/**
 * This example shows making an SMTP connection with authentication.
 */

//Import the PHPMailer class into the global namespace
use PHPMailer\PHPMailer\PHPMailer;
use PHPMailer\PHPMailer\SMTP;

//SMTP needs accurate times, and the PHP time zone MUST be set
//This should be done in your php.ini, but this is how to do it if you don't have access to that
date_default_timezone_set('Asia/Taipei');

require '/phpmailer/vendor/autoload.php';

content = file ('php://stdin');
text = implode (content);
html = nl2br(text);
subject = argv[1];
mailto = argv[2];

//Create a new PHPMailer instance
mail = new PHPMailer();
//Tell PHPMailer to use SMTP
mail->isSMTP();
//Enable SMTP debugging
//SMTP::DEBUG_OFF = off (for production use)
//SMTP::DEBUG_CLIENT = client messages
//SMTP::DEBUG_SERVER = client and server messages
mail->SMTPDebug = SMTP::DEBUG_OFF;
//Set the hostname of the mail server
mail->Host = 'SMTP伺服器';
//Set the SMTP port number - likely to be 25, 465 or 587
mail->Port = 25;
//Whether to use SMTP authentication
mail->SMTPAuth = true;
//Username to use for SMTP authentication
mail->Username = '寄件者帳號';
//Password to use for SMTP authentication
mail->Password = '寄件者密碼';
//Set who the message is to be sent from
mail->setFrom('寄件者email', '寄件者顯示名稱');
//Set an alternative reply-to address
mail->addReplyTo('寄件者email', '寄件者顯示名稱');
//Set who the message is to be sent to
mail->addAddress(mailto, '收件者顯示名稱');
//Set the subject line
mail->Subject = subject;
//Read an HTML message body from an external file, convert referenced images to embedded,
//convert HTML into a basic plain-text alternative body
mail->msgHTML(html, __DIR__);
//Replace the plain text body with one created manually
mail->AltBody = text;
//Attach an image file
//mail->addAttachment('images/phpmailer_mini.png');

//send the message, check for errors
if (!mail->send()) {
    echo 'Mailer Error: ' . mail->ErrorInfo;
} else {
    echo 'Message sent!';
}

編輯
command.cfg
找到
/usr/bin/mail -s "** NOTIFICATIONTYPE Host Alert: HOSTNAME is HOSTSTATE **" CONTACTEMAIL
修改成
/phpmailer/example/mail.php "** NOTIFICATIONTYPE Host Alert: HOSTNAME is HOSTSTATE **" CONTACTEMAIL

重啟 nagios 

文章標籤
全站熱搜
創作者介紹
創作者 helloworld 的頭像
helloworld

Hello World

helloworld 發表在 痞客邦 留言(0) 人氣(4)