Nagios安装与配置

第一部分:安装nagios
1.     安装前的准备:
(1)  安装必要的插件:
    #yum  -y  install  gcc  glibc  glibc-common  gd  gd-devel  libxml2  libxml2-devel
(2)  创建nagios用户和用户组
    #useradd  -s  /sbin/nologin   nagios
    #mkdir   /opt/app/nagios
    #chown  -R  nagios:nagios  /opt/app/nagios
(3)   开启系统的senmail服务
在nagios监控服务器上开启sendmail服务的主要作用是让nagios在检测到故障时可以发送报警邮件。目 前几乎所有的linux发行版本都默认自带了sendmail服务,所以,在安装系统时,只需开启sendmail服务 即可,并不需要在sendmail上进行任何配置;可以用下边的命令进行测试能否正常发送邮件:
#echo   “hello” |  mail    1352206****@139.com

2.      编译安装nagios
#cd  /opt/software
# wget   http://prdownloads.sourceforge.net/sourceforge/nagios/nagios.3.5.0.tar.gz
#tar   zxvf   nagios-3.5.0.tar.gz
#cd  nagios
#./configure   --prefix=/opt/app/nagios
#make  all
#make  install  (安装nagios主程序的CGI和HTML文件)
#make  install-init  (在/etc/init.d/目录下创建nagios启动脚本)
#make   install-commandmode    (配置目录权限)
#make   install-config  (安装nagios示例配置文件,路径为/opt/app/nagios/etc)


设置开机自启动:

#chkconfig  - -add   nagios
#chkconfig  --level  35  nagios  on

3.       安装nagios插件
#cd  /opt/software
#wget http://prdownloads.sourceforge.net/sourceforge/nagiosplug/nagios-plugins-
1.4.14.tar.gz
#tar   zxvf  nagios-plugins-1.4.14.tar.gz
#cd   nagios-plugins-1.4.14
#./configure  --prefix=/opt/app/nagios
#make

#make  install

插件安装路径最好和nagios安装路径一致,这样安装完插件后会在nagios主程序目录(即/opt/app/nagios下的libexec目录)下生成很多可执行文件,这些就是nagios所需要的插件。


4.     安装nagios汉化插件
#cd  /opt/software
#wget http://jaist.dl.sourceforge.net/project/nagios-cn/sourcecode/zh_CN%203.2.3/nagios-
cn-3.2.3.tar.bz2
#tar  jxvf  nagios-cn-3.2.3.tar.bz2
#cd  nagios-cn-3.2.3
#./configure  --prefix=/opt/app/nagios
#make  all
#make  install

5.       安装与配置apache和php
Apache不是安装nagios所必须的,但是nagios提供了web监控页面,通过web监控界面可以清晰的看到被 监控的主机和资源的运行状态,所以,安装一个web服务是很必要的,可选的web服务器有apache和nginx 等,这里选择apache,操作过程如下:
(1) 安装apache和php
 首先安装apache:
 #cd  /opt/software
 #tar  zxvf  httpd-2.2.4.tar.gz
 #cd  httpd-2.2.4
 #./configure --prefix=/opt/app/apache2 --enable-modules=so --enable-rewrite
 #make

 #make install


 接着安装php:
 #tar zxvf php-5.2.10.tar.gz
 #cd php-5.2.10
 #./configure --prefix=/opt/app/php5 --with-apxs2=/opt/app/apache2/bin/apxs
 #make ZEND_EXTRA_LIBS='-liconv'
 #make install
ln -s /usr/local/lib/libiconv.so.2 /usr/lib64/

(2)配置apache
  首先在apache的配置文件httpd.conf中修改apache进程的启动用户为nagios,即找到:
  User  nobody
  Group  nobody
  修改为:
  User   nagios

  Group   nagios


然后找到:
 DirectoryIndex   index.html  index.html.var
 修改为:
 DirectoryIndex   index.html  index.php

接着增加如下内容:
 AddType application/x-httpd-php  .php
为了安全起见,一般要求必须经过授权才能访问nagios的web监控页面,因此需要增加验证配置 ,即在httpd.conf文件的最后添加如下信息:
      #setting  for  nagios
ScriptAlias /nagios/cgi-bin /opt/app/nagios/sbin
<Directory "/opt/app/nagios/sbin">  
       AuthType Basic
       Options ExecCGI
       AllowOverride None
      Order allow,deny
       Allow from all
       AuthName "Nagios Access"
       AuthUserFile /opt/app/nagios/etc/htpasswd  
       Require valid-user
</Directory>
Alias /nagios /opt/app/nagios/share  
<Directory "/opt/app/nagios/share">  
       AuthType Basic
       Options None
       AllowOverride None
       Order allow,deny
       Allow from all
       AuthName "Nagios Access"
       AuthUserFile /opt/app/nagios/etc/htpasswd  
       Require valid-user
</Directory>

在配置文件最后加上一句:
AddDefaultCharset utf-8   (此语句能解决中文汉化后,nagios界面乱码的问题)

(3)       创建apache目录验证文件
  在上面的配置中,指定了目录验证文件hapassword,下面创建一个这个样的文件:
#/opt/app/apache2/bin/htpassword   -c  /opt/app/nagios/etc/htpasswd   test
输入两次密码即可;(第一次添加账号,需要-c参数,第二次以后就不能使用-c参数了,否则,前边的 账号都会被覆盖掉)
当通过http://ip/nagios/访问web监控界面时,就需要输入用户名和密码了。

(4)启动apache服务
#/opt/app/apache2/bin/apachectl   -t
#/opt/app/apache2/bin/apachectl  start
关闭防火墙和selinux,然后就可以通过web界面,看到nagios默认的监控界面了;


第二部分:配置主机是否存活

1.       主配置文件nagios.cfg的配置
主配置文件的内容很多,对于这个版本,我们需要修改和添加的主要是对象配置文件,即:
log_file=/opt/app/nagios/var/nagios.log
cfg_file=<file_name>
cfg_file=/opt/app/nagios/etc/objects/commands.cfg (命令定义文件)
cfg_file=/opt/app/nagios/etc/objects/contacts.cfg (联系人信息定义文件)
cfg_file=/opt/app/nagios/etc/objects/contactgroups.cfg (添加此行 联系人组定义文件)
cfg_file=/opt/app/nagios/etc/objects/hosts.cfg (添加此行 主机定义文件)
cfg_file=/opt/app/nagios/etc/objects/hostgroups.cfg (添加此行 主机组定义文件)
cfg_file=/opt/app/nagios/etc/objects/services.cfg (添加此行 服务定义文件)
#cfg_file=/opt/app/nagios/etc/objects/servicegroup.cfg (注释掉此行)
cfg_file=/opt/app/nagios/etc/objects/timeperiods.cfg (时间周期定义文件)
cfg_file=/opt/app/nagios/etc/objects/templates.cfg
# Definitions for monitoring the local (Linux) host
#cfg_file=/usr/local/nagios/etc/objects/localhost.cfg (注释掉此行)

在web界面下重启nagios,停止主机/服务器检查等操作,默认值为:
check_external_commands=1
根据自己的情况定义这个命令检查时间间隔,默认值为1秒:
command_check_interval=10s

2.       修改cgi配置文件cgi.cfg。这里只列出被修改过的地方
main_config_file=/opt/app/nagios/etc/nagios.cfg
physical_html_path=/opt/app/nagios/share
#如果有多个用户,中间用逗号隔开
authorized_for_system_information=test,nagiosadmin
authorized_for_configuration_information=test,nagiosadmin
authorized_for_system_commands=test,nagiosadmin
authorized_for_all_services=test,nagiosadmin
authorized_for_all_hosts=test,nagiosadmin
authorized_for_all_services_commands=test,nagiosadmin
authorized_for_all_host_commands=test,nagiosadmin
这里指定用户“test”可以通过浏览器操作nagios服务的关闭、重启等各种操作。

3.       主机定义文件的配置
#vim  objects/hosts.cfg
define host{
host_name
cms-1  #监控的名字
alias  192.168.1.38      #IP
address  192.168.1.38
check_command check-host-alive
check_interval 5
retry_interval 1
max_check_attempts 5
check_period 24x7
process_perf_data 0
retain_nonstatus_information 0
contact_groups
cms #组的名字
notification_interval 30
notification_period 24x7
notification_options d,u,r
}

define host{
host_name
cms-2
alias 192.168.1.78
address  192.168.1.78
check_command check-host-alive
check_interval 5
retry_interval 1
max_check_attempts 5
check_period 24x7
process_perf_data 0
retain_nonstatus_information 0
contact_groups
cms #组的名字
notification_interval 30
notification_period 24x7
notification_options d,u,r
}

4.       主机组定义文件配置
#vim objects/hostgroups.cfg
define hostgroup {
hostgroup_name cms   #输入监控组的名称
alias nagios admin
members
cms-1,cms-2  #加入监控机器的名字
}

5.       服务定义文件的配置
#vim objects/services.cfg
define service {
host_name  stoneage-1
service_description check-host-alive
check_period 24x7
max_check_attempts 4
normal_check_interval 3
retry_check_interval 2
contact_groups stoneage
notification_interval 10
notification_period 24x7
notification_options w,u,c,r
check_command check-host-alive
}

define service {
host_name  stoneage-2
service_description check-host-alive
check_period 24x7
max_check_attempts 4
normal_check_interval 3
retry_check_interval 2
contact_groups  stoneage
notification_interval 10
notification_period 24x7
notification_options w,u,c,r
check_command check-host-alive
}

6.       联系人定义文件的配置

#vim objects/contacts.cfg
define contact{
contact_name  test
use generic-contact
alias Nagios Admin
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 这个命令在command.cfg中定义
host_notification_commands notify-host-by-email 这个命令在command.cfg中定义
email 137777****@139.com
}
删除contactsgroup的配置

7.       联系人组定义文件的配置
#vim  objects/contactgroups.cfg
define contactgroup {
contactgroup_name stoneage
alias system administrator group
members test
}

8.       验证配置文件的正确性
# /opt/app/nagios/bin/nagios -v /opt/app/nagios/etc/nagios.cfg
如果验证没有问题,就启动nagios和apache,启动方法如下:
#/etc/init.d/nagios  start
#/opt/app/apache2/bin/apachectl  start

本文出自 “linux” 博客,谢绝转载!

你可能感兴趣的:(nagios)