免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 2034 | 回复: 0
打印 上一主题 下一主题

Linux系统监控之 Nagios详解(一) [第十四期] [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2009-12-07 22:27 |只看该作者 |倒序浏览
Linux系统监控之 Nagios详解(一)
ChinaUnix网友:赵舜东
实验目的:通过实验熟练掌握Nagios这个开源的监控解决方案的部署和应用。
实验环境:Red Hat Enterprise Linux +nagios.3.2.0
第一部分:Linux系统监控之 Nagios详解(一)
实验简介:
公司进行了机房改造,新系统也上线了,需要一个强大的监控方案,对服务器和各服务的运行情况进行有效的监控,我第一个想到的就是Nagios这个强大的开源解决方案,本文以监控八台服务器和Nagios服务器本身为例。根据先实现、后深入的方式,把本文分为三个部分,开始先实现了功能,到第三个部分,在对配置的内容进行详细的讲解
功能实现:
实现Web浏览器监控,Mail报警邮件收发,手机短信收发。手机短信怎么收发呢,网上有很多很多的方法,我推荐一种就是使用139信箱,139信箱有一项免费的功能就是发邮件通知到您的手机上,可以在手机上看邮件内容。正规的方法是公司购买短线猫,1000多块钱,支持MySQL数据库,只要往数据库的发送任务表里插入一行数据即可实现报警。
实验内容:
一、Nagios 简介
(一)什么是NagiosNagios有哪些特性。(引自官方)
官方网站:http://www.nagios.org/ (更多的详细说明请参考官方网站)
Nagios是一款用于系统和网络监控的应用程序。它可以在你设定的条件下对主机和服务进行监控,在状态变差和变好的时候给出告警信息。Nagios最初被设计为在Linux系统之上运行,然而它同样可以在类Unix的系统之上运行。
Nagios更进一步的特征包括:
  • 监控网络服务(SMTPPOP3HTTPNNTPPING等);
  • 监控主机资源(处理器负荷、磁盘利用率等);
  • 简单地插件设计使得用户可以方便地扩展自己服务的检测方法;
  • 并行服务检查机制;
  • 具备定义网络分层结构的能力,用"parent"主机定义来表达网络主机间的关系,这种关系可被用来发现和明晰主机宕机或不可达状态;
  • 当服务或主机问题产生与解决时将告警发送给联系人(通过EMail、短信、用户定义方式);
  • 具备定义事件句柄功能,它可以在主机或服务的事件发生时获取更多问题定位;
  • 自动的日志回滚;
  • 可以支持并实现对主机的冗余监控;
  • 可选的WEB界面用于查看当前的网络状态、通知和故障历史、日志文件等;
(二)Nagios工作方式图

二、部署Nagios监控服务器
(一)下载所需软件包
1.nagios.3.2.0.tar.gz  Nagios的主程序
[root@UnixHot src]# wget http://prdownloads.sourceforge.net/sourceforge/nagios/nagios.3.2.0.tar.gz     
2.nagios-plugins-1.4.13.tar.gz  Nagios 的插
[root@UnixHot src]# wget http://prdownloads.sourceforge.net/sourceforge/nagiosplug/nagios-plugins-1.4.13.tar.gz
3.nrpe-2.12.tar.gz  代理检测程序
[root@UnixHot src]# wget http://prdownloads.sourceforge.net/sourceforge/nagios/nrpe-2.12.tar.gz
[root@UnixHot src]# ls
nagios-3.2.0.tar.gz  nagios-plugins-1.4.13.tar.gz  nrpe-2.12.tar.gz
(二)安装Nagios
官方的安装文档:http://nagios.sourceforge.net/docs/3_0/quickstart-fedora.html
1.安装前的依赖检查
[root@UnixHot ~]# rpm -q httpd php gcc glibc glibc-common gd gd-devel
2.创建Nagios账户和组
[root@UnixHot src]# useradd -m nagios
[root@UnixHot src]#groupadd nagcmd
[root@UnixHot src]# usermod -a -G nagcmd nagios
[root@UnixHot src]#usermod -a -G nagcmd apache
3.编译安装
[root@UnixHot src]# tar xvf nagios-3.2.0.tar.gz
[root@UnixHot src]# cd nagios-3.2.0
至于每一步的具体意思,我不再详述,因为运行完毕后,提示信息会明确的告诉你,它干了什么,是怎么干的。当然,还有一个最好的办法,就是看Makefile文件,里面都会有注释的。
参考Makefile文件
[root@UnixHot nagios-3.2.0]# less Makefile
[root@UnixHot nagios-3.2.0]# ./configure  --with-command-group=nagcmd \
--with-nagios-user=nagios \
--with-nagios-group=nagios
[root@UnixHot nagios-3.2.0]# make all
[root@UnixHot nagios-3.2.0]# make install
[root@UnixHot nagios-3.2.0]# make install-init          (生成init启动脚本)
[root@UnixHot nagios-3.2.0]# make install-config        (生成一些模板配置文件)
[root@UnixHot nagios-3.2.0]# make install-commandmode (设置相应的权限)
[root@UnixHot nagios-3.2.0]# make install-webconf       (生成Apache配置文件nagios.conf
4.Nagios设置Web验证的密码
(注意第一次添加用户用-c选项,以后再添加千万别在用这个选项了,会覆盖以前的所有用户的,这个在RHCE考前冲刺我已经讲了。)
[root@UnixHot nagios-3.2.0]# htpasswd -c /usr/local/nagios/etc/htpasswd.users nagiosadmin
5.设置Nagios的开机启动
[root@UnixHot ~]# chkconfig --add nagios
[root@UnixHot ~]# chkconfig nagios on
6.修改SELinux
两种方法:
第一种最直接,关闭SELinux,对于SELinux不是很熟悉的用户,请选择此。
[root@UnixHot nagios-3.2.0]# cat /etc/sysconfig/selinux
SELINUX=disabled
第二种给打上正确的安全脉络。
chcon -R -t httpd_sys_content_t /usr/local/nagios/sbin/
chcon -R -t httpd_sys_content_t /usr/local/nagios/share/
(三)安装Nagios的插件nagios-plugin
插件是Nagios扩展功能的强大武器,一般好的软件,都支持插件扩展,你可以根据实际的应用,自己开发插件。
[root@UnixHot src]# tar xvf nagios-plugins-1.4.13.tar.gz
[root@UnixHot src]# cd nagios-plugins-1.4.13
[root@UnixHot nagios-plugins-1.4.13]# ./configure --prefix=/usr/local/nagios --with-nagios-user=nagios --with-nagios-group=nagios
[root@UnixHot nagios-plugins-1.4.13]# make && make install
(四)配置检测主机是否存活
1.配置文件概述
如果安装上面的步骤,安装完成后,配置文件在安装时放在了/usr/local/nagios/etc/目录下
[root@UnixHot ~]# cd /usr/local/nagios/etc
[root@UnixHot etc]# ls -l
-rw-rw-r-- 1 nagios nagios 11408 08-30 11:55 cgi.cfg        CGI配置文件)
-rw-r--r-- 1 root   root      26 08-30 11:56 htpasswd.users Apache的验证密码文件)
-rw-rw-r-- 1 nagios nagios 43776 08-30 11:55 nagios.cfg     (主配置文件)
drwxrwxr-x 2 nagios nagios  4096 08-30 11:55 objects      (对象定义文件目录)
-rw-rw---- 1 nagios nagios  1340 08-30 11:55 resource.cfg   (资源配置文件)
我们修要修改的的是nagios.cfg objects 目录下的文件,来检测主机是否存活。
2.主配置文件nagios.cfg的配置
主配置文件的内容很多,对于这个版本,我们需要修改和添加的主要是对象配置文件,即:cfg_file=<file_name>
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/contactgroups.cfg   (添加此行联系人组定义文件)
cfg_file=/usr/local/nagios/etc/objects/hosts.cfg          (添加此行  主机定义文件)
cfg_file=/usr/local/nagios/etc/objects/hostgroups.cfg     (添加此行  主机组定义文件)
cfg_file=/usr/local/nagios/etc/objects/services.cfg       (添加此行   服务定义文件)
cfg_file=/usr/local/nagios/etc/objects/timeperiods.cfg     (时间周期定义文件)
cfg_file=/usr/local/nagios/etc/objects/templates.cfg
# Definitions for monitoring the local (Linux) host
#cfg_file=/usr/local/nagios/etc/objects/localhost.cfg    (注释掉此行)
3.主机定义文件的配置
[root@UnixHot etc]# vi objects/hosts.cfg
define host{
        host_name                       Nagios-Server
        alias                           Nagios Server
        address                         192.168.0.206
        check_command                   check-host-alive
        check_interval                  5
        retry_interval                  1
        max_check_attempts              5
        check_period                    24x7
        process_perf_data               0
        retain_nonstatus_information    0
        contact_groups                  sagroup
        notification_interval           30
        notification_period             24x7
        notification_options            dur
        }
