温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

nagios的安装及配置方法

发布时间:2021-08-12 09:33:46 来源:亿速云 阅读:94 作者:chen 栏目:移动开发

这篇文章主要介绍“nagios的安装及配置方法”,在日常操作中,相信很多人在nagios的安装及配置方法问题上存在疑惑,小编查阅了各式资料,整理出简单好用的操作方法,希望对大家解答”nagios的安装及配置方法”的疑惑有所帮助!接下来,请跟着小编一起来学习吧!

一、服务端安装(主要监控磁盘或系统负载,可以自定义脚本,不能查看历史数据)

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.com

5.配置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的安装及配置方法”的学习就结束了,希望能够解决大家的疑惑。理论与实践的搭配能更好的帮助大家学习,快去试试吧!若想继续学习更多相关知识,请继续关注亿速云网站,小编会继续努力为大家带来更多实用的文章!

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI