Nagios安装

来源:互联网 发布:社会网络信息流模型 编辑:程序博客网 时间:2024/06/04 19:10

Nagios服务端安装

5.1 基础支持套件:gcc glibc glibc-common gd gd-devel xinetd openssl-devel 

# rpm -q gcc glibc glibc-common gd gd-devel xinetd openssl-devel

8

如果系统中没有这些套件,使用yum 安装

# yum install -y gcc glibc glibc-common gd gd-devel xinetd openssl-devel

5.2 创建nagios用户和用户组

# useradd -s /sbin/nologin nagios# mkdir /usr/local/nagios# chown -R nagios.nagios /usr/local/nagios

6

查看nagios 目录的权限

# ll -d /usr/local/nagios/

7

5.3 编译安装Nagios

wget http://downloads.sourceforge.net/project/nagios/nagios-4.x/nagios-4.2.0/nagios-4.2.0.tar.gz
tar zxvf nagios-4.2.0.tar.gz
cd nagios-4.2.0
./configure --prefix=/usr/local/nagios

9

10

# make all

11

12

# make install

13

# make install-init

14

# make install-commandmode

15

# make install-config

16

# chkconfig --add nagios

# chkconfig --level 35 nagios on

# chkconfig --list nagios

17

5.4 验证程序是否被正确安装

切换目录到安装路径(这里是/usr/local/nagios),看是否存在etc、bin、sbin、share、var 这五个目录,如果存在则可以表明程序被正确的安装到系统了。Nagios 各个目录用途说明如下:

binNagios 可执行程序所在目录etcNagios 配置文件所在目录sbinNagios CGI 文件所在目录,也就是执行外部命令所需文件所在的目录shareNagios网页文件所在的目录libexecNagios 外部插件所在目录varNagios 日志文件、lock 等文件所在的目录var/archivesNagios 日志自动归档目录var/rw用来存放外部命令文件的目录

5.5 安装Nagios 插件

wget http://nagios-plugins.org/download/nagios-plugins-2.1.2.tar.gz
tar zxvf nagios-plugins-2.1.2.tar.gz
cd nagios-plugins-2.1.2
./configure --prefix=/usr/local/nagios
make && make install

18

19

5.6 安装与配置Apache和Php

Apache 和Php 不是安装nagios 所必须的,但是nagios提供了web监控界面,通过web监控界面可以清晰的看到被监控主机、资源的运行状态,因此,安装一个web服务是很必要的。
需要注意的是,nagios在nagios3.1.x版本以后,配置web监控界面时需要php的支持。这里我们下载的nagios版本为nagios-3.4.3,因此在编译安装完成apache后,还需要编译php模块,这里选取的php版本为php5.4.10。

a. 安装Apache

wget http://archive.apache.org/dist/httpd/httpd-2.2.23.tar.gz
tar zxvf httpd-2.2.23.tar.gz
cd httpd-2.2.23
./configure --prefix=/usr/local/apache2 --enable-rewrite
make && make install

22

若出现错误:

则在编译时入加 --with-included-apr 即可解决。

b. 安装Php

wget http://cn2.php.net/distributions/php-5.4.26.tar.gz
yum install libxml2 –y
yum install libxml2-devel –y
tar -xvf php-5.4.26.tar.gz
cd php-5.4.26
./configure --prefix=/usr/local/php --with-apxs2=/usr/local/apache2/bin/apxs --with-gd --with-zlib --enable-sockets

有报错信息:

Configuring extensions

checking size of long... (cached) 8

checking size of int... (cached) 4

checking for int32_t... yes

checking for uint32_t... yes

checking for sys/types.h... (cached) yes

checking for inttypes.h... (cached) yes

checking for stdint.h... (cached) yes

checking for string.h... (cached) yes

checking for stdlib.h... (cached) yes

checking for strtoll... yes

checking for atoll... yes

checking for strftime... (cached) yes

checking which regex library to use... php

checking whether to enable LIBXML support... yes

checking libxml2 install dir... no

checking for xml2-config path...

configure: error: xml2-config not found. Please check your libxml2 installation.

 

需要重新安装lib

yum install libxml2 –y

yum install libxml2-devel –y

如果有问题:

yum install libpng-devel

yum install libpng

之后继续make,成功了

23

# make && make install

24

c. 配置apache 
找到apache 的配置文件/usr/local/apache2/conf/httpd.conf 
找到:

User daemon Group daemon 

修改为 

User nagios Group nagios 

然后找到 

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

修改为 

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

接着增加如下内容: 

AddType application/x-httpd-php .php 

最后修改添加 ServerName 127.0.0.1:80

为了安全起见,一般情况下要让nagios 的web 监控页面必须经过授权才能访问,这需要增加验证配置,即在httpd.conf 文件最后添加如下信息:

#setting for nagios
ScriptAlias /nagios/cgi-bin /usr/local/nagios/sbin
<Directory "/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
</Directory>
Alias /nagios /usr/local/nagios/share
<Directory "/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
</Directory>

d. 创建apache目录验证文件 

在上面的配置中,指定了目录验证文件htpasswd,下面要创建这个文件:

# /usr/local/apache2/bin/htpasswd -c /usr/local/nagios/etc/htpasswd david

25 
这样就在/usr/local/nagios/etc 目录下创建了一个htpasswd 验证文件,当通过http://192.168.1.108/nagios/ 访问时就需要输入用户名和密码了。

e. 查看认证文件的内容

# cat /usr/local/nagios/etc/htpasswd

26

f. 启动apache 服务

# /usr/local/apache2/bin/apachectl start

到这里nagios 的安装也就基本完成了,你可以通过web来访问了。

配置Nagios

Nagios 主要用于监控一台或者多台本地主机及远程的各种信息,包括本机资源及对外的服务等。默认的Nagios 配置没有任何监控内容,仅是一些模板文件。若要让Nagios 提供服务,就必须修改配置文件,增加要监控的主机和服务,下面将详细介绍。

6.1 默认配置文件介绍

Nagios 安装完毕后,默认的配置文件在/usr/local/nagios/etc目录下。

29

每个文件或目录含义如下表所示:

文件名或目录名用途cgi.cfg控制CGI访问的配置文件nagios.cfgNagios 主配置文件resource.cfg变量定义文件,又称为资源文件,在些文件中定义变量,以便由其他配置文件引用,如$USER1$objectsobjects 是一个目录,在此目录下有很多配置文件模板,用于定义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 主机的一个配置文件模板,默认没有启用此文件

6.2 配置文件之间的关系

在nagios的配置过程中涉及到的几个定义有:主机、主机组,服务、服务组,联系人、联系人组,监控时间,监控命令等,从这些定义可以看出,nagios各个配置文件之间是互为关联,彼此引用的。

成功配置出一台nagios监控系统,必须要弄清楚每个配置文件之间依赖与被依赖的关系,最重要的有四点:

第一:定义监控哪些主机、主机组、服务和服务组;
第二:定义这个监控要用什么命令实现;
第三:定义监控的时间段;
第四:定义主机或服务出现问题时要通知的联系人和联系人组。

6.3 配置Nagios

 为了能更清楚的说明问题,同时也为了维护方便,建议将nagios各个定义对象创建独立的配置文件:

  •  创建hosts.cfg文件来定义主机和主机组
  •  创建services.cfg文件来定义服务
  •  用默认的contacts.cfg文件来定义联系人和联系人组
  •  用默认的commands.cfg文件来定义命令
  •  用默认的timeperiods.cfg来定义监控时间段
  •  用默认的templates.cfg文件作为资源引用文件
commands.cfg文件
define command{        command_name    check_nt        command_line    $USER1$/check_nt -H $HOSTADDRESS$ -p 12489 -s 123456 -v $ARG1$ $ARG2$        }

 cgi.cfg文件

此文件用来控制相关cgi脚本,如果想在nagios的web监控界面执行cgi脚本,例如重启nagios进程、关闭nagios通知、停止nagios主机检测等,这时就需要配置cgi.cfg文件了。
由于nagios的web监控界面验证用户为david,所以只需在cgi.cfg文件中添加此用户的执行权限就可以了,需要修改的配置信息如下:

default_user_name=davidauthorized_for_system_information=nagiosadmin,david  authorized_for_configuration_information=nagiosadmin,david  authorized_for_system_commands=davidauthorized_for_all_services=nagiosadmin,david  authorized_for_all_hosts=nagiosadmin,davidauthorized_for_all_service_commands=nagiosadmin,david  authorized_for_all_host_commands=nagiosadmin,david 

nagios.cfg文件

nagios.cfg默认的路径为/usr/local/nagios/etc/nagios.cfg,是nagios的核心配置文件,所有的对象配置文件都必须在这个文件中进行定义才能发挥其作用,这里只需将对象配置文件在Nagios.cfg文件中进行引用即可。

6.4 验证Nagios 配置文件的正确性

Nagios 在验证配置文件方面做的非常到位,只需通过一个命令即可完成:

# /usr/local/nagios/bin/nagios -v /usr/local/nagios/etc/nagios.cfg
 
Nagios提供的这个验证功能非常有用,在错误信息中通常会打印出错误的配置文件以及文件中的哪一行,这使得nagios的配置变得非常容易,报警信息通常是可以忽略的,因为一般那些只是建议性的。 

看到上面这些信息就说明没问题了,然后启动Nagios 服务。

七、Nagios的启动与停止

7.1 启动Nagios

a. 通过初始化脚本启动nagios

# /etc/init.d/nagios start
or
# service nagios start

b. 手工方式启动nagios

通过nagios命令的“-d”参数来启动nagios守护进程:
# /usr/local/nagios/bin/nagios -d /usr/local/nagios/etc/nagios.cfg

7.2 重启Nagios

当修改了配置文件让其生效时,需要重启/重载Nagios服务。

a. 通过初始化脚本来重启nagios

# /etc/init.d/nagios reload
or
# /etc/init.d/nagios restart
or
# service nagios restart

b. 通过web监控页重启nagios

可以通过web监控页的 "Process Info" -> "Restart the Nagios process"来重启nagios

c. 手工方式平滑重启

# kill -HUP <nagios_pid>

7.3 停止Nagios

a. 通过初始化脚本关闭nagios服务

# /etc/init.d/nagios stop
or
# service nagios stop

b. 通过web监控页停止nagios

可以通过web监控页的 "Process Info" -> "Shutdown the Nagios process"来停止nagios

c. 手工方式停止Nagios

# kill <nagios_pid>

八、查看初步配置情况

8.1 启动完成之后,登录Nagios Web监控页http://192.168.1.108/nagios/ 查看相关信息。

8.2 点击左面的Current Status -> Hosts 可以看到所定义的三台主机已经全部UP了。

8.3 点击Current Status -> Services 查看服务监控情况。

看到Nagios-Linux和Nagios-Server的服务状态已经OK了,但是Nagios-Windows的服务状态为CRITICAL,Status Information 提示Connection refused。因为Nagios-Windows上还未安装插件,内部服务还无法查看,所以出现这种情况。将在下面具体讲解。



0 0