您的位置:首页 > 移动开发 > IOS开发

nagios安装配置

2016-06-16 09:17 369 查看
一、服务端安装(主要监控磁盘或系统负载,可以自定义脚本,不能查看历史数据)
1、配置好扩展源并安装
yum install -y httpd nagios nagios-plugins nagios-plugins-all nrpe nagios-plugins-nrpe
2、设置登录nagios后台的用户和密码:
htpasswd -c /etc/nagios/passwd nagiosadmin
3、编辑配置文件

vim /etc/nagios/nagios.cfg
nagios -v /etc/nagios/nagios.cfg #检测配置文件
4、启动服务
启动服务:service httpd start; service nagios start
5、浏览器访问: http://10.10.13.239/nagios
nagios管理员账号:nagiosadmin密码:abc.123

二、客户端安装
1、配置好扩展源后安装
yum install -y nagios-plugins nagios-plugins-all nrpe nagios-plugins-nrpe
2、编辑配置文件
vim /etc/nagios/nrpe.cfg 找到“allowed_hosts=127.0.0.1” 改为 “allowed_hosts=127.0.0.1,10.10.13.239” 后面的ip为服务端ip; 找到” dont_blame_nrpe=0” 改为 “dont_blame_nrpe=1”
启动客户端 /etc/init.d/nrpe start
3、服务端添加配置文件

cd /etc/nagios/conf.d/
vim 10.10.13.247.cfg

define host{

use linux-server
host_name 10.10.13.247
alias 13.247
address 10.10.13.247
}

define service{
use generic-service
host_name 10.10.13.247
service_description check_ping
check_command check_ping!100.0,20%!200.0,50%
max_check_attempts 5
normal_check_interval 1
}

define service{
use generic-service
host_name 10.10.13.247
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 10.10.13.247
service_description check_http
check_command check_http
max_check_attempts 5
normal_check_interval 1
}
#配置完检测是否有错
nagios -v /etc/nagios/nagios.cfg
4、客户端上重启一下nrpe服务: service nrpe restart
服务端也重启一下nagios服务: service nagios restart
5、 刷新地址http://10.10.13.239/nagios看host和services已经添加了相关项目
三、监控客户端的本地资源(需要借助于nrpe服务)
1、服务端vim /etc/nagios/objects/commands.cfg
增加:define command{
command_name check_nrpe
command_line $USER1$/check_nrpe -H $HOSTADDRESS$ -c $ARG1$
}
2、继续编辑 vim /etc/nagios/conf.d/10.10.13.247.cfg
增加如下内容:define service{
use generic-service
host_name 10.10.13.247
service_description check_load
check_command check_nrpe!check_load #可以在客户端/etc/nagios/nrpe.cfg里面找到check_load脚本路径
max_check_attempts 5
normal_check_interval 1
}

define service{
use generic-service
host_name 10.10.13.247
service_description check_disk_sda1
check_command check_nrpe!check_hda1#可以在客户端/etc/nagios/nrpe.cfg里面找到check_hda1脚本路径
max_check_attempts 5
normal_check_interval 1
}

3、修改客户端的nrpe配置文件
vim /etc/nagios/nrpe.cfg
修改command[check_hda1]=/usr/lib64/nagios/plugins/check_disk -w 20% -c 10% -p /dev/hda1
改为command[check_hda1]=/usr/lib64/nagios/plugins/check_disk -w 20% -c 10% -p /dev/sda1
4、重启服务端的nagios和nrpe服务重启客户端的nrpe服务
[root@server ~]# /etc/init.d/nagios restart
[root@server ~]# /etc/init.d/nrpe restart
[root@client ~]# /etc/init.d/nrpe restart
5、刷新地址http://10.10.13.239/nagios看services已经添加了一负载和硬盘使用量的监控
四、添加邮件告警功能
1、编辑配置文件
vim /etc/nagios/objects/contacts.cfg
添加
define contact{ #定义成员123
contact_name 123
use generic-contact
alias river
email 313841662@qq.com
}

define contact{
contact_name 456 #定义成员456
use generic-contact
alias aaa
email 313841662@qq.com
}

define contactgroup{ #定义组,组成员为123,456
contactgroup_name common
alias common
members 123,456
}
2、然后在要需要告警的服务里面加上contactgroup
vim /etc/nagios/conf.d/10.10.13.247.cfg
define service{
use generic-service
host_name 10.10.13.247
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,一般的主机(服务)就定义为上班时间。如果不在定义的时间段内,无论什么问题发生,都不会发送提醒。(时间格式为24x7不是24*7,如果写成后者就会报错)
notification_options w,u,c,r ;这个是service的状态。w为waning, u为unknown, c为critical, r为recover(恢复了),f为flapping,n为不发送提醒。类似的还有一个 host对应的状态:d,u,r d = 状态为DOWN, u = 状态为UNREACHABLE , r = 状态恢复为OK,需要加入到host的定义配置里。(这一选项后面不能跟冒号跟冒号就会报错,必须为空格)
}
#具体告警格式可以百度nagios 邮件告警
五、利用sendmail发送告警邮件

1.软件检测及安装#rpm -q sendmail若不存在则安装#yum -y install sendmail
2.停止sendmail服务这里不需要sendmail作为邮件服务端来运行,所以关闭sendmail服务。#service sendmail stop
3.配置sendmail服务配置发送邮件的邮箱认证信息vi /etc/mail.rc 添加如下内容:set from=chenconghe@benco.com.cn
set smtp=mail.benco.com.cn
set smtp-auth-user=chenconghe
set smtp-auth-password=20160427150456
set smtp-auth=login
4.测试发送邮件功能格式:echo "邮件内容" | mail -s "邮件标题" 目标邮件地址echo "hello word" | mail -s "mail title" 313841662@qq.com5.配置nagios邮件发送命令修改nagios命令配置文件#vim /etc/nagios/objects/commands.cfg#notify-host-by-email命令的定义 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" | /bin/mail -s "** $NOTIFICATIONTYPE$ Host Alert: $HOSTNAME$ is $HOSTSTATE$ **" $CONTACTEMAIL$ }
#notify-service-by-email命令的定义 define command{ #命令名称,即定义了一个服务异常时发送邮件的命令 command_name notify-service-by-email command_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" | /bin/mail -s "** $NOTIFICATIONTYPE$ Service Alert: $HOSTALIAS$/$SERVICEDESC$ is $SERVICESTATE$ **" $CONTACTEMAIL$ }6、接着修改nagios中定义的主机和服务的报警机制,实现nagios利用邮件自动报警。
vim /etc/nagios/conf.d/10.10.13.238.cfg

define service{
use generic-service
host_name 10.10.13.247
service_description notify-service-by-email
check_command notify-service-by-email
max_check_attempts 5
normal_check_interval 1
notification_period 24x7
contact_groups common
notifications_enabled 1
notification_options w,u,c,r

}
define service{
use generic-service
host_name 10.10.13.247
service_description notify-host-by-email
check_command notify-host-by-email
max_check_attempts 5
normal_check_interval 1
contact_groups common
notifications_enabled 1
notification_period 24x7
notification_options w,u,c,r

}
#当重启服务时出现以下错误:
Warning: Duplicate definition found for command 'notify-host-by-email' (config file '/etc/nagios/objects/commands.cfg', starting on line 244)
Error: Could not add object property in file '/etc/nagios/objects/commands.cfg' on line 245.
有可能是因为复制粘贴的时候复制了两次,注意检查配置文件
7、测试
将10.10.13.247关掉,QQ邮箱收到一封报警信,测试成功。

#当重启客户端的时候有可能在Nagios的监控页面上有关需要nrpe辅助的项目会显示出错,需要重新检查客户端nrpe配置文件allowed_hosts和dont_blame_nrpe是否需要修改。突然断电等可能导致配置信息丢失需要重新检查

测试自动发送邮件参考此文章:http://4709096.blog.51cto.com/4699096/1703980

参考:
调用短信接口 http://www.aminglinux.com/bbs/thread-7380-1-1.html
整合微信 http://www.aminglinux.com/bbs/thread-7917-1-1.html
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  nagios安装