温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

LAMP+NAGIOS+CACTI完整实现笔记

发布时间:2020-07-24 01:36:36 来源:网络 阅读:1008 作者:onlinekof2001 栏目:移动开发

源码包:

httpd-2.2.24.tar.gz

php-5.2.17.tar.gzcd

mysql-5.1.73-linux-x86_64-glibc23.tar.gz

其余依赖程序均通过yum源来安装,推荐EPEL和CentOS

163镜像

http://mirrors.163.com/

http://mirror.centos.org/

epel镜像

http://mirrors.fedoraproject.org

 

操作步骤:

mysql安装:

tar zxf mysql-5.1.73-linux-x86_64-glibc23.tar.gz -C /usr/local/

mv mysql-5.1.73-linux-x86_64-glibc23/ mysql

chown -R mysql.root mysql/     # 修改mysql目录的权限

chown -R mysql. mysql/data/   # 修改mysql数据目录的权限

初始化mysql

./scripts/mysql_install_db --basedir=/usr/local/mysql \
--datadir=/usr/local/mysql/data \
--defaults-file=/usr/local/mysql/my.cnf \
--skip-name-resolve --user=mysql

 

拷贝自启动脚本:

cp mysql.server /etc/init.d/mysqld

修改启动脚本中的内容,明确告诉启动脚本mysql的运行路径

basedir=/usr/local/mysql                             #46行 

datadir=/usr/local/mysql/data                     #47

pid_file=/usr/local/mysql/mysql_v51.pid   #59

conf=/usr/local/mysql/my.cnf                      #218

 

my.cnf配置文件主要部分

[mysqld]
port            = 3306
socket          = /usr/local/mysql/mysql.sock
skip-external-locking
key_buffer_size = 16M
max_allowed_packet = 1M
table_open_cache = 64
sort_buffer_size = 512K
net_buffer_length = 8K
read_buffer_size = 256K
read_rnd_buffer_size = 512K
myisam_sort_buffer_size = 8M

 

/etc/init.d/mysqld start
Starting MySQL.[  OK  ]

 

源码编译安装http:

先安装apr-1.5.1,编译安装时指定:

./configure --prefix=/usr/local/apr --disable-ipv6  LDFLAGS=-L/usr/lib64

apr-util-1.5.3,编译安装时指定:

./configure --prefix=/usr/local/apr \
--with-apr=/usr/local/apr/bin/apr-1-config \
--with-mysql=/usr/local/mysql \
LDFLAGS=-L/usr/lib64

编译安装apache

./configure --prefix=/usr/local/apache2 \-
-enable-so \
--enable-modules=all \
--enable-mods-shared=all \-
-with-mpm=worker 
--with-apr=/usr/local/apr/bin/apr-1-config \
--with-apr-util=/usr/local/apr/bin/apu-1-config \
--with-pcre \
--enable-disk-cache \
--enable-mem-cache \
--enable-file-cache \
--enable-cache \
--enable-cgi \
--enable-authn-alias \
--enable-proxy \
--enable-proxy-ftp \
--enable-proxy-http \
--enable-proxy-scgi \
--enable-proxy-connect \
--enable-proxy-balancer \
--enable-suexec \
LDFLAGS=-L/usr/lib64

 

make && make install

chown -R apache.root apache2/

 

http配置主要部分:篇幅问题无法写全

ServerRoot "/usr/local/apache2"

Listen 8085

ServerName 10.28.7.127:8085

DocumentRoot "/usr/local/apache2/htdocs"

<IfModule dir_module>
    DirectoryIndex index.html index.php
</IfModule> 

 

 

编译安装php:

编译参数参照yum源安装的PHP包时使用的选项

./configure --build=x86_64-redhat-linux-gnu \
--host=x86_64-redhat-linux-gnu \
--target=x86_64-redhat-linux-gnu \
--prefix=/usr/local/php \
--with-libdir=lib64 \
--with-config-file-path=/usr/local/php/etc \
--with-apxs2=/usr/local/apache2/bin/apxs \
--disable-debug \
--with-pic \
--disable-rpath \
--with-bz2 \
--with-exec-dir=/usr/bin \
--with-freetype-dir=/usr \
--with-png-dir=/usr \
--with-xpm-dir=/usr \
--enable-gd-native-ttf \
--without-gdbm \
--with-gettext \
--with-gmp \
--with-iconv \
--with-jpeg-dir=/usr \
--with-openssl \
--with-pcre-regex=/usr \
--with-zlib \
--with-layout=GNU \
--enable-exif \
--enable-ftp \
--enable-magic-quotes \
--enable-sockets \
--enable-sysvsem \
--enable-sysvshm \
--enable-sysvmsg \
--with-kerberos \
--enable-ucd-snmp-hack \
--enable-shmop \
--enable-calendar \
--without-sqlite \
--with-libxml-dir=/usr \
--enable-xml \
--enable-force-cgi-redirect \
--enable-pcntl \
--with-imap=shared \
--with-imap-ssl \
--enable-mbstring=shared \
--enable-mbregex \
--with-gd=shared \
--enable-bcmath=shared \
--enable-dba=shared \
--with-db4=/usr \
--with-xmlrpc=shared \
--with-ldap=shared \
--with-ldap-sasl \
--with-mysql=shared,/usr/local/mysql \
--with-mysqli=shared,/usr/local/mysql/bin/mysql_config \
--enable-dom=shared \
--enable-wddx=shared \
--with-snmp=shared,/usr \
--enable-soap=shared \
--with-xsl=shared,/usr \
--enable-xmlreader=shared \
--enable-xmlwriter=shared \
--with-curl=shared,/usr \
--enable-fastcgi \
--enable-pdo=shared \
--with-pdo-odbc=shared,unixODBC,/usr \
--with-pdo-mysql=shared,/usr/local/mysql/bin/mysql_config \
--with-pdo-sqlite=shared,/usr \
--enable-json=shared \
--enable-zip=shared \
--without-readline \
--enable-sysvmsg=shared \
--enable-sysvshm=shared \
--enable-sysvsem=shared \
--enable-posix=shared \
--with-unixODBC=shared,/usr \
--with-mysql-sock=/usr/local/mysql/mysql.sock \
--with-iconv-dir

make的时候加上ZEND_EXTRA_LIBS防止过程中报错

make ZEND_EXTRA_LIBS='-liconv'

php配置:

extension_dir = "/usr/local/php/lib/php/20060613-zts/"
extension=pdo_mysql.so  # 此处为范例,多个模块需要添加多个extension

date.timezone = Asia/Shanghai

 

LAMP环境完成后,检查php加载状况,在/usr/local/apache2/htdocs/下创建index.php文件语法:

<?php

phpinfo();

?>

通过访问http://domainname//index.php访问可以发现php插件是否正常加载

 

Apache   cat /usr/local/apache2/build/config.nice              #查看编译参数

                    apachectl -t -D DUMP_MODULES                     #查看加载模块

MySQL:    grep configure /usr/local/mysql/bin/mysqlbug     #查看mysql编译参数

PHP:          /usr/local/php/bin/php -i |grep configure               #查看PHP编译参数

 

为了方便调用命令把Apache、Mysql、PHP 三个软件的执行文件路径写入环境变量~/.bash_profile

PATH=$PATH:$HOME/bin:/usr/local/mysql/bin:/usr/local/apache/bin:/usr/local/php/bin

LD_LIBRARY_PATH=/lib:/lib64:/usr/lib:/usr/lib64:/usr/local/mysql/lib:/usr/local/lib:/usr/local/lib64  #64位环境下添加该环境变量,方便编译安装时查看库文件。

source ~/.bash_profile 使当前环境变量生效

 

CACTI搭建:

搭建前先通过YUM安装net-snmp,net-snmp-libs,net-snmp-utils,net-snmp-devel,elfutils-libelf-devel-static,elfutils-devel,lm_sensors-devel,lm_sensors,beecrypt-devel包,Cacti主要依赖snmp协议获取备监控主机的信息。

Cacti下载地址:http://www.cacti.net/downloads/

tar zxf cacti-0.8.8b.tar.gz

mv cacti-0.8.8b /usr/local/apache2/htdocs/cacti

mysql -u root -p
mysql> create database cactidb;
mysql> grant all privileges on cactidb.* to 'cacti'@'10.0.0.127' identified by 'cacti' with grant option;
mysql> flush privileges;
mysql> use cactidb
mysql> source /usr/local/apache2/htdocs/cacti/cacti.sql  #导入cacti表结构

修改cacti配置文件

include/config.php
include/global.php
$database_type = "mysql";
$database_default = "cacti";
$database_hostname = "10.0.0.127";
$database_username = "cacti";
$database_password = "cacti";
$database_port = "3306";
$database_ssl = false;
$url_path = "/cacti/";
chmod -R 775 rra/ log/ scripts/  #修改cacti目录中的文件执行权限

 

