`
xiaolin0199
  • 浏览: 565089 次
  • 性别: Icon_minigender_1
  • 来自: 深圳
社区版块
存档分类
最新评论

CentOS安装和配置Nagios

 
阅读更多

Nagios简介

写道
Nagios是一款开源的电脑系统和网络监视工具,能有效的监控windows、Linux和Unix的主机状态,交换机路由器等网络设置,打印机等。在系统或服务状态异常时发出邮件或短信报警第一时间通知网站运维人员,在状态恢复后发出正常的邮件或短信通知。

Nagios原名为Netsaint,由Ethan Galstad开发并维护至今。NAGIOS是一个缩写形式: "Nagios Ain't Gonna Insist On Sainthood" ,Sainthood 翻译为圣徒,而"Agios"是"saint"的希腊表示方法。Nagios被开发在Linux下使用,但在Unix下也工作得非常好。

 

主要功能

写道
网络服务监控(SMTP、POP3、HTTP、NNTP、ICMP、SNMP、FTP、SSH)
主机资源监控(CPU load、disk usage、system logs),也包括Windows主机(使用NSClient++ plugin)
可以指定自己编写的Plugin通过网络收集数据来监控任何情况(温度、警告……)
可以通过配置Nagios远程执行插件远程执行脚本
远程监控支持SSH或SSL加通道方式进行监控
简单的plugin设计允许用户很容易的开发自己需要的检查服务,支持很多开发语言(shell scripts、C++、Perl、ruby、Python、PHP、C#等)
包含很多图形化数据Plugins(Nagiosgraph、Nagiosgrapher、PNP4Nagios等)
可并行服务检查
能够定义网络主机的层次,允许逐级检查,就是从父主机开始向下检查
当服务或主机出现问题时发出通告,可通过email, pager, sms 或任意用户自定义的plugin进行通知
能够自定义事件处理机制重新激活出问题的服务或主机
自动日志循环
支持冗余监控
包括Web界面可以查看当前网络状态,通知,问题历史,日志文件等

 

工作原理

写道
Nagios的功能是监控服务和主机,但是他自身并不包括这部分功能,所有的监控、检测功能都是通过各种插件来完成的。

启动Nagios后,它会周期性的自动调用插件去检测服务器状态,同时Nagios会维持一个队列,所有插件返回来的状态信息都进入队列,Nagios每次都从队首开始读取信息,并进行处理后,把状态结果通过web显示出来。

Nagios提供了许多插件,利用这些插件可以方便的监控很多服务状态。安装完成后,在nagios主目录下的/libexec里放有nagios自带的可以使用的所有插件,如,check_disk是检查磁盘空间的插件,check_load是检查CPU负载的,等等。每一个插件可以通过运行./check_xxx –h 来查看其使用方法和功能。

Nagios可以识别4种状态返回信息,即 0(OK)表示状态正常/绿色、1(WARNING)表示出现警告/黄色、2(CRITICAL)表示出现非常严重的错误/红色、3(UNKNOWN)表示未知错误/深黄色。Nagios根据插件返回来的值,来判断监控对象的状态,并通过web显示出来,以供管理员及时发现故障。

再说报警功能,如果监控系统发现问题不能报警那就没有意义了,所以报警也是nagios很重要的功能之一。但是,同样的,Nagios 自身也没有报警部分的代码,甚至没有插件,而是交给用户或者其他相关开源项目组去完成的。

 

NRPE

写道
Nagios 通过NRPE 来远端管理服务

1. Nagios 执行安装在它里面的check_nrpe 插件,并告诉check_nrpe 去检测哪些服务。

2. 通过SSL,check_nrpe 连接远端机子上的NRPE daemon

3. NRPE 运行本地的各种插件去检测本地的服务和状态(check_disk,..etc)

4. 最后,NRPE 把检测的结果传给主机端的check_nrpe,check_nrpe 再把结果送到Nagios状态队列中。

5. Nagios 依次读取队列中的信息,再把结果显示出来。

 

Nagios服务端(192.168.0.182)安装

写道
安装基础支持套件

首先查看系统是否安装这些基础套件:
[root@nagios ~]# rpm -q gcc glibc glibc-common gd gd-devel xinetd openssl-devel
package gcc is not installed
glibc-2.12-1.107.el6.x86_64
glibc-common-2.12-1.107.el6.x86_64
package gd is not installed
package gd-devel is not installed
package xinetd is not installed
package openssl-devel is not installed

可以看出有很多还是没有安装的,安装:
[root@nagios ~]# yum install -y gcc glibc glibc-common gd gd-devel xinetd openssl-devel

 

创建Nagios用户和用户组

写道
[root@nagios ~]# useradd -s /sbin/nologin nagios
[root@nagios ~]# mkdir /usr/local/nagios
[root@nagios ~]# chown -R nagios.nagios /usr/local/nagios/

 

查看Nagios目录的权限

写道
[root@nagios ~]# ll -d /usr/local/nagios/
drwxr-xr-x. 2 nagios nagios 4096 11月 28 19:13 /usr/local/nagios/

 

编译安装Nagios

写道
[root@nagios src]# wget http://prdownloads.sourceforge.net/sourceforge/nagios/nagios-3.4.3.tar.gz
[root@nagios src]# tar xzvf nagios-3.4.3.tar.gz
[root@nagios nagios]# ./configure --prefix=/usr/local/nagios
*** Configuration summary for nagios 3.4.3 11-30-2012 ***:

General Options:
-------------------------
Nagios executable: nagios
Nagios user/group: nagios,nagios
Command user/group: nagios,nagios
Embedded Perl: no
Event Broker: yes
Install ${prefix}: /usr/local/nagios
Lock file: ${prefix}/var/nagios.lock
Check result directory: ${prefix}/var/spool/checkresults
Init directory: /etc/rc.d/init.d
Apache conf.d directory: /etc/httpd/conf.d
Mail program: /bin/mail
Host OS: linux-gnu

Web Interface Options:
------------------------
HTML URL: http://localhost/nagios/
CGI URL: http://localhost/nagios/cgi-bin/
Traceroute (used by WAP):


Review the options above for accuracy. If they look okay,
type 'make all' to compile the main program and CGIs.

 

编译完成以后,依据提示,运行make all:

写道
[root@nagios nagios]# make all
*** Compile finished ***

If the main program and CGIs compiled without any errors, you
can continue with installing Nagios as follows (type 'make'
without any arguments for a list of all possible options):

make install
- This installs the main program, CGIs, and HTML files

make install-init
- This installs the init script in /etc/rc.d/init.d

make install-commandmode
- This installs and configures permissions on the
directory for holding the external command file

make install-config
- This installs *SAMPLE* config files in /usr/local/nagios/etc
You'll have to modify these sample files before you can
use Nagios. Read the HTML documentation for more info
on doing this. Pay particular attention to the docs on
object configuration files, as they determine what/how
things get monitored!

make install-webconf
- This installs the Apache config file for the Nagios
web interface

make install-exfoliation
- This installs the Exfoliation theme for the Nagios
web interface

make install-classicui
- This installs the classic theme for the Nagios
web interface

 

按照提示一步一步安装,并添加Nagios服务:

写道
[root@nagios nagios]# make install
[root@nagios nagios]# make install-init
[root@nagios nagios]# make install-commandmode
[root@nagios nagios]# make install-config
[root@nagios nagios]# chkconfig --add nagios
[root@nagios nagios]# chkconfig --level 35 nagios on
[root@nagios nagios]# chkconfig --list nagios
nagios 0:关闭1:关闭2:关闭3:启用4:启用5:启用6:关闭

 

 

验证程序是否被正确安装

写道
切换目录到安装路径(这里是/usr/local/nagios),看是否存在etc、bin、sbin、share、var、libexec 这六个目录,如果存在则可以表明程序被正确的安装到系统了。

[root@nagios ~]# cd /usr/local/nagios/
[root@nagios nagios]# ls
bin etc libexec sbin share var

Nagios各个目录用途说明如下:
bin Nagios可执行程序所在目录
etc Nagios配置文件所在目录
sbin Nagios CGI文件所在目录,也就是执行外部命令所需文件所在的目录
share Nagios网页文件所在的目录
libexec Nagios外部插件所在目录
var Nagios日志文件、lock 等文件所在的目录
var/archives Nagios日志自动归档目录
var/rw 用来存放外部命令文件的目录

 

安装Nagios插件

[root@nagios src]# wget https://www.nagios-plugins.org/download/nagios-plugins-1.5.tar.gz
[root@nagios src]# tar xzvf nagios-plugins-1.5.tar.gz 
[root@nagios src]# cd nagios-plugins-1.5
[root@nagios nagios-plugins-1.5]# ./configure --prefix=/usr/local/nagios/
[root@nagios nagios-plugins-1.5]# make && make install 

 

安装Apache和Php

Apache和Php不是安装nagios所必须的,但是nagios提供了web监控界面,通过web监控界面可以清晰的看到被监控主机、资源的运行状态,因此,安装一个web服务是很必要的。需要注意的是,nagios在nagios3.1.x版本以后,配置web监控界面时需要php的支持。这里我们下载的nagios版本为nagios-3.4.3,因此在编译安装完成apache后,还需要编译php模块,这里选取的php版本为php5.5.6。

 

安装Apache 

写道
[root@nagios src]# wget http://archive.apache.org/dist/httpd/httpd-2.2.26.tar.gz
[root@nagios src]# tar xzvf httpd-2.2.26.tar.gz
[root@nagios src]# cd httpd-2.2.26
[root@nagios httpd-2.2.26]# ./configure --prefix=/usr/local/apache2
[root@nagios httpd-2.2.26]# make && make install

 

安装Php

写道
[root@nagios src]# wget http://cn2.php.net/distributions/php-5.5.6.tar.gz
[root@nagios src]# tar xzvf php-5.5.6.tar.gz
[root@nagios src]# cd php-5.5.6
[root@nagios php-5.5.6]# ./configure --prefix=/usr/local/php --with-apxs2=/usr/local/apache2/bin/apxs

编译时出现报错:
checking libxml2 install dir... no
checking for xml2-config path...
configure: error: xml2-config not found. Please check your libxml2 installation.

安装Libxml2和libxml-devel后再编译就可以了:
[root@nagios apache2]# yum install libxml2 libxml2-devel -y

编译成功以后,再安装:
[root@nagios php-5.5.6]# make && make install

 

 配置Apache

写道
编辑Apache配置文件/usr/local/apache2/conf/httpd.con:

[root@nagios php-5.5.6]# vim /usr/local/apache2/conf/httpd.conf
修改如下:
User nagios
Group nagios

<IfModule dir_module>
DirectoryIndex index.html index.php
</IfModule>

增加的内容:
AddType application/x-httpd-php .php

 

安全

写道
为了安全,一般情况下要让Nagios的Web监控页面必须经过授权才能访问,这需要增加验证配置,记载httpd.conf文件最后添加如下信息:

#setting for nagios
ScriptAlias /nagios/cgi-bin "/usr/local/nagios/sbin"
<Directory "/usr/local/nagios/sbin">
AuthType Basic
Options ExecCGI
AllowOverride None
Order allow,deny
Allow from all
AuthName "Nagios Access"
AuthUserFile /usr/local/nagios/etc/htpasswd //用于此目录访问身份验证的文件
Require valid-user
</Directory>
Alias /nagios "/usr/local/nagios/share"
<Directory "/usr/local/nagios/share">
AuthType Basic
Options None
AllowOverride None
Order allow,deny
Allow from all
AuthName "nagios Access"
AuthUserFile /usr/local/nagios/etc/htpasswd
Require valid-user
</Directory>

创建apache目录验证文件,在上面的配置中,指定了目录验证文件htpasswd,下面要创建这个文件:

[root@nagios bin]# /usr/local/apache2/bin/htpasswd -c /usr/local/nagios/etc/htpasswd eminum

 

检测Apache配置文件,并启动Apache:

写道
[root@nagios bin]# ./httpd -t -f /usr/local/apache2/conf/httpd.conf
httpd: apr_sockaddr_info_get() failed for nagios.localdomain
httpd: Could not reliably determine the server's fully qualified domain name, using 127.0.0.1 for ServerName
Syntax OK

[root@nagios php-5.5.6]# /usr/local/apache2/bin/apachectl start
httpd: apr_sockaddr_info_get() failed for nagios.localdomain
httpd: Could not reliably determine the server's fully qualified domain name, using 127.0.0.1 for ServerName

 

 

打开浏览器输入192.168.0.182/nagios,出现报错,于是猜测可能是Selinux和Iptables的问题,关闭:

写道

[root@nagios bin]# setenforce 0
[root@nagios bin]# getenforce
Permissive
[root@nagios bin]# service iptables stop
iptables: Flushing firewall rules: [ OK ]
iptables: Setting chains to policy ACCEPT: filter [ OK ]
iptables: Unloading modules: [ OK ]

 

 

再打开浏览器,就可以访问Nagios了:



 

 

配置Nagios 

Nagios 主要用于监控一台或者多台本地主机及远程的各种信息,包括本机资源及对外的服务等。默认的Nagios 配置没有任何监控内容,仅是一些模板文件。若要让Nagios 提供服务,就必须修改配置文件,增加要监控的主机和服务,下面将详细介绍。

 

默认配置文件介绍

Nagios安装完毕后,默认的配置文件在/usr/local/nagios/etc目录下:

写道

[root@nagios bin]# cd /usr/local/nagios/etc/
[root@nagios etc]# ls
cgi.cfg htpasswd nagios.cfg objects resource.cfg
[root@nagios etc]# cd objects/
[root@nagios objects]# ls
commands.cfg localhost.cfg switch.cfg timeperiods.cfg
contacts.cfg printer.cfg templates.cfg windows.cfg
[root@nagios objects]#

 

 

每个文件或目录含义如下表所示:

写道
文件名或目录名 用途
cgi.cfg 控制CGI访问的配置文件
nagios.cfg Nagios 主配置文件
resource.cfg 变量定义文件,又称为资源文件,在些文件中定义变量,以便由其他配置文件引用,如$USER1$
objects objects 是一个目录,在此目录下有很多配置文件模板,用于定义Nagios 对象
objects/commands.cfg 命令定义配置文件,其中定义的命令可以被其他配置文件引用
objects/contacts.cfg 定义联系人和联系人组的配置文件
objects/localhost.cfg 定义监控本地主机的配置文件
objects/printer.cfg 定义监控打印机的一个配置文件模板,默认没有启用此文件
objects/switch.cfg 定义监控路由器的一个配置文件模板,默认没有启用此文件
objects/templates.cfg 定义主机和服务的一个模板配置文件,可以在其他配置文件中引用
objects/timeperiods.cfg 定义Nagios 监控时间段的配置文件
objects/windows.cfg 监控Windows 主机的一个配置文件模板,默认没有启用此文件

 

 

配置文件之间的关系

在nagios的配置过程中涉及到的几个定义有:主机、主机组,服务、服务组,联系人、联系人组,监控时间,监控命令等,从这些定义可以看出,nagios各个配置文件之间是互为关联,彼此引用的。

成功配置出一台nagios监控系统,必须要弄清楚每个配置文件之间依赖与被依赖的关系,最重要的有四点:

第一:定义监控哪些主机、主机组、服务和服务组; 

第二:定义这个监控要用什么命令实现; 

第三:定义监控的时间段; 

第四:定义主机或服务出现问题时要通知的联系人和联系人组。

 

配置Nagios

写道
为了能更清楚的说明问题,同时也为了维护方便,建议将nagios各个定义对象创建独立的配置文件:
创建hosts.cfg文件来定义主机和主机组
创建services.cfg文件来定义服务
用默认的contacts.cfg文件来定义联系人和联系人组
用默认的commands.cfg文件来定义命令
用默认的timeperiods.cfg来定义监控时间段
用默认的templates.cfg文件作为资源引用文件

 

templates.cfg文件

nagios主要用于监控主机资源以及服务,在nagios配置中称为对象,为了不必重复定义一些监控对象,Nagios引入了一个模板配置文件,将一些共性的属性定义成模板,以便于多次引用。这就是templates.cfg的作用。

 

resource.cfg文件

resource.cfg是nagios的变量定义文件,文件内容只有一行:

写道
[root@nagios etc]# vim resource.cfg
# Sets $USER1$ to be the path to the plugins
$USER1$=/usr/local/nagios/libexec
其中,变量$USER1$指定了安装nagios插件的路径,如果把插件安装在了其它路径,只需在这里进行修改即可。需要注意的是,变量必须先定义,然后才能在其它配置文件中进行引用

 

commands.cfg 文件

此文件默认是存在的,无需修改即可使用,当然如果有新的命令需要加入时,在此文件进行添加即可。

 

hosts.cfg 文件

此文件默认不存在,需要手动创建,hosts.cfg主要用来指定被监控的主机地址以及相关属性信息,要根据项目要求配置。

 

  • 大小: 77.3 KB
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics