Nagios简介
 
      Nagios是一个用来监视系统和网络的开源应用软件,它通常运行于一个主服务器上,这个服务器运行 Liunx 或 Unix 操作系统。Nagios 利用其众多的插件实现对本机和远端服务的监控,当被监控对象出现异常,Nagios 就会及时给管理人员告警。它是一个基于TCP/IP协议的软件包,包含有nagios主程序和它的各个插件,配置非常灵活,可以监视的项目很多,也可以通过自定义 shell 脚本进行监控服务,非常适合各类企业的网络应用。 
       Nagios功能非常强大,它可以监控服务和主机,但是他自身并不包括这部分功能的代码,所有的监控、检测功能都是有插件来完成的。再说报警功能,如果监控系统发现问题不能报警那就没有意义了,所以报警也是nagios很重要的功能之一。但是,同样的,nagios自身也没有报警部分的代码,甚至没有插件,而是交给用户或者其他相关开源项目组去完成。nagios安装,是指基本平台,也就是nagios软件包的安装。它是监控体系的框架,也是所有监控的基础。 
       打开nagios官方的文档,会发现nagios基本上没有什么依赖包,只要求系统是linux或者其他nagios支持的系统。不过如果你没有安装apache(http服务),那么你就没有那么直观的界面来查看监控信息了,所以apache姑且算是一个前提条件。 
    
Nagios 系统的特点主要有下面几点:
  • 监控主机资源和网络服务
  •  
  • 允许用户通过设计实现简单的插件来监控自己特定的服务
  •  
  • 当被监控对象出现问题时,会及时通知管理人员
  •  
  • 事先定义事件处理程序,当对象出现问题时自动调用对应的处理程序
  •  
  • 通过 web 页面来监视对象状态,警告提示和日志文件。
 
需要安装软件列表
 
php-devel-5.1.6-23.2.el5_3.i386.rpm
php-json-ext-1.2.1.tar.bz2
cacti-plugin-0.8.7e-PA-v2.6.zip
nagios-3.2.1.tar.gz
nagios-plugins-1.4.14
ndoutils-1.4b9.tar.gz
npc-2.0.4.tar.gz
 

 

Nagios安装
 
[root@cacti-qmail soft]# tar -zxvf  nagios-3.2.1.tar.gz 
[root@cacti-qmail soft]#cd  nagios-3.2.1
[root@cacti-qmail  nagios-3.2.1]# ./configure --prefix=/usr/local/nagios
[root@cacti-qmail  nagios-3.2.1]#make all
 
输入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来安装主程序,CGI和HTML文件
 
 make install-init
     - This installs the init script in /etc/rc.d/init.d
 
    使用make install-init在/etc/rc.d/init.d安装启动脚本
 
 make install-commandmode
     - This installs and configures permissions on the
       directory for holding the external command file
 
    使用make install-commandmode来配置目录权限
 
 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-commandmode来安装示例配置文件,安装的路径是/usr/local/nagios/etc
 
 make install-webconf
     - This installs the Apache config file for the Nagios
       web interface
   使用make install-webconf来安装apache的配置文件
 
=================================================================
 
make install 在安装这个动作之前要增加一个 nagios 用户,并修改它的权限 , 不然会出现以下错误
=================================================================
cd ./base && make install
make[1]: Entering directory `/home/yahoon/nagios/nagios-2.9/base'
make install-basic
make[2]: Entering directory `/home/yahoon/nagios/nagios-2.9/base'
/usr/bin/install -c -m 775 -o nagios -g nagios -d /usr/local/nagios/bin
/usr/bin/install: invalid user `nagios' 非法用户nagios
make[2]: *** [install-basic] Error 1
make[2]: Leaving directory `/home/yahoon/nagios/nagios-2.9/base'
make[1]: *** [install] Error 2
make[1]: Leaving directory `/home/yahoon/nagios/nagios-2.9/base'
make: *** [install] Error 2
=====================================================================
 
[root@cacti-qmail  nagios-3.2.1]# useradd nagios
[root@cacti-qmail  nagios-3.2.1]#mkdir /usr/local/nagios
[root@cacti-qmail  nagios-3.2.1]#chown nagios.nagios /usr/local/nagios
[root@cacti-qmail  nagios-3.2.1]#make all
[root@cacti-qmail  nagios-3.2.1]#make install; make install-init;make install-commandmode;make install-config;make install-webconf
验证程序是否被正确安装
  切换目录到安装路径(这里是/usr/local/nagios),看是否存在 etc、bin、 sbin、 share、 var这五个目录,如果存在则可以表明程序被正确的安装到系统了。后表是五个目录功能的简要说明:
bin Nagios执行程序所在目录,nagios文件即为主程序
etc Nagios配置文件位置,初始安装完后,只有几个*.cfg-sample文件
sbin Nagios Cgi文件所在目录,也就是执行外部命令所需文件所在的目录
Share Nagios网页文件所在的目录
Var Nagios日志文件、spid 等文件所在的目录
修改nagios页面文件
[root@cacti-qmail nagios-3.2.1]# more /etc/httpd/conf/nagios.conf
# SAMPLE CONFIG SNIPPETS FOR APACHE WEB SERVER
# Last Modified: 11-26-2005
#
# This file contains examples of entries that need
# to be incorporated into your Apache web server
# configuration file. Customize the paths, etc. as
# needed to fit your system.
 
ScriptAlias /nagios/cgi-bin "/usr/local/nagios/sbin"
 
<Directory "/usr/local/nagios/sbin">
# SSLRequireSSL
   Options ExecCGI
   AllowOverride None
   Order allow,deny
   Allow from all
# Order deny,allow
# Deny from all
# Allow from 127.0.0.1
   AuthName "Nagios Access"
   AuthType Basic
   AuthUserFile /usr/local/nagios/etc/htpasswd.users
   Require valid-user
</Directory>
 
Alias /nagios "/usr/local/nagios/share"
 
<Directory "/usr/local/nagios/share">
# SSLRequireSSL
   Options None
   AllowOverride None
   Order allow,deny
   Allow from all
# Order deny,allow
# Deny from all
# Allow from 127.0.0.1
   AuthName "Nagios Access"
   AuthType Basic
   AuthUserFile /usr/local/nagios/etc/htpasswd.users
   Require valid-user
</Directory>
基本上这个文件可以直接使用,所以直接复制过去
[root@cacti-qmail nagios-3.2.1]# cp /etc/httpd/conf/nagios.conf  /var/www/html/
生成nagios页面访问账号
根据这个配置文件中指定的AuthUserFile项,它指定的是通过Apache访问Nagios的合法用户的帐户名单库文件,需要使用htpasswd命令对这个名单库进行用户的添加。而默认安装环境下在/usr/local/nagios/etc/目录下是没有 htpasswd.uses这个文件的,所以需要手动添加。请特别注意,用root身份建立的htpasswd.user文件的权限问题,应该在建立该文件完毕后立即更改为nagios用户nagios组的所有权
 
[root@cacti-qmail nagios-3.2.1]# touch /usr/local/nagios/etc/htpasswd.users
 
建立Nagios的网页访问用户帐户文件(这里必须要与Nagios网页配置文件当中AuthUserFile指定的路径文件名一致)
 
[root@cacti-qmail nagios-3.2.1]# /usr/bin/htpasswd -c /usr/local/nagios/etc/htpasswd.users
viong
New password:
Re-type new password:
Adding password for user viong
 
[root@cacti-qmail nagios-3.2.1]# cat /usr/local/nagios/etc/htpasswd.user                          
viong:HbfEis6w0gKqI
修改nagios主配置文件 
[root@cacti-qmail nagios-3.2.1]# vi /usr/local/nagios/etc/nagios.cfg
log_file=/usr/local/nagios/var/nagios.log( 默认无nagios.log文件,需要手动创建)
command_check_interval=15s
#command_check_interval=-1
设定命令检查的时间间隔,将原来值-1的项注释掉,启用原来备用值为15s(15秒)。这项的时间间隔值请按实际需求情况设定。
 
[root@cacti-qmail nagios-3.2.1]# touch /usr/local/nagios/var/nagios.log    
[root@cacti-qmail nagios-3.2.1]# chown nagios.nagios /usr/local/nagios/var/nagios.log     
NagiosCGI配置文件                      
[root@cacti-qmail nagios-3.2.1]# vi /usr/local/nagios/etc/cgi.cfg
use_authentication=1 (默认是启动身份验证)
设定启用身份验证
authorized_for_system_information=viong
设定系统信息的授权用户
authorized_for_configuration_information=viong
设定配置信息的授权用户
authorized_for_system_commands=viong
设定系统命令的授权用户
authorized_for_all_services=viong
设定全部监测服务的授权用户。
authorized_for_all_hosts=viong
设定全部被监测主机的授权用户
authorized_for_all_service_commands=viong
设定全部监测服务命令的授权用
authorized_for_all_host_commands=viong
设定全部主机命令的授权用户
测试nagios是否成功
设置nagios为开机启动
[root@cacti-qmailnagios-plugins-1.4.14]#chkconfig nagios on
[root@cacti-qmailnagios-plugins-1.4.14]# service nagios start
 
测试nagios是否成功启动
在IE输入  http://192.168.15.73/nagios/ 跳出以下窗口

 

 
 
nagios-plugins插件安装
[root@cacti-qmail soft]#wget
[root@cacti-qmail soft]# tar -zxvf nagios-plugins-1.4.14.tar.gz
[root@cacti-qmail nagios-plugins-1.4.14]#./configure --prefix=/usr/local/nagios/
(注意:安装目录是nagios而不是nagios-plugins的原因是直接把libexec目录的插件覆盖到nagios里面去的)
[root@cacti-qmail nagios-plugins-1.4.14]#make;make install
[root@cacti-qmail nagios-plugins-1.4.14]# grep ^User /etc/httpd/conf/httpd.conf
将apache的运行用户加到nagios组里面,从httpd.conf中过滤出当前的apache运行用户
我这里过滤出的用户无,如有用户需要运行以下命令
usermod -G nagios 用户名
 
PHP支持JSON扩展
PHP必须安装PDO 和 JSON扩展。由于NPC使用了Ext JS,如果没有JSON扩展,NPC的界面不会出来,只能看到一个空白页面。初始化PHP环境 需要用phpize命令,所以也要安装 php-devel。
root@cacti-qmail soft]#wget http://www.aurore.net/projects/php-json/php-json-ext-1.2.1.tar.bz2
root@cacti-qmail soft]# tar  jxvf php-json-ext-1.2.1.tar.bz2
[root@cacti-qmail soft]# cd php-json-ext-1.2.1
[root@cacti-qmail php-json-ext-1.2.1]# phpize
-bash: phpize: command not found
[root@cacti-qmail php-json-ext-1.2.1]# cd /misc/Server/
[root@cacti-qmail Server]# rpm -ivh  php-devel-5.1.6-23.2.el5_3.i386.rpm
warning: php-devel-5.1.6-23.2.el5_3.i386.rpm: Header V3 DSA signature: NOKEY, key ID 37017186
Preparing...                 ########################################### [100%]
   1:php-devel              ########################################### [100%]
[root@cacti-qmail Server]# cd /root/soft/php-json-ext-1.2.1
[root@cacti-qmail php-json-ext-1.2.1]# phpize                                       
Configuring for:
PHP Api Version:          20041225
Zend Module Api No:       20050922
Zend Extension Api No:    220051025
[root@cacti-qmail php-json-ext-1.2.1]# ./configure
 ===================================================================
如果遇到报以下错误,请直接yum install gcc-c++
a.configure: error: no acceptable C compiler found in $PATH
See `config.log' for more details.  请直接yum install gcc
b.configure: error: C++ preprocessor "/lib/cpp" fails sanity check   
====================================================================
[root@cacti-qmail php-json-ext-1.2.1]# make;make install
[root@cacti-qmail php-json-ext-1.2.1]# find / -name *json.so
/usr/lib/php/modules/json.so
/root/soft/php-json-ext-1.2.1/modules/json.so
/root/soft/php-json-ext-1.2.1/.libs/json.so
[root@cacti-qmail php-json-ext-1.2.1]# vi /etc/php.d/json.ini
添加以下内容
extension=json.so
保存退出
 
[root@cacti-qmail php-json-ext-1.2.1]# service httpd restart
 
测试PHP是否支持json扩展
[root@cacti-qmail php-json-ext-1.2.1]#vi /var/www/html/test.php
添加以下内容
<?php
phpinfo();
phpinfo(INFO_GENERAL);
phpinfo(1);
?>
保存退出
在IE输入 http://192.168.15.73/test.php    看到以下信息
 

 
ndoutils安装
ndoutils是将Nagios的配置及监控信息存储到数据库里,NPC通过调用ndo所存储的数据来展现Nagios的信息。ndoutils需要用到mysql的mysql-lib及mysql-inc,因此需要安装mysql-devel。由于刚装系统的时候已经装过了.这个软件本节就不用安装.
[root@cacti-qmail soft]# tar -zxvf ndoutils-1.4b9.tar.gz
[root@cacti-qmail soft]# cd ndoutils-1.4b9
[root@cacti-qmail ndoutils-1.4b9]#./configure
--with-mysql-inc=/usr/include/mysql --with-mysql-lib=/usr/lib/mysql --enable-mysql --with-ndo2db-user=nagios --with-ndo2db-group=nagios
 
[root@cacti-qmail ndoutils-1.4b9]#make
编译完成后(不需要make install)
[root@cacti-qmail ndoutils-1.4b9]# cd src/
[root@cacti-qmail src]# ll
总计 1624
-rw-r--r-- 1 1001 1001 22480 09-15 20:04 db.c
-rw-r--r-- 1 root root 145468 03-02 01:54 dbhandlers-2x.o
-rw-r--r-- 1 root root 145444 03-02 01:54 dbhandlers-3x.o
-rw-r--r-- 1 1001 1001 169907 10-27 15:51 dbhandlers.c
-rw-r--r-- 1 root root 37872 03-02 01:54 db.o
-rwxr-xr-x 1 root root 31342 03-02 01:54  file2sock
-rw-r--r-- 1 1001 1001    5421 10-27 16:08 file2sock.c
-rw-r--r-- 1 1001 1001 11485 10-21 15:53 io.c
-rw-r--r-- 1 root root 16432 03-02 01:54 io.o
-rwxr-xr-x 1 root root 32602 03-02 01:54  log2ndo
-rw-r--r-- 1 1001 1001    6780 10-27 16:08 log2ndo.c
-rw-r--r-- 1 root root    4427 03-02 01:53 Makefile
-rw-r--r-- 1 1001 1001    4336 09-15 20:04 Makefile.in
-rwxr-xr-x 1 root root 224645 03-02 01:54 ndo2db-2x
-rwxr-xr-x 1 root root 224629 03-02 01:54  ndo2db-3x
-rw-r--r-- 1 1001 1001 57565 10-27 16:08 ndo2db.c
-rwxr-xr-x 1 root root 120333 03-02 01:54 ndomod-2x.o
-rwxr-xr-x 1 root root 128498 03-02 01:54  ndomod-3x.o
-rw-r--r-- 1 1001 1001 136855 10-27 16:08 ndomod.c
-rw-r--r-- 1 1001 1001     514 2007-01-08 protonum.c
-rwxr-xr-x 1 root root 28503 03-02 01:54 sockdebug
-rw-r--r-- 1 1001 1001    3418 10-27 16:08 sockdebug.c
-rw-r--r-- 1 1001 1001    4623 2009-07-17 utils.c
-rw-r--r-- 1 root root    8824 03-02 01:54 utils.o 
 
[root@cacti-qmail src]# cp ndomod-3x.o ndo2db-3x log2ndo file2sock /usr/bin/
[root@cacti-qmail src]# cd ../config
 
[root@cacti-qmail config]# ll
总计 64
-rw------- 1 root root 439 03-02 01:53 misccommands.cfg
-rw-r--r-- 1 1001 1001 427 09-07 04:51 misccommands.cfg.in
-rw------- 1 root root 566 03-02 01:53 nagios.cfg
-rw-r--r-- 1 1001 1001 522 09-07 04:51 nagios.cfg.in
-rw------- 1 root root 4486 03-02 02:06 ndo2db.cfg
-rw------- 1 root root 4493 03-02 01:53  ndo2db.cfg-sample
-rw-r--r-- 1 1001 1001 4489 09-15 20:08 ndo2db.cfg-sample.in
-rw------- 1 root root 5130 03-02 01:59 ndomod.cfg
-rw------- 1 root root 5130 03-02 01:53  ndomod.cfg-sample
-rw-r--r-- 1 1001 1001 5112 09-07 04:51 ndomod.cfg-sample.in
[root@cacti-qmail config]#cp ndo2db.cfg-sample ndo2db.cfg
[root@cacti-qmail config]#cp ndomod.cfg-sample ndomod.cfg
[root@cacti-qmail config]#cp ndo2db.cfg ndomod.cfg /usr/local/nagios/etc/
[root@cacti-qmail config]#cd /usr/local/nagios/etc
[root@cacti-qmail etc]# vi ndo2db.cfg
修改以下参数
socket_name=/usr/local/nagios/var/ndo.sock
db_name=cacti 
db_prefix=npc_ 
db_user=viong
db_pass=viong0623
debug_level=1
debug_file=/usr/local/nagios/var/ndo2db.debug
保存退出
 
[root@cacti-qmail etc]# vi ndomod.cfg
修改以下参数
output=/usr/local/nagios/var/ndo.sock
buffer_file=/usr/local/nagios/var/ndomod.tmp
保存退出
 
NPC安装
NPC(Nagios Plugin for Cacti)是一个Cacti插件,安装后可以在Cacti界面里使用Nagios的功能
[root@cacti-qmail soft]# wget http://www.constructaegis.com/downloads/npc-2.0.4.tar.gz
[root@cacti-qmail soft]# tar -zxvf npc-2.0.4.tar.gz
[root@cacti-qmail soft]# mv npc/ /var/www/html/cacti/plugins/
[root@cacti-qmail soft]# vi /var/www/html/cacti/include/global.php
添加以下参数
$plugins= array();
$plugins[] = 'npc';
如下图:

保存退出
 
NPC界面设置
点击(console)菜单栏 user management------->admin

   

在realm permission---plugin management(打勾)
 
返回(console)菜单栏,点击configuration--------plugin management—安装并激活npc插件
 
返回(console)菜单栏,点击configuration-------settings----NPC,修改一下三个参数
 
 
返回(console)菜单栏,点击插件链接区-NPC

 
关于nagios 使用设置-后续
cacti插件npc(Nagios Plugin for Cacti),是一个让cacti可以抓取nagios性能数据的插件。网上搜索cacti npc一般都是讲怎么安装的,基本上没讲装好后怎么使用。就连官方网站上都没讲(反正我是没找到)。
  至于怎么安装,需要注意哪些事项,这里就不说了。网上可以搜索到很多。此文只讲简单建立一个绘图。这只是我摸索出来的方法,没能参考到官方文档,是否正确我不确定,反正有绘图出来。基本流程是:定义数据输入方法-定义数据模版-定义绘图模版
  我的运行环境是ubuntu 10.04; nagios 3; ndoutils 1.4b7; mysql 5.1; cacti 0.8.7c; npc 2.0.4
  首先进入管理页面,点开npc标签。下面有个N2C的菜单,从注释意思看,好像是从这里把nagios的主机转成cacti的模版。但我点了import成功后,在cacti里没找到任何东西。也许从这里添加到cacti才是正确的。但我没成功,只好手动来弄了。
  cacti绘图实例要引用绘图模版,绘图模版要引用数据模版,数据模版要引用数据输入方法。所以我们先从添加数据输入方法Data Input Method开始。
Screenshot-Cacti - Mozilla Firefox.png 
  点services,可以看到nagios里定义的所有服务。在想要取数据绘图的服务上双击或点右键选Service Detail。
Screenshot-Cacti - Mozilla Firefox-1.png 
  在上面右边有个Data Input Method标签,点一下,OK。这样就往cacti里引入了一条数据输入脚本。这里以一台名称为node7主机上的检查cpu负载服务为例。好了,npc标签要做的就没有了。下面转到console标签。
