环境:

Nagios Server  Nagios 6.5

IP:192.168.0.58

Nagios Clint   Nagios 6.5

IP:192.168.0.83

另注意:

Nagios需要PHP支持,安装Apache+PHP步骤略....

------------------------------------------------------

——安装Nagios

1)下载Nagios

[root@Nagios ~]# cd /Linux
[root@Nagios Linux]#  http://jaist.dl.sourceforge.net/project/nagios/nagios-4.x/nagios-4.0.2/nagios-4.0.2.tar.gz

2)编译安装

[root@Nagios Linux]# tar fzvx nagios-4.0.2.tar.gz
[root@Nagios Linux]# cd nagios-4.0.2
[root@Nagios nagios-4.0.2]# useradd -M -s /sbin/nologin nagios
[root@Nagios nagios-4.0.2]# mkdir /usr/local/nagios
[root@Nagios nagios-4.0.2]# chown -R nagios:nagios /usr/local/nagios/
[root@Nagios nagios-4.0.2]# ./configure --prefix=/usr/local/nagios/
[root@Nagios nagios-4.0.2]# make all
[root@Nagios nagios-4.0.2]# make install
[root@Nagios nagios-4.0.2]# make install-init
[root@Nagios nagios-4.0.2]# make install-commandmode
[root@Nagios nagios-4.0.2]# make install-config
[root@Nagios nagios-4.0.2]# chkconfig --add nagios
[root@Nagios nagios-4.0.2]# chkconfig --level 35 nagios on
[root@Nagios nagios-4.0.2]# chkconfig --list nagios
nagios          0:关闭  1:关闭  2:关闭  3:启用  4:关闭  5:启用  6:关闭
[root@Nagios nagios-4.0.2]# service nagios restart

3)服务端安装Nagios插件plugins

[root@Nagios Linux]# tar zfvx nagios-plugins-1.5.tar.gz
[root@Nagios Linux]# cd nagios-plugins-1.5
[root@Nagios nagios-plugins-1.5]# ./configure --prefix=/usr/local/nagios/
[root@Nagios nagios-plugins-1.5]# make && make install

----------------------------------------------------------------------------

4)服务端安装nrpe

[root@Nagios Linux]# tar zfvx nrpe-2.15.tar.gz
[root@Nagios Linux]# cd nrpe-2.15
[root@Nagios nrpe-2.15]# ./configure
[root@Nagios nrpe-2.15]# make all
[root@Nagios nrpe-2.15]# make install-plugin

——Apache配置修改

1)使apache支持nagios

[root@Nagios nagios-plugins-1.5]# vi /usr/local/apache-2.4/conf/httpd.conf
ScriptAlias /nagios/cgi-bin "/usr/local/nagios/sbin"
<Directory "/usr/local/nagios/sbin">AuthType BasicOptions ExecCGIAllowOverride NoneOrder allow,denyAllow from allAuthName "Nagios Access"AuthUserFile /usr/local/nagios/etc/htpasswdRequire valid-user
</Directory>
Alias /nagios "/usr/local/nagios/share"
<Directory "/usr/local/nagios/share">AuthType BasicOptions NoneAllowOverride NoneOrder allow,denyAllow from allAuthName "nagios Access"AuthUserFile /usr/local/nagios/etc/htpasswdRequire valid-user
</Directory>

2)添加nagios登陆用户

[root@Nagios nagios-plugins-1.5]# /usr/local/apache-2.4/bin/htpasswd -c  /usr/local/nagios/etc/htpasswd nagios
New password:
Re-type new password:
Adding password for user nagios

3)修改apache的用户和用户组

[root@Nagios nagios-plugins-1.5]# vi /usr/local/apache-2.4/conf/httpd.conf
User nagios
Group nagios

--------------------------------------------------------------------------

4)解决Nagios乱码问题

