Centos7安装nagios并配置邮件报警
系统环境
Centos 7,全部按照Centos7最小化安装
所需软件包
存放在我的云盘中
配置开发环境
同步时间
把监控系统里的所有机器都同步一次网络时间(非常重要)
[root@localhost ~]# timedatectl # 该命令用来检查当前时间和时区
Local time: 四 2017-03-09 15:55:25 CST
Universal time: 四 2017-03-09 07:55:25 UTC
RTC time: 四 2017-03-09 07:55:25
Time zone: Asia/Shanghai (CST, +0800)
NTP enabled: no
NTP synchronized: no
RTC in local TZ: no
DST active: n/a
[root@localhost ~]# timedatectl list-timezones # 该命令列出所有时区
[root@localhost ~]timedatectl set-timezone Asia/Shanghai # 该命令把时区设置为上海
[root@localhost ~]# ntpdate time.nist.gov # 同步网络时间,如果提示没有ntpdate命令,则安装ntp,并且配置系统自动更新时间
[root@localhost ~]# yum install ntp -y
[root@localhost ~]# /usr/sbin/ntpdate time.nist.gov
[root@localhost ~]# echo '#time sync'>>/var/spool/cron/root
[root@localhost ~]# echo '*/10**** /usr/sbin/ntpdate time.nist.gov >/dev/null 2>&1'>>/var/spool/cron/root # 写入计划任务自动同步
关闭selinux
上传相关软件包
安装邮件服务
因为邮件报警服务需要安装mail功能
yum install -y mailx
yum install -y sendmail
启动服务:
systemctl restart sendmail.service
mail -s Test xxxxxx@qq.com
此时进入输入模式,输入完邮件内容后按ctrl + d退出并且发送
监控主机安装
安装nagios所需要的运行环境
yum install gcc glibc glibc-common -y
yum install php php-gd perl -y
yum install httpd gd gd-devel openssl openssl-devel -y
systemctl enable httpd.service # 设置apache开机启动
增加用户
useradd -m nagios
groupadd nagcmd
usermod -G nagcmd nagios
usermod -a -G nagcmd apache # 把apache用户添加到与nagios的一个组(apache用户会在安装apache时自动创建)
安装nagios
tar -zxf nagios-4.0.8.tar.gz
cd nagios-4.0.8
./configure --with-command-group=nagcmd
make all
make install
make install-init
make install-config
make install-commandmode
make install-webconf
[root@localhost nagios]# ls # 如果看到以下文件代表nagios安装完成
bin etc libexec sbin share var
nagios设置
设置nagios告警信息的邮件地址,默认是本机的nagios用户。
vim /usr/local/nagios/etc/objects/contacts.cfg
define contact{
contact_name nagiosadmin ; Short name of user
use generic-contact ; Inherit default values from generic-contact tem
plate (defined above)
alias Nagios Admin ; Full name of user
email xxxxxxx@qq.com #更改为自己的邮箱地址 ; <<***** CHANGE THIS TO YOUR EMAIL ADDRESS *****
*
}
创建一个登录nagios web程序的用户(用户名配置为nagiosadmin则不需要配置权限,设置为其他用户名就要配置权限),我在这里把用户名设置为yxyw,密码为yxyw@2017,这个用户帐号在以后通过web登录nagios认证时所用:
因为nagios默认把全部的权限给nagiosadmin,所以可以通过修改cgi.cfg文件赋予kylinlin权限,切换到/usr/local/nagios/etc目录下:
sed -i 's#nagiosadmin#yxyw#g' cgi.cfg # 这条命令将nagiosadmin用户名替换为kylinlin
grep yxyw cgi.cfg # 这条命令检查是否修改成功
authorized_for_system_information=yxyw
authorized_for_configuration_information=yxyw
authorized_for_system_commands=yxyw
authorized_for_all_services=yxyw
authorized_for_all_hosts=yxyw
authorized_for_all_service_commands=yxyw
authorized_for_all_host_commands=yxyw
以上过程结束后需要重新启动httpd:
systemctl restart httpd.service
[root@localhost etc]# /etc/init.d/nagios checkconfig # 使用这个命令来检查语法
Running configuration check...
OK.
安装插件
tar -zxf nagios-plugins-2.0.3.tar.gz
cd nagios-plugins-2.0.3
./configure --with-nagios-user=nagios --with-nagios-group=nagios
make
make install
ls /usr/local/nagios/libexec/|wc -l # 查看安装了多少插件
chkconfig --add nagios # 添加nagios至服务列表
chkconfig nagios on # 添加开机自启动
/usr/local/nagios/bin/nagios -v /usr/local/nagios/etc/nagios.cfg # 验证配置文件
启动nagios服务
/usr/local/nagios/bin/nagios -d /usr/local/nagios/etc/nagios.cfg
之后可以在浏览器上访问链接:http://10.20.10.99 输入登陆名和密码,如果能够正常看到页面,证明主程序和插件都安装和配置成功!点击“Service”的链接来查看你本机的监视详情。此时可能需要给点时间让Nagios来检测你机器上所依赖的服务
安装nrpe
[root@localhost nrpe-2.15]# ./configure --with-nrpe-user=nagios \
> --with-nrpe-group=nagios \
> --with-nagios-user=nagios \
> --with-nagios-group=nagios \
> --enable-command-args \
> --enable-ssl
make all
make install-plugin
make install-daemon
make install-daemon-config
ls /usr/local/nagios/libexec/ |grep check_nrpe # 检查一下/usr/local/nagios/libexec目录下是否已经安装了check_nrpe插件
监控机安装pnp4nagios
配置开发环境
yum install rrdtool librrds-perl -y
tar -zxf pnp4nagios-0.6.25.tar.gz
cd pnp4nagios-0.6.25
./configure --with-nagios-user=nagios --with-nagios-group=nagios
make all
make install
make install-webconf
make install-config
make install-init
cd ./sample-config
make install-webconf
配置pnp4nagios
cd /usr/local/pnp4nagios/etc/
mv misccommands.cfg-sample misccommands.cfg
mv rra.cfg-sample rra.cfg
mv nagios.cfg-sample nagios.cfg
cd pages/
mv web_traffic.cfg-sample web_traffic.cfg
cd ../check_commands/
mv check_all_local_disks.cfg-sample check_all_local_disks.cfg
mv check_nrpe.cfg-sample check_nrpe.cfg
mv check_nwstat.cfg-sample check_nwstat.cfg
/etc/init.d/npcd start
chkconfig npcd on
配置Nagios数据输出接口(以BULK模式运行)详情参考官网https://docs.pnp4nagios.org/pnp-0.6/config#bulk_mode:
vim /usr/local/nagios/etc/nagios.cfg
process_performance_data=1 # 默认为0,修改为1
# 并在该文件中添加下面的内容:
#
# service performance data
#
service_perfdata_file=/usr/local/pnp4nagios/var/service-perfdata
service_perfdata_file_template=DATATYPE::SERVICEPERFDATA\tTIMET::$TIMET$\tHOSTNAME::$HOSTNAME$\tSERVICEDESC::$SERVICEDESC$\tSERVICEPERFDATA::$SERVICEPERFDATA$\tSERVICECHECKCOMMAND::$SERVICECHECKCOMMAND$\tHOSTSTATE::$HOSTSTATE$\tHOSTSTATETYPE::$HOSTSTATETYPE$\tSERVICESTATE::$SERVICESTATE$\tSERVICESTATETYPE::$SERVICESTATETYPE$
service_perfdata_file_mode=a
service_perfdata_file_processing_interval=15
service_perfdata_file_processing_command=process-service-perfdata-file
#
# host performance data starting with Nagios 3.0
#
host_perfdata_file=/usr/local/pnp4nagios/var/host-perfdata
host_perfdata_file_template=DATATYPE::HOSTPERFDATA\tTIMET::$TIMET$\tHOSTNAME::$HOSTNAME$\tHOSTPERFDATA::$HOSTPERFDATA$\tHOSTCHECKCOMMAND::$HOSTCHECKCOMMAND$\tHOSTSTATE::$HOSTSTATE$\tHOSTSTATETYPE::$HOSTSTATETYPE$
host_perfdata_file_mode=a
host_perfdata_file_processing_interval=15
host_perfdata_file_processing_command=process-host-perfdata-file
配置command.cfg
vim /usr/local/nagios/etc/objects/commands.cfg
# 在该文件中添加下面的内容
define command{
command_name process-service-perfdata-file
command_line /usr/local/pnp4nagios/libexec/process_perfdata.pl --bulk=/usr/local/pnp4nagios/var/service-perfdata
}
define command{
command_name process-host-perfdata-file
command_line /usr/local/pnp4nagios/libexec/process_perfdata.pl --bulk=/usr/local/pnp4nagios/var/host-perfdata
}
在模板配置文件中添加图表图标模板:
vim /usr/local/nagios/etc/objects/templates.cfg
define host {
name host-pnp
action_url /pnp4nagios/index.php/graph?host=$HOSTNAME$&srv=_HOST_
register 0
}
define service {
name service-pnp
action_url /pnp4nagios/index.php/graph?host=$HOSTNAME$&srv=$SERVICEDESC$
register 0
}
在监控主机和服务中调用图表模板(在主机和服务后面添加新的模板):
# 按下面的内容修改(不是添加)该文件
define host{
use linux-server,host-pnp
host_name localhost
alias localhost
address 127.0.0.1
}
define service{
use local-service,service-pnp
host_name localhost
service_description PING
check_command check_ping!100.0,20%!500.0,60%
}
define service{
use local-service,service-pnp
host_name localhost
service_description Root Partition
check_command check_local_disk!20%!10%!/
}
重启nagios和apache
systemctl restart nagios.service
systemctl restart httpd.service
访问nagios界面即可看到图表小图标,点击图标会显示pnp4nagios测试页面
全是绿色代表配置正常,如果不是全绿,要逐个解决错误。然后移除或修改install.php文件:
mv /usr/local/pnp4nagios/share/install.php /usr/local/src/
再次点击图标就会显示当前监控服务由pnp4nagios生成的图表了:
邮件报警之利用mutt+msmtp实现邮件报警
编译安装
yum install mutt
wget https://jaist.dl.sourceforge.net/project/msmtp/msmtp/1.4.30/msmtp-1.4.30.tar.bz2
tar jxvf msmtp-1.4.30.tar.bz2
./configure --prefix=/usr/local/msmtp
make
make install
配置msmstp信息
mkdir /usr/local/msmtp/etc
vim /usr/local/msmtp/etc/msmtprc # 添加如下内容:
# Set default values for all following accounts.
defaults
# 日志地址
logfile /usr/local/msmtp/msmtp.log
# 邮箱用户名
account lw845170
# SMTP邮件服务器地址
host smtp.163.com
# 发送的邮件Email
from xxxxx@163.com
auth login
# 端口
#port 25
tls off
# 邮件服务器登录帐号
user xxxxx@163.com
# 邮箱密码
password xxxxxxx
# 默认用户名
account default:xxxx
由于password是明码,所以最好需要修改此文件的权限:chmod 600 etc/msmtprc
配置mutt信息
vim /etc/Muttrc # 在最后插入:
set from="xxxxx@163.com"
set sendmail="/usr/local/msmtp/bin/msmtp"
set use_from=yes
set realname="xxxxx"
set editor="vim"
修改文件权限以供nagios可以使用
setfacl -m u:nagios:rwx /usr/local/msmtp/bin/msmtp
setfacl -m u:nagios:rwx /usr/local/msmtp/etc/msmtprc
setfacl -m u:nagios:rwx /usr/local/msmtp/msmtp.log
测试
格式:echo "邮件内容" | mutt -s "邮件标题" 目标邮件地址
echo "This is a test mail" |mutt -s "hello" 764391557@qq.com
配置nagios邮件发送命令
修改nagios命令配置文件:
# 'notify-host-by-email' command definition
define command{
command_name notify-host-by-email
command_line /usr/bin/printf "%b" "***** Nagios *****\n\nNotification Type: $NOTIFICATIONTYPE$\nHost:
$HOSTNAME$\nState: $HOSTSTATE$\nAddress: $HOSTADDRESS$\nInfo: $HOSTOUTPUT$\n\nDate/Time: $LONGDATETIME$\n" | /usr
/bin/mutt -s(修改) "** $NOTIFICATIONTYPE$ Host Alert: $HOSTNAME$ is $HOSTSTATE$ **" $CONTACTEMAIL$
}
# 'notify-service-by-email' command definition
define command{
command_name notify-service-by-email
command_line /usr/bin/printf "%b" "***** Nagios *****\n\nNotification Type: $NOTIFICATIONTYPE$\n\nServ
ice: $SERVICEDESC$\nHost: $HOSTALIAS$\nAddress: $HOSTADDRESS$\nState: $SERVICESTATE$\n\nDate/Time: $LONGDATETIME$
\n\nAdditional Info:\n\n$SERVICEOUTPUT$\n" | /usr/bin/mutt -s (修改)"** $NOTIFICATIONTYPE$ Service Alert: $HOSTALIAS$/$
SERVICEDESC$ is $SERVICESTATE$ **" $CONTACTEMAIL$
}