Screenshot-Cacti - Mozilla Firefox-2.png 
  点下Data Input Methods,可以看到刚才添加的cacti数据输入方法“NPC - Perfdata - node7: Current Load”在列表里了。点一下”NPC - Perfdata - node7: Current Load“,可以看它的属性
Screenshot-Cacti - Mozilla Firefox-3.png 
  可以看到它的Input Type为Script/Command,一般的cacti数据输入都是通过snmp的,这里则是通过执行脚本来读取的。下面显示是执行perfdata.php脚本文件,参数为 --type=service --id=14。npc插件把nagios的每个服务都编了号,我这个node7主机check_load服务被编到14号。下面Output Fields栏显示的是这个输入方法定义的输出字段名,待会儿要定义的数据模版就要引用这些字段。(这里npc 有个bug,点开load5和load15。“Field [Output]”的值" load5"或" load15"前面多了个空格,把空格去掉,不然后来取不到5分钟和15分钟的数据)好了,现在点击左侧菜单Data Templates,开始添加数据模版。
Screenshot-Cacti - Mozilla Firefox-4.png 
  这里cacti自带的模版当然没有从npc取数据的。我们要添加一个,点右上方Add,也可以右下方Choose a actionuplicate复制一个已有的来修改。我复制了一个Unix - Load Average,因为跟我的cpu负载检查服务比较接近,只要改一下输入方法Data Input Method就好了。
Screenshot-Cacti - Mozilla Firefox-5.png 
  修改模版名,我改成了npc - Unix - Load Average。把Data Input Method从Unix - Get Load Average改成前面定义的NPC - Perfdata - node7:Current Load。
Screenshot-Cacti - Mozilla Firefox-6.png 
  下面已经有了三个Data Source Item,load_1min;load_5min;load_15min。如果刚才模版不是复制的,而是添加的,就要手动建立三个。把这三个Data Source Item项,下面Output Field选成相应的先前数据输入方法里的字段。比如load_1min的Output Field要选成load1 - load1,不然绘出来的图数据不正确。好了,数据模版完成了,再左侧点开Graph Templates开始建立绘图模版。
Screenshot-Cacti - Mozilla Firefox-7.png 
  跟刚才一样,绘图模版也可以自己新建或是复制一个已有的模版来修改。我复制了一个Unix - Load Average来修改。改模版名为npc - Unix - Load Average。
Screenshot-Cacti - Mozilla Firefox-8.png 
     首先来看中间有三项Graph Item Inputs,1 Minute Data Source;5 Minute Data Source;15 Minute Data Source。这是绘图的三个输入数据项名称。每个数据项又是引用的上面Graph Item中的某项。点开1 Minute Data Source
Screenshot-Cacti - Mozilla Firefox-9.png 
  可以看到是引用了刚才Graph Item栏中的哪几项,1 Minute Data Source是引用了Graph Item中Item # 1和Item # 2的数据。回到刚才的页面。
Screenshot-Cacti - Mozilla Firefox-10.png 
    点开Graph Item栏中的Item # 1
Screenshot-Cacti - Mozilla Firefox-11.png 
  把Data Source数据源改成先前在数据模版中定义Data Source Item项,因为Item # 1是被1 Minute Data Source引用,1 Minute Data Source很明示是绘图时显示1分钟内负载的数据,所以Item # 1就选npc - Unix - Load Average - (load 1min)。刚才Graph Item Inputs项1 Minute Data Source中也包括了Item # 2,所以Item # 2的Data Source Item也选npc - Unix - Load Average - (load 1min)
Screenshot-Cacti - Mozilla Firefox-12.png 
   接下来就是cacti的常规操作了。添加设备,我添加了跟刚才服务一致的主机node7,建立绘图时选择刚才建立的npc - Unix - Load Average绘图模版。这时候还看不到图表的,我这张截图是后来截的。
Screenshot-Cacti - Mozilla Firefox-13.png 
   把绘图添加到Graph Trees。数分钟后就可以看到图表了
   如果没有,看看rra目录权限有没有问题。或者直接用npc的那个脚本看能不能获取数据。在Data Input Methods里点开自己定义的数据输入方法,可以查到npc里自己这个服务的编号。然后用php -q 路径/perfdata.php --type=service --id=服务编号,查看能不能获取到数据
Screenshot.png