define host{
        host_name                       Java-Server
        alias                           Java Server
        address                         192.168.0.157
        check_command                   check-host-alive
        check_interval                  5
        retry_interval                  1
        max_check_attempts              5
        check_period                    24x7
        process_perf_data               0
        retain_nonstatus_information    0
        contact_groups                  sagroup
        notification_interval           30
        notification_period             24x7
        notification_options            dur
        }
define host{
        host_name                       Oracle-Server
        alias                           Oracle Server
        address                         192.168.0.155
        check_command                   check-host-alive
        check_interval                  5
        retry_interval                  1
        max_check_attempts              5
        check_period                    24x7
        process_perf_data               0
        retain_nonstatus_information    0
        contact_groups                  sagroup
        notification_interval           30
        notification_period             24x7
        notification_options            dur
        }
define host{
        host_name                       MySQL-Server
        alias                           MySQL Server
        address                         192.168.0.100
        check_command                   check-host-alive
        check_interval                  5
        retry_interval                  1
        max_check_attempts              5
        check_period                    24x7
        process_perf_data               0
        retain_nonstatus_information    0
        contact_groups                  sagroup
        notification_interval           30
        notification_period             24x7
        notification_options            dur
        }
4.主机组定义文件的配置
[root@UnixHot etc]# vi objects/hostgroups.cfg
define hostgroup {
         hostgroup_name  System-Admin
         alias           system Admin
         members         Nagios-ServerOracle-ServerJava-ServerMySQL-Server
         }
5.服务定义文件的配置
[root@UnixHot etc]# vi objects/services.cfg
define service {
        host_name               Nagios-Server
        service_description     check-host-alive
        check_period            24x7
        max_check_attempts      4
        normal_check_interval   3
        retry_check_interval    2
        contact_groups          sagroup
        notification_interval   10
        notification_period     24x7
        notification_options    wucr
        check_command           check-host-alive
        }
define service {
        host_name               Oracle-Server
        service_description     check-host-alive
        check_period            24x7
        max_check_attempts      4
        normal_check_interval   3
        retry_check_interval    2
        contact_groups          sagroup
        notification_interval   10
        notification_period     24x7
        notification_options    wucr
        check_command           check-host-alive
        }
define service {
        host_name               Java-Server
        service_description     check-host-alive
        check_period            24x7
        max_check_attempts      4
        normal_check_interval   3
        retry_check_interval    2
        contact_groups          sagroup
        notification_interval   10
        notification_period     24x7
        notification_options    wucr
        check_command           check-host-alive
        }
define service {
        host_name               MySQL-Server
        service_description     check-host-alive
        check_period            24x7
        max_check_attempts      4
        normal_check_interval   3
        retry_check_interval    2
        contact_groups          sagroup
        notification_interval   10
        notification_period     24x7
        notification_options    wucr
        check_command           check-host-alive
        }
6.联系组定义文件配置
[root@UnixHot etc]# vi objects/contactgroups.cfg
define contactgroup {

        contactgroup_name    sagroup
        alias                system administrator group
        members              nagiosadmin
}
7.修改目录的所有者
[root@UnixHot etc]# chown -R nagios:nagios objects/
8.检测配置文件是否正确
[root@UnixHot etc]# /usr/local/nagios/bin/nagios -v /usr/local/nagios/etc/nagios.cfg
Total Warnings: 0
Total Errors:   0
如果都是0说明配置成功了,如果有错误,好好检查一下。
9.启动Nagios服务
[root@UnixHot etc]# /etc/init.d/httpd restart
[root@UnixHot etc]# /etc/init.d/nagios start
(五)第一部分功能测试
在浏览器输入网址:http://192.168.0.206/nagios/

  第一部分的功能已经实现了,就是简单的监控四台服务器是否存活,当然,对于系统监控来说,这是远远不够的,对于Nagios来说,他的真正功能功能还没有发挥出来,而且我想,朋友们都每一步怎么配置,为什么这么配了解的还不够深入,在下部分的文档中,会详细的叙述,别担心!
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP