nagios安装使用指南

话不多说,下面开始,nagios具体的介绍,可以搜一下,这篇文章为作者在实际操作中整理出来,写出来的都是负责人的内容~

环境准备

此文档共用2台服务器的配置,操作系统均为centOS6.7,安装用户都为nagios用户,所有的安装都必须基于nagios,当权限不足时,采用sudo的方式,具体信息如下:

IP地址 角色 安装用户
192.168.1.10 Nagios服务端 nagios
192.168.1.11 被监控主机 nagios
nagios服务端安装步骤

编译安装nagios软件

安装需要192.168.1.10服务器预装cc,glibc,glibc-common,gd,gd-devel,xinetd,openssl-devel等,执行命令:

[nagios@localhost ~]$ rpm -qa | grep **
#依次检查是否安装以上软件,如果没有安装则执行命令:
[nagios@localhost ~]$ sudo yum install -y **
1
2
3
1
2
3
依赖部分处理好之后,下面进行nagios的编译安装,nagios的下载地址为:nagios官网下载 ,下载好之后,进入防止安装包的目录,解压压缩包:

[nagios@localhost ~]$ tar zxvf nagios-3.5.0.tar.gz
1
1
进入解压后的目录: cd /home/nagios/nagios依次执行:

./configure --prefix=/usr/local/nagios
make all
sudo make install
sudo make install-init
make install-commandmode
make install-config
sudo chkconfig --add nagios
sudo chkconfig --level 35 nagios on
1
2
3
4
5
6
7
8
1
2
3
4
5
6
7
8
最后执行

sudo chkconfig --list nagios
1
1
显示如下结果:

这里写图片描述

下面来验证nagios是否被安装:
切换到目录/usr/local/nagios/下,查看目录结构如下:

这里写图片描述

如果以上目录都存在,说明程序已经被安装到服务器中,各个目录的作用为:

目录 作用
bin 可执行程序所在目录
etc 配置文件所在目录
libexec 外部插件所存放的目录
sbin Nagios CGI文件所在目录,也就是执行外部命令所需文件所在的目录
share 网页文件存放目录
var 日志,lock文件目录
安装nagios插件

nagios插件下载地址为:官网下载解压安装包:

tar zxvf nagios-plugins-2.0.1.tar.gz
1
1
进入解压目录:

[nagios@localhost ~]$ cd nagios-plugins-2.0.1
[nagios@localhost nagios-plugins-2.0.1]$ pwd
/home/nagios/nagios-plugins-2.0.1 #解压路径
1
2
3
1
2
3
分别执行命令:

./configure --prefix=/usr/local/nagios
make
sudo make install
1
2
3
1
2
3
安装apache和php

安装apache

nagios提供了web页面,可以观看被监控的资源状态,所以需安装apache与PHP来支持此项功能,那么首,查看操作系统是否自带了apache,如果有就删除掉:

[nagios@localhost nagios-plugins-2.0.1]$ rpm -qa | grep httpd
httpd-tools-2.2.15-45.el6.centos.x86_64
httpd-2.2.15-45.el6.centos.x86_64
#删除自带的apache
[nagios@localhost nagios-plugins-2.0.1]$ sudo rpm -e --nodeps httpd
1
2
3
4
5
1
2
3
4
5
下载apache安装包 官网下载 解压下载的安装包并安装:

#创建文件夹,存到执行脚本配置文件等
sudo mkdir /usr/local/apache2
#解压安装包
tar zxvf httpd-2.2.23.tar.gz
#进入安装路径
cd httpd-2.2.23
#配置值向目录,通第一条命令的目录
./configure --prefix=/usr/local/apache2
#编译
make
#安装
sudo make install
安装php

php安装包下载地址:官网下载,下载后执行以下命令:

#解压安装包
tar zxvf php-5.4.10.tar.gz
#创建文件夹,存到执行脚本配置文件等
sudo mkdir /usr/local/php
#进入解压之后的目录
cd php-5.4.10
#配置指向目录
./configure --prefix=/usr/local/php --with-apxs2=/usr/local/apache2/bin/apxs
执行最后一条语句时,出现如下错误:

这里写图片描述
说明没有安装libxml2和libxml2-devel,安装即可:

#已经存在libxml2,说明只安装libxml2-devel即可
[nagios@localhost php-5.4.10]$ rpm -qa libxml2
libxml2-2.7.6-20.el6.x86_64
#安装libxml2-devel
sudo yum install -y libxml2-devel
执行成功,接续执行:

#编译
make
#安装
make install

最后将安装目录下的php.ini-production文件,拷贝至/usr/local/php/lib/下并命名为php.ini

sudo cp php.ini-production /usr/local/php/lib/php.ini
1
1
配置apache

编辑apache的配置文件httpd.conf

sudo vim /usr/local/ www.yongshiyule178.com apache2/conf/httpd.conf
1
1
找到:

User daemon
Group daemon
1
2
1
2
修改为:

Group nagios
1
1
找到:

<IfModule dir_module>
DirectoryIndex index.html
</IfModule>
1
2
3
1
2
3
修改为:

<IfModule dir_module>
DirectoryIndex index.html index.php
AddType application/x-httpd-php .php
</IfModule>
1
2
3
4
1
2
3
4
为了增加用户的访问权限,在此文件中增加以下配置:

#setting for magion
ScriptAlias /nagios/cgi-bin "/usr/local/nagios/sbin"
<Directory "/usr/local/nagios/sbin">
AuthType Basic
Options ExecCGI
AllowOverride None
Order allow,deny
Allow from www.feifanshifan8.cn 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
创建apache目录验证文件
在上面的配置中,指定了权限验证文件/usr/local/nagios/etc/htpasswd,下面来创建这个文件,最后的nagios是用户名,输入命令后,需要手工输入密码:

/usr/local/apache2/bin/htpasswd -c /usr/local/nagios/etc/htpasswd nagios
1
1
最后启动apache服务:

sudo /usr/local/apache2/bin/apachectl start
1
1
访问地址http://www.jimeiyulept.com yourserver/nagios
这里写图片描述
输入用户名密码
这里写图片描述
出现上述页面,即apache安装成功

配置nagios server

默认情况下,nagios没有做任何配置,那么下面介绍一些主要配置文件和如何配置服务,配置文件主要在目录: /usr/local/nagios/etc/
这里写图片描述
nagios的配置文件,是相互联系的,搞清楚具体的配置,就可以弄清楚他们之间的依赖:

templates.cfg
nagios主要用于监控主机资源以及服务,在nagios配置中称为对象,为了不必重复定义一些监控对象,nagios引入了一个模板配置文件,将一些共性的属性定义成模板,以便于多次引用,这就是templates.cfg的作用,具体的含义如下:
#一般的通用配置,下面具体的某种类型配置中,可以引用次配置
define contact{
name generic-contact ; 别名
service_notification_period 24x7 当服务出现异常时,发送通知的时间段,这个时间段"24x7"在timeperiods.cfg文件中定义
host_notification_period 24x7 ;当主机出现异常时,发送通知的时间段,这个时间段"24x7"在timeperiods.cfg文件中定义
service_notification_options w,u,c,r,f,s ; 什么级别的通知(针对主机),会被发送出去(w:warn|u:unknow|c:criticle紧急状态|r:recover恢复状态|d:宕机),f,s含义还没查到
host_notification_options d,u,r,f,s ;什么级别的警告(针对服务)会被通知出去
service_notification_commands notify-service-by-email ; 服务故障时,发送通知的方式,可以是邮件和短信,这里发送的方式是邮件; 其中“notify-service-by-email”在commands.cfg文件中定义
host_notification_commands www.wmyl15.com notify-host-by-email ; 主机故障时,发送通知的方式,可以是邮件和短信,这里发送的方式是邮件; 其中“notify-host-by-email”在commands.cfg文件中定义
register 0 ; DONT REGISTER THIS DEFINITION - ITS NOT A REAL CONTACT, JUST A TEMPLATE!
}

