1.配置好yum源,为了提高效率建议搭建本地yum源
yum install -y http*yum install -y mysql*
yum install -y php-*
yum install -y net-snmp*
yum install -y httpd gcc glibc glibc-common gd gd-devel php php-gd ntp
2.安装nagios相关软件
#!/bin/shNAGIOS_TOOLS_DIR=/home/oldboy/tools
NAGIOS_INSTALL_DIR=/usr/local/nagios
echo "------ step 2: config profile------"
echo 'export LC_ALL=C'>> /etc/profile
source /etc/profile
echo "------ step 3: stop iptables and selinux------"
/etc/init.d/iptables stop
/etc/init.d/iptables stop
chkconfig iptables off
setenforce 0
if [ -f /etc/selinux/config ]; then
sed -i 's#SELINUX=enforcing#SELINUX=disabled#g' /etc/selinux/config
setenforce 0
fi
echo "------ step 4: config time sync------"
/usr/sbin/ntpdate pool.ntp.org
echo '#time sync by oldboy at 2010-2-1'>>/var/spool/cron/root
echo '*/10 * * * * /usr/sbin/ntpdate pool.ntp.org >/dev/null 2>&1'>>/var/spool/cron/root
crontab -l
echo "------ step 5: install gcc and lamp env etc------"
yum install gcc glibc glibc-common -y
yum install gd gd-devel -y
yum install httpd php php-gd -y
yum install mysql* -y
echo "------ step 6: add nagios user and group------"
/usr/sbin/useradd -m nagios
#/usr/sbin/useradd apache
/usr/sbin/groupadd nagcmd
/usr/sbin/usermod -a -G nagcmd nagios
/usr/sbin/usermod -a -G nagcmd apache
echo "------ step 7: download and install nagios------"
cd /home/oldboy/tools
[ ! -f oldboy_training_nagios_soft.zip ] && exit 1
unzip oldboy_training_nagios_soft.zip
tar xzf nagios-3.5.1.tar.gz
cd nagios
./configure --with-command-group=nagcmd
make all
make install
make install-init
make install-config
make install-commandmode
make install-webconf
cd ../
echo "------ step 8: config web auth------"
htpasswd -cb /usr/local/nagios/etc/htpasswd.users oldboy 123456
echo "------ step 9: install nagios-plugins------"
yum -y install perl-devel
tar zxf nagios-plugins-1.4.16.tar.gz
cd nagios-plugins-1.4.16
./configure --with-nagios-user=nagios --with-nagios-group=nagios --enable-perl-modules
make && make install
cd ../
ls /usr/local/nagios/libexec/|wc -l
echo "------ step 10: install nrpe------"
tar zxvf nrpe-2.12.tar.gz
cd nrpe-2.12
./configure
make all
make install-plugin
make install-daemon
make install-daemon-config
cd ..
echo "------ step 11: startup service and check------"
/etc/init.d/nagios start
/etc/init.d/httpd start
echo -----------
lsof -i tcp:80
echo ----------
ps -ef|grep nagios
echo "NAGIOS is successfully installed."
echo ok
3.安装cacti
①安装cacti依赖的软件
yum -y install mysql mysql-server mysql-devel httpd php php-pdo php-snmp php-mysql lm_sensors net-snmp net-snmp-utils net-snmp-libs rrdtool rrdtool-devel perl-PlRPC perl-DBI perl-rrdtool perl-DBD-MySQL
②装好依赖软件之后,snmp有3个地方需要配置一下(对服务器本身进行监控)
vim /etc/snmp/snmpd.conf
第41行:将 com2sec notConfigUser default public 中的 "default" 改为 "127.0.0.1"
第62行:将 access notConfigGroup "" any noauth exact systemview none none 中的 "systemview" 改为 "all"
第85行:将 #view all include .1 80 这一行前面的 # 号去掉
③配置好snmp以后我们来启动3个重要的服务
service snmpd start //启动snmp服务
chkconfig snmpd on //设置开机启动
service httpd start
chkconfig httpd on
service mysqld start
chkconfig mysqld on
④安装并配置cacti
获取cacti主文件
http://www.cacti.net/downloads/cacti-0.8.8b.tar.gz
tar -zxvf cacti-0.8.8b.tar.gz
mv cacti-0.8.8b /var/www/html/cacti //将cacti-0.8.8b文件夹移到/var/www/html目录下,并将cacti-0.8.8b重命名为cacti
⑤设置mysql数据库
mysql -uroot -p #新装的mysql没有密码,直接回车进入mysql数据库
create database cacti; #首先创建cacti数据库,注意别丢了分号
grant all privileges on cacti.* to cacti@localhost identified by 'cacti' with grant option; #创建cacti用户并授权
use cacti;
source /var/www/html/cacti/cacti.sql; #导入cacti数据库文件
mysql>\q
⑥配置cacti
vim /var/www/html/cacti/include/config.php
$database_type = "mysql";
$database_default = "cacti";
$database_hostname = "localhost";
$database_username = "cactiuser"; //因为我们先前在数据库建的是 "cacti" 用户,所以这里默认的 "cactiuser" 要改为 "cacti"
$database_password = "cactiuser"; //这里默认的密码我们也要改为 "cacti"
$database_port = "3306";
⑦增加 cacti 用户以用来写入rrd和log目录的数据,否则就会生成不了图片
useradd cacti
cd /var/www/html/cacti
chown -R cacti.cacti rra/ log/
⑧在cacti用户下创建计划任务以画图
具体多长时间让cacti生成一次监控图表自己决定,这里以cacti的默认时间5分钟生成一次为例
su cacti
crontab –e
*/5 * * * * php /var/www/html/cacti/poller.php > /dev/null 2>&1
exit
# 如果暂时未看到图表,可以手工执行,生成图表
php /var/www/html/cacti/poller.php > /dev/null 2>&1
⑩配置好之后重启输入http://ip/cacti,从web页面启动cacti,安装,并查看图形化界面
4.整合Nagios与Cacti
①下载并安装ndoutils
wget http://sourceforge.net/projects/nagios/files/ndoutils-1.x/ndoutils-1.4b9/ndoutils-1.4b9.tar.gz/download
tar zxvf ndoutils-1.4b9.tar.gz
cd ndoutils-1.4b9
./configure --prefix=/usr/local/nagios --enable-mysql --disable-pgsql --with-mysql-inc=/usr/include/mysql --with-mysql-lib=/usr/lib/mysql
make
②准备配置文件
cp -v src/{ndomod-3x.o,ndo2db-3x,file2sock,log2ndo} /usr/local/nagios/bin
cd db
./installdb -ucacti -pcacti -hlocalhost -d cacti
cd ..
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 644 /usr/local/nagios/etc/ndo*
chown nagios:nagios /usr/local/nagios/etc/*
chown nagios:nagios /usr/local/nagios/bin/*
③修改nagios.cfg配置文件以适应当前环境
vim /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
④修改ndo2db.cfg以适应当前环境
vim /usr/local/nagios/etc/ndo2db.cfg
# 确保下列内容为唯一项
socket_type=tcp
db_servertype=mysql
db_host=localhost
db_port=3306
db_name=cacti
db_prefix=nagios_
db_user=cacti
db_pass=cacti
⑤修改ndomod.cfg以适应当前环境
vim /usr/local/nagios/etc/ndomod.cfg
# 确保下列项的唯一内容为
output_type=tcpsocket
output=127.0.0.1
⑥为ndo2db添加启动进程
cp ./daemon-init /etc/init.d/ndo2db
vim /etc/init.d/ndo2db
# 检查里面的路径确保不会出现“//",并将Ndo2dbBin修改成下面的值:
Ndo2dbBin=/usr/local/nagios/bin/ndo2db-3x
chmod +x /etc/init.d/ndo2db
⑦启动守护进程与nagios
service ndo2db start
tail -20 /var/log/messages ## 查看其中是否有错误出现。如无报错请继续
service nagios start
5.安装ncp,以在Cacti中展现Nagios
1.先安装cacti扩展模块wget http://www.cacti.net/downloads/pia/cacti-plugin-0.8.7h-PA-v3.0.tar.gz
tar xvf cacti-plugin-0.8.7h-PA-v3.0.tar.gz
cp -R cacti-plugin-arch/* /var/www/html/cacti/
cd /var/www/html/cacti/
mysql -ucacti -pcacti cacti < pa.sql
patch -p1 -N < cacti-plugin-0.8.7h-PA-v3.0.diff
## 为cacti配置文件打补丁的时候注意在为include/config.php打补丁的时候有可能它将 include/config.php.dist给打补丁了,我们只需要手动加入下面的内容即可。
vim include/config.php
# 修改(或新加入)为
$url_path = "/cacti/";
从web进入cacti,启用cacti plugin扩展
2.安装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';
3.安装 npc支持:json
wget http://pkgs.fedoraproject.org/repo/pkgs/php-pecl-json/json-1.2.1.tgz/d8904d2f004ceec85eeacf524cd25539/json-1.2.1.tgz
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 Warning: PHP Startup: Unable to load dynamic library '/usr/lib64/php/modules/json.so' - /usr/lib64/php/modules/json.so: undefined symbol: ZVAL_DELREF in Unknown on line 0
解决办法:
vim JSON_parser.c
/* error */#define S_ERR -1
#define ZVAL_DELREF Z_DELREF_P 加入这一行
/* space */
#define S_SPA 0
重新编译即可
4.打开php.ini文件添加对json的支持
vim /etc/php.ini
# 添加
extension=json.so
5.为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生效
php -m ## 查看是否有json被加载
6.修改配置文件以让npc读取到新的数据。
vim /usr/local/nagios/etc/ndo2db.cfg
db_prefix=npc_
6.1从Web进入Cacti,确保正确使用npc选项。
6.2 修改mysql中的表结构:
./mysql -ucacti -p cactimysql> 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;
7.重启ndo2db进程,重新加载配置文件重启nagios服务。
service ndo2db restart
service nagios restart
8.在Web上配置NPC以使之正常读取工作。
在Settings->npc中 ,勾上Remote Commands
Nagios Command File Path= /usr/local/nagios/var/rw/nagios.cmd
Nagios URL=你的地址(http://localhost/nagios/)
接下来刷新npc就可以看到数据啦:
6.Nconf的安装与配置
1.nconf的准备工作# 进入mysql数据库
mysql> create database nconf;
# 创建nconf数据库
mysql> grant all privileges on nconf.* to nconf@localhost identified by 'nconf';
# 创建nconf用户并赋权
mysql> flush privileges;
# 刷新特权表
mysql> quit
2.安装nconf
wget https://sourceforge.net/projects/nconf/files/nconf/1.3.0-0/nconf-1.3.0-0.tgz/download
tar -zxf nconf-1.3.0-0.tgz -C /var/www/html/
cd /var/www/html/nconf
chown -R apache.apache config/ temp/ static_cfg/ output/
3.进入Web页面在web页面按照安装提示一步步安装
4.使用刚才创建的nconf数据库和nconf数据库用户/密码
5.成功连接mysql数据库,之后一切保持默认,直到下面的,设置nconf管理员密码,此时默认登录用户为admin
6.之后,网页安装完成。
rm -rf INSTALL INSTALL.php UPDATE UPDATE.php
# 删除安装所需的初始化文件
ln -s /usr/local/nagios/bin/nagios bin/nagios
chmod +x /var/www/html/nconf/bin/generate_config.pl
chown -R apache.apache bin
chmod +x /usr/local/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路径为 /usr/local/nagios/etc/
7.重新刷新页面,输入admin/您设置的密码,进入nconf页面
8.点击Generate Nagios config尝试生成配置文件,以测试是否与nagios成功关联
9.最后的设置
①在web页面对监控服务和主机配置完毕,点击Generate Nagios config
②deploy_local.sh是nconf自带配置文件导入脚本
/var/www/html/nconf/ADD-ONS/deploy_local.sh
手动执行此脚本,将在/usr/local/nagios/etc下生成两目录Default_collector与global
③修改nagios主配置文件
vim /usr/local/nagios/etc/nagios.cfg
# 将其中cfg_file=*******字段全部注释 ,并加入
cfg_dir=/usr/local/nagios/etc/Default_collector
cfg_dir=/usr/local/nagios/etc/global
service nagios reload
# 重载nagios配置文件使之生效
报错:
Error: Cannot open main configuration file '/var/www/html/nconf/temp/test/Default_collector.cfg' for reading!
应该是没有执行chown -R apache.apache config/ temp/ static_cfg/ output/
重新安装nconf即可