LAMP+NAGIOS+CACTI完整实现笔记
源码包:
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=3306socket=/usr/local/mysql/mysql.sockskip-external-lockingkey_buffer_size=16Mmax_allowed_packet=1Mtable_open_cache=64sort_buffer_size=512Knet_buffer_length=8Kread_buffer_size=256Kread_rnd_buffer_size=512Kmyisam_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-uroot-pmysql>createdatabasecactidb;mysql>grantallprivilegesoncactidb.*to'cacti'@'10.0.0.127'identifiedby'cacti'withgrantoption;mysql>flushprivileges;mysql>usecactidbmysql>source/usr/local/apache2/htdocs/cacti/cacti.sql#导入cacti表结构
修改cacti配置文件
include/config.phpinclude/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-R775rra/log/scripts/#修改cacti目录中的文件执行权限
http://10.0.0.127:8085/cti/install/index.php通过页面安装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/
激活插件
安装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.confDB_Host10.x.x.127DB_DatabasecactidbDB_UsercactiDB_PasscactiDB_Port3306DB_PreG0
Cacti搭建完成以后,出现无法生成图片的方式,观察cacti.log正常执行poller.php脚本生产数据,查看
查看apache的error日志,一直出现如下报错:
ERROR: opening '*.rrd': No such file or directory
很明显是没有在rra下创建文件图片数据文件,但是rra的权限已经给到777了啊,对于这类头疼的问题只能一点点的试
在数据源目录下选择一台主机,选择其中一个数据模板内容,打开debug模式
看到那行rrdtool的命令,粘贴到主机上运行,确认错误的原因。查看生产图形数据的语句
检查是否运行正常!
插件的错误访问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
tarzxfnagios-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/confmakeall
makeinstall-Thisinstallsthemainprogram,CGIs,andHTMLfilesmakeinstall-init-Thisinstallstheinitscriptin/etc/rc.d/init.dmakeinstall-commandmode-Thisinstallsandconfigurespermissionsonthedirectoryforholdingtheexternalcommandfilemakeinstall-config-Thisinstalls*SAMPLE*configfilesin/usr/local/nagios/etcYou'llhavetomodifythesesamplefilesbeforeyoucanuseNagios.ReadtheHTMLdocumentationformoreinfoondoingthis.Payparticularattentiontothedocsonobjectconfigurationfiles,astheydeterminewhat/howthingsgetmonitored!makeinstall-webconf-ThisinstallstheApacheconfigfilefortheNagioswebinterfacemakeinstall-exfoliation-ThisinstallstheExfoliationthemefortheNagioswebinterfacemakeinstall-classicui-ThisinstallstheclassicthemefortheNagioswebinterface
tarzxfnagios-plugins-2.0.3.tar.gz./configure--with-nagios-user=nagios--with-nagios-group=root--enable-redhat-pthread-workaroundmake&&makeinstall
./configure--with-nrpe-user=nagios\--with-nrpe-group=nagios\--with-nagios-user=nagios\--with-nagios-group=nagios\--enable-command-argsGeneralOptions:-------------------------NRPEport:5666#默认端口为5666makeallmakeinstall-pluginmakeinstall-daemonmakeinstall-daemon-configmakeinstall-xinetd
ScriptAlias/nagios/cgi-bin"/usr/local/nagios/sbin"<Directory"/usr/local/nagios/sbin">OptionsExecCGIAllowOverrideNoneOrderallow,denyAllowfromallAuthName"NagiosAccess"AuthTypeBasicAuthUserFile/usr/local/nagios/etc/htpasswd.usersRequirevalid-user</Directory>Alias/nagios"/usr/local/nagios/share"<Directory"/usr/local/nagios/share">OptionsNoneAllowOverrideNoneOrderallow,denyAllowfromallAuthName"NagiosAccess"AuthTypeBasicAuthUserFile/usr/local/nagios/etc/htpasswd.usersRequirevalid-user</Directory>Alias/pub/p_w_picpaths"/usr/local/nagios/share/docs/p_w_picpaths"<Directory"/usr/local/nagios/share/docs/p_w_picpaths">OptionsNoneAllowOverrideNoneOrderallow,denyAllowfromallAuthName"NagiosAccess"AuthTypeBasicAuthUserFile/usr/local/nagios/etc/htpasswd.usersRequirevalid-user</Directory>
在http的配置文件中添加上面的内容后,通过http的秘钥命令在nagios配置目录中生产秘钥文件。
htpasswd-c/usr/local/nagios/etc/htpasswd.usersnagiosadmin
修改NRPE配置
vi/usr/local/nagios/etc/nrpe.cfgallowed_hosts=10.x.0.0/24vi/etc/xinetd.d/nrpeservicenrpe{flags=REUSEsocket_type=streamport=5666wait=nouser=nagiosgroup=nagiosserver=/usr/local/nagios/bin/nrpeserver_args=-c/usr/local/nagios/etc/nrpe.cfg--inetdlog_on_failure+=USERIDdisable=noonly_from=127.0.0.110.28.7.127}vi/etc/servicesnrpe5666/tcp#NRPEservicexinetdrestartnetstat-anltp|grep5666/usr/local/nagios/libexec/check_nrpe-H10.x.x.127#测试NRPEv2.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 submittedby users”,觉得应该是apache的用户需要有对该文件具有写的权限,因此修改apache的所属可选组为nagios,问题彻底解决。
usermod-Gapache,nagiosnagios
关于nagios的配置内容,需要另开篇详述。这里就不在说明
双剑合璧Nagios+Cacti
Cacti通过npc(Nagios plugin forCacti)插件将Nagios集成在一起。
wgethttp://prdownloads.sourceforge.net/sourceforge/nagios/ndoutils-2.0.0.tar.gz
tarxvfndoutils-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的库文件没有找到,虽然可以正常安装该软件,但启动时最终会导致"Supportforthespecifieddatabaseserveriseithernotyetsupported,orwasnotfoundonyoursystem."这样的错误信息:***MySQLlibrarycouldnotbelocated...**************************YouchosetocompileNDOutilswithMySQLsupport,butIwasunabletolocatetheMySQLlibraryonyoursystem.Ifthelibraryisinstalled,usethe--with-mysql-libargumenttospecifythelocationoftheMySQLlibrary.installed,usethe--with-mysql=DIRargumenttospecifythelocationoftheMySQLlibrary,Weassumemysql_configisinDIR/dirNOTE:Afteryouinstallthenecessarylibrariesonyoursystem:1.Makesure/etc/ld.so.confhasanentryforthedirectoryinwhichtheMySQLlibrariesareinstalled.2.Run'ldconfig'toupdatetherun-timelinkeroptions.3.Run'makedevclean'intheNDBXTdistributiontocleanoutanyoldreferencestoyourpreviouscompile.4.Reruntheconfigurescript.TIP:Trythefollowing...../configure--with-mysql=/usr/lib/mysql********************************************************************如果出现如下报错信息:Infileincludedfromio.c:11:../include/config.h:261:25:error:mysql/mysql.h:Nosuchfileordirectory../include/config.h:262:26:error:mysql/errmsg.h:Nosuchfileordirectory修改include/config.h中第261行的内容去掉mysql/,这是因为生产的编译文件路径指定错误。make&&makeinstallcdconfigcpndo2db.cfg-sample/usr/local/nagios/etc/ndo2db.cfgcpndomod.cfg-sample/usr/local/nagios/etc/ndomod.cfgvi/usr/local/nagios/etc/ndo2db.cfgsocket_name=/usr/local/nagios/var/ndo.sockdb_name=cactidbdb_prefix=npc_db_user=cactidb_pass=cactidebug_level=1debug_file=/usr/local/nagios/var/ndo2db.debugvi/usr/local/nagios/etc/nagios.cfgbroker_module=/usr/local/nagios/bin/ndomod.oconfig_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>oconfurllistpushhttp://mirrors.163.com/cpan/CPAN>oconfprerequisites_policyfollowCPAN>oconfcommitCPAN>installDBD::mysql
一切就绪后运行ndoutil的升级库脚本:
cdndoutils-2.0.0/db./installdb-ucacti-pcacti-h10.x.x.127-dcactidb#foranewinstallation./upgradedb-ucacti-pcacti-h10.x.x.127-dcactidb#foranexistingone
解压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.logndomod:Successfullyconnectedtodatasink.4160queueditemstoflush.#出现如下提示表示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
ALTERTABLE`npc_hostchecks`MODIFYCOLUMN`long_output`varchar(8192)NOTNULLdefault''AFTER`output`;ALTERTABLE`npc_hoststatus`MODIFYCOLUMN`long_output`varchar(8192)NOTNULLdefault''AFTER`output`;ALTERTABLE`npc_servicechecks`MODIFYCOLUMN`long_output`varchar(8192)NOTNULLdefault''AFTER`output`;ALTERTABLE`npc_servicestatus`MODIFYCOLUMN`long_output`varchar(8192)NOTNULLdefault''AFTER`output`;ALTERTABLE`npc_statehistory`MODIFYCOLUMN`long_output`varchar(8192)NOTNULLdefault''AFTER`output`;ALTERTABLE`npc_eventhandlers`MODIFYCOLUMN`long_output`varchar(8192)NOTNULLdefault''AFTER`output`;ALTERTABLE`npc_systemcommands`MODIFYCOLUMN`long_output`varchar(8192)NOTNULLdefault''AFTER`output`;ALTERTABLE`npc_notifications`MODIFYCOLUMN`long_output`varchar(8192)NOTNULLdefault''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/
声明:本站所有文章资源内容,如无特殊说明或标注,均为采集网络资源。如若本站内容侵犯了原著者的合法权益,可联系本站删除。