[root@Nagios nagios-plugins-1.5]# vi /usr/local/apache-2.4/conf/httpd.conf
LoadModule cgid_module modules/mod_cgid.so
LoadModule actions_module modules/mod_actions.so
[root@Nagios nagios-plugins-1.5]# service httpd restart

5)测试访问

——客户端配置

1)客户端安装nagios-plugins

[root@CentOS ~]# cd /Linux
[root@CentOS Linux]# useradd -M -s /sbin/nologin nagios
[root@CentOS Linux]# tar zfvx nagios-plugins-1.5.tar.gz
[root@CentOS Linux]# cd nagios-plugins-1.5
[root@CentOS nagios-plugins-1.5]# ./configure --prefix=/usr/local/nagios/
[root@CentOS nagios-plugins-1.5]# make && make install

2)客户端安装nrpe

[root@CentOS nagios-plugins-1.5]# cd ..
[root@CentOS Linux]# tar zfvx nrpe-2.15.tar.gz
[root@CentOS Linux]# cd nrpe-2.15
[root@CentOS nrpe-2.15]# ./configure
[root@CentOS nrpe-2.15]# make all
[root@CentOS nrpe-2.15]# make install-plugin
[root@CentOS nrpe-2.15]# make install-daemon
[root@CentOS nrpe-2.15]# make install-daemon-config
[root@CentOS nrpe-2.15]# vi /usr/local/nagios/etc/nrpe.cfg
allowed_hosts=127.0.0.1,192.168.0.58

3)启动nrpe

[root@CentOS nrpe-2.15]# /usr/local/nagios/bin/nrpe -c /usr/local/nagios/etc/nrpe.cfg -d
[root@CentOS nrpe-2.15]# echo "/usr/local/nagios/bin/nrpe -c /usr/local/nagios/etc/nrpe.cfg -d" /etc/rc.local

4)检查是否启动

[root@CentOS nrpe-2.15]# netstat -antup |grep 5666
tcp        0      0 0.0.0.0:5666                0.0.0.0:*                   LISTEN      48456/nrpe
tcp        0      0 :::5666                     :::*                        LISTEN      48456/nrpe

5)在服务端检查 [如果返回NRPE版本及成功,相反则检查配置]

[root@Nagios ~]# /usr/local/nagios/libexec/check_nrpe -H 192.168.0.83
NRPE v2.15

=========================================================================================

——配置nagios

[root@Nagios nrpe-2.15]# cd /usr/local/nagios/etc/

1)编辑nagios.cfg

[root@Nagios etc]# vi nagios.cfg
注释下面的内容
#cfg_file=/usr/local/nagios/etc/objects/commands.cfg
#cfg_file=/usr/local/nagios/etc/objects/contacts.cfg
#cfg_file=/usr/local/nagios/etc/objects/timeperiods.cfg
#cfg_file=/usr/local/nagios/etc/objects/templates.cfg
#cfg_file=/usr/local/nagios/etc/objects/localhost.cfg
#cfg_file=/usr/local/nagios/etc/objects/windows.cfg
#cfg_file=/usr/local/nagios/etc/objects/switch.cfg
#添加监控主机目录
cfg_dir=/usr/local/nagios/etc/objects/hosts
#添加模板目录
cfg_dir=/usr/local/nagios/etc/objects/templates

2)创建相关目录

[root@Nagios etc]# mdkir  objects/templates
[root@Nagios etc]# mdkir  objects/hosts

3)复制所需文件

[root@Nagios etc]# cp objects/commands.cfg objects/templates/
[root@Nagios etc]# cp objects/timeperiods.cfg objects/templates/

4)创建自定义模板

