标签:nagioscacti
原创作品,允许转载,转载时请务必以超链接形式标明文章 原始出处 、作者信息和本声明。否则将追究法律责任。http://alvin1.blog.51cto.com/6223380/1071323


centos5.5 + cacti + nagios +  npc

都是用的yum做的,软件版本如下:
cacti-0.8.7e.tar.gz
cacti-plugin-0.8.7e-PA-v2.6.zip
nagios-3.4.1.tar.gz
nagios-plugins-1.4.16.tar.gz
ndoutils-1.4b9.tar.gz
npc-2.0.4.tar.gz
PDO_MYSQL-1.0.2.tgz
php-json-ext-1.2.1.tar.gz
rrdtool-1.4.4.tar.gz


一、安装环境LAMP

  1. #yum -y install *gcc*

  2. #yum -y install httpd httpd-manual mysql mysql-devel mysql-server php php-devel php-mysql php-common php-pdo php-gd lm_sensors net-snmp php-snmp net-snmp-utils


  3. # chkconfig mysqld on

  4. # chkconfig httpd on

  5. # chkconfig snmpd on

  6. # service mysqld start

  7. # service httpd start

  8. # service snmpd start


  9. # mysqladmin -u root password 'redhat321'


  10. # mysql -u root -p

  11. :输入密码

  12. mysql>create database cacti;

  13. Query OK, 1 row affected (0.00 sec)


  14. mysql>grant all on cacti.* to cactiadmin@localhost identified by 'cacti321';

  15. Query OK, 0 rows affected (0.01 sec)


  16. mysql>flush privileges;

  17. Query OK, 0 rows affected (0.00 sec)


  18. mysql> \q

二、安装cacti
1、安装rrdtool需要的软件包
# yum -y install  cairo-devel libxml2-devel pango pango-devel

2、安装rrdtool

  1. # tar xf rrdtool-1.4.4.tar.gz

  2. # cd rrdtool-1.4.4

  3. # ./configure --prefix=/usr/local/rrdtool && make && make install

  4. # ln -s /usr/local/rrdtool/bin/* /usr/local/bin/

3、安装cacti-0.8.7.e

  1. # tar xf cacti-0.8.7e.tar.gz -C /var/www/html/

  2. # cd /var/www/html/

  3. # mv cacti-0.8.7e/ cacti

  4. # chown -R root.root /var/www/html/cacti/

4、导入数据库

  1. # mysql -u root -p cacti <cacti/cacti.sql

  2. Enter password:

5、修改cacti配置文件,连接数据库

  1. # vim /var/www/html/cacti/include/config.php

  2. $database_username = "cactiadmin";

  3. $database_password = "cacti321";


  4. # vim /var/www/html/cacti/include/global.php

  5. $database_username = "cactiadmin";

  6. $database_password = "cacti321";

6、安装插件cacti-plugin

  1. # wget http://mirror.cactiusers.org/downloads/plugins/cacti-plugin-0.8.7e-PA-v2.6.zip

  2. # unzip cacti-plugin-0.8.7e-PA-v2.6.zip

  3. # cd cacti-plugin-arch/

  4. # cp cacti-plugin-arch/{cacti-plugin-0.8.7e-PA-v2.6.diff,pa.sql} /var/www/html/cacti/

  5. # cd /var/www/html/cacti/

7、打补丁导入数据库

  1. # patch -p1 -N <cacti-plugin-0.8.7e-PA-v2.6.diff

  2. # mysql -uroot -p cacti<pa.sql

8、修改配置文件,如下:

  1. # vim include/global.php

  2. $config['url_path'] = '/cacti/';

9、添加计划任务

  1. # crontab -e

  2. */5 * * * * /usr/bin/php /var/www/html/cacti/poller.php

  3. # service crond restart

10、浏览器输入http://192.168.163.143/cacti/install初始化cacti,最后如下:

cacti + nagios + npc 整合部署_cacti

另外cacti无法显示图片,图片显红叉,但是点进去有图:
解决方法:
settings里面的RRDTOOLS版本设置不对,默认为1.0.x,改为1.2.x就行了。

三、被监控端配置

1、安装snmp服务
# rpm -ivh  lm_sensors-2.10.7-4.el5.i386.rpm  net-snmp-5.3.2.2-7.el5.i386.rpm

