系统:CentOS6.5 内核版本系统:2.6.32-431.23.3.el6.x86_64
关闭iptables 、selinux
chkconfig iptables off
安装前的准备工作
yum install -y httpd-* mysql-* php-* net-snmp* gcc glibc glibc-common gd gd-devel openssl*
一、nagios 环境的搭建
1.安装nagios
useradd nagios
tar zxf nagios-3.2.0.tar.gz
cd nagios-3.2.0
./configure -prefix=/var/www/html/nagios
make all
make install && make install-init && make install-commandmode && make install-config && make install-webconf
2.安装中文的nagios
这个比较简单就跟打补丁一样
tar zxf nagios-cn-3.2.0.tar.bz2
cd nagios-cn-3.2.0
./configure -prefix=/var/www/html/nagios
make all
make install && make install-init && make install-commandmode && make install-config && make install-webconf
3.增加nagios登陆认证文件,一定要用默认的nagiosadmin作为用户
htpasswd -c /var/www/html/nagios/etc/htpasswd.users nagiosadmin
*否则需要修改/var/www/html/nagios/etc/cgi.cfg这个文件
vi /etc/nagios/cgi.cfg
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
4.安装插件:
tar xf nagios-plugins-1.4.14.tar.gz
cd nagios-plugins-1.4.14
./configure -prefix=/var/www/html/nagios/
make
make install
5.将运行Apache的用户添加到nagios组里,这样Apache才有权限读取文件
usermod -G nagios apache ##将apache用户加到nagios组。
6.阶段测试
chown nagios.nagios /var/www/html/nagios/ -R
service httpd restart
chkconfig httpd on
/etc/init.d/nagios start
*问http://IP/nagios 看看账号密码对不对
二:cacti环境的搭建
1.rrdtool的安装
这里需要安装的是:rrdtool,rrdtool-devel.rrdtool-perl,rrdtool-php, rrdtool-python我用的rrdtools的版本是 1.3.8-7.el6.x86_64,由于我用的是阿里云yum里有rrdtool 所以我就直接yum install rrdtool* 来进行安装
rrdtool-devel-1.3.8-7.el6.x86_64
rrdtool-perl-1.3.8-7.el6.x86_64
rrdtool-1.3.8-7.el6.x86_64
rrdtool-php-1.3.8-7.el6.x86_64
rrdtool-python-1.3.8-7.el6.x86_64
如果没有自行下载。下载完成后,本地yum安装。(百度云里面有1.4的)
yum localinstall -y --nogpgcheck rrdtool-*
service mysqld start
2.配置snmp
vim /etc/snmp/snmpd.conf
com2sec notConfigUser 127.0.0.1 public
access notConfigGroup "" any noauth exact all none none
view all included .1 80
*前两行是修改最后一行是取消注释
service snmpd restart
snmpwalk -v 1 -c public localhost IP-MIB::ipAdEntIfIndex
IP-MIB::ipAdEntIfIndex.127.0.0.1 = INTEGER: 1
IP-MIB::ipAdEntIfIndex.xxx.xxx.xxx.xxx = INTEGER: 2
IP-MIB::ipAdEntIfIndex.xxx.xxx.xxx.xxx = INTEGER: 3
显示以上信息,则snmp配置成功,xxx.xxx.xxx.xxx会根据你本机的IP信息配置而不同。
3.安装cacti
cacti-0.8.7e-cn-utf8.tar.gz
mv cacti-0.8.7e-cn-utf8 /var/www/html/cacti
4.创建cacti数据库
#mysql
mysql>create database cacti default character set utf8; //数据库字符集设置utf8,否则乱码
mysql>grant all privileges on cacti.* to cacti@localhost identified by ‘cacti’ with grant option;
mysql>flush privileges;
5.将cacti的表内容导入创建的数据库
cd /var/www/html/cacti
mysql -ucacti -pcacti cacti < cacti.sql
6.编辑.php文件,以适应环境。
vim /var/www/html/cacti/include/config.php
修改内容如下
$database_type = "mysql";
$database_default = "cacti";
$database_hostname = "localhost";
$database_username = "cacti";
$database_password = "cacti";
useradd -r -M cacti
chown -R cacti /var/www/html/cacti/rra/
chown -R cacti /var/www/html/cacti/log/
7.在cacti用户下创建计划任务以画图
vim /etc/crontab
*/5 * * * * root php /var/www/html/cacti/poller.php &>/dev/null
php /var/www/html/cacti/poller.php > /dev/null 2>&1
8.安装中文字体否则图像文字上是乱码
mkdir /ttf
mv ttf-arphic-ukai_0.2.20080216.1.orig.tar.gz /ttf
cd /ttf
tar xf ttf-arphic-ukai_0.2.20080216.1.orig.tar.gz
mv ukai.ttc /usr/share/fonts/ukai.ttc
rm –rf /ttf
9.从web页面启动cacti,安装,并查看图形化界面
控制面板 > 设备 > 本机 > SNMP选项 > SNMP 版本 选择 版本1 > 保存
控制面板 > 设置 > 路径 > RRDTool默认字体路径 /usr/share/fonts/ukai.ttc > 保存
如果配置正确,隔几分钟,我们的cacti就会有数据了,如图,你也可以手动执行以下命令手动生成rrd图像
php /var/www/html/cacti/poller.php
650) this.width=650;" height="401" src="http://a4.qpic.cn/psb?/V13tFvpi2Yisdp/4OBWuc3PCiHqjDQQmU3cU.t2EwNCfmn1PL.V*veg1pA!/b/dFcBAAAAAAAA&ek=1&kp=1&pt=0&bo=FQOSAQAAAAADAKA!&su=0256865921&tm=1492138800&sce=0-12-12&rf=2-9" width="788" style="margin:0px;padding:0px;border-width:0px;border-style:none;vertical-align:top;" alt="dFcBAAAAAAAA&ek=1&kp=1&pt=0&bo=FQOSAQAAA" /> |
650) this.width=650;" height="420" src="http://a4.qpic.cn/psb?/V13tFvpi2Yisdp/VyGfKJ8FDzAzixqfreZh5X.4wNz58jOiaAV7eVQHhEo!/b/dFcBAAAAAAAA&ek=1&kp=1&pt=0&bo=FQOlAQAAAAADAJc!&su=0164574145&tm=1492138800&sce=0-12-12&rf=2-9" width="788" style="margin:0px;padding:0px;border-width:0px;border-style:none;vertical-align:top;" alt="dFcBAAAAAAAA&ek=1&kp=1&pt=0&bo=FQOlAQAAA" /> |
SNMP 版本号
字体路径
中文的图表
650) this.width=650;" height="420" src="http://a3.qpic.cn/psb?/V13tFvpi2Yisdp/il09J6s7A7VZkn8ewYgqQRmKmrj6OunKBBFFj.CkEtw!/b/dGIAAAAAAAAA&ek=1&kp=1&pt=0&bo=GAOlAQAAAAADAJo!&su=021471489&tm=1492138800&sce=0-12-12&rf=2-9" width="791" style="margin:0px;padding:0px;border-width:0px;border-style:none;vertical-align:top;" alt="dGIAAAAAAAAA&ek=1&kp=1&pt=0&bo=GAOlAQAAA" /> |
三:安装cacti-spine
1. cacti-spine的安装
由于默认cmd.php来轮询数据,速度会很慢,特别是在监控节点比较多的情况下,cmd.php就更显不足了,因此我们采用Spine来轮询数据。cacti-spine是一个由C语言开发的,用于替代cmd.php的快速获取速度的引擎。
tar xf cacti-spine-0.8.7e.tar.gz
cd cacti-spine-0.8.7e
./configure
#make && make install
vi /usr/local/spine/etc/spine.conf
修改内容如下:
DB_Host localhost
DB_Database cacti
DB_User cacti
DB_Pass cacti
DB_Port 3306
/usr/local/spine/bin/spine
echo /usr/local/spine/bin/spine >>/etc/rc.d/rc.local
*如果你直接成功了那么恭喜你请直接看2.cacti-spine的应用,如果出现报错*
[root@iZ2314cchhjZ cacti-spine-0.8.7e]# ./configure
configure: error: cannot run /bin/sh config/config.sub
[root@iZ2314cchhjZ cacti-spine-0.8.7e]#
解决步骤
cd cacti-spine-0.8.7e/config
ll -h
rm -rf config.guess config.sub ltmain.sh
ln -s -f /usr/share/libtool/config/config.sub config.sub
ln -s -f /usr/share/libtool/config/config.guess config.guess
ln -s -f /usr/share/libtool/config/ltmain.sh ltmain.sh
cd ..
./configure
make
make install
vi /usr/local/spine/etc/spine.conf
修改内容如下:
DB_Host localhost
DB_Database cacti
DB_User cacti
DB_Pass cacti
DB_Port 3306
/usr/local/spine/bin/spine
echo /usr/local/spine/bin/spine >>/etc/rc.d/rc.local
*如果你make 成功了请忽略这里*
这时候你make 报错,这是因为你可能没有装 libtool 或者libtool的版本不匹配
./libtool: line 467: CDPATH: command not found
./libtool: line 1145: func_opt_split: command not found
libtool: Version mismatch error. This is libtool 2.2.6b, but the
libtool: definition of this LT_INIT comes from an older release.
libtool: You should recreate aclocal.m4 with macros from libtool 2.2.6b
libtool: and run autoconf again.
make: *** [spine] 错误 63
先检查你有没有安装libtool 如果没有就yum install libtool* 然后在进行尝试
如果还不行的
[root@iZ2314cchhjZ cacti-spine-0.8.7e]# autoreconf --force --install
[root@iZ2314cchhjZ cacti-spine-0.8.7e]# make
make install
vi /usr/local/spine/etc/spine.conf
修改内容如下:
DB_Host localhost
DB_Database cacti
DB_User cacti
DB_Pass cacti
DB_Port 3306
/usr/local/spine/bin/spine
echo /usr/local/spine/bin/spine >>/etc/rc.d/rc.local
2.cacti-spine的应用
控制面板 > 设置 > 路径 > Spine轮询器路径 /usr/local/spine/bin/spine
控制面板 > 设置 > 轮询器 > 轮询器类型 spine
650) this.width=650;" height="399" src="http://a3.qpic.cn/psb?/V13tFvpi2Yisdp/.n.gjMdFpq3syLK09QtxbRrhjJpNSBkbw6fvF52tsxI!/b/dFoBAAAAAAAA&ek=1&kp=1&pt=0&bo=EwOQAQAAAAADAKQ!&su=0238996465&tm=1492138800&sce=0-12-12&rf=2-9" width="786" style="margin:0px;padding:0px;border-width:0px;border-style:none;vertical-align:top;" alt="dFoBAAAAAAAA&ek=1&kp=1&pt=0&bo=EwOQAQAAA" /> |
650) this.width=650;" height="402" src="http://a3.qpic.cn/psb?/V13tFvpi2Yisdp/Xs2V3FJr1PIX3hlh8xsYldz85UOJnam*p7poRx7XeDQ!/b/dFoBAAAAAAAA&ek=1&kp=1&pt=0&bo=FQOTAQAAAAADAKE!&su=026970977&tm=1492138800&sce=0-12-12&rf=2-9" width="788" style="margin:0px;padding:0px;border-width:0px;border-style:none;vertical-align:top;" alt="dFoBAAAAAAAA&ek=1&kp=1&pt=0&bo=FQOTAQAAA" /> |
四:整合Nagios与Cacti
1.下载并安装ndoutils
tar zxvf ndoutils-1.4b9.tar.gz
cd ndoutils-1.4b9
./configure -prefix=/var/www/html/nagios -enable-mysql -disable-pgsql -with-mysql-inc=/usr/include/mysql -with-mysql-lib=/usr/lib/mysql
make
2.准备配置文件
cp -v src/{ndomod-3x.o,ndo2db-3x,file2sock,log2ndo} /var/www/html/nagios/bin
cd db
./installdb -ucacti -pcacti -hlocalhost -d cacti
cd ..
cp -v config/{ndo2db.cfg-sample,ndomod.cfg-sample} /var/www/html/nagios/etc
mv /var/www/html/nagios/etc/ndo2db.cfg-sample /var/www/html/nagios/etc/ndo2db.cfg
mv /var/www/html/nagios/etc/ndomod.cfg-sample /var/www/html/nagios/etc/ndomod.cfg
chmod 644 /var/www/html/nagios/etc/ndo*
chown nagios.nagios /var/www/html/nagios/etc/*
chown nagios.nagios /var/www/html/nagios/bin/*
3.修改nagios.cfg配置文件以适应当前环境
vim /var/www/html/nagios/etc/nagios.cfg
# 在文件中添加:
broker_module=/var/www/html/nagios/bin/ndomod-3x.o config_file=/var/www/html/nagios/etc/ndomod.cfg
# 检查
event_broker_options=-1 ## 为Nagios开启event broker
4.修改ndo2db.cfg以适应当前环境
vim /var/www/html/nagios/etc/ndo2db.cfg
# 确保下列内容为唯一项
socket_type=unix
db_servertype=mysql
db_host=localhost
db_port=3306
db_name=cacti
db_prefix=nagios_
db_user=cacti
db_pass=cacti
5.修改ndomod.cfg以适应当前环境
vim /var/www/html/nagios/etc/ndomod.cfg
# 确保下列项的唯一内容为
output_type=unixsocket
output=127.0.0.1
6.为ndo2db添加启动进程
cp ./daemon-init /etc/init.d/ndo2db
vim /etc/init.d/ndo2db
# 并将Ndo2dbBin修改成下面的值:
Ndo2dbBin=/var/www/html/nagios/bin/ndo2db-3x
chmod +x /etc/init.d/ndo2db
7.启动守护进程与nagios
service ndo2db start
tail -20 /var/log/messages ## 查看其中是否有错误出现。如无报错请继续
service nagios restart
五:安装cacti-plugin 插件管理
Cacti-Plugin安装过程如下:
gunzip cacti-plugin-0.8.7e-PA-v2.5-cn-utf8.diff.gz
mv cacti-plugin-0.8.7e-PA-v2.5-cn-utf8.diff /var/www/cacti/
cd /var/www/html/cacti
mysql -ucacti -pcacti cacti < pa.sql
patch -p1 -N < cacti-plugin-0.8.7e-PA-v2.5-cn-utf8.diff
如果你的cacti访问时候是使用http://ip/cacti,你需要修改/var/www/cacti/include/global.php
修改如下:
$database_type = "mysql";
$database_default = "cacti";
$database_hostname = "localhost";
$database_username = "cacti";
$database_password = "cacti";
$database_port = "3306";
$config['url_path'] = '/cacti/'; #记住是修改不是添加
配置Cacti
控制面板 > 用户管理 > admin > 区域权限,选择”插件管理” > 保存
然后我们就能够在控制面板的”配置”下面看到多出一个”插件管理”的选项
650) this.width=650;" height="419" src="http://a2.qpic.cn/psb?/V13tFvpi2Yisdp/2yM689m*MG8PUKDW350BtfX9DKrcD3beLY.q5Tg.uNQ!/b/dFkBAAAAAAAA&ek=1&kp=1&pt=0&bo=GQOkAQAAAAADAJo!&su=0255312993&tm=1492138800&sce=0-12-12&rf=2-9" width="792" style="margin:0px;padding:0px;border-width:0px;border-style:none;vertical-align:top;" alt="dFkBAAAAAAAA&ek=1&kp=1&pt=0&bo=GQOkAQAAA" /> |
六:安装NPC 插件
tar zxvf npc-2.0.4.tar.gz
mv npc /var/www/html/cacti/plugins/
vim /var/www/html/cacti/include/config.php
# 加入:
$plugins[] = 'npc';
650) this.width=650;" height="141" src="http://a4.qpic.cn/psb?/V13tFvpi2Yisdp/68sW5d9uApXGhYvadP4YMw6JecnR2TnIkP1ychsgOJU!/b/dFcBAAAAAAAA&ek=1&kp=1&pt=0&bo=KgKOAAAAAAADAIM!&su=0121440129&tm=1492138800&sce=0-12-12&rf=2-9" width="553" style="margin:0px;padding:0px;border-width:0px;border-style:none;vertical-align:top;" alt="dFcBAAAAAAAA&ek=1&kp=1&pt=0&bo=KgKOAAAAA" />
1.安装 npc支持:json (看情况进行安装)
先使用这条命令看看PHP里有没有JSON的模块php -m | grep json如果有了就略过。
tar zxvf json-1.2.1.tgz
cd json-1.2.1
phpize ## 如果发现没有Phpize文件,则是因为php-devel包没有安装导致的
./configure
make && make install
php -i | grep php.ini ## 查看是否有导入信息
打开php.ini文件添加对json的支持
vim /etc/php.ini
# 添加
extension=json.so
php -m ## 查看是否有json被加载
2.配置npc基本环境
一.为apache添加php支持。
vim /etc/httpd/conf/httpd.conf
# 添加一行:
AddType application/x-httpd-php .php .phtml
# 修改原DirectoryIndex内容为:
DirectoryIndex index.html index.html.var index.php
usermod -G cacti apache
service httpd restart ## 重启apache以让php生效
学神科技有限公司Linux讲师:top 联系QQ 3059942361 想要深入学习Linux系统架构师,可以随时与我联系
二.修改配置文件以让npc读取到新的数据。
vim /var/www/html/nagios/etc/ndo2db.cfg
db_prefix=npc_
三. 修改mysql中的表结构:
#mysql -ucacti -pcacti
mysql> use cacti;
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;
四.重启ndo2db进程,重新加载配置文件重启nagios服务。
service ndo2db restart
service nagios restart
五.在Web上配置NPC以使之正常读取工作。
控制面板 > 插件管理 > 安装 > 启用
控制面板 > 设置 > NPC
Nagios Command File Path= /var/www/html/nagios/var/rw/nagios.cmd
Nagios URL=你的地址(http://101.200.175.24/nagios)
接下来刷新npc就可以看到数据啦
如果你点开NPC的时候状态是OFF的话。进行操作如下:
vim /var/www/html/nagios/etc/ndomod.cfg
# 修改
output_type=tcpsocket
vim /var/www/html/nagios/etc/ndo2db.cfg
# 修改
socket_type=tcp
*一定要保证是唯一项*
七:Nconf的安装与配置
1.nconf的准备工作
#mysql
mysql> create database nconf;
mysql> grant all privileges on nconf.* to nconf@localhost identified by ‘nconf’;
mysql> flush privileges;
mysql> quit
2.安装nconf
tar -zxf nconf-1.2.6-0.tgz -C /var/www/html/
cd /var/www/html/nconf
chown -R apache.apache config/ temp/ static_cfg/ output/
3.进入Web页面在web页面按照安装提示一步步安装
*使用刚才创建的nconf数据库和nconf数据库用户/密码
4.成功连接mysql数据库之后一切保持默认,直到下面的,设置nconf管理员密码,此时默认登录用户为admin
5.之后,网页安装完成。
rm -rf INSTALL INSTALL.php UPDATE UPDATE.php
# 删除安装所需的初始化文件
ln -s /var/www/html/nagios/bin/nagios bin/nagios
chmod +x /var/www/html/nconf/bin/generate_config.pl
chown -R apache.apache bin
chmod +x /var/www/html/nagios/bin/nagios
chmod +x /var/www/html/nconf/ADD-ONS/deploy_local.sh
vim /var/www/html/nconf/ADD-ONS/deploy_local.sh
#修改里面的Nagios路径为 /var/www/html/nagios/etc/
650) this.width=650;" height="112" src="http://a3.qpic.cn/psb?/V13tFvpi2Yisdp/I2TUiNo9HHNPZWJ*IOHmIRLclF5aw7At6rpL.5VFnhA!/b/dFoBAAAAAAAA&ek=1&kp=1&pt=0&bo=wwFxAAAAAAADAJY!&su=051212017&tm=1492138800&sce=0-12-12&rf=2-9" width="450" style="margin:0px;padding:0px;border-width:0px;border-style:none;vertical-align:top;" alt="dFoBAAAAAAAA&ek=1&kp=1&pt=0&bo=wwFxAAAAA" />
6.重新刷新网页
7.点击Generate Nagios config尝试生成配置文件,以测试是否与nagios成功关联
8.最后的设置
一.在web页面对监控服务和主机配置完毕,点击Generate Nagios config
二.deploy_local.sh是nconf自带配置文件导入脚本
/var/www/html/nconf/ADD-ONS/deploy_local.sh
手动执行此脚本,将在/var/www/html/nagios/etc下生成两目录Default_collector与global
三.修改nagios主配置文件
vim /var/www/html/nagios/etc/nagios.cfg
# 将其中cfg_file=*******字段全部注释 ,并加入
cfg_dir=/var/www/html/nagios/etc/Default_collector
cfg_dir=/var/www/html/nagios/etc/global
service nagios reload
# 重载nagios配置文件使之生效
本文出自 “xinsz08の平行时空” 博客,请务必保留此出处http://xinsz08.blog.51cto.com/10565212/1915915