由于服务器本身没有在公网上配置域名,我们也不需要用这个邮箱来收发邮件,仅仅是在出问题的时候nagios能够发送报警邮件。
因此很简单几步就能做到。
首先安装postfix以及mailutils辅助工具,因为简单,也不用mysql
sudo apt install postfix mailutils
如果ufw或iptables防火墙开启的话,允许25端口
postfix的配置文件是/etc/postfix/main.cf,你可以修改一些配置,更改主机名,域名什么的,这个另外的技术文档有记录。但是为了简易,可以什么都不改。对,就是什么都不改,postfix仍然可以用!用来发个告警邮件也足够了。
测试一下,看看状态是Active的。
root@Nagios_THN:~# systemctl status postfix
postfix.service - Postfix Mail Transport Agent
Loaded: loaded (/lib/systemd/system/postfix.service; enabled; vendor preset:
Active: active (exited) since Mon 2019-01-14 23:59:24 GMT; 19min ago
Process: 8387 ExecStart=/bin/true (code=exited, status=0/SUCCESS)
Main PID: 8387 (code=exited, status=0/SUCCESS)
Jan 14 23:59:24 Nagios_THN systemd[1]: Starting Postfix Mail Transport Agent...
Jan 14 23:59:24 Nagios_THN systemd[1]: Started Postfix Mail Transport Agent.
lines 1-8/8 (END)
测试下发个邮件
root@Nagios_THN:~# mail a.xing@cxxxxx.co.uk
Cc:
Subject: This is a test mail.
Test email
Ctrl+d 发送出去,那边立刻收到了:
屏幕快照 2019-01-15 00.20.58.png
那么再看下Nagios告警邮件是否收的到。
检查下Nagios里的配置:
contacts.cfg里的配置
#定义联系人模板
define contact {
name ax-contact
service_notification_period 24x7
host_notification_period 24x7
service_notification_options w,u,c,r,f,s
host_notification_options d,u,r,f,s
service_notification_commands ax-notify-service-by-email
host_notification_commands ax-notify-host-by-email
register 0
}
#定义联系人
define contact {
contact_name axing
use ax-contact
alias A Xing
email a.xing@cxxxxxx.co.uk
host_notification_options d,u,r,s
service_notification_options w,u,c,r,s
}
#定义联系人组
define contactgroup {
contactgroup_name network
alias Network Team
members axing
}
在commands.cfg文件里定义发邮件的动作
define command{
command_name ax-notify-host-by-email
command_line /usr/bin/printf "%b" "Subject: $NOTIFICATIONTYPE$ Host Alert: $HOSTNAME$ is $HOSTSTATE$\n\n***** Nagios *****\n\nNotification Type: $NOTIFICATIONTYPE$\nHost: $HOSTALIAS$\nAddress: $HOSTADDRESS$\nState: $HOSTSTATE$\nDuration: $HOSTDURATION$\n\nInfo: $HOSTOUTPUT$\n\nDate/Time: $LONGDATETIME$\n\n&" | /usr/sbin/sendmail -vt $CONTACTEMAIL$
}
define command{
command_name ax-notify-service-by-email
command_line /usr/bin/printf "%b" "Subject: $NOTIFICATIONTYPE$ Service Alert: $HOSTALIAS$/$SERVICEDESC$ is $SERVICESTATE$\n\n***** Nagios *****\n\nNotification Type: $NOTIFICATIONTYPE$\n\nService: $SERVICEDESC$\nHost: $HOSTALIAS$\nAddress: $HOSTADDRESS$\nState: $SERVICESTATE$\n\nDate/Time: $LONGDATETIME$\n\nAdditional Info:\n\n$SERVICEOUTPUT$\n\n" | /usr/sbin/sendmail -vt $CONTACTEMAIL$
}
拿我定义的一台主机为例,定义了电池容量的告警参数:
define host {
use generic-switch ; Inherit default values from a template
host_name ol01.com.lon ; The name we're giving to this switch
alias MA5800 Comber ; A longer name associated with the switch
address 10.XXX.XXX.2 ; IP address of the switch
hostgroups olts,MA5800X7 ; Host groups this switch is associated with
parents vlan69.com
_BRC_WARN_LEVEL 95
_BRC_CRIT_LEVEL 65
icon_image olt41.jpg
# statusmap_image router.gd2
}
在service.cfg(或者其他什么配置文件里)定义了查询电池容量的告警,这条命令是说,通过调用ax-check_snmp这个命令, -C 指定密码, -I 要查询的参数起个名字, -w 到达阈值发警告消息 -c 到达阈值发cirtcal消息 -o 要查询的OID
define service {
use ax-service
hostgroup_name MA5800X7
# host_name ol01.xxx.lon
service_description MA5800X7 Battery Capacity-1
check_command ax-check_snmp!-C public -l olt-BRC -w $_HOSTBRC_WARN_LEVEL$: -c $_HOSTBRC_CRIT_LEVEL$: -o 1.3.6.1.4.1.2011.6.2.1.6.1.1.2.2.0
}
现在电池容量出了问题,收到告警邮件了吗?
转到邮箱去看,果然收到了,邮件看起来是这个样子的:
屏幕快照 2019-01-15 00.52.30.png
能够看到主机名,IP地址,时间,那个参数出问题,告警级别等,简单明了,不用打开邮件就知道是什么告警。