设为首页 收藏本站
查看: 1413|回复: 0

监控工具之NAGIOS的配置与应用(centos7)

[复制链接]

尚未签到

发表于 2017-11-3 13:25:51 | 显示全部楼层 |阅读模式
一、什么是nagios

Nagios 是一款开源的免费网络监控报警服务,能有效监控 Windows、Linux 和 Unix 的主机状态以及交换机、路由器和防火墙等网络设置,还有打印机、网络投影、网络摄像等设备。在系统或服务状态异常时发出邮件或其他即时通讯报警,以第一时间通知运维人员。在状态恢复后发出正常的邮件或即时通讯通知。

Nagios 有完善的插件功能,可以方便的根据应用服务扩展功能。

Nagios 可以使用服务-代理(Server-Agent)方式获取主机信息,也可以使用 SNMP 方式获取设备信息。使用服务器-代理(Server-Agent)方式在每个主机上运行代理软件,主机上的代理软件主动提交数据信息,可以避免服务器轮询带来的延迟和非实时性等问题。Nagios 已经可以支持由数万台服务器或上千台网络设备组成的云技术平台的监控,它可以充分发挥自动化运维技术特点在设备和人力资源减少成本。

Nagios 的缺点在于无法将多个相同应用集群的数据集合起来,也不能监控到集群中特殊节点的迁移和恢复。

Nagios更进一步的特征包括:
                监控网络服务(SMTP、POP3、HTTP、NNTP、PING等);        --公共服务
                监控主机资源(处理器负荷、磁盘利用率等);                            --私有服务
                简单地插件设计使得用户可以方便地扩展自己服务的检测方法;
                并行服务检查机制;
                当服务或主机问题产生与解决时将告警发送给联系人(通过EMail、短信、用户定义方式);
                具备定义事件句柄功能,它可以在主机或服务的事件发生时获取更多问题定位;
                可选的WEB界面用于查看当前的网络状态、通知和故障历史、日志文件等;

nagios的组成:
        1、nagios的核心组件
        2、nagios监控插件
        3、nagios web页面(html/cgi/php)
        4、nrpe远程主机私有服务的监控组件


二、配置nagios的运行环境

首先建立一个Nagios的Web主机,用来接受用户的Web访问。这里以servera作为nagios的server端:执行以下操作

安装nagios(rpm包)
[iyunv@servera pkg]# ls              #rpm包太多此处省略,下载地址:http://down.51cto.com/data/2339789

[iyunv@servera pkg]# yum localinstall *.rpm

了解
httpd的nagios虚拟主机
[iyunv@servera conf.d]# vim /etc/httpd/conf.d/nagios.conf
ScriptAlias /nagios/cgi-bin/ "/usr/lib64/nagios/cgi-bin/"
Alias /nagios "/usr/share/nagios/html"

<Directory "/usr/share/nagios/html">
#  SSLRequireSSL
   Options None
   AllowOverride None

   AuthName "Nagios Access"
   AuthType Basic
   AuthUserFile /etc/nagios/passwd
</Directory>

我们需要使用 htpasswd 工具设置 /etc/nagios/passwd 文件,用户名是 nagiosadmin,密码我们设置为 123456 。

[iyunv@servera conf.d]# htpasswd -c /etc/nagios/passwd nagiosadmin
New password:
Re-type new password:
Adding password for user nagiosadmin
[iyunv@servera conf.d]# cat /etc/nagios/passwd
nagiosadmin:$apr1$YKvTZ4/1$Puhwv8zlVMOJRke9xtJxC/

配置正常后,启动
httpd 和nagios服务,
[iyunv@servera conf.d]# systemctl restart
httpd
[iyunv@servera conf.d]# systemctl restart nagios

浏览器访问:http://172.25.0.10/nagios/
acf9ad87e49e29ce36379a3d93d543b8.png-wh_500x0-wm_3-wmp_4-s_3499630160.png
0904df4f4a9d94bbea1114f12b7798f8.png-wh_500x0-wm_3-wmp_4-s_1978605057.png

若在这里想要看到右边service health处于绿色的状态的话,需要将本地的snmpd服务打开

[iyunv@servera conf.d]# yum -y install net-snmp net-snmp-utils
[iyunv@servera conf.d]# systemctl restart snmpd


三、配置nagios监控本机
[iyunv@servera ~]# cat /etc/nagios/objects/localhost.cfg  |grep -v ^# |grep -v ^$
define host{
        use                     linux-server
        host_name               localhost
        alias                   nagios监控器
        address                 127.0.0.1
        }
define hostgroup{
        hostgroup_name  linux-servers ; The name of the hostgroup
        alias           Linux Servers ; Long name of the group
        members         localhost     ; Comma separated list of hosts that belong to this group
        }
define service{
        use                             local-service         ; Name of service template to use
        host_name                       localhost
        service_description             PING
        check_command                        check_ping!100.0,20%!500.0,60%
        }
define service{
        use                             local-service         ; Name of service template to use
        host_name                       localhost
        service_description             Root Partition
        check_command                        check_local_disk!20%!10%!/
        }