http://10.0.0.127:8085/cti/install/index.php 通过页面安装cacti

配置时注意执行命令路径是否被找到,如路径不对后续也可以再设置。

LAMP+NAGIOS+CACTI完整实现笔记

 

安装部分插件:

thold-v0.5.0.tgz

monitor-v1.3-1.tgz

settings-v0.71-1.tgz

解压后移至插件目录下

mv settings monitor thold /usr/local/apache2/htdocs/cacti/plugins/

LAMP+NAGIOS+CACTI完整实现笔记

激活插件

安装cacti-spine插件

cacti-spine-0.8.8b

./configure --with-mysql \
--with-snmp=/usr/include/net-snmp \
LDFLAGS=-L/usr/local/mysql/lib

编译过程中遇到 Cannot find UCD-SNMP libraries(snmp)报错,查看configure文件,里面把snmp库路径指向--with-snmp所指的路径之后,而snmp默认的库安装路径是/usr/lib64,因此通过软链方式解决:

ln -s /usr/lib64/libsnmp* /usr/include/net-snmp/lib64/

还有一类据说是spine-0.8.7c文档提供的编译方式

[root@SH-021Y-DBAP cacti-spine-0.8.8b] aclocal

[root@SH-021Y-DBAP cacti-spine-0.8.8b] libtoolize

[root@SH-021Y-DBAP cacti-spine-0.8.8b] autoconf && autoheader && automake

这类编译过程需要借助 autoconf,automake工具。直接从GNU上下载最近版本的就可以了。发现yum来的或者低版本的会遇到些问题。

GNU软件下载地址,另外GNU现在在募集免费软件基金。毕竟一直在使用,大家能支援的就支援点吧。

http://www.gnu.org/software

 

安装完成后进spine行配置

vi /usr/local/spine/etc/spine.conf
DB_Host         10.x.x.127
DB_Database     cactidb
DB_User         cacti
DB_Pass         cacti
DB_Port         3306
DB_PreG         0

 

 

Cacti搭建完成以后,出现无法生成图片的方式,观察cacti.log正常执行poller.php脚本生产数据,查看

 查看apache的error日志,一直出现如下报错:

 ERROR: opening '*.rrd': No such file or directory

很明显是没有在rra下创建文件图片数据文件,但是rra的权限已经给到777了啊,对于这类头疼的问题只能一点点的试

LAMP+NAGIOS+CACTI完整实现笔记

在数据源目录下选择一台主机,选择其中一个数据模板内容,打开debug模式

LAMP+NAGIOS+CACTI完整实现笔记

看到那行rrdtool的命令,粘贴到主机上运行,确认错误的原因。查看生产图形数据的语句

LAMP+NAGIOS+CACTI完整实现笔记

检查是否运行正常!

 

插件的错误访问http://forums.cacti.net网站寻找解决方式

 

安装Nagios

下载地址:

 http://sourceforge.jp/projects/sfnet_nagios/releases/

最新nagios-plugin地址:

http://www.nagios.org/download/plugins/

nrpe下载地址:

wget http://sourceforge.net/projects/nagios/files/nrpe-2.x/nrpe-2.9/nrpe-2.9.tar.gz

目前能获取的最新版本:

nagios-4.0.8.tar.gz

nagios-plugins-2.0.3.tar.gz

 nrpe-2.9.tar.gz

tar zxf nagios-4.0.8.tar.gz
./configure --with-nagios-user=nagios \
--with-nagios-group=nagios \
--with-command-user=nagios \
--with-command-group=nagios \
--with-httpd-conf=/usr/local/apache2/conf
make all
  make install
     - This installs the main program, CGIs, and HTML files
  make install-init
     - This installs the init script in /etc/rc.d/init.d
  make install-commandmode
     - This installs and configures permissions on the
       directory for holding the external command file
  make install-config
     - This installs *SAMPLE* config files in /usr/local/nagios/etc
       You'll have to modify these sample files before you can
       use Nagios.  Read the HTML documentation for more info
       on doing this.  Pay particular attention to the docs on
       object configuration files, as they determine what/how
       things get monitored!
  make install-webconf
     - This installs the Apache config file for the Nagios
       web interface
  make install-exfoliation
     - This installs the Exfoliation theme for the Nagios
       web interface
  make install-classicui
     - This installs the classic theme for the Nagios
       web interface