2、修改配置文件如下:(这里我省略掉了#行和空行)

  1. # vim /etc/snmp/snmpd.conf

  2. com2sec notConfigUser default public

  3. group notConfigGroup v1 notConfigUser

  4. group notConfigGroup v2c notConfigUser

  5. view systemview included .1.3.6.1.2.1.1

  6. view systemview included .1.3.6.1.2.1.25.1.1

  7. access notConfigGroup "" any noauth exact mib2 none none

  8. view mib2 included .iso.org.dod.internet.mgmt.mib-2 fc

  9. syslocation Unknown (edit /etc/snmp/snmpd.conf)

  10. syscontact Root <root@localhost> (configure /etc/snmp/snmp.local.conf)

  11. pass .1.3.6.1.4.1.4413.4.1 /usr/bin/ucd5820stat


3、重启服务
# service snmpd  restart

到此就差不多了,我们可以做些检测在服务器上,有信息返回说名正常:
# snmpwalk -v 2c -c public ServerIP if

四、安装nagios

1、建立用户

  1. # useradd -s /sbin/nologins nagios

  2. # mkdir -p /usr/local/nagios

  3. # chown -R  nagios.nagios /usr/local/nagios/

2、编译安装nagios

  1. # tar xf nagios-3.4.1.tar.gz

  2. # cd nagios

  3. # ./configure --prefix=/usr/local/nagios/

  4. # make all

  5. # make install

  6. # make install-init

  7. # make install-commandmode

  8. # make install-config

  9. # make install-webconf


  10. # chkconfig --add nagios

  11. # chkconfig nagios on

  12. # service nagios start

3、安装插件

  1. # tar xf nagios-plugins-1.4.16.tar.gz

  2. # cd nagios-plugins-1.4.16

  3. # ./configure --prefix=/usr/local/nagios/ && make && make install


4、修改apache的配置文件

  1. # vim /etc/httpd/conf/httpd.conf


  2. 最后添加如下内容,设置成需要输入密码才能登陆

  3. ScriptAlias /nagios/cgi-bin "/usr/local/nagios/sbin"

  4. <Directory "/usr/local/nagios/sbin">

  5. AuthType Basic

  6. Options ExecCGI

  7. AllowOverride   None

  8. Order allow,deny

  9. Allow from all

  10. AuthName "Nagions Access"

  11. AuthUserFile /usr/local/nagios/etc/htpasswd

  12. Require valid-user

  13. </Directory>


  14. Alias /nagios   "/usr/local/nagios/share"

  15. <Directory "/usr/local/nagios/share">

  16. AuthType Basic

  17. Options None

  18. AllowOverride None

  19. Order allow,deny

  20. Allow from all

  21. AuthName "Nagios Access"

  22. AuthUserFile /usr/local/nagios/etc/htpasswd

  23. Require valid-user

  24. </Directory>

5、配置nagios添加主机,服务信息
在/usr/local/nagios/etc/objects/下建立2个文件:

  1. (1)hosts.cfg定义主机的文件

  2. define host{

  3. use linux-server

  4. host_name 192.168.163.143

  5. alias 192.168.163.143

  6. address 192.168.163.143

  7. }


  8. (2)services.cfg 定义主机服务的文件

  9. define service{

  10. use local-service  //引用local-service的属性信息,在template.cfg中定义

  11. host_name 192.168.163.143 //监控那个主机,host-142在hosts.cfg中定义

  12. service_description ssh //对监控内容的描述

  13. check_command check_ssh //指定监控的命令,check_ping在commands.cfg中定义

  14. }

  15. define service{

  16. use local-service

  17. host_name 192.168.163.143

  18. service_description Ping

  19. check_command check_ping

  20. }


  21. # chown nagios.nagios /usr/local/nagios/etc/objects/{hosts.cfg,services.cfg}

6、建立登陆的用户名密码
# htpasswd -c  /usr/local/nagios/etc/htpasswd nagiosadmin

7、修改nagios.cfg文件,添加文件路径信息:

  1. cfg_file=/usr/local/nagios/etc/objects/hosts.cfg

  2. cfg_file=/usr/local/nagios/etc/objects/services.cfg

  3. cfg_file=/usr/local/nagios/etc/objects/commands.cfg

  4. cfg_file=/usr/local/nagios/etc/objects/contacts.cfg

  5. cfg_file=/usr/local/nagios/etc/objects/timeperiods.cfg

  6. cfg_file=/usr/local/nagios/etc/objects/templates.cfg

8、启动nagios以后访问输入用户名密码就可以看到Nagios了

cacti + nagios + npc 整合部署_nagios_02

如果启动有问题,可以使用下面的命令检查:
#  /usr/local/nagios/bin/nagios -v /usr/local/nagios/etc/nagios.cfg

四、整合cacti+nagios
整合cacti和nagios是利用了cacti的一个插件nagios for  cacti,它的原理是将nagios的数据通过ndo2db导入到mysql数据库(cacti的库中),然后cacti读取数据库信息将nagios

的结果展示出来。

1、安装php-json扩展,因为NPC使用了ExtJS,如过没有json的扩展NPC界面无法出来。

  1. # tar xf php-json-ext-1.2.1.tar.gz

  2. # cd php-json-ext-1.2.1

  3. # phpize

  4. 如果没有命令的话安装下面的软件包

  5. # rpm -ihv /media/CentOS/php-devel-5.1.6-27.el5.i386.rpm


  6. # sh configure && make && make install

2、新建文件,添加信息如下:

  1. # vim /etc/php.d/json.ini

  2. extension=json.so

安装完成后如果先验证的话,可以写个index.php的页面看下,有json信息即可,如下:

cacti + nagios + npc 整合部署_npc_03

3、安装ndoutils,需要安装mysql-devel前面已经装过这里就不用再装了

  1. # tar xf ndoutils-1.4b9.tar.gz

  2. # cd ndoutils-1.4b9

  3. # ./configure --with-mysql-inc=/usr/include/mysql --with-mysql-lib=/usr/lib/mysql --enable-mysql --with-ndo2db-user=nagios--with-ndo2db-group=nagios

  4. # make(不用make install)

4、复制文件

  1. # cp src/{ndomod-3x.o,ndo2db-3x,log2ndo,file2sock} /usr/bin/

  2. # cp config/ndo2db.cfg-sample /usr/local/nagios/etc/ndo2db.cfg

  3. # cp config/ndomod.cfg-sample /usr/local/nagios/etc/ndomod.cfg

  4. # chmod 644 /var/www/html/nagios/etc/ndo*

5、修复配置文件信息,连接数据库

  1. # vim /usr/local/nagios/etc/ndo2db.cfg

  2. socket_type=tcp

  3. db_host=127.0.0.1

  4. db_port=3306

  5. db_name=cacti

  6. db_prefix=npc_

  7. db_user=cactiadmin

  8. db_pass=cacti321

  9. debug_level=1


  10. # vim /usr/local/nagios/etc/ndomod.cfg

  11. output_type=tcpsocket

  12. output=127.0.0.1

6、添加启动脚本,并修改Ndo2dbBin

  1. # cp /download/ndoutils-1.4b9/daemon-init /etc/init.d/ndo2db

  2. # vim /etc/init.d/ndo2db

  3. Ndo2dbBin=/usr/bin/ndo2db-3x


  4. # chmod +x /etc/init.d/ndo2db

  5. # service ndo2db start

7、安装npc

  1. # tar xf npc-2.0.4.tar.gz

  2. # mv npc /var/www/html/cacti/plugins/


  3. 修改配置文件,添加如下信息

  4. # vim /var/www/html/cacti/include/global.php

  5. $plugins = array();

  6. $plugins[]='npc';

8、修改nagios添加信息:

  1. # vim /usr/local/nagios/etc/nagios.cfg

  2. broker_module=/usr/bin/ndomod-3x.o config_file=/usr/local/nagios/etc/ndomod.cfg

  3. event_broker_options=-1

# service nagios restar

启动之后在cacti中添加npc,如下:

cacti + nagios + npc 整合部署_npc_04

cacti + nagios + npc 整合部署_nagios_05

cacti + nagios + npc 整合部署_cacti_06

cacti + nagios + npc 整合部署_nagios_07

cacti + nagios + npc 整合部署_nagios_08


当访问到npc这个页面的时候已经擦不多了

9、但是看npc里发现空白的,没有数据,是因为php没有PDO_MYSQL支持,所以安装PDO_MYSQL

  1. # wget http://pecl.php.net/get/PDO_MYSQL-1.0.2.tgz

  2. # tar zxvf PDO_MYSQL-1.0.2.tgz

  3. # cd PDO_MYSQL-1.0.2

  4. # /usr/local/php/bin/phpize

  5. #./configure --with-php-config=/usr/bin/php-config

  6. #make

  7. #make install


  8. 修改php.ini,添加如下信息,之后重启httpd即可

  9. extension = "pdo_mysql.so"

10、之后再访问还不行,还要修改数据库结构:

  1. # mysql -u root -p

  2. mysql> alter table npc_eventhandlers add long_output TEXT NOT NULL default '' after output;

  3. mysql> alter table npc_hostchecks add long_output TEXT NOT NULL default '' after output;

  4. mysql> alter table npc_hoststatus add long_output TEXT NOT NULL default '' after output;

  5. mysql> alter table npc_notifications add long_output TEXT NOT NULL default '' after output;

  6. mysql> alter table npc_servicechecks add long_output TEXT NOT NULL default '' after output;

  7. mysql> alter table npc_servicestatus add long_output TEXT NOT NULL default '' after output;

  8. mysql> alter table npc_statehistory add long_output TEXT NOT NULL default '' after output;

  9. mysql> alter table npc_systemcommands add long_output TEXT NOT NULL default '' after output;

之后再访问就可看到有数据了:

cacti + nagios + npc 整合部署_cacti_09

到此,cacti+nagios的整合就完事,搭建这个确实有好多问题,百度、谷歌才解决,希望能对有的人有帮助,如果有问题请及时指出,谢谢~