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
#yum -y install *gcc*
#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
# chkconfig mysqld on
# chkconfig httpd on
# chkconfig snmpd on
# service mysqld start
# service httpd start
# service snmpd start
# mysqladmin -u root password 'redhat321'
# mysql -u root -p
:输入密码
mysql>create database cacti;
Query OK, 1 row affected (0.00 sec)
mysql>grant all on cacti.* to cactiadmin@localhost identified by 'cacti321';
Query OK, 0 rows affected (0.01 sec)
mysql>flush privileges;
Query OK, 0 rows affected (0.00 sec)
mysql> \q
二、安装cacti
1、安装rrdtool需要的软件包
# yum -y install cairo-devel libxml2-devel pango pango-devel
2、安装rrdtool
# tar xf rrdtool-1.4.4.tar.gz
# cd rrdtool-1.4.4
# ./configure --prefix=/usr/local/rrdtool && make && make install
# ln -s /usr/local/rrdtool/bin/* /usr/local/bin/
3、安装cacti-0.8.7.e
# tar xf cacti-0.8.7e.tar.gz -C /var/www/html/
# cd /var/www/html/
# mv cacti-0.8.7e/ cacti
# chown -R root.root /var/www/html/cacti/
4、导入数据库
# mysql -u root -p cacti <cacti/cacti.sql
Enter password:
5、修改cacti配置文件,连接数据库
# vim /var/www/html/cacti/include/config.php
$database_username = "cactiadmin";
$database_password = "cacti321";
# vim /var/www/html/cacti/include/global.php
$database_username = "cactiadmin";
$database_password = "cacti321";
6、安装插件cacti-plugin
# wget http://mirror.cactiusers.org/downloads/plugins/cacti-plugin-0.8.7e-PA-v2.6.zip
# unzip cacti-plugin-0.8.7e-PA-v2.6.zip
# cd cacti-plugin-arch/
# cp cacti-plugin-arch/{cacti-plugin-0.8.7e-PA-v2.6.diff,pa.sql} /var/www/html/cacti/
# cd /var/www/html/cacti/
7、打补丁导入数据库
# patch -p1 -N <cacti-plugin-0.8.7e-PA-v2.6.diff
# mysql -uroot -p cacti<pa.sql
8、修改配置文件,如下:
# vim include/global.php
$config['url_path'] = '/cacti/';
9、添加计划任务
# crontab -e
*/5 * * * * /usr/bin/php /var/www/html/cacti/poller.php
# service crond restart
10、浏览器输入http://192.168.163.143/cacti/install初始化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、修改配置文件如下:(这里我省略掉了#行和空行)
# vim /etc/snmp/snmpd.conf
com2sec notConfigUser default public
group notConfigGroup v1 notConfigUser
group notConfigGroup v2c notConfigUser
view systemview included .1.3.6.1.2.1.1
view systemview included .1.3.6.1.2.1.25.1.1
access notConfigGroup "" any noauth exact mib2 none none
view mib2 included .iso.org.dod.internet.mgmt.mib-2 fc
syslocation Unknown (edit /etc/snmp/snmpd.conf)
syscontact Root <root@localhost> (configure /etc/snmp/snmp.local.conf)
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、建立用户
# useradd -s /sbin/nologins nagios
# mkdir -p /usr/local/nagios
# chown -R nagios.nagios /usr/local/nagios/
2、编译安装nagios
# tar xf nagios-3.4.1.tar.gz
# cd nagios
# ./configure --prefix=/usr/local/nagios/
# make all
# make install
# make install-init
# make install-commandmode
# make install-config
# make install-webconf
# chkconfig --add nagios
# chkconfig nagios on
# service nagios start
3、安装插件
# tar xf nagios-plugins-1.4.16.tar.gz
# cd nagios-plugins-1.4.16
# ./configure --prefix=/usr/local/nagios/ && make && make install
4、修改apache的配置文件
# vim /etc/httpd/conf/httpd.conf
最后添加如下内容,设置成需要输入密码才能登陆
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 "Nagions 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>
5、配置nagios添加主机,服务信息
在/usr/local/nagios/etc/objects/下建立2个文件:
(1)hosts.cfg定义主机的文件
define host{
use linux-server
host_name 192.168.163.143
alias 192.168.163.143
address 192.168.163.143
}
(2)services.cfg 定义主机服务的文件
define service{
use local-service //引用local-service的属性信息,在template.cfg中定义
host_name 192.168.163.143 //监控那个主机,host-142在hosts.cfg中定义
service_description ssh //对监控内容的描述
check_command check_ssh //指定监控的命令,check_ping在commands.cfg中定义
}
define service{
use local-service
host_name 192.168.163.143
service_description Ping
check_command check_ping
}
# chown nagios.nagios /usr/local/nagios/etc/objects/{hosts.cfg,services.cfg}
6、建立登陆的用户名密码
# htpasswd -c /usr/local/nagios/etc/htpasswd nagiosadmin
7、修改nagios.cfg文件,添加文件路径信息:
cfg_file=/usr/local/nagios/etc/objects/hosts.cfg
cfg_file=/usr/local/nagios/etc/objects/services.cfg
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/timeperiods.cfg
cfg_file=/usr/local/nagios/etc/objects/templates.cfg
8、启动nagios以后访问输入用户名密码就可以看到Nagios了
如果启动有问题,可以使用下面的命令检查:
# /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界面无法出来。
# tar xf php-json-ext-1.2.1.tar.gz
# cd php-json-ext-1.2.1
# phpize
如果没有命令的话安装下面的软件包
# rpm -ihv /media/CentOS/php-devel-5.1.6-27.el5.i386.rpm
# sh configure && make && make install
2、新建文件,添加信息如下:
# vim /etc/php.d/json.ini
extension=json.so
安装完成后如果先验证的话,可以写个index.php的页面看下,有json信息即可,如下:
3、安装ndoutils,需要安装mysql-devel前面已经装过这里就不用再装了
# tar xf ndoutils-1.4b9.tar.gz
# cd 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
# make(不用make install)
4、复制文件
# cp src/{ndomod-3x.o,ndo2db-3x,log2ndo,file2sock} /usr/bin/
# cp config/ndo2db.cfg-sample /usr/local/nagios/etc/ndo2db.cfg
# cp config/ndomod.cfg-sample /usr/local/nagios/etc/ndomod.cfg
# chmod 644 /var/www/html/nagios/etc/ndo*
5、修复配置文件信息,连接数据库
# vim /usr/local/nagios/etc/ndo2db.cfg
socket_type=tcp
db_host=127.0.0.1
db_port=3306
db_name=cacti
db_prefix=npc_
db_user=cactiadmin
db_pass=cacti321
debug_level=1
# vim /usr/local/nagios/etc/ndomod.cfg
output_type=tcpsocket
output=127.0.0.1
6、添加启动脚本,并修改Ndo2dbBin
# cp /download/ndoutils-1.4b9/daemon-init /etc/init.d/ndo2db
# vim /etc/init.d/ndo2db
Ndo2dbBin=/usr/bin/ndo2db-3x
# chmod +x /etc/init.d/ndo2db
# service ndo2db start
7、安装npc
# tar xf npc-2.0.4.tar.gz
# mv npc /var/www/html/cacti/plugins/
修改配置文件,添加如下信息
# vim /var/www/html/cacti/include/global.php
$plugins = array();
$plugins[]='npc';
8、修改nagios添加信息:
# vim /usr/local/nagios/etc/nagios.cfg
broker_module=/usr/bin/ndomod-3x.o config_file=/usr/local/nagios/etc/ndomod.cfg
event_broker_options=-1
# service nagios restar
启动之后在cacti中添加npc,如下:
当访问到npc这个页面的时候已经擦不多了
9、但是看npc里发现空白的,没有数据,是因为php没有PDO_MYSQL支持,所以安装PDO_MYSQL
# wget http://pecl.php.net/get/PDO_MYSQL-1.0.2.tgz
# tar zxvf PDO_MYSQL-1.0.2.tgz
# cd PDO_MYSQL-1.0.2
# /usr/local/php/bin/phpize
#./configure --with-php-config=/usr/bin/php-config
#make
#make install
修改php.ini,添加如下信息,之后重启httpd即可
extension = "pdo_mysql.so"
10、之后再访问还不行,还要修改数据库结构:
# mysql -u root -p
mysql> alter table npc_eventhandlers add long_output TEXT NOT NULL default '' after output;
mysql> alter table npc_hostchecks add long_output TEXT NOT NULL default '' after output;
mysql> alter table npc_hoststatus add long_output TEXT NOT NULL default '' after output;
mysql> alter table npc_notifications add long_output TEXT NOT NULL default '' after output;
mysql> alter table npc_servicechecks add long_output TEXT NOT NULL default '' after output;
mysql> alter table npc_servicestatus add long_output TEXT NOT NULL default '' after output;
mysql> alter table npc_statehistory add long_output TEXT NOT NULL default '' after output;
mysql> alter table npc_systemcommands add long_output TEXT NOT NULL default '' after output;
之后再访问就可看到有数据了:
到此,cacti+nagios的整合就完事,搭建这个确实有好多问题,百度、谷歌才解决,希望能对有的人有帮助,如果有问题请及时指出,谢谢~