[root@Nagios etc]# vi objects/templates/templates.cfg
#联系人模板
define contact{#联系人名称name                            tao#当服务出现异常时 发送通知的时间段service_notification_period     24x7#当主机出现异常时 发送通知的时间段host_notification_period        24x7#w 表示警告 u 表示不明 c 表示紧急 r 表示恢复service_notification_options    w,u,c,r#d 表示宕机 u 表示不可达 r 表示重新恢复host_notification_options       d,u,r#服务故障时 发送通知的方式[邮件]service_notification_commands   notify-service-by-email#主机故障时 发送通知的方式[邮件]host_notification_commands      notify-host-by-emailregister                        0}
#主机模板
define host{#定义主机名name                            linux-host#其值为1 Nagios将收集的数据写入某个文件中 以备提取process_perf_data               1passive_checks_enabled          0#主机与服务的刷新检测 0为关闭check_freshness                 0#事件处理开启event_handler_enabled           1#打开抖动感知flap_detection_enabled          1active_checks_enabled           1#发送通知的时间段notification_period             24x7#检查主机的时间段check_period                    24x7#对主机的检查时间间隔/分钟check_interval                  5#如果宕机重试检查时间/分钟retry_interval                  1#宕机后对主机的最大检查次数max_check_attempts                  2#检查主机状态指令[在commands.cfg中定义]check_command                   check-host-alive#在主机出现异常后 故障一直没有解决 再次发送通知的时间/分钟notification_interval           5#d 表示宕机 u 表示不可达 r 表示重新恢复notification_options            d,u,r#1为开启报警信息notifications_enabled           0#指定联系人组[在contacts.cfg中定义]contact_groups                  admins#处理性能数据#一天刷新检测一次,以防止检测结果不是实时的freshness_threshold             86400#0为不注册,意思是这个作为模板register                        0}
#服务模板
define service{#定义一个服务名称name                            linux-service#启用主动服务检查active_checks_enabled           1#启用被动服务检查passive_checks_enabled          0#主机与服务的刷新检测check_freshness                 0#开启报警信息notifications_enabled           1#重新发送报警信息的间隔notification_interval           5#事件处理开启event_handler_enabled           1#启用抖动感知flap_detection_enabled          1#处理性能数据process_perf_data               1#发送通知的时间段notification_period             24x7#检查时间段check_period                    24x7#如果宕机最大检查次数max_check_attempts              5#检查服务时间间隔/分钟normal_check_interval           1#如果宕机再次检查的间隔时间/分钟retry_check_interval            1#一天刷新检测一次,以防止检测结果不是实时的freshness_threshold             86400#0为不注册,意思是这个作为模板register                        0}
define service  {name                                    test#1为开启报警信息notifications_enabled                   0#重新发送报警信息的间隔notification_interval                   5notification_options                    w,u,c,#事件处理开启event_handler_enabled                   1#打开抖动感知flap_detection_enabled                  1#发送通知的时间段notification_period                     24x7#检查时间段check_period                            24x7#每一次检测的间隔时间check_interval                          1#如果宕机再次检查的间隔时间retry_interval                          1#检测次数,如果2次都是宕机的话 就报警max_check_attempts                      2active_checks_enabled                   1passive_checks_enabled                  0#处理性能数据process_perf_data                       1#主机与服务的刷新检测 0为关闭check_freshness                         0#一天刷新检测一次,以防止检测结果不是实时的freshness_threshold                     86400#0为不注册,意思是这个作为模板register                                0}
[root@Nagios etc]# vi objects/templates/contacts.cfg
#联系人
define contact{#用户名称contact_name                    nagios#引用模板use                             tao#别名alias                           Nagios Admin#发送邮箱email                           xxxxxxxx@qq.com}
#联系人组
define contactgroup{#组名称contactgroup_name       admins#组别名alias                   Nagios Administrators#组用户成员members                 nagios}

5)添加监控主机

最好使用被监控机IP为名称、比如:被监控服务器为192.168.0.83 [后期服务器多的时候、容易维护]

这里为了省事、一台服务器监控多种服务。大家测试的时候、可以添加多个服务器监控不同服务。

[root@Nagios etc]# vi objects/hosts/192.168.0.83.cfg
#监控主机
define host {host_name                           192.168.0.83address                             192.168.0.83use                                 linux-host
}
#监控SSH
define service  {service_description                 check_sshcheck_command                       check_ssh!-p 22host_name                           192.168.0.83use                                 linux-serviceregister                            1
}
#监控登陆用户
define service  {service_description                 check_userscheck_command                       check_nrpe!check_usershost_name                           192.168.0.83use                                 linux-serviceregister                            1
}
#监控mysql [需要在被监控配置、下面有配置说明]
define service  {service_description                 check_mysqlcheck_command                       check_mysqlhost_name                           192.168.0.83use                                 linux-serviceregister                            1
}
#监控交换空间
define service  {service_description                 check_swapcheck_command                       check_nrpe!check_swaphost_name                           192.168.0.83use                                 linux-serviceregister                            1
}
#监控磁盘
define service  {service_description                 check_diskcheck_command                       check_nrpe!check_diskhost_name                           192.168.0.83use                                 linux-serviceregister                            1
}
#监控HTTP
define service  {service_description                 check_httpcheck_command                       check_http!-p 80host_name                           192.168.0.83use                                 linux-serviceregister                            1
}
#监控负载
define service  {service_description                 check_loadcheck_command                       check_nrpe!check_loadhost_name                           192.168.0.83use                                 linux-serviceregister
}

6)重启启动nagios、登陆WEB查看

网上很多人说配置后,执行命令检查是否有错误。个人感觉完全没必要、因为启动的时候Nagios默认会自动检查配置、如果有错误Nagios会提示。

[root@Nagios etc]# service nagios restart

——测试

1)在客户端 登陆nagios

注意:监控MySQL需要在被监控端配置如下:

1)客户端连接mysql添加nagios用户

mysql> create database nagios;
Query OK, 1 row affected (0.00 sec)
mysql> grant select on nagios.* to nagios@'192.168.1.18' identified by 'nagios';
Query OK, 0 rows affected (0.11 sec)
mysql> flush privileges;
Query OK, 0 rows affected (0.00 sec)
mysql> quit

2)Nagios服务器端添加mysql监控

[root@Nagios nrpe-2.15]# vi /usr/local/nagios/etc/objects/templates/commands.cfg
# 'check_mysql' command definition
define command{command_name    check_mysqlcommand_line    $USER1$/check_nrpe -H $HOSTADDRESS$ -u nagios -d nagios -p nagios}

——安装msmtp

1)下载安装msmtp

[root@Nagios Linux]# wget http://jaist.dl.sourceforge.net/project/msmtp/msmtp/1.4.31/msmtp-1.4.31.tar.bz2
[root@Nagios Linux]# tar fvx msmtp-1.4.31.tar.bz2
[root@Nagios Linux]# cd msmtp-1.4.31
[root@Nagios msmtp-1.4.31]# ./configure --prefix=/usr/local/msmtp
[root@Nagios msmtp-1.4.31]# make && make install
[root@Nagios msmtp-1.4.31]# cd /usr/local/msmtp/

2)配置msmtp

[root@Nagios msmtp]# mkdir etc
[root@Nagios msmtp]# cd etc/
[root@Nagios etc]# vi msmtprc
account default
logfile /usr/local/msmtp/msmtp.log
host smtp.163.com
from xxxxxx@163.com
auth login
user xxxxxx@163.com
#邮箱密码
password xxxxxx

3)安装mutt

[root@Nagios etc]# yum install mutt -y

4)配置mutt

[root@Nagios etc]# vi /etc/Muttrc
set sendmail="/usr/local/msmtp/bin/msmtp"
set use_from=yes
set realname="YingTao"
set from=hypocriticals@163.com
set envelope_from=yes
set editor="vim"
set record="/usr/local/msmtp/etc/sent"

5)Nagios默认使用sendmail发送邮件、只需要把 /bin/mail 改为 /usr/bin/mutt即可。

[root@Nagios etc]# vi objects/templates/commands.cfg
# 'notify-host-by-email' command definition
define command{command_name    notify-host-by-emailcommand_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-emailcommand_line    /usr/bin/printf "%b" "***** 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" | /usr/bin/mutt -s "** $NOTIFICATIONTYPE$ Service Alert: $HOSTALIAS$/$SERVICEDESC$ is $SERVICESTATE$ **" $CONTACTEMAIL$}

——测试能否通过邮箱报警

1)在被监控端关闭mysql

[root@CentOS ~]# /etc/init.d/mysqld stop
Shutting down MySQL. SUCCESS!
[root@CentOS ~]# netstat -antup |grep 3306

2)登陆WEB Nagios查看MySQL状态

3)在服务端查看日志 [默认日志路径为/usr/local/nagios/var/]

[root@Nagios ~]# cd /usr/local/nagios/var/
[root@Nagios var]# tail -f nagios.log
#检查五次、如果五次服务还是无法检测到服务是正常的,则发邮件通知。
[1388002399] SERVICE ALERT: 192.168.0.83;check_mysql;CRITICAL;SOFT;1;Can't connect to MySQL server on '192.168.0.83' (111)
[1388002459] SERVICE ALERT: 192.168.0.83;check_mysql;CRITICAL;SOFT;2;Can't connect to MySQL server on '192.168.0.83' (111)
[1388002519] SERVICE ALERT: 192.168.0.83;check_mysql;CRITICAL;SOFT;3;Can't connect to MySQL server on '192.168.0.83' (111)
[1388002579] SERVICE ALERT: 192.168.0.83;check_mysql;CRITICAL;SOFT;4;Can't connect to MySQL server on '192.168.0.83' (111)
[1388002639] SERVICE ALERT: 192.168.0.83;check_mysql;CRITICAL;HARD;5;Can't connect to MySQL server on '192.168.0.83' (111)
[1388002639] SERVICE NOTIFICATION: nagios;192.168.0.83;check_mysql;CRITICAL;notify-service-by-email;Can't connect to MySQL server on '192.168.0.83' (111)

4)登陆邮箱查看是否收到邮件 [由于是虚拟机时间没有调整、和现实时间有差别]

5)重新启动MySQL

[root@CentOS ~]# /etc/init.d/mysqld start
Starting MySQL.. SUCCESS!
[root@CentOS ~]# netstat -antup |grep 3306
tcp        0      0 0.0.0.0:3306           0.0.0.0:*            LISTEN       51040/mysqld

6)检测服务恢复正常后,是否收到邮件通知

转载于:https://blog.51cto.com/hypocritical/1352319

CentOS 6.5+Nagios4.0.2+Msmtp+Mutt邮箱报警相关推荐

  1. 烂泥:ubuntu下配置msmtp+mutt发送邮件

    2019独角兽企业重金招聘Python工程师标准>>> 本文由ilanniweb提供友情赞助,首发于烂泥行天下 想要获得更多的文章,可以关注我的微信ilanniweb. 本文主要介绍 ...

  2. linux下msmtp+mutt+shell发送邮件

    Linux下安装配置msmtp和mutt发送邮件 作者: 纵横域内 日期: 2014 年 12 月 11 日发表评论 (0)查看评论 Linux下安装配置msmtp和mutt Linux下有发送邮件的 ...

  3. 【编译打包】nagios-4.0.4-2.el6.src.rpm

    看着centos 6 的naigos 包有点旧,顺手打包了一个新版的. nagios 4.x 主要的变化是增加了ramcache功能(/etc/sysconfig/nagios 中默认没有开启),开启 ...

  4. ubuntu下配置msmtp+mutt发送邮件

    想要获得更多的文章,可以关注我的微信ilanniweb. 本文主要介绍如何在Linux下使用msmtp+mutt发送邮件,当然Linux下发送邮件的工具很多,比如sendmail+dovecot等.但 ...

  5. Mutt+Msmtp如何实现邮箱报警(阿里云)

    一.监控与报警 1.先来谈谈跟本文不是直接相关的问题 1)运维监控的重要性: 运维工作中比较重要的一个部分,可以说,一切线上系统都需要监控.如果没有了监控,你的服务器死机了,又或者你的Nginx服务没 ...

  6. mysql 4.0.27 下载,CentOS编译Mysql 4.0.27数据库问题解决

    CentOS编译Mysql 4.0.27数据库问题解决以下文字资料是由(历史新知网www.lishixinzhi.com)小编为大家搜集整理后发布的内容,让我们赶快一起来看一下吧! 现在好多人都用起了 ...

  7. Nagios监控之8:利用mutt+msmtp实现邮件报警

    使用mutt+msmtp搭建一个邮件客户端,实现利用第三方邮件服务发送邮件. 此功能是在nagios服务正常安装运行的基础上实现的;因此要先安装好基本的nagios运行环境,这个可以参考本人相关的博文 ...

  8. Linux中使用Msmtp+mutt+shell自动发邮件程序安装说明

    Linux中使用Msmtp+mutt+shell自动发邮件程序安装说明 ------by liyoujia 环境: 需要用到mutt 和 msmtp 这两款软件 Mutt:linux初始化安装的时候自 ...

  9. linux 安装nodejs8,CentOS 安装NodeJS V8.0.0的方法

    下载nodejs 解压 tar -xvf node-v8.0.0-Linux-x64.tar.xz 测试是否安装成功 进入解压目录下的 bin 目录,执行 ls 命令 cd node-v8.0.0-l ...

最新文章

  1. Python远程连接服务器
  2. Java 9 揭秘(14. HTTP/2 Client API)
  3. JavaScript对象、原型、原型链知识总结思维导图
  4. 安装完Oracle 11g之后启动SQL Developer 时报cannot find a j2se sdk错误的解决办法
  5. Envoy Proxy构建控制平面指南
  6. 大疆云台如何使用华为mate20pro_华为Mate30+大疆灵眸Osmo3,让你的照片和短视频称霸朋友圈...
  7. Spark精华问答 | Spark 会替代Hadoop 吗?
  8. 升级Python2.7后 no module name yum
  9. 获取Android Studio的Sha1
  10. CNN更新换代!性能提升算力减半,还即插即用
  11. 天下的母亲都是一样的
  12. Xilinx FPGA中RS编码IP核之Generator Start
  13. 服务器机柜设备信息卡,信息机房标识标准V.doc
  14. 马化腾和朱啸虎互怼之后,摩拜ofo合并可能性基本为零
  15. U盘有容量有一半不见了,修复U盘
  16. 桌面路径改到D盘的目录下
  17. 网页中title乱码问题解决方案
  18. Context都没弄明白,还怎么做Android开发?
  19. ElasticSearch 索引创建
  20. 网络资源的定义--URI,URL,URN

热门文章

  1. 2019年智能手机AI要被深度开发,这五项技术将是重点
  2. “神经网络”的逆袭:80年AI斗争史
  3. SAP MM 物料主数据consumption视图数据的更新?
  4. SAP ABAP 因系统维护使ABAP语法不再被支持导致使用很久的程序报错问题之分析
  5. 资讯丨谷歌 AutoML AI系统写的机器学习代码,完爆程序员
  6. 小数据,大前景 !美国智库最新报告:长期被忽略的小数据人工智能潜力不可估量...
  7. 2020年中国面向人工智能“新基建”的知识图谱行业白皮书
  8. 2020十大新兴技术揭晓!每一项都可能颠覆我们的生活
  9. 5G将如何推动未来十年智能城市的发展
  10. 【解析】工业机器人中的各类传感器技术应用