nagios+cacti部署安装
一、说明
闲暇之余,安装nagios+cacti,想看下最终实现效果;本文档规范网络监控系统安装流程及安装内容,供运维人员在部署新的运营环境时参考使用。
二、Nagios的安装
1、下载
#wget
http://jaist.dl.sourceforge.net/project/nagios/nagios-3.x/nagios-3.5.0/nagios-3.5.0.tar.gz (nagios主程序)
#wgethttp://jaist.dl.sourceforge.net/project/nagiosplug/nagiosplug/1.4.16/nagios-plugins-1.4.16.tar.gz (监控插件)
#wgethttp://jaist.dl.sourceforge.net/project/nagios/nrpe-2.x/nrpe-2.14/nrpe-2.14.tar.gz (nrpe插件)
2、需要的基础套件
#rpm-q gcc glibc glibc-common gd gd-devel openssl-devel httpd php
3、创建用户和组
#useradd nagios
#mkdir /usr/local/nagios
#chown -R nagios.nagios /usr/local/nagios
4、编译安装
#./configure --prefix=/usr/local/nagios
#make all
#make install
#make install-init
#make install-commandmode
#make install-config
#make install-webconf
5、创建Apache用户验证文件
创建一个nagiosadmin账户:
#htpasswd -c /usr/local/nagios/etc/htpasswd.users nagiosadmin
6、常用的命令
#/usr/local/nagios/bin/nagios -v /usr/local/nagios/etc/nagios.cfg
7、安装nagios-plugins
#./configure --with-nagios-user=nagios--with-nagios-group=nagios
#make&& make install
8、安装nrpe(监控端)
#./configure
#make all && make install-plugin
9、被监控端安装
1、系统环境
10、下载
#wgethttp://jaist.dl.sourceforge.net/project/nagiosplug/nagiosplug/1.4.16/nagios-plugins-1.4.16.tar.gz (监控插件)
#wgethttp://jaist.dl.sourceforge.net/project/nagios/nrpe-2.x/nrpe-2.14/nrpe-2.14.tar.gz (nrpe插件)
11、创建用户
#useradd nagios
#passwd nagios
12、检查基础套件
#rpm-q gcc glibc glibc-common gd gd-devel openssl-devel
13、安装nagios-plugins
#./configure
#make && make install
14、修改目录权限
#chown nagios.nagios /usr/local/nagios
#chown -R nagios.nagios /usr/local/nagios/libexec
15、安装nrpe
#./configure
#make all
#make install-plugin 安装check_nrpe
#make install-daemon
#make install-daemon-config
16、配置nrpe
#vi /usr/local/nagios/etc/nrpe.cfg 添加 allowed_hosts=192.168.40.128
17、启动nrpe
#/usr/local/nagios/bin/nrpe -c/usr/local/nagios/etc/nrpe.cfg -d
18、测试一下
#/usr/local/nagios/libexec/check_nrpe -H localhost
19、随机启动
#echo "/usr/local/nagios/bin/nrpe -c/usr/local/nagios/etc/nrpe.cfg -d" >> /etc/rc.local
20、配置
1、# vi /usr/local/nagios/etc/objects/commands.cfg
definecommand{
command_name check_nrpe
command_line $USER1$/check_nrpe -H $HOSTADDRESS$ -c$ARG1$
}
参考资料:http://www.cnblogs.com/mchina/archive/2013/02/20/2883404.html
至此nagios的服务端与客户端搭建完成
三、安装cacti
Cacti的运行需要LAMP(Linux+Apache + MySQL + PHP), RRDTool ,net-snmp , AMP
正确配置各主机网络参数,本实验以一台机器既为服务端也为被监测端
[root@localhost~]#vim/etc/sysconfig/network-scrip/ifcfg-eth0
在被监测端安装net-snmp软件包,net-snmp软件需要用到lm_sensors提供的硬件监视模块,安装时直接使用RHEL5系统光盘中的rpm包文件,依次安装lm_sensors、net-snmp两个软件包即可。
[root@localhost~]# cd /mnt/Server/
[root@localhostServer]# rpm -ivh lm_sensors-2.10.0-3.1.i386.rpm
[root@localhostServer]# rpm -ivh net-snmp-5.3.1-14.el5.i386.rpm
3、配置及启动snmpd服务,修改文件/etc/snmp/snmpd.conf第41、62、85行,设置SNMP访问权限
41行中将default修改为cacti监测服务器的IP地址本实验为192.168.4.1将public修改为共享SNMP信息的组识别字串publicsvr,62行 将systemview修改为all,85行去掉开头注释符号
41com2sec notConfigUser 192.168.4.1 publicsvr
62access notConfigGroup "" any noauth exact all none none
iewall included .1 80
4、启动snmpd服务并设置为自启动
[root@localhostServer]# service snmpd start
启动 snmpd: [确定]
[root@localhostServer]# chkconfig --level 35 snmpd on
5、配置监测服务端
Cacti监测服务端需要有apache、mysql、php网站平台的支持,且需要预先安装有gd、zlib等软件包
httpd-2.2.3-6.el5
mysql-5.0.22-2.1
mysql-server-5.0.22-2.1
mysql-connector-odbc-3.51.12-2.2
php-5.1.6-5.el5
php-mysql-5.1.6-5.1.6-5.el5、
php-common-5.1.6-5.1.6-5.el5、php-pdo-5.1.6-5.el5
注:此处用源码包安装如下(也可用yum安装)
编译安装apache
[root@localhostLNS-SG4]# tar zxvf httpd-2.2.9.tar.gz �C C /usr/src
[[email protected]]# ./configure --prefix=/usr/local/apache2 --enable-so--enable-rewrite --enable-ssl --with-ssl=/usr/lib --enable-auth-digest--enable- cgi --enable-suexec --with-suexec-caller=daemon--with-suexec-docroot=/usr/local/apache2/htdocs
[[email protected]]# make && make install
查看相关服务
[[email protected]]# ls /usr/local
apache2 bin etc games include lib libexec sbin share src
执行启动服务
[root@localhostbin]# ./apachectl start
安装mysql
[root@localhostLNS-SG5]# useradd -M -s /sbin/nologin mysql
[root@localhostLNS-SG5]# tar zxvf mysql-5.0.56.tar.gz -C /usr/src
[[email protected]]# ./configure --prefix=/usr/local/mysql
[[email protected]]# make & make install
[[email protected]]# cp support-files/my-medium.cnf /etc/my.cnf
[[email protected]]# chown -R root:mysql /usr/local/mysql
[[email protected]]# chown -R mysql /usr/local/mysql
[[email protected]]# /usr/local/mysql/bin/mysql_install_db --user=mysql
[[email protected]]# echo "/usr/local/mysql/lib/mysql" >>/etc/ld.so.conf
[[email protected]]# ldconfig
[[email protected]]# /usr/local/mysql/bin/mysqld_safe --user=mysql &
[1]13582
[[email protected]]# Starting mysqld daemon with databases from /usr/local/mysql/var
[[email protected]]# cp support-files/mysql.server /etc/init.d/mysqld
[[email protected]]# chmod +x /etc/init.d/mysqld
[[email protected]]# chkconfig -add mysqld
安装编译php
[root@localhostLNS-SG5]# tar jxvf php-5.2.6.tar.bz2 -C /usr/src
[[email protected]]# ./configure --prefix=/usr/local/php5--with-apxs2=/usr/local/apache2/bin/apxs--with-mysql=/usr/local/mysql --enable-sockets--enable-mbstring
[[email protected]]# make && make install
编辑文件vim/usr/local/apache2下的配置文件
AddTypeapplication/x-httpd-php .php
DirectoryIndex index.html index.php
(此处也可以用rpm包安装相关的依赖包)
[root@localhostServer]# rpm -ivh perl-DBI-1.52-1.fc6.i386.rpm
[root@localhostServer]# rpm -ivh mysql-5.0.22-2.1.i386.rpm
[root@localhostServer]# rpm -ivh perl-DBD-MySQL-3.0007-1.fc6.i386.rpm
[root@localhostServer]# rpm -ivh mysql-server-5.0.22-2.1.i386.rpm
[root@localhostServer]# rpm -ivh unixODBC-2.2.11-7.1.i386.rpm
[root@localhostServer]# rpm -ivh libtool-ltdl-1.5.22-6.1.i386.rpm
[root@localhostServer]# rpm -ivh libdbi-0.8.1-2.1.i386.rpm
[root@localhostServer]# rpm -ivh libdbi-drivers-0.8.1a-1.2.2.i386.rpm
[root@localhostServer]# rpm -ivh libdbi-dbd-mysql-0.8.1a-1.2.2.i386.rpm
[root@localhostServer]#rpm-ivhmysql-connector-odbc-3.51.12-2.2.i386.rpm
[root@localhostServer]# rpm -ivh php-common-5.1.6-5.el5.i386.rpm
[root@localhostServer]# rpm -ivh php-cli-5.1.6-5.el5.i386.rpm
[root@localhostServer]# rpm -ivh php-5.1.6-5.el5.i386.rpm
[root@localhostServer]# rpm -ivh php-pdo-5.1.6-5.el5.i386.rpm
启动已安装好的httpd、mysqld服务,设置为每次开机随系统自动运行
[root@localhost Server]# chkconfig --level35 mysqld on
[root@localhostServer]# chkconfig --level 35 httpd on
6、安装net-snmp-utils软件包
[root@localhostServer]# rpm -ivh net-snmp-utils-5.3.1-14.el5.i386.rpm
warning:net-snmp-utils-5.3.1-14.el5.i386.rpm: Header V3 DSA signature: NOKEY, key ID37017186
Preparing... ########################################### [100%]
1:net-snmp-utils
###########################################[100%]
7、安装rrdtool软件包
[root@localhost ~]# mount /dev/cdrom /mnt
mount:block device /dev/cdrom is write-protected, mounting read-only
[root@localhost~]# cd /mnt/Linux-gateway/LGS-SG6
[ root@localhost LGS-SG6]# tar zxvfrrdtool-1.2.27.tar.gz
[root@localhost src]# cd rrdtool-1.2.27/
[root@localhost rrdtool-1.2.27]#./configure --prefix=/usr/local
[root@localhost rrdtool-1.2.27]# nake&& make install
8、安装cacti文件包
[root@localhost html]# cd /var/www/html
[root@localhost html]# tar zxvf/mnt/Linux-gateway/LGS-SG6/cacti-0.8.7b-cn-utf8.tar.gz
[root@localhosthtml]# mv cacti-0.8.7b-cn-utf8 cacti
[root@localhosthtml]# useradd cactiuser
[root@localhosthtml]# chown -R cactiuser.cactiuser cacti/rra/ cacti/log/
9、设置监测数据库、表,设置好授权访问数据库的用户和密码
[root@localhosthtml]# mysql -u root -p
Enterpassword:
Welcometo the MySQL monitor. Commands end with; or \g.
YourMySQL connection id is 2 to server version: 5.0.22
Type'help;' or '\h' for help. Type '\c' to clear the buffer.
mysql>create database cactidb default character set utf8;
QueryOK, 1 row affected (0.00 sec)
mysql>grant all on cactidb.* to cactiuser@localhost identified by 'pwd@123';
QueryOK, 0 rows affected (0.02 sec)
mysql>
[root@localhosthtml]# mysql -u cactiuser -p cactidb < cacti/cacti.sql
Enterpassword
10、调整cacti配置文件
[root@localhosthtml]# cd /var/www/html/cacti/
[root@localhostcacti]# vim include/config.php
$database_type= "mysql";
$database_default= "cactidb";
$database_hostname= "localhost";
$database_username= "cactiuser";
$database_password= "pwd@123";
$database_port= "3306";
$config['url_path']="/"
11、调整httpd设置并重新启动服务
DocumentRoot"/var/www/html/cacti"
<Directory"/var/www/html/cacti>
Options FollowSymLinks
AllowOverride None
</Directory>
<Directory"/var/www/html/cacti">
Options None
AllowOverride None
Orderallow,deny
Allowfrom all
</DirectoryOA>
DirectoryIndexindex.html index.php index.html.var//设置默认索引页
12、初始化cacti系统
为了在监测图像中正常显示中文,还需要设置RRDtool所使用的中文字体路径。同样通过左侧栏中的“设置”链接,找到路径选项卡中的“RRDtool默认字体路径”将字体路径设置为“/usr/share/fonts/zh_CN/TureType/zysong.ttl”点击右下方的“保存”即可。
整合cacti和nagios
整合cacti和nagios是利用了cacti的一个插件nagios for cacti,它的原理是将nagios的数据通过ndo2db导入到mysql数据库(cacti的库中),然后cacti读取数据库信息将nagios的结果展示出来
(1)、安装ndoutils
首先需要安装ndoutils以将nagios的数据能导入到mysql数据库中。
yum-y install mysql-devel 安装mysql开发包以编译ndoutils
前提:因为本次安装nagios用的是4.0的版本,所以要安装2.0以上的ndoutils不然会显示加载模块不成功
tar-zcvf ndoutils-2.0.0.tar.gz
cdndoutils-2.0.0
./configure--prefix=/usr/local/nagios/ --with-mysql-inc=/usr/include/mysql--with-mysql-lib=/usr/lib/mysql --enable-mysql --disable-pgsql--with-ndo2db-user=nagios --with-ndo2db-group=nagios
Make
makeinstall
cp-v {ndomod-4x.o,ndo2db-4x,file2sock,log2ndo} /usr/local/nagios/bin
./installdb-ucactiuser -ppwd@123 -d cactidb
cpndomod.cfg-sample /usr/local/nagios/etc/ndomod.cfg
修改nagios主配置文件
#vi /usr/local/nagios/etc/nagios.cfg
10、添加以下内容
check_external_commands=1
command_check_interval=-1
event_broker_options=-1
broker_module=/usr/local/nagios/bin/ndomod.oconfig_file=/usr/local/nagios/etc/ndomod.cfg
process_performance_data=1
添加的内容至此结束
11、修改配置文件ndocmd.cfg和ndo2db.cfg,这里我的配置文件内容为:
[root@localhost downloads]# cat /usr/local/nagios/etc/ndomod.cfg |grep -v'^#'|sed /^$/d
instance_name=default
output_type=tcpsocket
output=127.0.0.1
tcp_port=5668
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
[root@localhostdownloads]# cat /usr/local/nagios/etc/ndo2db.cfg |grep -v '^#'|sed /^$/d
ndo2db_user=nagios
ndo2db_group=nagios
socket_type=tcp
socket_name=/usr/local/nagios/var/ndo.sock
tcp_port=5668
db_servertype=mysql
db_host=127.0.0.1
db_port=3306
db_name=cacti
db_prefix=npc_
db_user=cacti
db_pass=cacti2008
max_timedevents_age=1440
max_systemcommands_age=10080
max_servicechecks_age=10080
max_hostchecks_age=10080
max_eventhandlers_age=44640
debug_level=1
debug_verbosity=1
debug_file=/usr/local/nagios/var/ndo2db.debug
max_debug_file_size=1000000
12、#/usr/local/nagios/bin/ndo2db-c /usr/local/nagios/etc/ndo2db.cfg 启动ndo2db
(2)、安装安装php-json npc展示部分用到json,需要在php中安装php-json的支持
Yuminstall php-devel 否则,phpsize命令不好用
#wgethttp://www.aurore.net/projects/php-json/php-json-ext-1.2.0.tar.bz2
#tarxvjf php-json-ext-1.2.0.tar.bz2
#cdphp-json-ext-1.2.0
#phpize 编译前初始化php环境
#./configure
#make
#makeinstall
#vi/etc/php.d/json.ini
extension=php_json.so
#cp/usr/lib/php/modules/json.so /usr/lib/php/modules/php_json.so 这一步骤很关键,因为,apache否则日志报错加载不到php_json.so文件。
#/usr/sbin/httpd-k graceful 重启apache
为了验证是否成功可使用phpinfo查看是否已支持json
(3)安装npc
wgethttp://www.assembla.com/spaces/npc/documents/aUjAwmdW8r3BuPab7jnrAJ/download?filename=npc-2.0.0b.166.tar.gz
wget http://dlwt.csdn.net/fd.php?i=659714146741849&s=796b68562511c6534bfc15d7b04711f4/npc-2.0.3.tar.gz 新链接可用
#mv npc /usr/local/wwwroot/cacti/plugins/
启用cacti的插件功能,以admin用户登陆cacti,在console中的usermanagement里对admin的用户权限进行编辑,勾选上PluginManagement,然后到插件管理中心安装并启用NPC即可。
在'User Management'中选择'admin',在下边'Realm Permissions'中钩选'Plugin Management'。这时右侧会出现'Plugin Management'连接,进去后在'uninstalled'中安装npc,然后在'intalled'中enablenpc,然后在回去admin的'RealmPermissions'中会出现'use npc',如果没有钩选就把它选中。
接着在右侧栏目中选择settings,点选npc的标签
钩选Remote Commands
NagiosCommand File Path: /usr/local/nagios/var/rw/nagiosNaNd
<这个文件启动nagios后会产生,根据实际的位置写>
NagiosURL: [url]http://yourserver/nagios/[/url]
保存就可以了。
至此安装就完成了
启动mysql httpd ndo nagios
servicemysqld start
servicehttpd start
/usr/local/nagios/bin/ndo2db-c /usr/local/nagios/etc/ndo2db.cfg
servicenagios start
访问[url]http://yourserver/cacti/[/url]
(4)测试页面
在IE浏览器的地址栏输入:http://192.168.30.162/cacti
用户名:admin 密码:admin
第一次登录的时候,会提示修改管理员密码,按照提示修改即可。
查看npc是否显示
(5)正常显示
报错解决
A、安装完成npc之后无法显示页面,最终找到解决办法是因为php的扩展不支持
解决如下
安装前准备
yum -y install php-devel php-pear gcc make
2.安装json
pear install pecl/json
或者
pecl install json
3.让php支持json
我的php是yum安装的,所以
cd /etc/php.d
echo "extension=json.so" > json.ini
重启apache
service httpd restart
如果有selinux,执行下面命令
chcon system_u:object_r:textrel_shlib_t /usr/lib/php/modules/json.so
或
chcon -c -v -R -u system_u -r object_r -t textrel_shlib_t/usr/lib/php/modules/json.so
4.验证
php -m|grep json
json
可以看到php已经支持json了,现在可以重新去执行了php页面了.
B、页面显示之后,可惜的是nagios显示off的状态,找各种办法,是ndoutils的版本问题,重新安装即可,至此可正常显示