nagios是个不错的监控管理工具,这几天利用业余时间在机器上装了一下,都说配置比较麻烦,其实还好,按步骤来,其实只是配置的东西有点多,并不会太麻烦。

机器环境:

os : ubuntu 13.10 64bit

web server: nginx1.4.1 + spawncgi + php-fpm + php5.5

在安装之前先安装好上面的运行环境,至少能跑cgi和php。

一,安装准备

建立相关用户组和用户:

root@~# groupadd nagcmd
root@~# groupadd nagios
root@~# useradd -g nagios nagios

 

二,安装nagios核心

解压核心包后,配置nagios:

dongyado:~/nagios/nagios-4.0.8 $ ./configure  --prefix=/usr/local/nagios --with-command-group=nagcmd

配置成功之后会打印出下列我们需要用到的信息:

 

*** Configuration summary for nagios 4.0.8 08-12-2014 ***:

 General Options:
 -------------------------
        Nagios executable:  nagios
        Nagios user/group:  nagios,nagios
       Command user/group:  nagios,nagcmd
             Event Broker:  yes
        Install ${prefix}:  /usr/local/nagios
    Install ${includedir}:  /usr/local/nagios/include/nagios
                Lock file:  ${prefix}/var/nagios.lock
   Check result directory:  ${prefix}/var/spool/checkresults
           Init directory:  /etc/init.d
  Apache conf.d directory:  /etc/httpd/conf.d
             Mail program:  /usr/bin/mail
                  Host OS:  linux-gnu
          IOBroker Method:  epoll

 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

编译:

dongyado:~/nagios/nagios-4.0.8$ 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

  make install-init
     - This installs the init script in /etc/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

按照上面的提示一步一步安装,需要注意的是如果没有安装apache2或者安装的版本太新,在安装web配置的时候会报错:

dongyado:~/nagios/nagios-4.0.8$ sudo make install-webconf
/usr/bin/install -c -m 644 sample-config/httpd.conf /etc/httpd/conf.d/nagios.conf
/usr/bin/install: cannot create regular file ‘/etc/httpd/conf.d/nagios.conf’: No such file or directory
make: *** [install-webconf] Error 1

就是说/etc/httpd/con.d/这个目录不存在,建立一个即可:

dongyado:~/nagios/nagios-4.0.8$ sudo mkdir /etc/httpd/conf.d/ -p

再执行:

dongyado:~/nagios/nagios-4.0.8$ sudo make install-webconf
/usr/bin/install -c -m 644 sample-config/httpd.conf /etc/httpd/conf.d/nagios.conf

*** Nagios/Apache conf file installed ***

提示安装成功,这里注意一下nagios.conf的路径,等下我们配置nagios的虚拟机的时候会用到。

然后再安装一个主题:

dongyado:~/nagios/nagios-4.0.8$ sudo make install-exfoliation
cp -rf contrib/exfoliation/stylesheets/* /usr/local/nagios/share/stylesheets
cp -rf contrib/exfoliation/images/* /usr/local/nagios/share/images

*** Exfoliation theme installed ***
NOTE: Use 'make install-classicui' to revert to classic Nagios theme

三,配置相关用户

1,创建访问账户

这里一般把用户信息放入nagios安装目录的etc里面,以后也方便管理,不过在服务器上不建议这么做,应该放在一个更安全的地方。

dongyado:~/nagios/nagios-4.0.8$ sudo htpasswd -c /usr/local/nagios/etc/htpasswd.users nagiosuser
New password: 
Re-type new password: 
Adding password for user nagiosuser
dongyado:~/nagios/nagios-4.0.8$ cat /usr/local/nagios/etc/htpasswd.users 
nagiosuser:$apr1$sWTOOLSD$yfPXgmyA/k5j5303xuqIy1

打开cgi.cfg配置文件把刚创建的用户添加进去,是该用户有执行相关操作的权限:

dongyado:~/nagios/nagios-4.0.8$ sudo vim  /usr/local/nagios/etc/cgi.cfg

搜索 authorized_for,然后在每一行后面加上nagiosuser,用逗号与前面的用户隔开:

authorized_for_system_information=nagiosadmin,nagiosuser
authorized_for_configuration_information=nagiosadmin,nagiosuser
authorized_for_system_commands=nagiosadmin,nagiosuser
authorized_for_all_services=nagiosadmin,nagiosuser
authorized_for_all_hosts=nagiosadmin,nagiosuser
authorized_for_all_host_commands=nagiosadmin,nagiosuser
authorized_for_all_service_commands=nagiosadmin,nagiosuser

2. 目录信息

目录名称 功能
bin 可执行程序所在目录
etc 配置文件目录
sbin Nagios cgi 文件所在目录, 也就是执行外部 命令所需要文件所在的目录
share Nagios 网页存放路径
libexec 外部插件存放目录,安装的插件一般都会在此目录生成一些可调用的命令
var 日志文件、Lock 等文件所在的目录
var/archives 日志自动归档目录
var/rw 用来存放外部命令文件的目录

3. etc下面配置文件的作用

配置文件 说明
cgi.cfg 控制 CGI 访问的配置文件
nagios.cfg Nagios 主配置文件
resource.cfg 变量定义文件,又称为资源文件,在此文件中定义变量,以便由其他配置文件引用,如$USER1$
objects objects 是一个目录,在此目录下有很多配置文件模板,用于定义 Nagios 对象
objects/commands.cfg 命令定义配置文件,其中定义的命令可以被其他配置文件引用
objects/contacts.cfg 定义联系人和联系人组的配置文件
objects/localhost.cfg 定义监控本地主机的配置文件
objects/printer.cfg 定义监控打印机的一个配置文件模板,默认没有启用此文件
objects/switch.cfg 监控路由器的一个配置文件模板,默认没有启用此文件
objects/templates.cfg 定义主机和服务的一个模板配置文件,可以在其他配置文件中引用
objects/timeperiods.cfg 定义 Nagios 监控时间段的配置文件
objects/windows.cfg 监控 Windows 主机的一个配置文件模板,默认没有启用此文件

四,安装插件

$ ./configure --prefix=/usr/local/nagios --with-nagios-user=nagios --with-nagios-group=nagios
....
config.status: creating po/Makefile
            --with-apt-get-command: /usr/bin/apt-get
              --with-ping6-command: /bin/ping6 -n -U -w %d -c %d %s
               --with-ping-command: /bin/ping -n -U -w %d -c %d %s
                       --with-ipv6: yes
                      --with-mysql: no
                    --with-openssl: yes
                     --with-gnutls: no
               --enable-extra-opts: yes
                       --with-perl: /usr/bin/perl
             --enable-perl-modules: no
                     --with-cgiurl: /nagios/cgi-bin
               --with-trusted-path: /bin:/sbin:/usr/bin:/usr/sbin
                   --enable-libtap: no

然后再 make && make install即可,查看libexec我们会发现插件已经生成了各种check命令了

五,验证配置文件并启动

$ sudo /usr/local/nagios/bin/nagios -v /usr/local/nagios/etc/nagios.cfg 
Reading configuration data...
   Read main config file okay...
   Read object config files okay...

Running pre-flight check on configuration data...

Checking objects...
	Checked 8 services.
	Checked 1 hosts.
	Checked 1 host groups.
	Checked 0 service groups.
	Checked 1 contacts.
	Checked 1 contact groups.
	Checked 24 commands.
	Checked 5 time periods.
	Checked 0 host escalations.
	Checked 0 service escalations.
Checking for circular paths...
	Checked 1 hosts
	Checked 0 service dependencies
	Checked 0 host dependencies
	Checked 5 timeperiods
Checking global event handlers...
Checking obsessive compulsive processor commands...
Checking misc settings...

Total Warnings: 0
Total Errors:   0

Things look okay - No serious problems were detected during the pre-flight check

说明nagios配置是没有问题的,之后我们修改了配置文件在启动之前都可以通过这个方法检查配置文件。然后就可以通过 -d参数命令启动nagios,再通过nagiostat查看运行状态

$ sudo /usr/local/nagios/bin/nagios -d /usr/local/nagios/etc/nagios.cfg
$ sudo /usr/local/nagios/bin/nagiostats
Nagios Stats 4.0.8
Copyright (c) 2003-2008 Ethan Galstad (www.nagios.org)
Last Modified: 08-12-2014
License: GPL

CURRENT STATUS DATA
------------------------------------------------------
Status File:                            /usr/local/nagios/var/status.dat
Status File Age:                        0d 0h 0m 10s
Status File Version:                    4.0.8

Program Running Time:                   0d 0h 0m 29s
Nagios PID:                             9295
.......

六,配置web服务

最新版的apache2的配置文件已经不在/etc/httpd/里面了,如果需要拷贝到apache2的配置文件里面去,或者包含进去。这里用的是niginx所以就不演示apache2的配置了,配置nginx:

$ cd /etc/nginx/sites-available
$ sudo cp default nagios
$ sudo vim nagios

打开/etc/httpd/conf.d/nagios.conf:

# SAMPLE CONFIG SNIPPETS FOR APACHE WEB SERVER
#
# 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"

<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>

里面指定了两个位置的信息,一个是cgi脚本目录,一个是web php的目录,在这里我们需要配置cgi使用spawn-cgi执行,php使用php-fpm