define service{
        use                             local-service         ; Name of service template to use
        host_name                       localhost
        service_description             Current Users
        check_command                        check_local_users!20!50
        }
define service{
        use                             local-service         ; Name of service template to use
        host_name                       localhost
        service_description             Total Processes
        check_command                        check_local_procs!250!400!RSZDT
        }
define service{
        use                             local-service         ; Name of service template to use
        host_name                       localhost
        service_description             Current Load
        check_command                        check_local_load!5.0,4.0,3.0!10.0,6.0,4.0
        }
define service{
        use                             local-service         ; Name of service template to use
        host_name                       localhost
        service_description             Swap Usage
        check_command                        check_local_swap!20!10
        }
define service{
        use                             local-service         ; Name of service template to use
        host_name                       localhost
        service_description             SSH
        check_command                        check_ssh
        notifications_enabled                0
        }
define service{
        use                             local-service         ; Name of service template to use
        host_name                       localhost
        service_description             HTTP
        check_command                        check_
http
        notifications_enabled                0
        }

[iyunv@servera objects]# service nagios start
[iyunv@servera objects]# nagios -v /etc/nagios/nagios.cfg


四、配置nagios监控远程主机
在这里,我们被监控的主机为serverb这台服务器,ip地址为172.25.0.11。

NRPE是Nagios的一个功能扩展,它可在远程Linux/Unix主机上执行插件程序。通过在远程服务器上安装NRPE插件及Nagios插件程序来向Nagios监控平台提供该服务器的本地情况,如CPU负载,内存使用,磁盘使用等。我们将Nagios监控端称为Nagios服务器端,而将远程被监控的主机称为Nagios客户端。

Nagios监控远程主机的方法有多种,其方式包括SNMP、NRPE、SSH、NCSA等。Nagios官方推荐监控主机使用NRPE方式,而监控设备使用SNMP方式。

NRPE(Nagios Remote Plugin Executor)是用于在远端服务器上运行监测命令的守护进程,它用于让Nagios监控端基于安装的方式触发远端主机上的检测命令,并将检测结果返回给监控端。而其执行的开销远低于基于SSH的检测方式,而且检测过程不需要远程主机上的系统账号信息,其安全性也高于SSH的检测方式。

[iyunv@serverb nagios]# yum -y localinstall nagios-plugins-2.0.3-1.el7.x86_64.rpm nrpe-2.15-2.el7.x86_64.rpm
[iyunv@serverb ~]# yum -y install openssl-devel


编辑nrpe配置文件

[iyunv@serverb nagios]# vim /etc/nagios/nrpe.cfg               
allowed_hosts=127.0.0.1, 172.25.0.10  # 定义监控主机的ip地址
command[check_users]=/usr/lib64/nagios/plugins/check_users -w 5 -c 10  
command[check_load]=/usr/lib64/nagios/plugins/check_load -w 15,10,5 -c 30,25,20
command[check_hda1]=/usr/lib64/nagios/plugins/check_disk -w 20% -c 10% -p /dev/hda1
command[check_zombie_procs]=/usr/lib64/nagios/plugins/check_procs -w 5 -c 10 -s Z
command[check_total_procs]=/usr/lib64/nagios/plugins/check_procs -w 150 -c 200
# 以上为定义命令的方式,默认已配好一部分命令的定义,也可以自己调用插件来完成相关定义

启动nrpe服务
[iyunv@serverb ~]# systemctl restart  xinetd
[iyunv@serverb ~]# netstat -tnlp |grep :5666
tcp        0      0 0.0.0.0:5666            0.0.0.0:*               LISTEN      2330/nrpe           
tcp6       0      0 :::5666                 :::*                    LISTEN      2330/nrpe

[iyunv@serverb nrpe-2.12]# netstat -at | grep nrpe
tcp6       0      0 [::]:nrpe               [::]:*                  LISTEN

本地检测nrpe服务:
[iyunv@serverb nrpe-2.12]# /usr/local/nagios/libexec/check_nrpe -H localhost

配置监控主机的相关内容
先在主配置文件里增加一个条目,定义serverb.conf该配置文件专门用来定义serverb的监控实例:

[iyunv@servera objects]# vim /etc/nagios/nagios.cfg
cfg_file=/etc/nagios/objects/serverb.cfg

编写serverb.conf的相关内容:

[iyunv@servera objects]# vim serverb.cfg
# 定义主机
define host{
        use                     linux-server                                                         
        host_name               serverb.pod0.example.com
        alias                   serverb
        address                 172.25.0.11
        }
# 定义主机组
define hostgroup{
        hostgroup_name  uplooking-servers
        alias           uplooking
        members         serverb.pod0.example.com     
        }
# 定义命令
define command{
        command_name check_nrpe
        command_line $USER1$/check_nrpe -H $HOSTADDRESS$ -c $ARG1$
}
# 定义监控服务
define service{
        use generic-service
        host_name serverb.pod0.example.com
        service_description load
        check_command check_nrpe!check_load
}
define service{
        use generic-service
        host_name serverb.pod0.example.com
        service_description user
        check_command check_nrpe!check_users
}