define host{
name generic-host ; 主机名称,这里的主机名,并不是直接对应到真正机器的主机名 ; 乃是对应到在主机配置文件里所设定的主机名
notifications_enabled 1 ; Host notifications are enabled 主机启动通知,1已启用,0为启用
event_handler_enabled 1 ; Host event handler is enabled
flap_detection_enabled 1 ; Flap detection is enabled
failure_prediction_enabled 1 ; Failure prediction is enabled
process_perf_data 1 ; Process performance data 其值可以为0或1,其作用为是否启用Nagios的数据输出功能;如果将此项赋值为1,那么Nagios就会将收集的数据写入某个文件中,以备提取
retain_status_information 1 ; Retain status information across program restarts
retain_nonstatus_information 1 ; www.wmyl11.com/ Retain non-status information across program restarts
notification_period 24x7 ; Send host notifications at any time 指定“发送通知”的时间段,也就是可以在什么时候发送通知给使用者
register 0 ; DONT REGISTER THIS DEFINITION - ITS NOT A REAL HOST, JUST A TEMPLATE!
}


# 功能同上,只不过这个是针对linux服务器的特殊配置
define host{
name linux-server ; The name of this host template
use generic-host ; This template inherits other values from the generic-host template use表示引用,也就是将主机generic-host的所有属性引用到linux-server中来; 在nagios配置中,很多情况下会用到引用
check_period 24x7 ; By default, Linux hosts are checked round the clock 这里的check_period告诉nagios检查主机的时间段
check_interval 5 ; Actively check the host every 5 minutes 对主机的检查间隔时间,单位/分钟
retry_interval 1 ; Schedule host check retries at 1 minute intervals cho 重试间隔,单位/分钟
max_check_attempts 10 ; Check each Linux host 10 times (max) 最大重试此书
check_command check-host-alive ; Default command to check Linux hosts 指定检查主机状态的命令,其中“check-host-alive”在commands.cfg文件中定义
notification_period workhours ; Linux admins hate to be woken up, so we only notify during the day 主机故障时,发送通知的时间范围,其中“workhours”在timeperiods.cfg中进行了定义
; Note that the notification_period variable is being overridden from
; the www.jiyuannnn.cn/ value that is inherited from the generic-host template!
notification_interval 0 ; Resend notifications every 2 hours 在主机出现异常后,故障一直没有解决,nagios再次对使用者发出通知的时间,0代表值通知1次
notification_options d,u,r ; Only send notifications for specific host states 定义主机在什么状态下可以发送通知给使用者
contact_groups admins ; Notifications get sent to the admins by default 指定联系人组,这个“admins”在contacts.cfg文件中定义
register 0 ; DONT REGISTER THIS DEFINITION - ITS NOT A REAL HOST, JUST A TEMPLATE!
}

# Generic service definition template - This is NOT a real service, just a template!定义服务的通用配置,下面的可以利用use来引用

define service{
name generic-service ; The 'name' of this service template 为这个服务取一个名字
active_checks_enabled 1 ; Active service checks are enabled
passive_checks_enabled 1 ; Passive service checks are enabled/accepted
parallelize_check 1 ; Active service checks should be parallelized (disabling this can lead to major performance problems)
obsess_over_service 1 ; We should obsess over this service (if necessary)
check_freshness 0 ; Default is to NOT check service 'freshness'
notifications_enabled 1 ; Service notifications are enabled
event_handler_enabled 1 ; Service event handler is enabled
flap_detection_enabled 1 ; Flap detection is enabled
failure_prediction_enabled 1 ; Failure prediction is enabled
process_perf_data 1 ; Process performance data
retain_status_information 1 ; Retain status information across program restarts
retain_nonstatus_information 1 ; Retain non-status information across program restarts
is_volatile 0 ; The service is not volatile
check_period 24x7 ; The www.yule8766.com service can be checked at any time of the day 告诉nagios检查服务的时间段
max_check_attempts 3 ; Re-check the service up to 3 times in order to determine its final (hard) state
normal_check_interval 10 ; Check the service every 10 minutes under normal conditions
retry_check_interval 2 ; Re-check the service every two minutes until a hard state can be determined
contact_groups admins ; Notifications get sent out to everyone in the 'admins' group 指定联系人组
notification_options w,u,c,r ; Send notifications about warning, unknown, critical, and recovery events
notification_interval 60 ; Re-notify about service problems every hour
notification_period 24x7 ; Notifications can be sent out at any time
register 0 ; DONT REGISTER THIS DEFINITION - ITS NOT A REAL SERVICE, JUST A TEMPLATE!
}
#定义一个监控本机的service
define service{
name local-service ; The name www.wanmeiyuLept.com of this service template
use generic-service ; Inherit default values from the generic-service definition
max_check_attempts 4 ; Re-check the service up to 4 times in order to determine its final (hard) state
normal_check_interval 5 ; Check the service every 5 minutes under normal conditions
retry_check_www.yuheng119.com/ interval 1 ; Re-check the service every minute until a hard state can be determined
register 0 ; DONT REGISTER THIS DEFINITION - ITS NOT A REAL SERVICE, JUST A TEMPLATE!
}rce.cfg
这个配置文件只有一行:
$USER1$=/usr/local/nagios/libexec
1
1
指定了nagios可执行插件的路径,这个根据自己的实际路径来决定.将来配置插件或者自定义插件,会大量用到这个变量

