最近一直在弄Nagios和Cacti,找Nagios和Cacti整合的方案的时候,发现了这篇博文,粗看一下,很不错,至少思路上很清晰,里面也提到了之前遇到的几个问题,而且也是用源码方式安装的,和我的方案一样,于是,转过来先,转完再细看.原文如下:
一、系统环境部署及调整
1、系统:CentOS 5.4
2、关闭iptables与SElinux
3、检查系统是否正常
(1)more /var/log/messages (检查有无系统级错误信息)
(2)dmesg (检查硬件设备是否有错误信息)
(3)ifconfig(检查网卡设置是否正确)
(4)ping www.baidu.com (检查网络是否正常)
======================================================================
二、LAMP环境的安装(使用源码方式安装LAMP)
由于Cacti与Nagios都是基于WEB环境的,所以在安装它们之前,必须先搭建好LAMP环境(LNMP也是可以)
LAMP环境搭建:参考http://hi.baidu.com/9812658/blog/item/873c3f14541aaa5df2de3272.html,本文只安装如下一些软件
(1)设置yum源,同时安装Lamp环境所需要的依赖包
(2)安装apache
(3)安装mysql
(4)安装php
(5)安装phpmyadmin
除此之外的一些php扩展及优化软件(例如: memcache、memcached、eAccelerator等等)都没有安装
======================================================================
三、安装Cacti
1、下载Cacti并安装
第一步:安装rrdtool(http://oss.oetiker.ch/rrdtool/)
#yum install pango pango-devel #由于我安装时,缺少这两个包
#wget http://oss.oetiker.ch/rrdtool/pub/rrdtool-1.4.3.tar.gz
#tar zxvf rrdtool-1.4.3.tar.gz && cd rrdtool-1.4.3
#./configure –prefix=/usr/local/rrdtool && make && make install
说明:也可以使用yum install rrdtool来进行安装
第二步:安装net-snmp(http://www.net-snmp.org/)
#wget http://netcologne.dl.sourceforge.net/project/net-snmp/net-snmp/5.5/net-snmp-5.5.tar.gz
#tar zxvf net-snmp-5.5.tar.gz && cd net-snmp-5.5
#./configure –prefix=/usr/local/net-snmp –with-mysql=/lamp/database/mysql/bin/mysql_config && make && make install
###########提示信息##############
default version of-snmp-version(3):3(在这里版本通常有三种形式:1,2c,3)
Systemcontact information(配置该设备的联系信息): ruanxi@163.com(也可以是邮箱地址)
System location (该系统设备的地理位置):Fujian P.R.C
Location to write logfile (日志文件位置):/var/log/snmpd.log
Location to Write persistent(数据存储目录):/var/net-snmp
说明:也可以使用yum install net-snmp net-snmp-devel net-snmp-libs net-snmp-utils net-snmp-perl
或者yum net-snmp*来进行安装
第三步:安装cacti与cacti-spine(http://cactiusers.org/)
#wget http://www.cacti.net/downloads/cacti-0.8.7f.tar.gz
#wget http://www.cacti.net/downloads/spine/cacti-spine-0.8.7e.tar.gz
(1)安装cacti
#tar zxvf cacti-0.8.7f.tar.gz
#mv cacti-0.8.7f /lamp/cacti
#useradd -s /sbin/nologin cacti
#passwd cacti
#chown -R cacti:cacti /lamp/cacti
(2)安装cacti-spine
#tar zxvf cacti-spine-0.8.7f.tar.gz && cd cacti-spine-0.8.7f
#./configure –prefix=/usr/local/cacti-spine –with-mysql=/lamp/database/mysql –with-snmp=/usr/local/net-snmp && make && make install
说明:也可以使用yum install cacti cacti-spine来进行安装,但是使用该方式安装,安装完cacti后,会自动的增加一个名为cacti的用户账号,所以我们就不用再增加一个用户账号
第四步:配置cacti、cacti-spine
1)创建cacti数据库,并初始化该数据库
#mysql -uroot -pXXXX
mysql> create database cacti
mysql> grant all on cacti.* to ‘cacti’@’localhost’ identified by ‘2010’;
mysql> flush privileges;
#mysql -ucacti -p2010 cacti < /lamp/cacti/cacti.sql
2)编译配置文件/usr/local/cacti-spine/etc/spine.conf(注意:如果是RPM方式安装,那么配置文件位于/etc/spine.conf)
#vi /usr/local/cacti-spine/etc/spine.conf
DB_Host localhost
DB_Database cacti
DB_User cacti
DB_Pass 2010
DB_Port 3306
3)配置cacti的虚拟目录
#vi /lamp/web/apache/etc/extra/httpd-cacti.conf
Alias /cacti /lamp/cacti
<Directory /lamp/cacti>
DirectoryIndex index.php
AllowOverride none
order deny,allow
deny from all
Allow from 127.0.0.1 192.168.1.0/24
AddType application/x-httpd-php .php
php_flag magic_quotes_gpc on
php_flag track_vars on
</Directory>
#vi /lamp/web/apache/etc/httpd.conf,增加如下一行
Include etc/extra/httpd-cacti.conf
4)配置cacti的config.php配置文件
#vi /lamp/cacti/include/config.php
$database_type = “mysql”;
$database_default = “cacti”;
$database_hostname = “localhost”;
$database_username = “cacti”;
$database_password = “2010”;
$database_port = “3306”;
$cacti_session_name = “Cacti”;
5)配置/etc/cron.d/cacti任务文件,即:系统每5分钟进行收集SNMP代理上的数据
#vi /etc/cron.d/cacti
*/5 * * * * cacti /lamp/web/php/bin/php /lamp/cacti/poller.php &>/dev/null
#chkconfig –level 3 crond on
#service crond start
6)测试一下
#killall -HUP httpd #重新加载一下httpd.conf配置文件
#http://ip/cacti,登录cacti,还要相关配置,下面是一个配置例子
说明:上图中的路径要设置对,也可以先跳过,然后登录cacti主页后,选择”settings” -> “Paths”进行修改
———————————————————————————
2、Cacti插件的安装
1)安装Plugin Architecture(http://cactiusers.org/)
作用:让cacti支持更多的插件
#wget http://mirror.cactiusers.org/downloads/plugins/cacti-plugin-0.8.7f-PA-v2.7.tar.gz
#tar zxvf cacti-plugin-0.8.7f-PA-v2.7.tar.gz
#mv cacti-plugin-arch /lamp/cacti/
#cd /lamp/cacti/
#mysql -ucacti -p2010 cacti < cacti-plugin-arch/pa.sql
#patch -p1 -N < cacti-plugin-arch/cacti-plugin-0.8.7f-PA-v2.7.diff
#rm -rf cacti-plugin-arch
#chown -R cacti:cacti /lamp/cacti
#vi /lamp/cacti/include/config.php,修改并增加如下
$database_type = “mysql”;
$database_default = “cacti”;
$database_hostname = “localhost”;
$database_username = “cacti”;
$database_password = “2010”;
$database_port = “3306”;
/* cacti’s path with dns */
$config[‘url_path’] = ‘/cacti/’; #也就是cacti虚拟目录配置中的Alias
/*Plugins List*/
$plugins = array();
#显示Plugin-Management
User Management -> “admin” or “other user” -> “Realm Permissions” -> “” -> 打勾并保存
2)安装相关插件
(1)安装Settings插件(http://docs.cacti.net/plugins)
#wget http://docs.cacti.net/_media/plugin:settings-latest.tgz -O settings-latest.tgz
#tar zxvf settings-latest.tgz
#mv settings-0.6 /lamp/cacti/plugins/settings
#vi /lamp/cacti/include/config.php,在该文件中的”$plugins = array()”后增加如下一行内容
$plugins[] = ‘settings’;
注意:也可以在/lamp/cacti/include/global.php文件中增加,但是在config.php文件中增加设置更好
(2)安装Thold插件(http://docs.cacti.net/plugins)
#wget http://docs.cacti.net/_media/plugin:thold-latest.tgz -O thold-latest.tgz
#tar zxvf thold-latest.tgz
#mv thold-0.41 /lamp/cacti/plugins/thold
#vi /lamp/cacti/include/config.php,在该文件中的”$plugins = array()”后增加如下一行内容
$plugins[] = ‘thold’;
最后:
如果该插件是旧版的,那么http://ip/cacti -> Plugin Management -> Thresholds -> “Old Plugin Architecture – Running”
如果该插件是新版的,那么http://ip/cacti -> Plugin Management -> Thresholds ->点 “Install + Enable”
(3)安装Monitor插件(http://docs.cacti.net/plugins)
#wget http://docs.cacti.net/_media/plugin:monitor-latest.tgz -O monitor-latest.tgz
#tar zxvf monitor-latest.tgz
#mv monitor-0.9 /lamp/cacti/plugins/monitor
#vi /lamp/cacti/include/config.php,在该文件中的”$plugins = array()”后增加如下一行内容
$plugins[] = ‘monitor’;
User Management -> “admin” or “other user” -> “Realm Permissions” -> “View Monitoring” -> 打勾并保存
最后:根据新旧版本插件,进行设置
(4)安装WeatherMap插件(http://www.network-weathermap.com/download)
#wget http://www.network-weathermap.com/files/php-weathermap-0.97a.zip
#unzip php-weathermap-0.97a.zip
#mv weathermap /lamp/cacti/plugins/
#cd /lamp/cacti/plugins/weathermap/
#chown -R cacti output
#chown -R daemon configs #设置configs目录所属主为:daemon(这里得用ps -ef | grep httpd查看一下apache是以谁的身份启动的)
#chmod u+w configs #设置configs目录的所属主对该目录可写
#cp editor-config.php-dist editor-config.php
#vi editor-config.php,修改如下参数
$cacti_base = “/usr/local/cacti”; #cacti目录的绝对路径
$cacti_url = “http://localhost/cacti/“; #访问cacti的地址
$mapdir= $cacti_base.’/plugins/weathermap/configs’; #设置configs目录的绝对路径
#vi /lamp/cacti/include/config.php,在该文件中的”$plugins = array()”后增加如下一行内容
$plugins[] = ‘weathermap’;
最后:根据新旧版本插件,进行设置
======================================================================
四、安装Nagios(http://www.nagios.org/)
1、Nagios是怎么实现监控的
1)Nagios服务器对Windows客户端的监控,如下图
(1)Nagios服务器想监控安装有windows系统的主机,那么windows主机必须安装NSClient++这个组件
(2)其次,Nagios服务器使用check_nt(属于:nagios-plugins安装包,如果使用RPM方式安装,那么位于:/usr/lib/nagios/plugins)来对windows主机进行监控
2)Nagios服务器对linux客户端的监控,如下图
(1)Nagios服务器想监控安装有linux系统的主机,那么linux主机必须安装nrpe这个插件(例如:nagios-nrpe的RPM包)
(2)其次,Nagios服务器使用check_nrpe(属于:nagios-plugins安装包,如果使用RPM方式安装,那么位于:/usr/lib/nagios/plugins)来对linux主机进行监控
———————————————————————————
2、Nagios软件的安装
1)安装依赖包
#rpm -q httpd gcc glibc glibc-common gd gd-devel #检查一下这些包是否已经安装
#yum install httpd gcc glibc glibc-common gd gd-devel
2)安装nagios及nagios-plugins
#wget http://prdownloads.sourceforge.net/sourceforge/nagios/nagios-3.2.1.tar.gz
#wget http://prdownloads.sourceforge.net/sourceforge/nagiosplug/nagios-plugins-1.4.14.tar.gz
#groupadd nagcmd
#useradd -G nagcmd -s /sbin/nologin nagios
#usermod -G nagcmd daemon #把daemon加入到nagcmd组,以便于在通过web Interface操作nagios时能够具有足够的权限
#passwd nagios
(1)安装nagios(http://netcologne.dl.sourceforge.net/project/nagios/)
#tar zxvf nagios-3.2.1.tar.gz && cd nagios-3.2.1
#./configure –prefix=/usr/local/nagios –with-nagios-user=nagios –with-nagios-group=nagios –with-command-group=nagcmd –with-httpd-conf=/lamp/web/apache/etc/extra –with-gd-lib –with-gd-inc –enable-event-broker –enable-embedded-perl
#make all
#make install #安装the main program, CGIs, and HTML files
#make install-init #安装启动脚本到/etc/init.d/
#make install-commandmode #安装和设置permissions on the directory for holding the external command file
#make install-config #安装初始配置文件到/usr/local/nagios
#make install-webconf #安装the Apache config file for the Nagios web interface
#chkconfig –add nagios #把nagios添加为系统服务并将之加入到自动启动服务队列
(2)安装nagios-plugins(http://netcologne.dl.sourceforge.net/project/nagiosplug/)
#nagios的所有监控工作都是通过插件完成的,因此,在启动nagios之前还需要为其安装官方提供的插件
#yum install fping perl-Net-SNMP qstat #呵呵,这一步可以不要
#tar zxvf nagios-plugins-1.4.14.tar.gz
#cd nagios-plugins-1.4.14
#./configure –prefix=/usr/local/nagios –with-nagios-user=nagios –with-nagios-group=nagios –enable-extra-opts –enable-perl-modules –enable-libtap –with-openssl –with-perl=/usr/bin/perl –with-mysql=/lamp/database/mysql
注意:nagios-plugins与nagios要安装在同一个目录
#make && make install
说明:也可以使用yum install nagios nagios-plugins进行安装,但是使用这种安装方式,相关的用户账户(例如:nagios)都会自动创建
3)配置nagios与apache,只让授权的用户访问
#htpasswd -c /usr/local/nagios/etc/htpasswd.users nagiosadmin
#cat /usr/local/nagios/etc/htpasswd.users #查看一下论证文件
nagiosadmin:k4MOQhzhwUgt6
#vi /usr/local/nagios/etc/cgi.cfg #配置cgi.cfg配置文件,确认以下使用的用户认证为nagiosadmin
use_authentication=1 #启用身份认证
authorized_for_system_information=nagiosadmin
authorized_for_configuration_information=nagiosadmin
authorized_for_system_commands=nagiosadmin
authorized_for_all_services=nagiosadmin
authorized_for_all_hosts=nagiosadmin
authorized_for_all_service_commands=nagiosadmin
authorized_for_all_host_commands=nagiosadmin
#mv /lamp/web/apache/etc/extra/nagios.conf /lamp/web/apache/etc/extra/httpd-nagios.conf
#vi /lamp/web/apache/etc/extra/httpd-nagios.conf
ScriptAlias /nagios/cgi-bin “/usr/local/nagios/sbin”
<Directory “/usr/local/nagios/sbin”>
# SSLRequireSSL
Options ExecCGI
AllowOverride None
Order deny,allow
Deny from all
Allow from 127.0.0.1 192.168.1.0/24
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 192.168.1.0/24
AuthName “Nagios Access”
AuthType Basic
AuthUserFile /usr/local/nagios/etc/htpasswd.users
Require valid-user
</Directory>
#vi /lamp/web/apache/etc/httpd.conf,增加如下一行
Include etc/extra/httpd-nagios.conf
注意:为了nagios系统的安全,建议您使用信息摘要强认证模式(如MD5),强制使用web的TSL/SSL安全通讯模式,并且通过访问控制列表设定能访问nagios的web界面的客户端地址
4)检查nagios的配置文件(/etc/negios/目录下)
#echo “/usr/local/lib/” >> /etc/ld.so.conf
#echo “/lamp/database/mysql/lib/mysql/” >> /etc/ld.so.conf
#ldconfig
#/usr/local/nagios/bin/nagios -v /usr/local/nagios/etc/nagios.cfg #检查一下nagios的主配置文件有没有语法错误(如果有出现*.so找不到,那么可以自己搜索一下,然后使用ldconfig XXXX/lib)
Total Warnings: 0
Total Errors: 0
说明:如果出现上面这两行的值都是0,那么表示配置文件没有出错
5)设置nagios服务
#chkconfig nagios off
#chkconfig –level 3 nagios on
#service nagios start
#/usr/local/nagios/bin/nagiostats #显示nagios当前运行的情况
6)配置selinux
———————————————————————————
3、Nagios整合到Cacti
1)安装Nagios Plugin for Cacti(NPC)插件(http://trac2.assembla.com/npc)
作用:将nagios的数据通过ndo2db导入到mysql数据库,然后cacti读取数据库信息将nagios的结果通过NPC展示出来
首先:安装NPC
#wget http://www.constructaegis.com/downloads/npc-2.0.4.tar.gz
#tar zxvf npc-2.0.4.tar.gz
#mv npc /lamp/cacti/plugins/
#vi /lamp/cacti/include/config.php,在该文件中的”$plugins = array()”后增加如下一行内容
$plugins[] = ‘npc’;
最后:根据新旧版本插件,进行设置
其次:安装php-json(http://pecl.php.net/package/json),因为NPC需要用到它
#wget http://pecl.php.net/get/json-1.2.1.tgz
#tar zxvf json-1.2.1.tgz && cd json-1.2.1
#/usr/bin/phpize && ./configure –with-php-config=/lamp/web/php/bin/php-config –enable-shared –enable-static –with-json && make && make install
#service httpd restart
#http://ip /index.php,确定一下是否已经加载了json,如下图
注意:如果没有加载json,那么得vi /lamp/web/php/etc/php.ini,增加或者修改如下
extension_dir = “/lamp/web/php/lib/php/extensions/no-debug-non-zts-20090626/”
extension = “json.so”
说明:如果您是使用RPM方式安装php,那么一些动态的扩展是自动被加入的,这些动态扩展 在:/etc/php.d/*.ini这些文件,这里确认一下json.ini文件有这一行内容:extension=json.so;同时还要确认一下 /usr/lib/php/modules目录下有没有json.so文件,如果没有的话,要手动复制一份到这个目录,这样才能确保json.ini起作 用
2)下载并安装NDoutils
NDoutils作用:把Nagios的数据写到mysql,所以它是集成nagios到cacti的关健的一部分
#wget http://netcologne.dl.sourceforge.net/project/nagios/ndoutils-1.x/ndoutils-1.4b9/ndoutils-1.4b9.tar.gz
#tar zxvf ndoutils-1.4b9.tar.gz
#cd ndoutils-1.4b9
#编译之前做一上有关mysql库文件的软链接
#ln -s /lamp/database/mysql/include/* /usr/include/
#ln -s /lamp/database/mysql/lib/* /usr/lib/
#./configure –with-mysql=/lamp/database/mysql –disable-pgsql –with-ndo2db-user=nagios –with-ndo2db-group=nagios –with-ssl –with-ssl-inc –with-ssl-lib –enable-mysql –enable-ssl
#make (注意:这里不需要make install)
#cp -v src/{ndomod-3x.o,ndo2db-3x,file2sock,log2ndo} /usr/local/nagios/bin
注意:以上复制的文件中前两项是版本相关的,即如果您的nagios主版本号是2系列,则需要拷贝名为ndomod-2x.o和ndo2db-2x的两个文件,而后两项是通用文件,无论哪个版本都需要复制
2)复制、编辑相关的配置文件
#cd ndoutils-1.4b9
#cp -v config/{ndo2db.cfg-sample,ndomod.cfg-sample} /usr/local/nagios/etc
#mv /usr/local/nagios/etc/ndo2db.cfg-sample /usr/local/nagios/etc/ndo2db.cfg
#mv /usr/local/nagios/etc/ndomod.cfg-sample /usr/local/nagios/etc/ndomod.cfg
#chmod 0664 /usr/local/nagios/etc/{ndo2db.cfg,ndomod.cfg}
#chown nagios:nagios /usr/local/nagios/etc/{ndo2db.cfg,ndomod.cfg}
#vi /usr/local/nagios/etc/nagios.cfg,然后在该配置文件中添加或者修改如下几行内容
broker_module=/usr/local/nagios/bin/ndomod-3x.o config_file=/usr/local/nagios/etc/ndomod.cfg
event_broker_options=-1 #为Nagios开启event broker
check_external_commands = 1
command_check_interval = -1
process_performance_data = 1
3)编辑ndo2db守护进程和ndomod的配置文件
# vi /usr/local/nagios/etc/ndo2db.cfg,修改如下内容
lock_file=/usr/local/nagios/var/ndo2db.lock
ndo2db_user=nagios
ndo2db_group=nagios
socket_type=tcp
socket_name=/usr/local/nagios/var/ndo.sock
tcp_port=5668
use_ssl=0
db_servertype=mysql
db_host=localhost
db_port=3306
db_name=cacti
db_prefix=npc_
db_user=cacti
db_pass=2010
max_timedevents_age=1440
max_systemcommands_age=10080
max_servicechecks_age=10080
max_hostchecks_age=10080
max_eventhandlers_age=44640
max_externalcommands_age=44640
debug_level=1
debug_verbosity=1
debug_file=/usr/local/nagios/var/ndo2db.debug
max_debug_file_size=1000000
说明如下:
(1)以上以”db_”开头的选项用来指定连接数据库的属性
(2)其默认用来接收数据的方式为Unix域套接字,这里修改成了TCP套接字;同时ndomod.cfg配置文件中指定的输出方式也应该做相应的修改
(3)由于要整合cacti,所以这设置为:db_name=cacti,db_prefix=npc_,db_user=cacti,db_pass=2010
#接着编辑ndomod的配置文件,为其指定数据的输出方式和输出目标主机
# vi /usr/local/nagios/etc/ndomod.cfg
instance_name=default
output_type=tcpsocket
output=127.0.0.1
tcp_port=5668
use_ssl=0
output_buffer_items=5000
buffer_file=/usr/local/nagios/var/ndomod.tmp
file_rotation_interval=14400
file_rotation_timeout=60
reconnect_interval=15
reconnect_warning_interval=15
data_processing_options=-1
config_output_options=2
4)启动NDoutils和Nagios
#/usr/local/nagios/bin/ndo2db-3x -c /usr/local/nagios/etc/ndo2db.cfg
#service nagios restart
#echo “/usr/local/nagios/bin/ndo2db-3x -c /usr/local/nagios/etc/ndo2db.cfg” >>/etc/rc.local
原文地址:http://hi.baidu.com/9812658/blog/item/3aaa0b750e524317b051b913.html