网站建设资讯

NEWS

网站建设资讯

Nagios安装真可爱

煮酒品茶:cwtea.blog.51cto.com

创新互联是一家集网站建设,绥滨企业网站建设,绥滨品牌网站建设,网站定制,绥滨网站建设报价,网络营销,网络优化,绥滨网站推广为一体的创新建站企业,帮助传统企业提升企业形象加强企业竞争力。可充分满足这一群体相比中小企业更为丰富、高端、多元的互联网需求。同时我们时刻保持专业、时尚、前沿,时刻以成就客户成长自我,坚持不断学习、思考、沉淀、净化自己,让我们为更多的企业打造出实用型网站。

简述:本文讲述了nagios安装的过程以及Nagios整个工作过程。你可以定制一些你需要的。哪怕修改源码都可以。你会发现原来以前你不迷糊的地方在这里都有了解释。nagios会变得这么可爱,生命是那么的精彩。
 
Nagios官方网站:http://www.nagios.org/
Nagios下载地址:http://sourceforge.net/projects/nagios/?source=directory-featured
Nagios-plus下载地址:
http://sourceforge.net/projects/nagiosplug/files/nagiosplug/1.4.16/nagios-plugins-1.4.16.tar.gz/download?use_mirror=jaist
NRPE下载地址:
http://sourceforge.net/projects/nagios/files/nrpe-2.x/nrpe-2.13/nrpe-2.13.tar.gz/download?use_mirror=jaist
SystemOS:CentOS6.3
Nagios 官方文档 http://nagios.sourceforge.net/docs/nagioscore/3/en/config.html
 
有依赖关系:
httpd php 
gcc make
#如果没有安装以上程序,可以用yum安装。
 #yum intsall -y httpd php gcc make
#安装:
 tar zxvf nagios-3.5.0rc2.tar.gz 
cd nagios
#详见下方./configure: make:将有说明。
 ./configure --prefix=/usr/local/nagios
make all
make install
useradd nagios
#把nagios加入到apache组。
usermod -G nagios apache
make install
make install-init
make install-commandmode
make install-config
make install-webconf
 
#安装nagios-plugins
#依赖:openssl yum可以安装。
#yum install -y openssl openssl-devel 
 ./configure --prefix=/usr/local/nagios
make
make install
 
#检查一下:
[root@nagios nagios-plugins-1.4.16]# ls /usr/local/nagios/libexec/
 
 
./configure:
 Creating sample config files in sample-config/ ...
 
 
*** Configuration summary for nagios 3.5.0rc2 03-09-2013 ***:
 
 General Options:
 -------------------------
        Nagios executable:  nagios
        Nagios user/group:  nagios,nagios
       Command user/group:  nagios,nagios
            Embedded Perl:  no
             Event Broker:  yes
        Install ${prefix}:  /usr/local/nagios
#PID号
                Lock file:  ${prefix}/var/nagios.lock
#检查结果目录
   Check result directory:  ${prefix}/var/spool/checkresults
#服务配置目录
           Init directory:  /etc/rc.d/init.d
#apache for nagios目录
  Apache conf.d directory:  /etc/httpd/conf.d
#mail程序
             Mail program:  /bin/mail
#主机名
                  Host OS:  linux-gnu
 
 Web Interface Options:
 ------------------------
                 HTML URL:  http://localhost/nagios/
                  CGI URL:  http://localhost/nagios/cgi-bin/
 Traceroute (used by WAP):  
 
 
Review the options above for accuracy.  If they look okay,
type 'make all' to compile the main program and CGIs.
 
make all:
 *** Compile finished ***
 
If the main program and CGIs compiled without any errors, you
can continue with installing Nagios as follows (type 'make'
without any arguments for a list of all possible options):
 
  make install
     - This installs the main program, CGIs, and HTML files
#安装主程序,CGI文件和Html文件。
  make install-init
     - This installs the init script in /etc/rc.d/init.d
#安装nagios服务,脚本在/etc/rc.d/init.d/nagios
  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!
#把样版配置文件拷到/usr/local/nagios/etc目录。
  make install-webconf
     - This installs the Apache config file for the Nagios
       web interface
#安装apache配置nagios选项文件。yum安装会创建/etc/http/conf.d/nagios.cfg
  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
   #经典主题
  
二、安装完后的配置
#启动httpd、nagios服务
#service httpd start
#service nagios start
#访问http://localhost/nagios会提示输入帐号密码,这是我们的make install-webconf生效了。
#打开看看
[root@nagios nagios-plugins-1.4.16]# cat /etc/httpd/conf.d/nagios.conf 
 # SAMPLE CONFIG SNIPPETS FOR APACHE WEB SERVER
# Last Modified: 11-26-2005
#
# This file contains examples of entries that need
# to be incorporated into your Apache web server
# configuration file.  Customize the paths, etc. as
# needed to fit your system.
 
ScriptAlias /nagios/cgi-bin "/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
 
Alias /nagios "/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
#可以清楚的看到可以定制访问权限,以及验证所在文件:/usr/local/nagios/etc/htpasswd.users
#生成一个
[root@nagios nagios-plugins-1.4.16]# htpasswd -c /usr/local/nagios/etc/htpasswd.users cwtea
#重新进入http://localhost/nagios,输入刚才的帐号密码,应该进去了。
图1:
 

Nagios安装真可爱

#开始配置服务了。
#我们make install-config会烤贝一些样例在...prefix/nagios/etc下
#主配置文件nagios.cfg中文说明文档:转载
1、http://blog.chinaunix.net/uid-374372-id-89822.html
2、http://blog.chinaunix.net/uid-374372-id-89823.html
 
#error
 The server encountered an internal error or misconfiguration and was unable to complete your request.
Please contact the server administrator, root@localhost and inform them of the time the error occurred, and anything you might have done that may have caused the error. 
More information about this error may be available in the server error log.
#查看日志,selinux在作怪
 [Fri Mar 15 00:22:46 2013] [error] [client 192.168.100.80] (13)Permission denied: exec of '/usr/local/nagios/sbin/status.cgi' failed, referer: http://192.168.100.84/nagios/side.php
#setenforce 0/1 0为宽容,1为紧张。
#setenforce 0 
#配置cgi.cgi以使我们创建的帐号获取更高的权限,而不会提示:
 It appears as though you do not have permission to view information for any of the services you requested...
#由于我们并没有nagiosadmin此用户,所以我们直接替换。多个可以用逗号隔开。可先备份cgi.cgi文件
[root@nagios etc]# sed -i 's/nagiosadmin/cwtea/g' cgi.cfg 
[root@nagios etc]# cat cgi.cfg |grep cwtea
 authorized_for_system_information=cwtea
authorized_for_configuration_information=cwtea
authorized_for_system_commands=cwtea
authorized_for_all_services=cwtea
authorized_for_all_hosts=cwtea
authorized_for_all_service_commands=cwtea
authorized_for_all_host_commands=cwtea
#下面就是修改配置文件了添加服务等。请自行查看nagios中文说明文档再进行操作。
#我这里创建一个主机组主机 联系人组联系人共存的配置文件。如果主机够多可再建个文件。
#具体函义请查看说明文档。
[root@nagios etc]# vim objects/hc.cfg
 #hostgroup
define hostgroup {
         hostgroup_name  ktm-server
         alias           ktm server
         members         nagios-server
         }
#host
define host {
       host_name                  nagios-server
       alias                      nagios server
       address                    192.168.100.85
       contact_groups             ktm
       check_command              check-host-alive
       max_check_attempts         5
       notification_interval      10
       notification_period        24x7
       notification_options        d,u,r
       }
#contact
define contact {
        contact_name    cwtea
        alias   system administrator
        service_notification_period     24x7
        host_notification_period        24x7
        service_notification_options    w,u,c,r
        host_notification_options       d,u,r
        service_notification_commands   notify-service-by-email
        host_notification_commands      notify-host-by-email
        email                          zwhset@163.com
        pager                          15010228888
        }
#contactgroup
define contactgroup {
        contactgroup_name ktm
        alias   system administrator
        members cwtea
}
#再创建服务模块
[root@nagios etc]# vim objects/service.cfg
 #service in host
define service {
        host_name        nagios-server
        service_description   check-host-alive
        check_period          24x7
        max_check_attempts    4
        normal_check_interval 3
        retry_check_interval  2
        contact_groups        ktm
        notification_interval   10
        notification_period     24x7
        notification_options    w,u,c,r
        check_command           check-host-alive
        }
#查看一下templates.cfg  timeperiods.cfg两个配置文件的内容以达到我们配置的目的。
#编辑nagios.cfg文件以加载这几个文件的目的。并且注销掉localhost.cfg文件。我们不需要。
[root@nagios objects]# vim ../nagios.cfg 
 cfg_file=/usr/local/nagios/etc/objects/hc.cfg
cfg_file=/usr/local/nagios/etc/objects/service.cfg
#add #
#cfg_file=/usr/local/nagios/etc/objects/localhost.cfg
 
#验证一下nagios配置是否正确,直到Total Warnings: 0 Total Errors:   0。当然有错误排查错误,一般都会提示非常清楚。
[root@nagios objects]# /usr/local/nagios/bin/nagios -v /usr/local/nagios/etc/nagios.cfg 
#滑溜启动一下nagios
#根据上面编译提示我们的PID保存文件。
#[root@nagios objects]# kill -Hup `cat /usr/local/nagios/var/nagios.lock`
#太长了,我们可以取巧一下。也可添加到~/.bashrc文件中,这样我们就可以knagios滑溜升级,vnagios验证配置文件。
[root@nagios ~]# alias knagios='kill -Hup `cat /usr/local/nagios/var/nagios.lock`'
[root@nagios ~]# alias vnagios='/usr/local/nagios/bin/nagios -v /usr/local/nagios/etc/nagios.cfg'
#再看看Nagios监控我们的web界面。
图2:

Nagios安装真可爱

 
添加服务到监控报警并邮件可看另一篇文章。
Nagios检测一些记序:
http://cwtea.blog.51cto.com/4500217/1153384
 
煮酒品茶:我们再来看看一些好玩的东西吧。
[root@nagios var]# pwd
/usr/local/nagios/var
[root@nagios var]# tree
 .
├── archives
├── nagios.lock #PID号保存文件。
├── nagios.log #当然是日志啦。
├── objects.cache #当你查看这个文件,你就会喜欢上他了。缓存的,看下面解释。
├── retention.dat #上面缓存一个,这里又保留一个。好兄弟要哥俩啊。
├── rw
│?? └── nagios.cmd #看下面解决。
├── spool
│?? └── checkresults #这个文件夹在工作的时候会经常写册文件,这当然是在采集数据存放于此。
└── status.dat #这个就有意思了,网站读的就是这个文件,service模块下有个performance_data=rta=0.497000ms;3000.000000;5000.000000;0.000000 pl=0%;80;100;0,对比网站上的是不是一样?
 
#objects.cache 解释:
 # OBJECT CACHE FILE
# This option determines where object definitions are cached when
# Nagios starts/restarts.  The CGIs read object definitions from
# this cache file (rather than looking at the object config files
# directly) in order to prevent inconsistencies that can occur
# when the config files are modified after Nagios starts.
对象缓存文件。这些选项将决定当Nagios启动时或重新启动时,对象定义将被缓存在什么地方。CGI将从这个对象文件中读取对象的定义,而不是在之前的对象配置文件路径中去找。这样做是为了避免修改Nagios配置文件后引起的不一致问题。换句简单的话说就是更改配置文件后要重新运新Nagios新的配置参数才会生效,而当前运行的Nagios只参考缓存中的配置参数而已。
 
object_cache_file=/var/log/nagios/objects.cache
 
#nagios.cmd 解释:
 # EXTERNAL COMMAND FILE
# This is the file that Nagios checks for external command requests.
# It is also where the command CGI will write commands that are submitted
# by users, so it must be writeable by the user that the web server
# is running as (usually 'nobody').  Permissions should be set at the
# directory level instead of on the file, as the file is deleted every
# time its contents are processed.
这是Nagios用来检查外部命令请求的文件。这个文件同样也是用户操作提交与CGI命令写入的地方,所以这个文件必须对于相关服务可写,一般是针对Apache的宿主用户可写。并且注意,这个文件所在的目录的权限必须被Apache可写,而不单指这文件,因为这个文件在工作当中是频繁被删除的。(之前我们将Apahe归入Nagios组的时候就已经完成了)
 
command_file=/var/log/nagios/rw/nagios.cmd
 
#status.dat 解释:
 # STATUS FILE
# This is where the current status of all monitored services and
# hosts is stored.  Its contents are read and processed by the CGIs.
# The contents of the status file are deleted every time Nagios
#  restarts.
状态文件。这个文件将保存着目前检测到的服务和主机数据信息。这个文件当中的内容是被CGI读取并处理的,而它也是在每次Nagios重新启动的时候被删除清空的。
 
status_file=/var/log/nagios/status.dat
 
[root@nagios bin]# pwd
/usr/local/nagios/bin
[root@nagios bin]# tree
 .
├── nagios  #这个是启动文件
└── nagiostats #运行一下这个,你会发现世界是多么的精彩。这个不就是nagios web界面上的信息吗?他也是采集这个文件。
图3:
 

Nagios安装真可爱

#看看权限,是不是印证了?
[root@nagios bin]# ll nagiostats 
-rwxrwxr--. 1 nagios nagios 44104 Mar 14 21:49 nagiostats
#那么都有些啥目录呢?都有啥用?
[root@nagios nagios]# ls
bin  etc  include  libexec  sbin  share  var
 #网上找的说明,难得敲字。
bin   Nagios执行程序所在目录,nagios文件即为主程序
etc   Nagios配置文件位置,初始安装完后,只有几个*.cfg-sample文件
sbin  Nagios Cgi文件所在目录,也就是执行外部命令所需文件所在的目录
Share Nagios网页文件所在的目录
Var   Nagios日志文件、spid 等文件所在的目录
#发现每次打开http://localhost/nagios登入后都是广告。头疼。那我们修改一下吧。
#用firefox打开后只显示那个广告框架。发现加载的是main.php。再加载我们想要的service标签。发现加载的是cgi-bin/status.cgi这个cgi
[root@nagios share]# pwd
/usr/local/nagios/share
#注销掉或者直接更改都可。
[root@nagios share]# vim index.php
 //$corewindow="main.php"; 
$corewindow="cgi-bin/status.cgi";
#再次打开http://localhost/nagios 直接加载的就是service标签了。见图4
图4:
 

Nagios安装真可爱

那么整个nagios的工作过程你明白了吗?
煮酒品茶:我们的玩nagios就结束了。哈哈哈,还蛮有意思的。   

 


分享文章:Nagios安装真可爱
本文路径:http://cdweb.net/article/gheisc.html