commands.cfg
这个文件是定义一些命令的时候,需要修改的文件,目前不需要修改默认的配置文件,后面章节中会有添加自定义服务,那里会详细介绍如何修改

hosts.cfg文件
这个文件需要手动创建,在/usr/local/nagios/etc/objects目录下创建,内容如下:

define host{
use linux-server ;引用主机linux-server的属性信息,linux-server主机在templates.cfg文件中进行了定义。
host_name nagios1 ;主机名
alias nagios1 ;主机别名
address 192.168.1.11 ;被监控的主机地址,这个地址可以是ip,也可以是域名。
}

#定义一个主机组
define hostgroup{
hostgroup_name bsmart-servers ;主机组名称,可以随意指定。
alias bsmart servers ;主机组别名
members nagios1 ;主机组成员,其中“Nagios-Linux”就是上面定义的主机。
}
localhost.cfg
这个配置文件是监控nagios server本身的
# 为本机定义一个host,取名为localhost
define host{
use linux-server ; 引用模板配置文件里的内容
; This host definition will inherit all variables that are defined
; in (or inherited by) the linux-server host template definition.
host_name localhost
alias localhost
address 127.0.0.1
}
#定义主机组,成员只有localhost
define hostgroup{
hostgroup_name linux-servers ; www.caihonyule.com The name of the hostgroup
alias Linux Servers ; www.hbwfjx.cn/ Long name of the group
members localhost ; Comma separated list of hosts that belong to this group
}
#配置文件的其他内容都为利用默认的插件,配置的一些服务,也就是localhost需要监控的一些项目
services.cfg
这个文件也需要手工创建, 在/usr/local/ www.jpg521.com nagios/ etc/objects下创建,主要内容为:
define service{
use local-service ;引用local-service服务的属性值,local-service在templates.cfg文件中进行了定义。
host_name nagios1 ;指定要监控哪个主机上的服务,“Nagios-Server”在hosts.cfg文件中进行了定义。
service_description check-host-alive ;对监控服务内容的描述,以供维护人员参考。
check_command check-host-alive ;指定检查的命令。
}

define service{
use local-service
host_name nagios1
service_description Current Load
check_command check_nrpe!check_load
}

define service{
use local-service
host_name nagios1
service_description Check Disk sda1
check_command check_nrpe!check_sda1
}

define service{
use local-service
host_name nagios1
service_description Total Processes
check_command check_nrpe!check_total_procs
}

define service{
use local-service
host_name nagios1
service_description Current Users
check_command check_nrpe!check_users
}

