开源监控工具cacti和naigos安装文档(一)

参考1:http://bristol.blog.51cto.com/1004110/1177671

参考2:http://www.cactifans.org/cacti/409.html

  • 安装环境要求及其所需要的安装文件:
  1. 安装环境:CentOS 6 + http + php + mysql
  2. Cacti及其相关包如下:

cacti-0.8.8c.tar.gz

settings-v0.71-1.tgz

thold-v0.4.9-3.tgz

npc.tar.gz

ntop-v0.2-1.tgz

  1. Nagios及其相关包如下:

nagios-4.0.8.tar.gz

nagios-plugins-2.0.3.tar.gz

nagiosql_320.tar.gz

ndoutils-2.0.0.tar.gz

 

  • 安装环境
  1. Yum安装环境

# yum -y install httpd php php-mysql mysql mysql-server mysql-devel php-snmp net-snmp-utils rrdtool rrdtool-devel rrdtool-php

  1. 修改mysql root密码

# mysqladmin –uroot password  ‘123456’

 

  • 安装cacti
  1. 由于一些包在官方的库中没有,因此我们要建立repo,定义非官方库

# vi /etc/yum.repos.d/dag.repo

[dag]

name=Dag RPM Repository for Red Hat Enterprise Linux

baseurl=http://apt.sw.be/redhat/el$releasever/en/$basearch/dag

gpgcheck=1

enabled=1

导入非官方库的GPG:

# rpm –import http://dag.wieers.com/rpm/packages/RPM-GPG-KEY.dag.txt

  1. 安装cacti需要的环境包

# yum -y install  php-devel php-common net-snmp net-snmp-utils zlib libpng freetype libjpeg fontconfig gd libxml2 patch wget rrdtool xinetd gd gd-devel openssl-devel

  1. 安装cacti

# cd /usr/local/src/monitor

# tar zxvf cacti-0.8.8.c.tar.gz  -C /var/www/html/

# chown –R apache:apache /var/www/html/cacti

  1. 导入cacti的sql文件

# 建立cacti数据库

# mysql –uroot –p123456

mysql> create database cacti;

mysql> grant all privileges on cacti.* to cacti@localhost identified by ‘cacti123’;

mysql> flush privileges;

# 导入sql

# mysql –ucacti –pcacti123 cacti < /var/www/html/cacti/cacti.sql

  1. 修改cacti配置文件

# vi /var/www/html/cacti/include/config.php

$database_type = “mysql”;

$database_default = “cacti”;

$database_hostname = “localhost”;

$database_username = “cacti”;

$database_password = “cacti123”;

$database_port = “3306”;

$database_ssl = false;

# vi /var/www/html/cacti/include/global.php

$database_type = “mysql”;

$database_default = “cacti”;

$database_hostname = “localhost”;

$database_username = “cacti”;

$database_password = “cacti123”;

$database_port = “3306”;

$database_ssl = false;

  1. 配置httpd

# vi /etc/httpd/conf.d/cacti.conf

Alias /cacti “/var/www/html/cacti”

 

<Directory “/var/www/html/cacti”>

Options ExecCGI

AllowOverride None

Order allow,deny

Allow from all

</Directory>

#修改php.ini的时区

# sed -i ‘s/^;date.timezone =/date.timezone =PRC/g’ /etc/php.ini

  1. 修改被监控端SNMP配置文件

# vi /etc/snmp/snmpd.conf

com2sec 192.168.1.11 default     monitor

access  notConfigGroup “”      any       noauth    exact  all none none

view all    included  .1                               80

  1. 设置开机启动并重启服务

# chkconfig snmpd on

# chkconfig httpd on

# chkconfig mysqld on

# chkcofnig crond on

# /etc/init.d/mysqld restart

# /etc/init.d/httpd restart

  1. WEB页面安装

http://ip/cacti

Next >> Next >> Finish

用户名和密码为:admin/admin,之后输入新的密码,就完成了最基本的安装。

开启插件管理:在cacti 页面点console–>User Management->admin->在Plugin Management上打钩->Save

  1. 添加计划任务

*/5 * * * *  /usr/bin/php  /var/www/html/cacti/poller.php > /dev/null 2>&1

 

  • 安装nagios
  1. 建立用户及用户组

# useradd nagios

# groupadd nagcmd

  1. 安装nagios

# tar zxvf nagios-4.0.8.tar.gz

# cd nagios-4.0.8

# ./configure –with-command-group=nagcmd –with-gd-lib=/usr/lib

# make all

# make install

# make install-init

# make install-config

# make install-commandmode

# make install-webconf

# cp –R contrib/eventhandlers/ /usr/local/nagios/libexec

# chown –R nagios:nagios /usr/local/nagios/libexec/eventhandlers

# 检查nagios配置文件是否有错

# /usr/local/nagios/bin/nagios –v /usr/local/nagios/etc/nagios.cfg

# 启动nagios

/etc/init.d/nagios start

# 为nagios访问页面添加访问权限

# htpasswd -c /usr/local/nagios/etc/htpasswd.users nagiosadmin

  1. 为httpd添加nagios虚拟目录(安装nagios会自动添加到http中,没有自己添加)

# vi /etc/httpd/conf.d/nagios.conf

ScriptAlias /nagios/cgi-bin “/usr/local/nagios/sbin”

 

<Directory “/usr/local/nagios/sbin”>

#  SSLRequireSSL

Options ExecCGI

AllowOverride None

Order allow,deny

Allow from all

#  Order deny,allow

#  Deny from all

#  Allow from 127.0.0.1

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

AuthName “Nagios Access”

AuthType Basic

AuthUserFile /usr/local/nagios/etc/htpasswd.users

Require valid-user

</Directory>

 

  1. 为nagios客户端(被监控端)安装nagios-plugins和NRPE监控程序

# tar zxvf nagios-plugins-2.0.3.tar.gz

# cd nagios-plugins-2.0.3

# ./configure –with-nagios-user=nagios –with-nagios-group=nagios

# make && make install

# 安装NRPE

# tar zxvf nrpe-2.15.tar.gz

# cd nrpe-2.15

# ./configure

# make all

# make install-plugin

# make install-daemon

# make install-daemon-config

# make install-xinetd

# 修改NRPE允许特定IP访问NRPE

#  sed  -i  ‘s/127.0.0.1/127.0.0.1  192.168.1.11/g’  /etc/xinetd.d/nrpe

# 将NRPE添加到系统服务

# echo “nrpe                                 5666/tcp                              #nrpe” >> /etc/services

# /etc/init.d/xinetd restart

# chkconfig xinetd on

 

  • 安装ndoutils 和 NPC插件整合cacti
  1. 安装ndotuils

# tar zxvf ndotuils-2.0.0.tar.gz

# cd ndoutils-2.0.0

# echo “/usr/lib64/mysql” >> /etc/ld.so.conf

# ldconfig

# ./configure –prefix=/usr/local/nagios –enable-mysql –disable-pgsql –with-mysql-inc=/usr/include/mysql –with-mysql-lib=/usr/lib64/mysql

# make

# cp –v src/{ndomod-4x.o,ndo2db-4x,file2sock,log2ndo} /usr/local/nagios/bin/

# cd db

# ./installdb –ucati –pcacti123 –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/*

2.  修改nagios.cfg

#vi /usr/local/nagios/etc/nagios.conf

check_external_commands=1

event_broker_options = -1

broker_module=/usr/local/nagios/bin/ndomod-4x.o \ config_file=/usr/local/nagios/etc/ndomod.cfg

process_performance_data=1

 

3. 修改ndo2db.cfg

sed -i ‘s/socket_type=unix/socket_type=tcp/g’ /usr/local/nagios/etc/ndo2db.cfg

sed -i ‘s/db_name=nagios/db_name=cacti/g’  /usr/local/nagios/etc/ndo2db.cfg

sed -i ‘s/db_user=ndouser/db_user=cacti/g’  /usr/local/nagios/etc/ndo2db.cfg

sed –i ‘s/db_pass=ndopassword/db_pass=cacti123/g’  /usr/local/nagios/etc/ndo2db.cfg

sed –i ‘s/db_prefix=nagios_/db_prefix=npc_/g’ /usr/local/nagios/etc/ndo2db.cfg

sed –i ‘s/debug_level=0/debug_level=1/g’ /usr/local/nagios/etc/ndo2db.cfg

4. 修改ndomod.cfg

output_type=tcpsocket

output=127.0.0.1

buffer_file=/usr/local/nagios/var/ndomod.tmp

5. 为ndo2db添加启动进程

# cp ./daemon-init /etc/init.d/ndo2db

# sed -i ‘s?Ndo2dbBin=/usr/local/nagios/bin/ndo2db?Ndo2dbBin=/usr/local/nagios/bin/ndo2db-3x?g’ /etc/init.d/ndo2db

# chmod +x /etc/init.d/ndo2db

# chkconfig –add ndo2db

# chkconfig ndo2db on

6. 启动守护进程与nagios

# /etc/init.d/ndo2db start

#检查是否报错

# tail –f /var/log/messages

# /etc/init.d/nagios restart

7. 安装NPC

# tar zxvf npc.tar.gz –C /var/www/html/cacti/plugins

# chown –R apache:apache /var/www/html/cacti/plugins/npc

# 进入cacti,点击 Console–Plugin Management–>安装并启用npc插件

接着在右侧栏目中选择settings,点选npc的标签

钩选Remote Commands

Nagios Command File Path:      /usr/local/nagios/var/rw/nagios.cmd

Nagios URL:]http://yourserver/nagios/

保存就可以了。

#安装好之后在npc里只看到nagios status是on 别的主机信息却是没有查看

# 查看/var/log/message发现现实数据库错误,是因为缺少一个表导致的。

# 向cacti数据表添加字段

# mysql -ucacti -pcacti123

use cacti;

ALTER TABLE `npc_hostchecks` ADD COLUMN `long_output` varchar(8192) NOT NULL default ” AFTER `output`;

