Cacti+Nagios完全攻略(二)整合cacti与nagios安装部署

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 使用设置-后续
posted on 2012-02-16 16:43  草原和大树  阅读(9168)  评论(0编辑  收藏  举报