define service{
use local-service
host_name nagios1
service_description Check Zombie Procs
check_command check_nrpe!check_zombie_procs
contacts.cfg
主要配置一些联系人,联系人组:
#联系人
define contact{
contact_name gzd ; Short name of user
use generic-contact ; Inherit default values from generic-contact template (defined above)引用generic-contact的属性信息,其中“generic-contact”在templates.cfg文件中进行定义
alias gzd ; Full name of user
email gzd4215@163.com ;
}
#联系人组,成员只有gzd
define contactgroup{
contactgroup_name admins
alias admins
members gzd
}
timeperiods.cfg
时间的定义文件:
#24*7也就是每天
define timeperiod{
timeperiod_name 24x7
alias 24 Hours A Day, 7 Days A Week
sunday 00:00-24:00
monday 00:00-24:00
tuesday 00:00-24:00
wednesday 00:00-24:00
thursday 00:00-24:00
friday 00:00-24:00
saturday 00:00-24:00
}
# 工作日,工作时间端
define timeperiod{
timeperiod_name workhours
alias Normal Work Hours
monday 09:00-17:00
tuesday 09:00-17:00
wednesday 09:00-17:00
thursday 09:00-17:00
friday 09:00-17:00
}
#美国的节假日,用不到,可以直接删除
define timeperiod{
name us-holidays
timeperiod_name us-holidays
alias U.S. Holidays

january 1 00:00-00:00 ; New Years
monday -1 may 00:00-00:00 ; Memorial Day (last Monday in May)
july 4 00:00-00:00 ; Independence Day
monday 1 september 00:00-00:00 ; Labor Day (first Monday in September)
thursday 4 november 00:00-00:00 ; Thanksgiving (4th Thursday in November)
december 25 00:00-00:00 ; Christmas
cgi.cfg
此文件用来控制相关cgi脚本,如果想在nagios的web监控界面执行cgi脚本,例如重启nagios进程,关闭nagios通知,停止nagios主机检测等,就需要配置cgi.cfg文件了,由于nagios的web监控界面验证用户为nagios,所以只需在cgi.cfg文件中需要修改的配置信息如下:

default_user_name=nagios
authorized_for_system_information=nagiosadmin, nagios
authorized_for_configuration_information=nagiosadmin, nagios
authorized_for_system_commands= nagios
authorized_for_all_services=nagiosadmin, nagios
authorized_for_all_hosts=nagiosadmin, nagios
authorized_for_all_service_commands=nagiosadmin, nagios
authorized_for_all_host_commands=nagiosadmin, nagios
nagios.cfg

nagios的核心配置文件,所有的配置,需要在此定义,才可以生效.
log_file=/usr/local/nagios/var/nagios.log # 定义nagios日志文件的路径
cfg_file=/usr/local/nagios/etc/objects/commands.cfg # “cfg_file”变量用来引用对象配置文件,如果有更多的对象配置文件,在这里依次添加即可。
cfg_file=/usr/local/nagios/etc/objects/contacts.cfg
cfg_file=/usr/local/nagios/etc/objects/hosts.cfg
cfg_file=/usr/local/nagios/etc/objects/services.cfg
cfg_file=/usr/local/nagios/etc/objects/timeperiods.cfg
cfg_file=/usr/local/nagios/etc/objects/templates.cfg
cfg_file=/usr/local/nagios/etc/objects/localhost.cfg # 本机配置文件
cfg_file=/usr/local/nagios/etc/objects/windows.cfg # windows 主机配置文件(不用可以删掉)
object_cache_file=/usr/local/nagios/var/objects.cache # 该变量用于指定一个“所有对象配置文件”的副本文件,或者叫对象缓冲文件
precached_object_file=/usr/local/nagios/var/objects.precache
resource_file=/usr/local/nagios/etc/resource.cfg # 该变量用于指定nagios资源文件的路径,可以在nagios.cfg中定义多个资源文件。
status_file=/usr/local/nagios/var/status.dat # 该变量用于定义一个状态文件,此文件用于保存nagios的当前状态、注释和宕机信息等。
status_update_interval=10 # 该变量用于定义状态文件(即status.dat)的更新时间间隔,单位是秒,最小更新间隔是1秒。
nagios_user=nagios # 该变量指定了Nagios进程使用哪个用户运行。
nagios_group=nagios # 该变量用于指定Nagios使用哪个用户组运行。
check_external_commands=1 # 该变量用于设置是否允许nagios在web监控界面运行cgi命令;
# 也就是是否允许nagios在web界面下执行重启nagios、停止主机/服务检查等操作;
# “1”为运行,“0”为不允许。
command_check_interval=10s # 该变量用于设置nagios对外部命令检测的时间间隔,如果指定了一个数字加一个"s"(如10s);
# 那么外部检测命令的间隔是这个数值以秒为单位的时间间隔;
# 如果没有用"s",那么外部检测命令的间隔是以这个数值的“时间单位”的时间间隔。
interval_length=60 # 该变量指定了nagios的时间单位,默认值是60秒,也就是1分钟 # 即在nagios配置中所有的时间单位都是分钟。
最后验证配置文件是正确:

posted @ 2017-07-01 17:11  王二狗的人生  阅读(400)  评论(0编辑  收藏  举报