ALTER TABLE `npc_hoststatus` ADD COLUMN `long_output` varchar(8192) NOT NULL default ” AFTER `output`;

ALTER TABLE `npc_servicechecks` ADD COLUMN `long_output` varchar(8192) NOT NULL default ” AFTER `output`;

ALTER TABLE `npc_servicestatus` ADD COLUMN `long_output` varchar(8192) NOT NULL default ” AFTER `output`;

ALTER TABLE `npc_statehistory` ADD COLUMN `long_output` varchar(8192) NOT NULL default ” AFTER `output`;

ALTER TABLE `npc_eventhandlers` ADD COLUMN `long_output` varchar(8192) NOT NULL default ” AFTER `output`;

ALTER TABLE `npc_systemcommands` ADD COLUMN `long_output` varchar(8192) NOT NULL default ” AFTER `output`;

ALTER TABLE `npc_notifications` ADD COLUMN `long_output` varchar(8192) NOT NULL default ” AFTER `output`;

 

  • 安装nagiosql图形化管理nagios配置
  1. 安装nagiosql

# tar zxvf nagiosql_320.tar.gz  -C /usr/local/

# chown –R nagios:nagios  /usr/local/nagiosql

# mkdir –p /usr/local/nagios/etc/nagiosql/{hosts,services,backup/{hosts,services},import}

# chown –R apache:nagios/usr/local/nagios/etc/nagiosql

# chmod –R 755 /usr/local/nagios/etc/nagiosql

  1. 添加http虚拟目录

# vi /etc/httpd/conf.d/nagiosql.conf

Alias /nagiosql “/usr/local/nagiosql”

<Directory “/usr/local/nagiosql”>

Options FollowSymLinks MultiViews

AllowOverride None

Order allow,deny

Allow from all

</Directory>

# /etc/init.d/httpd restart

  1. 进行web安装

访问http://ip/nagiosql进行安装

右上角选择语言为中文简体,点开始安装–>如果环境测试成功通过,可点下一步继续–>下一步–>登录服务器命令行执行删除

/usr/local/nagiosql/install目录后,点完成–>出现NagiosQL欢迎界面,安装成功–>用刚才设置的密码登录nagiosql可以

看到nagiosql管理界面进行管理。

 

  1. 将nagiosql 文件添加到conf

# cd /usr/local/nagios/etc

# chown nagios:nagcmd nagios.cfg

# chown nagios:nagcmd resource.cfg

# chown –R nagios:nagcmd /usr/local/nagios/var/

# 向nagios.conf添加nagiosql配置目录

# vi /usr/local/nagios/etc/nagios.cfg

cfg_dir=/usr/local/nagios/etc/nagiosql/services

cfg_dir=/usr/local/nagios/etc/nagiosql/hosts

# 向nagios.conf添加nagiosql配置文件

# sed -i ‘s/^cfg_file/#cfg_file/g’ /usr/local/nagios/etc/nagios.cfg

# vi /usr/local/nagios/etc/nagios.cfg

cfg_file=/usr/local/nagios/etc/nagiosql/commands.cfg

cfg_file=/usr/local/nagios/etc/nagiosql/contactgroups.cfg

cfg_file=/usr/local/nagios/etc/nagiosql/contacts.cfg

cfg_file=/usr/local/nagios/etc/nagiosql/contacttemplates.cfg

cfg_file=/usr/local/nagios/etc/nagiosql/hostgroups.cfg

cfg_file=/usr/local/nagios/etc/nagiosql/hosttemplates.cfg

cfg_file=/usr/local/nagios/etc/nagiosql/servicegroups.cfg

cfg_file=/usr/local/nagios/etc/nagiosql/servicetemplates.cfg

cfg_file=/usr/local/nagios/etc/nagiosql/timeperiods.cfg

  1. 进入nagiosql管理页面导入配置文件

导入配置文件:

(1)通过手工编辑的配置文件导入方法:

拷贝老的备份文件到 /usr/local/nagios/etc/nagiosql/import/ 目录

登录http://ip/nagiosql –>管理–>域–>localhost点修改–>修改为自己的目录后点保存,应该显示数据成功写入数据库。–>

工具–>导入数据,选择要导入的配置文件,点导入按钮–>nagios控制–>写入检测数据–>写入其他数据–>检查配置文件–>检查通过后,重新启动Nagios–>结束

(2)通过nagiosql生成的配置文件导入方法:

如果是导入以前的nagiosql管理的配置文件,可以备份以前的nagiosql的数据库(默认数据库名是db_nagiosql_v3),然后覆盖新nagiosql的数据,在登录web管理页面

执行工具–>导入数据,选择要导入的配置文件,点导入按钮–>nagios控制–>写入检测数据–>写入其他数据–>检查配置文件–>检查通过后,重新启动Nagios–>

九:安装完成效果图:

  1. cacti效果

cacti2. nagios效果

nagios1

3. nagiosql效果

nagiosql

发表评论

电子邮件地址不会被公开。