先运行nagios指令查看一下配置文件是否有误,根据报错修改配置参数:
[iyunv@servera objects]# nagios -v /etc/nagios/nagios.cfg

Nagios Core 3.5.1
Copyright (c) 2009-2011 Nagios Core Development Team and Community Contributors
Copyright (c) 1999-2009 Ethan Galstad
Last Modified: 08-30-2013
License: GPL

Website:
http://www.nagios.org
Reading configuration data...
   Read main config file okay...
Processing object config file '/etc/nagios/objects/commands.cfg'...
Processing object config file '/etc/nagios/objects/contacts.cfg'...
Processing object config file '/etc/nagios/objects/timeperiods.cfg'...
Processing object config file '/etc/nagios/objects/templates.cfg'...
Processing object config file '/etc/nagios/objects/localhost.cfg'...
Processing object config file '/etc/nagios/objects/serverb.cfg'...
Processing object config directory '/etc/nagios/conf.d'...
   Read object config files okay...

Running pre-flight check on configuration data...

Checking services...
        Checked 10 services.
Checking hosts...
        Checked 2 hosts.
Checking host groups...
        Checked 2 host groups.
Checking service groups...
        Checked 0 service groups.
Checking contacts...
        Checked 1 contacts.
Checking contact groups...
        Checked 1 contact groups.
Checking service escalations...
        Checked 0 service escalations.
Checking service dependencies...
        Checked 0 service dependencies.
Checking host escalations...
        Checked 0 host escalations.
Checking host dependencies...
        Checked 0 host dependencies.
Checking commands...
        Checked 25 commands.
Checking time periods...
        Checked 5 time periods.
Checking for circular paths between hosts...
Checking for circular host and service dependencies...
Checking global event handlers...
Checking obsessive compulsive processor commands...
Checking misc settings...

Total Warnings: 0
Total Errors:   0

Things look okay - No serious problems were detected during the pre-flight check

确认配置文件无误后,重启nagios服务

[iyunv@servera objects]# systemctl restart nagios


访问测试:
浏览器访问
http://172.25.0.10/nagios,输入用户名 nagiosadmin,密码123456。点击 map ,我们可以看到类似下图的结构。
da206cee795e5bf0e87f286b3539e635.png-wh_500x0-wm_3-wmp_4-s_1015436961.png 846fb24a1ba72e0ec7111b55e8b1996a.png-wh_500x0-wm_3-wmp_4-s_206067185.png

点击services,查看对应主机监控内容是否ok,如果处于pending的状态,则稍等片刻查看数据结果:
50a180fd5ffa086805174b16a5e8df63.png-wh_500x0-wm_3-wmp_4-s_4195933540.png


运维网声明 1、欢迎大家加入本站运维交流群:群②:261659950 群⑤:202807635 群⑦870801961 群⑧679858003
2、本站所有主题由该帖子作者发表,该帖子作者与运维网享有帖子相关版权
3、所有作品的著作权均归原作者享有,请您和我们一样尊重他人的著作权等合法权益。如果您对作品感到满意,请购买正版
4、禁止制作、复制、发布和传播具有反动、淫秽、色情、暴力、凶杀等内容的信息,一经发现立即删除。若您因此触犯法律,一切后果自负,我们对此不承担任何责任
5、所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其内容的准确性、可靠性、正当性、安全性、合法性等负责,亦不承担任何法律责任
6、所有作品仅供您个人学习、研究或欣赏,不得用于商业或者其他用途,否则,一切后果均由您自己承担,我们对此不承担任何法律责任
7、如涉及侵犯版权等问题,请您及时通知我们,我们将立即采取措施予以解决
8、联系人Email:admin@iyunv.com 网址:www.yunweiku.com

所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其承担任何法律责任,如涉及侵犯版权等问题,请您及时通知我们,我们将立即处理,联系人Email:kefu@iyunv.com,QQ:1061981298 本贴地址:https://www.yunweiku.com/thread-406513-1-1.html 上篇帖子: nagios 详细部署安装 下篇帖子: 监控之--Nagios监控远程windows主机及邮件告警功能的配置
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

扫码加入运维网微信交流群X

扫码加入运维网微信交流群

扫描二维码加入运维网微信交流群,最新一手资源尽在官方微信交流群!快快加入我们吧...

扫描微信二维码查看详情

客服E-mail:kefu@iyunv.com 客服QQ:1061981298


QQ群⑦:运维网交流群⑦ QQ群⑧:运维网交流群⑧ k8s群:运维网kubernetes交流群


提醒:禁止发布任何违反国家法律、法规的言论与图片等内容;本站内容均来自个人观点与网络等信息,非本站认同之观点.


本站大部分资源是网友从网上搜集分享而来,其版权均归原作者及其网站所有,我们尊重他人的合法权益,如有内容侵犯您的合法权益,请及时与我们联系进行核实删除!



合作伙伴: 青云cloud

快速回复 返回顶部 返回列表