tar zxf nagios-plugins-2.0.3.tar.gz 
./configure --with-nagios-user=nagios --with-nagios-group=root --enable-redhat-pthread-workaround
make && make install
./configure --with-nrpe-user=nagios \
--with-nrpe-group=nagios \
--with-nagios-user=nagios \
--with-nagios-group=nagios \
--enable-command-args
 General Options:
 -------------------------
 NRPE port:    5666   #默认端口为5666
make all
make install-plugin
make install-daemon
make install-daemon-config
make install-xinetd
ScriptAlias /nagios/cgi-bin "/usr/local/nagios/sbin"
<Directory "/usr/local/nagios/sbin">
   Options ExecCGI
   AllowOverride None
   Order allow,deny
   Allow from all
   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">
   Options None
   AllowOverride None
   Order allow,deny
   Allow from all
   AuthName "Nagios Access"
   AuthType Basic
   AuthUserFile /usr/local/nagios/etc/htpasswd.users
   Require valid-user
</Directory>
Alias /pub/p_w_picpaths "/usr/local/nagios/share/docs/p_w_picpaths"
<Directory "/usr/local/nagios/share/docs/p_w_picpaths">
   Options None
   AllowOverride None
   Order allow,deny
   Allow from all
   AuthName "Nagios Access"
   AuthType Basic
   AuthUserFile /usr/local/nagios/etc/htpasswd.users
   Require valid-user
</Directory>

在http的配置文件中添加上面的内容后,通过http的秘钥命令在nagios配置目录中生产秘钥文件。

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

修改NRPE配置

vi /usr/local/nagios/etc/nrpe.cfg
allowed_hosts=10.x.0.0/24
 
vi /etc/xinetd.d/nrpe
service nrpe
{
        flags           = REUSE
        socket_type     = stream
        port            = 5666
        wait            = no
        user            = nagios
        group           = nagios
        server          = /usr/local/nagios/bin/nrpe
        server_args     = -c /usr/local/nagios/etc/nrpe.cfg --inetd
        log_on_failure  += USERID
        disable         = no
        only_from       = 127.0.0.1 10.28.7.127
}
vi /etc/services
nrpe            5666/tcp                # NRPE
service xinetd restart
netstat -anltp | grep 5666
/usr/local/nagios/libexec/check_nrpe -H 10.x.x.127 #测试
NRPE v2.15

 

在启动nagios之后发现部分监控报警没启动,启动时发现如下报错:

Error: Could not open command file '/usr/local/nagios/var/rw/nagios.cmd' for update!

在nagios.cfg中对该命令进行解释,参考了网上的解决方式,发现修改命令权限为777时,无报错,判断一定是权限的问题。但此方法仅临时解决问题,当nagios重启后,该命令重新创建,问题依然存在。看到解释中提到 “It is also where the command CGI will write commands that are submitted  by users”,觉得应该是apache的用户需要有对该文件具有写的权限,因此修改apache的所属可选组为nagios,问题彻底解决。

usermod -G apache,nagios  nagios

关于nagios的配置内容,需要另开篇详述。这里就不在说明

 

双剑合璧Nagios+Cacti

Cacti通过npc(Nagios plugin for Cacti)插件将Nagios集成在一起。

wget http://prdownloads.sourceforge.net/sourceforge/nagios/ndoutils-2.0.0.tar.gz

tar xvf ndoutils-2.0.0.tar.gz
./configure --prefix=/usr/local/nagios \
--mandir=/usr/local/share/man \
--enable-mysql \
--disable-pgsql \
--with-ndo2db-user=nagios \
--with-ndo2db-group=nagios \
--with-mysql
这里需要特别注意的是,仔细观察编译内容,当出现如下情况时说明mysql的库文件没有找到,虽然可以正常安装该软件,但启动时最终会导致"Support for the specified database server is either not yet supported, or was not found on your system."这样的错误信息:
*** MySQL library could not be located... **************************
You chose to compile NDOutils with MySQL support, but I was unable to
locate the MySQL library on your system.  If the library is
installed,  use the --with-mysql-lib argument to specify the
location of the MySQL library.
installed, use the --with-mysql=DIR argument to specify the
location of the MySQL library, We assume mysql_config is in DIR/dir
NOTE: After you install the necessary libraries on your system:
      1. Make sure /etc/ld.so.conf has an entry for the directory in
         which the MySQL libraries are installed.
      2. Run 'ldconfig' to update the run-time linker options.
      3. Run 'make devclean' in the NDBXT distribution to clean out
         any old references to your previous compile.
      4. Rerun the configure script.
TIP: Try the following....
      ./configure --with-mysql=/usr/lib/mysql
********************************************************************
如果出现如下报错信息:
In file included from io.c:11:
../include/config.h:261:25: error: mysql/mysql.h: No such file or directory
../include/config.h:262:26: error: mysql/errmsg.h: No such file or directory
修改include/config.h中第261行的内容去掉mysql/,这是因为生产的编译文件路径指定错误。
make && make install
cd config
cp ndo2db.cfg-sample /usr/local/nagios/etc/ndo2db.cfg
cp ndomod.cfg-sample /usr/local/nagios/etc/ndomod.cfg
vi /usr/local/nagios/etc/ndo2db.cfg
socket_name=/usr/local/nagios/var/ndo.sock 
db_name=cactidb  
db_prefix=npc_  
db_user=cacti 
db_pass=cacti 
debug_level=1 
debug_file=/usr/local/nagios/var/ndo2db.debug
vi /usr/local/nagios/etc/nagios.cfg
broker_module=/usr/local/nagios/bin/ndomod.o config_file=/usr/local/nagios/etc/ndomod.cfg

到这步需要做个小改动,因为ndoutils的数据库脚本中定义的表名为nagios_,而配置文件中定义为npc_,需要手动改一下脚本nstalldb,upgradedb及mysql.sql的内容将nagios替换为npc。替换的方式用vi在command模式下:

%s/nagios_/npc_/

除此之外还有perl的相关包也需要安装,主要是DBI,Data::ShowTables,DBD::mysql,一般通过perl的CPAN项目(类似YUM)安装。涉及的相关包如lynx,ncftp,ftp

cpan源地址使用 http://mirrors.163.com/cpan/

CPAN> o conf urllist push http://mirrors.163.com/cpan/
CPAN> o conf prerequisites_policy follow
CPAN> o conf commit 
CPAN> install DBD::mysql

一切就绪后运行ndoutil的升级库脚本:

cd ndoutils-2.0.0/db
./installdb -u cacti -p cacti -h 10.x.x.127 -d cactidb #for a new installation
./upgradedb -u cacti -p cacti -h 10.x.x.127 -d cactidb #for an existing one

 

解压NPC移置cacti的plugins目录下修改cacti配置文件

vi /usr/local/apache/htdocs/cacti/include/plugins.php
$plugins[] = 'npc';   #第30行,$plugins = array();下方添加该语句(目前看该步骤非必要)

 

一切就绪开启ndoutils服务:

/usr/local/nagios/bin/ndo2db -c /usr/local/nagios/etc/ndo2db.cfg

查看一下nagios的日志记录

tail -100 /usr/local/nagios/var/nagios.log 
ndomod: Successfully connected to data sink.  4160 queued items to flush. #出现如下提示表示ndo正常连接数据库,把nagios的数据写入到cactidb库中了。

 

Dec 13 00:55:37 021Y-SH-BKAP ndo2db: Error: max retries exceeded sending message to queue. Kernel queue parameters may neeed to be tuned. See README

ALTER TABLE `npc_hostchecks` MODIFY COLUMN `long_output` varchar(8192) NOT NULL default '' AFTER `output`; 
ALTER TABLE `npc_hoststatus` MODIFY COLUMN `long_output` varchar(8192) NOT NULL default '' AFTER `output`; 
ALTER TABLE `npc_servicechecks` MODIFY COLUMN `long_output` varchar(8192) NOT NULL default '' AFTER `output`; 
ALTER TABLE `npc_servicestatus` MODIFY COLUMN `long_output` varchar(8192) NOT NULL default '' AFTER `output`; 
ALTER TABLE `npc_statehistory` MODIFY COLUMN `long_output` varchar(8192) NOT NULL default '' AFTER `output`; 
ALTER TABLE `npc_eventhandlers` MODIFY COLUMN `long_output` varchar(8192) NOT NULL default '' AFTER `output`; 
ALTER TABLE `npc_systemcommands` MODIFY COLUMN `long_output` varchar(8192) NOT NULL default '' AFTER `output`; 
ALTER TABLE `npc_notifications` MODIFY COLUMN `long_output` varchar(8192) NOT NULL default '' AFTER `output`;


 

参考:

http://os.51cto.com/art/201411/458006_all.htm

http://blog.chinaunix.net/uid-24727220-id-3025015.html

http://yahoon.blog.51cto.com/13184/49722/

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI