华风 发表于 2019-1-14 06:49:34

Nagios的搭建

前些天搭建了一个Cacti上线后运行的不错监控服务器的实时运行状态也配置了报警邮件接收等功能后,心情大好。去论坛里溜达一圈后好多人都说Nagios不错功能上比Cacti还要强上不少呢,于似乎就开始了Nagios研究。花了三天的时候研究Nagios的架构。今天开始着手开动了;
1、首先Nagios搭建的准备工作很多,LAMP架构得搭建吧;
2、LAMP搭建之前记得把SElinux与Iptables关了啊,不要在后面给自己创造麻烦;
一、搭建LAMP环境;
(1)、所需要安装依赖包我在这里就Yum了,没有rpm一个一个的安装原因你懂得,在这里我搭建的系统环境是CentOS-5.9-x86_64;我一直喜欢用CentoS那是因为依赖包的问题好解决啊。RedHat要也想像CentoS那样方便解决依赖包的问题的话得先把本地的rpm包与第三方的依赖包整合到一起然后在统一搭建Yum源库的,好了费话不说了要不然该挨喷了。
# yum -y install perl gd gd-devel libpng libpng-devel libjpeg libjpeg-devel zlib zlib-devel pcre-devel gcc gcc-c++ make cmake autoconf openssl openssl-devel ncurses-devel patch libxml2 libxml2-devel curl-devel openldap openldap-devel libevent libevent-devel bison icu libicu-devel libtool readline-devel net-snmp-devel bzip2-devel freetype-devel vim
(2)、apache安装;
安装http
# tar -zxf httpd-2.2.22.tar.gz
# cd httpd-2.2.22
# ./configure --prefix=/usr/local/apache --enable-so --enable-sl --enable-cgi--enable-rewrite --with-zlib --with-pcre
# make && make install
Apache安装完成了下面我来起动一下;
# /usr/local/apache/bin/apachectlstart
起动了要是不放心的话在看看apache是否在动行;
# ps aux |grep httpd
http://s3.运维网.com/wyfs02/M02/26/AA/wKioL1NsjBXTtk87AANip6-1koQ298.jpg
      http://192.168.10.162/
http://s3.运维网.com/wyfs02/M01/26/AA/wKioL1NsjJHyrlFpAADk5CQ7qNc734.jpg
下面把apache加入开机自动起动拷贝如下;
# cp /usr/local/apache/bin/apachectl/etc/rc.d/init.d/httpd
在/etc/rc.d/init.d/httpd这里加入这两行;
http://s3.运维网.com/wyfs02/M00/26/AA/wKioL1NsjKCCWmTgAAD0t6fn7Gk889.jpg
授予权限;
# chmod u+x /etc/rc.d/init.d/httpd
加入自动起动;
# chkconfig --add httpd
# chkconfig httpd on


安装Mysql5.0.22;
(1)解压
#tar –zxf mysql-5.0.22.tar.gz
(2)进入解压的目录
#cd mysql-5.0.22
配置
#./configure –prefix=/usr/local/mysql
(1)编译并安装
#make &&make install
(2)添加mysql用户和组
#useradd mysql
(3)拷贝配置文件
# cp support-files/my-medium.cnf/etc/my.cnf
(4)进入mysql安装后目录
## cd /usr/local/mysql/
(5)修改目录权限
# chown -R mysql.mysql .
(6)初始化数据库(#cd/usr/local/myssql/:必须在此目录下才可以进行数据库的初始化)
# ./bin/mysql_install_db --user=mysql
(7)修改权限
#chown -R root .
#chown -R mysql var
#ps –ef |grep mysql(此为查看mysql是否运行)
(8)启动服务
# ./bin/mysqld_safe --user=mysql &
(9)连接测试
# /usr/local/mysql/bin/mysql
(10)设置环境变量
# PATH=$PATH:/usr/local/mysql/bin
# mysql
(11)配置服务的启动或关闭
# cd mysql-5.0.22
# cpsupport-files/mysql.server /etc/rc.d/init.d/mysqld
#chmod u+x /etc/rc.d/init.d/mysqld
#chkconfig --add mysqld
# chkconfig mysqld on
可以了,到这里Mysql成功安装了啊,下面进行php的安装;
php的安装;
# useradd-s /sbin/nologin -M fpmuser
# tar zxvfphp-5.5.3.tar.gz
# ./configure --prefix=/usr/local/php --with-apxs2=/usr/local/apache/bin/apxs --with-config-file-path=/etc --with-freetype-dir --with-jpeg-dir --with-png-dir --enable-zip --with-zlib --enable-xml --with-gd --with-mhash --with-libxml-dir=/usr --enable-mbstring --enable-fpm --with-fpm-user=fpmuser --with-fpm-group=fpmuser--disable-ipv6 --enable-sockets --with-openssl --with-bz2 --with-curl --enable-dba=shared --with-pcre-dir --with-gd --with-jpeg-dir --with-png-dir --with-zlib-dir --enable-mbstring --with-mysql=/usr/local/mysql/ --with-mysql-sock=/tmp/mysql.sock --with-mysqli=/usr/local/mysql/bin/mysql_config
#make && make install
#cp php.ini-production /usr/local/php/lib/php.ini
#cd /usr/local/php/etc/
# cp -aphp-fpm.conf.default php-fpm.conf
# cd ..
启动php
#sbin/php-fpm
当起动php时可能无法起动,这时编译一下php的配置文件
# vim /usr/local/php/etc/php-fpm.conf
http://s3.运维网.com/wyfs02/M01/48/8C/wKiom1QJfVaDowMfAALzd9DOqKM033.jpg


(4) 配置apache
#vim /usr/local/apache/conf/httpd.conf
找到DirectoryIndexindex.html index.html.var
修改为DirectoryIndexindex.html index.php
接着增加如下内容:
AddTypeapplication/x-httpd-php .php
修改完如下:

    DirectoryIndex index.html index.php

AddTypeapplication/x-httpd-php .php
添加虚拟目录测试lamp
#vim /usr/local/apache/conf/httpd.conf
添加以下内容
Alias /test"/usr/local/apache/test"

    AuthType Basic
    Options None
    AllowOverride None
    Order allow,deny
    Allow from all



创建Php测试页面
#vi /usr/local/apache/htdocs/index.php
添加以下内容:

配置完成后重启apache
#/usr/local/apache/bin/apachectl restart
测试PHP 页面




打开浏览器访问:http://192.168.10.162/index.php
http://s3.运维网.com/wyfs02/M02/26/AA/wKioL1NsjLbSKCf3AAQSBg9FO9I335.jpg
到现在Nagios监控搭建在准备工作算是完事了;
一、Nagios安装前准备;
(1)、创建nagios的用户和用户组
# useradd nagios
# mkdir /usr/local/nagios
# chown nagios.nagios /usr/local/nagios/
(2)、查看一下Linux下有没有安装gd库看看两个是不是都安装上了,
# rpm -qa |grep gd
gdbm-1.8.0-28.el5
sysklogd-1.4.1-46.el5
gd-2.0.33-9.4.el5_4.2
gdbm-1.8.0-28.el5
gd-devel-2.0.33-9.4.el5_4.2
gd-2.0.33-9.4.el5_4.2
gd-devel-2.0.33-9.4.el5_4.2
1、编译安装Nagios
# tar -zxf nagios-3.4.2.tar.gz
# cd nagios
# ./configure --prefix=/usr/local/nagios --with-gd-lib=/usr/lib --with-gd-inc=/usr/include/
注意:这两个参数比较重要(添加之前要确保gd库已装好)否则可能nagios装完拓扑图页面出不来
#指定nagios的安装目录,这里指定nagios安装到/usr/local/nagios目录
# make all(这里要注意啊多个all)
# make install
# make install用来安装nagios的主程序,CGI和HTML文件
# makeinstall-init
#通过makeinstall-init命令可以在/etc/rc.d/init.d目录下创建nagios启动脚本
# make install-commandmode
#通过makeinstall-commandmode命令来配置目录权限
# make install-config
#makeinstall-cofig命令用来安装nagios示例配置文件,这里安装的路径是/usr/local/nagios/etc
2、安装Nagios插件
这里下载的版本是nagios-plugins-1.4.14。
注意:插件版本与nagios版本的关联并不大。
# tar -zxf nagios-plugins-1.4.15.tar.gz
# cd nagios-plugins-1.4.15
#./configure --prefix=/usr/local/nagios/
# make
# make install
安装完成,在/usr/local/nagios下的libexec目录下,生成很多可执行文件,这些正是nagios所需要的插件。
3、安装Nagios的中文插件
中文插件下载地址:
http://sourceforge.net/projects/nagios-cn/files/
# tar -jxfnagios-cn-3.2.3.tar.bz2
# cdnagios-cn-3.2.3
#make all
#make install
4、配置apache
找到apache的配置文件
# vi/usr/local/apache/conf/httpd.conf
找到AddType application/x-compress .Z
增加AddType application/x-httpd-php .php
AddType application/x-httpd-php.php .php3 .phtml .inc
AddTypeapplication/x-httpd-php-source .phps
为了安全起见,要让nagios的web监控界面授权才能访问,增加验证配置,在httpd.conf文件最后添加如下信息:
#setting for nagios
ScriptAlias /nagios/cgi-bin"/usr/local/nagios/sbin"

   AuthType Basic
   Options ExecCGI
   AllowOverride None
   Order allow,deny
   Allow from all
   AuthName "Nagios Access"
   AuthUserFile /usr/local/nagios/etc/htpasswd
   Require valid-user



Alias /nagios"/usr/local/nagios/share"

   AuthType Basic
   Options None
   AllowOverride None
   Order allow,deny
   Allow from all
   AuthName "nagios Access"
   AuthUserFile /usr/local/nagios/etc/htpasswd
   Require valid-user

5、创建apache目录验证文件
在上面的配置中,指定了目录验证文件htpasswd,下面要创建这个文件:
#/usr/local/apache/bin/htpasswd -c /usr/local/nagios/etc/htpasswd nagios
New password:(输入密码)   
Re-type new password:(再输入一次密码)
Adding password for user nagios
#我设置的密码是“nagios”
这样就在/usr/local/nagios/etc目录下创建了一个htpasswd验证文件,当通过
最后,重启启动服务:
找到:
User daemon
Group daemon
修改为
User nagios
Group nagios
然后找到
DirectoryIndex index.html index.html.var
修改为
DirectoryIndex index.html index.php
#/usr/local/apache/bin/apachectl restart
# /etc/init.d/nagios start
Starting nagios: done.
这里有可能在起动的时候报错
# vi/usr/local/nagios/etc/nagios.cfg
找到1043 #service_check_timeout_state=c先#一下。
果然起动了。
http://192.168.10.162/nagios/访问时就需要输入用户名和密码了。
http://s3.运维网.com/wyfs02/M01/26/AB/wKioL1NskDHTJRL4AAEqfoO_MEo465.jpg
账号:nagios
密码:nagios
http://s3.运维网.com/wyfs02/M00/26/AB/wKiom1NskG_jwZS_AAR361I_zSc719.jpg
有乱码;
解决办法:修改http.conf
添加 AddDefaultCharset utf-8
http://s3.运维网.com/wyfs02/M02/26/AB/wKioL1NskWPzNUeGAAVA4jiIeDI461.jpg
当点击查看主机的时候显示成如下图时编译
#vi/usr/local/nagios/etc/cgi.cfg
将use_authentication的值改为0.
use_authentication=0
http://s3.运维网.com/wyfs02/M00/26/AB/wKiom1NskbLAGzynAATYgaUzH44934.jpg
编辑完成之后
#/etc/init.d/nagios restart 一下哦!!
http://s3.运维网.com/wyfs02/M02/26/AB/wKiom1NskWjCo9X0AAYNMGSPXLc896.jpg
到此Nagios的安装是完成了啊!一天的时间啊!
  




页: [1]
查看完整版本: Nagios的搭建