Nagios
支持短信,邮件通知
Nagios 官网 http://www.nagios.org
1. Nagios安装 - 服务端( 192.168.0.11 )
Centos6默认的 yum 源里没有 nagios 相关的 rpm 包,但是我们可以安装一个 epel 的扩展源:
yum install -y epel-release
然后安装 nagios 相关的包
yum install -y httpd nagios nagios-pluginsnagios-plugins-all nrpe nagios-plugins-nrpe
设置登录 nagios 后台的用户和密码: htpasswd -c /etc/nagios/passwd nagiosadmin
nagios -v /etc/nagios/nagios.cfg 检测配置文件
启动服务: service httpd start; servicenagios start
浏览器访问: http://ip/nagios
vim /etc/nagios/nagios.cfg # 暂时先不管
2. Nagios安装 - 客户端( 192.168.0.12 )
在客户端机器上
yum install -y epel-release
yum install -y nagios-plugins nagios-plugins-allnrpe nagios-plugins-nrpe
vim /etc/nagios/nrpe.cfg 找到 “allowed_hosts=127.0.0.1” 改为 “allowed_hosts=127.0.0.1, 192.168.0.11 ” # 服务器的 ip
找到 ” dont_blame_nrpe=0” 改为 “dont_blame_nrpe=1”
启动客户端 /etc/init.d/nrpe start
3. 监控中心( 192.168.0.11 )添加被监控主机( 192.168.0.12 )
vim /etc/nagios/conf.d/192.168.0.12.cfg
define host{
use linux-server
host_name 192.168.0.12
alias 0.12
address 192.168.0.12
}
define service{
use generic-service
host_name 192.168.0.12
service_description check_ping
check_command check_ping!100.0,20%!200.0,50% #0是 ok , 20 是警告 ,50 是危险
max_check_attempts5 #单位是秒数
normal_check_interval 1
}
define service{
use generic-service
host_name 192.168.0.12
service_description check_ssh
check_command check_ssh
max_check_attempts 5 ;当 nagios 检测到问题时,一共尝试检测 5 次都有问题才会告警,如果该数值为 1 ,那么检测到问题立即告警
normal_check_interval 1 ;重新检测的时间间隔,单位是分钟,默认是 3 分钟
notification_interval 60 ;在服务出现异常后,故障一直没有解决, nagios 再次对使用者发出通知的时间。单位是分钟。如果你认为,所有的事件只需要一次通知就够了,可以把这里的选项设为 0 。
}
define service{
use generic-service
host_name 192.168.0.12
service_description check_http
check_command check_http
max_check_attempts 5
normal_check_interval 1
}
以上 普通服务不依赖于客户端 nrpe 服务 ,我们可以想象,我们在自己电脑上可以使用 ping 或者 telnet 探测远程任何一台机器是否存活、是否开启某个端口或服务。 而当我们想要检测客户端上的某个 具体特殊服务 的情况时, 就需要借助于 nrpe 了,比如想知道客户端机器的负责或磁盘使用情况。
红色圈住的是因为 80 端口没有启用, yum 安装 nginx 才会显示 200 , http 也会是 403
4. 继续添加服务
服务端 vim/etc/nagios/objects/commands.cfg
增加: define command{
command_name check_nrpe # 去对方获得服务状态,可自定义
command_line $USER1$/check_nrpe -H $HOSTADDRESS$-c $ARG1$
}
继续编辑 vim/etc/nagios/conf.d/192.168.0.12.cfg
增加如下内容: define service{
use generic-service
host_name 192.168.0.12
service_description check_load
check_command check_nrpe!check_load
max_check_attempts 5
normal_check_interval 1
}
define service{
use generic-service
host_name 192.168.0.12
service_description check_disk_hda1
check_command check_nrpe!check_hda1
max_check_attempts 5
normal_check_interval 1
}
define service{
use generic-service
host_name 192.168.0.12
service_description check_disk_hda2
check_command check_nrpe!check_hda2 #这个不要写错,是对应客户端的
max_check_attempts 5
normal_check_interval 1
}
说明: check_nrpe!check_load :这里的 check_nrpe 就是在 commands.cfg 刚刚定义的, check_load 是远程主机上的一个检测脚本
在客户端上 vim/etc/nagios/nrpe.cfg 搜索 check_load ,这行就是在服务端上要执行的脚本了,我们可以手动执行这个脚本
把 check_hda1 更改一下: /dev/hda1 改为 /dev/sda1
再加一行 command[check_hda2]=/usr/lib/nagios/plugins/check_disk-w 20% -c 10% -p /dev/sda2 # w = warnning
c = critial
Crital不能比warning的值大
机制:首先在服务端定义check_nrpe命令,再通过check_nrpe后面跟的command(客户端的nrpe.cfg里)
客户端上重启一下 nrpe 服务 : service nrpe restart
服务端也重启一下 nagios 服务 : service nagios restart
5. 配置告警
vim /etc/nagios/objects/contacts.cfg //增加:
define contact{
contact_name 123
use generic-contact
alias aming
email 280472479@qq.com
}
define contact{
contact_name 456
use generic-contact
alias aaa
email wsw26@139.com
}
define contactgroup{ # 定义联系组
contactgroup_name common
alias common
members 123,456
}
然后在要需要告警的服务里面加上 contactgroup
define service{
use generic-service
host_name 192.168.0.12
service_description check_load
check_command check_nrpe!check_load
max_check_attempts 5
normal_check_interval 1
contact_groups common # 监控哪个发邮件
notifications_enabled 1 ;是否开启提醒功能。 1 为开启, 0 为禁用。一般,这个选项会在主配置文件( nagios.cfg )中定义,效果相同。
notification_period 24x7 ;发送提醒的时间段。非常重要的主机(服务)我定义为 7×24 ,一般的主机(服务)就定义为上班时间。如果不在定义的时间段内,无论什么问题发生,都不会发送提醒。
notification_options:w,u,c,r ;这个是 service 的状态。 w 为 waning , u 为 unknown, c 为 critical, r 为 recover( 恢复了),类似的还有一个 host 对应的状态: d,u,r d = 状态为 DOWN, u = 状态为 UNREACHABLE , r = 状态恢复为 OK ,需要加入到 host 的定义配置里。
}
6. 配置图形显示 pnp4nagios
( 1 )安装
yum install pnp4nagios rrdtool
( 2 )配置主配置文件
vim /etc/nagios/nagios.cfg //修改如下配置
1. process_performance_data=1
2. host_perfdata_command=process-host-perfdata
3. service_perfdata_command=process-service-perfdata
4. enable_environment_macros=1
( 3 )修改 commands.cfg
vim/etc/nagios/objects/commands.cfg //注释掉原有对 process-host-perfdata 和 process-service-perfdata ,重新定义
1. define command {
2. command_name process-service-perfdata
3. command_line /usr/bin/perl/usr/libexec/pnp4nagios/process_perfdata.pl
4. }
5.
6. define command {
7. command_name process-host-perfdata
8. command_line /usr/bin/perl/usr/libexec/pnp4nagios/process_perfdata.pl -d HOSTPERFDATA
9. }
( 4 )修改配置文件 templates.cfg
vim /etc/nagios/objects/templates.cfg definehost {
name hosts-pnp
register 0
action_url/pnp4nagios/index.php/graph?host=$HOSTNAME$&srv=_HOST_
process_perf_data 1
}
define service {
name srv-pnp
register 0
action_url/pnp4nagios/index.php/graph?host=$HOSTNAME$&srv=$SERVICEDESC$
process_perf_data 1
}
( 5 )修改 host 和 service 配置
vim /etc/nagios/conf.d/192.168.0.12.cfg
把 “define host{
use linux-server”
改为:
define host{
use linux-server,hosts-pnp
修改对应的 service ,比如
把
define service{
use generic-service
host_name 192.168.0.12
service_description check_disk_hda1
check_command check_nrpe!check_hda1
max_check_attempts 5
normal_check_interval 1
}
改为:
define service{
use generic-service,srv-pnp
host_name 192.168.0.12
service_description check_disk_hda1
check_command check_nrpe!check_hda1
max_check_attempts 5
normal_check_interval 1
}
( 6 ) 重启和启动各个服务:
service nagios restart
service httpd restart
service npcd start
(7) 访问测试
两种访问方法:
ip/nagios/
ip/pnp4nagios/
运维网声明
1、欢迎大家加入本站运维交流群:群②:261659950 群⑤:202807635 群⑦870801961 群⑧679858003
2、本站所有主题由该帖子作者发表,该帖子作者与运维网 享有帖子相关版权
3、所有作品的著作权均归原作者享有,请您和我们一样尊重他人的著作权等合法权益。如果您对作品感到满意,请购买正版
4、禁止制作、复制、发布和传播具有反动、淫秽、色情、暴力、凶杀等内容的信息,一经发现立即删除。若您因此触犯法律,一切后果自负,我们对此不承担任何责任
5、所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其内容的准确性、可靠性、正当性、安全性、合法性等负责,亦不承担任何法律责任
6、所有作品仅供您个人学习、研究或欣赏,不得用于商业或者其他用途,否则,一切后果均由您自己承担,我们对此不承担任何法律责任
7、如涉及侵犯版权等问题,请您及时通知我们,我们将立即采取措施予以解决
8、联系人Email:admin@iyunv.com 网址:www.yunweiku.com