nagios3 install

nagios3 ubuntu 10.04下编译安装

1.下载nagios3.21 下载plugins1.4 下载远程监控插件 安装需要的软件包
-----------------------------------------------------------------------
wget http://prdownloads.sourceforge.net/sourceforge/nagios/nagios-3.2.1.tar.gz
wget http://prdownloads.sourceforge.net/sourceforge/nagiosplug/nagios-plugins-1.4.14.tar.gz
wget http://prdownloads.sourceforge.net/sourceforge/nagios/nrpe-2.12.tar.gz
apt-get install libgd2-xpm-dev  libssl-dev php5 libapache2-mod-php5 apache2 gcc
----------------------------------------------------------------------------
# Nagios3.21,Nagios-plugins1.4,nrpe2.12安装在监控服务器上。
# 去http://www.nagios.org/download/addons/下载nrpe和nsclient++插件。
# nrpe2.12安装在Linux/Unix被监控端。
2.创建帐号及组
----------------------
useradd -m nagios
passwd nagios
groupadd nagcmd
usermod -a -G nagcmd nagios
usermod -a -G nagcmd daemon
# daemon为运行apache的帐号。
---------------------------------------
3。解压好nagios3并进入该文件目录
--------------------------------------------------------------------------
./configure --with-command-group=nagcmd --with-gd-lib=/usr/local/libgd/lib/ --with-gd-inc=/usr/local/libgd/include/
make all
# 使用make install来安装主程序,CGI和HTML文件
make install
# 使用make install-init在/etc/rc.d/init.d安装启动脚本
make install-init
# 使用make install-cofig来安装示例配置文件,安装的路径是/usr/local/nagios/etc.
make install-config
# 使用make install-commandmode来配置目录权限
make install-commandmode
-----------------------------------------------------------------------------
nagios目录功能的简要说明:

* bin Nagios执行程序所在目录,nagios文件即为主程序
* etc Nagios配置文件位置
* sbin Nagios Cgi文件所在目录,也就是执行外部命令所需文件所在的目录
* Share Nagios网页文件所在的目录
* var Nagios日志文件、spid 等文件所在的目录
* var/archives 日志归档目录
* var/rw 用来存放外部命令文件
4.配置apache2
将现面行加入apache配置文件的alias模块<IfModule alias_module>
---------------------------------------------------------------
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>
------------------------------------------------------------------
# 创建apache目录验证文件
-------------------------------------------------------------------
/usr/local/apache/bin/htpasswd -c /usr/local/nagios/etc/htpasswd.users nagiosadmin
New password: (输入密码)
Re-type new password: (再输入一次密码)
Adding password for user nagiosadmin
-------------------------------------------------------------------------
#编辑apache2.conf 加入对php网页的支持

AddType application/x-httpd-php .php

# 重启apache:
-------------------
/etc/init.d/apache2 restart
---------------------------
# 配置nagios配置文件
----------------------------------
vi /usr/local/nagios/etc/objects/contacts.cfg
# 将里面的email地址改为自己的email地址。
----------------------------------------------------
5.安装Nagios插件
------------------------------------------------------------
./configure –with-nagios-user=nagios –with-nagios-group=nagios
make
make install
---------------------------------------------------------------
6.Nagios
--------------------------------------------------------
# 配置机器启动时自动启动Nagios
chkconfig –add nagios
chkconfig nagios on
# 检查Nagios配置文件
/usr/local/nagios/bin/nagios -v /usr/local/nagios/etc/nagios.cfg
# 启Nnagios
service nagios start
-----------------------------------------------------------------
7.访问Nagios服务器
-----------------------------------------------------------------------
http://localhost/nagios/
输入用户名及密码登录。
---------------------------------------------------------------------
8.安装nrpe插件,用来监控Linux机器
---------------------------------------------------------------
tar xzvf nrpe-2.12.tar.gz
cd nrpe-2.12
./configure
make all
# 在Nagios服务器端只要安装nrpe监控插件就行
make install-plugin
------------------------------------------------
#在/usr/local/nagios/etc/objects/commands.cfg中定义check_nrpe命令
--------------------------------------------------------
vi /usr/local/nagios/etc/objects/commands.cfg
#加入以下内容
# ‘check_nrpe ‘ command definition
define command{
command_name check_nrpe
command_line $USER1$/check_nrpe -H $HOSTADDRESS$ -c $ARG1$
}
----------------------------------------------------------
9.在被监控服务器(Linux/unix)上安装Nagios-plugins和nrpe
---------------------------------------------------------
useradd nagios
-------------------
# Nagios-plugins默认安装到/usr/local/nagios
如果客户端是UBUNTU 直接运行
-------------------------------------------------------------
apt-get install nagios-nrpe-server  nagios-plguins libssl-dev
文件在 /etc/nagios/nrpe.cfg  修改里面的
allowed_hosts=210.21.67.28,127.0.0.1
可以用逗号分隔多台主机!
/etc/init.d/nagios-nrpe-server restart  启动
-----------------------------------------------------------------
./configure
make
make install
chown nagios.nagios /usr/local/nagios/
chown -R nagios.nagios /usr/local/nagios/libexec/

tar xzvf nrpe-2.12.tar.gz
cd nrpe-2.12
./configure
make all

# 安装nrpe插件,本监控端可以不装
make install-plugin
# 安装nrpe守护进程
make install-daemon
# 安张nrpe配置文件
make install-daemon-config
# 修改nrpe配置文件,允许Nagios监控服务器(192.168.0.19)监控
vi nrpe.cfg
# 多台机器用逗号隔开
allowed_hosts=127.0.0.1,192.168.0.19
# 以独立守护进程启动nrpe,也可以使用xinetd启动nrpe,具体清查看nrpe官方文档。
/usr/local/nagios/bin/nrpe -c /usr/local/nagios/etc/nrpe.cfg -d
# 开机自动启动nrpe
vi /etc/rc.d/rc.local
# 加入下面行
/usr/local/nagios/bin/nrpe -c /usr/local/nagios/etc/nrpe.cfg -d
# 检查nrpe是否安装正常
[root@wiki etc]# /usr/local/nagios/libexec/check_nrpe -H localhost
NRPE v2.12
# 返回nrpe版本说明安装没问题。

# 查看启动端口

[root@wiki ~]# netstat -tunlp
Active Internet connections (only servers)
Proto Recv-Q Send-Q Local Address Foreign Address State PID/Program name
tcp 0 0 0.0.0.0:5666 0.0.0.0:* LISTEN 27387/nrpe
------------------------------------------------------------------
**************************************************************************
注意:我们需要在/usr/local/nagios/etc/nrpe.cfg中定义我们用到的监控本地资源的命令。
下面的命令是默认定义的:
------------------------------------------------------------------------
command[check_users]=/usr/local/nagios/libexec/check_users -w 5 -c 10
command[check_load]=/usr/local/nagios/libexec/check_load -w 15,10,5 -c 30,25,20
command[check_hda1]=/usr/local/nagios/libexec/check_disk -w 20% -c 10% -p /dev/hda1
command[check_zombie_procs]=/usr/local/nagios/libexec/check_procs -w 5 -c 10 -s Z
command[check_total_procs]=/usr/local/nagios/libexec/check_procs -w 150 -c 200
--------------------------------------------------------------------------
下面的命令是自己定义的:
# 监控交换分区的使用情况,使用超过20%时为警告状态,超过10%时为严重状态

command[check_swap]=/usr/local/nagios/libexec/check_swap -w 20% -c 10%

# 监控根分区磁盘使用情况

command[check_disk_root]=/usr/local/nagios/libexec/check_disk -w 20% -c 10% -p /


***************************************************************************************8

NRPE总共由两部分组成:
(1).check_nrpe插件,运行在监控主机上。
(2).NRPE daemon,运行在远程的linux主机上(通常就是被监控机)
按照上图,整个的监控过程如下:
当Nagios需要监控某个远程linux主机的服务或者资源情况时:
1).nagios会运行check_nrpe插件,我们要在nagios配置文件中告诉它要检查什么.
2).check_nrpe插件会通过SSL连接到远程的NRPE daemon.
3).NRPE daemon会运行相应的nagios插件来执行检查本地资源或服务.
4).NRPE daemon将检查的结果返回给check_nrpe插件,插件将其递交给nagios做处理.
注意:NRPE daemon需要nagios插件安装在远程被监控linux主机上,否则,daemon不能做任何的监控.


11.Nagios的配置文件
# 控制cgi访问的配置文件
cgi.cfg
# Nagios主配置文件
nagios.cfg
# resource.cfg定义了一些变量,以便被其他文件引用,如$USER1$
resource.cfg
# objects是一个目录,用于定义Nagios对象
objects
# servers是自己创建的一个目录,Nagios可以加载一个目录下面的所有配置文件(需要在nagios.cfg中配置)
mkdir /usr/local/nagios/etc/servers
./objects:
# 命令定义配置文件,里面定义的命令可以被其他文件引用
commands.cfg
# 联系人和联系人组配置文件
contacts.cfg
# 监控本地机器的配置文件
localhost.cfg
# 监控打印机的一个事例配置文件(默认未启用)
printer.cfg
# 监控路由器的一个事例配置文件(默认未启用)
switch.cfg
# 模板配置文件,在此可以定义模板,在其他文件中引用
templates.cfg
# 定义监控时间段的配置文件
timeperiods.cfg
# 监控Windows的一个事例配置文件(默认未启用)
windows.cfg
servers:目录下的文件为自己创建
# 自己创建的主机群组配置文件
------------------------------------------------
touch /usr/local/nagios/etc/servers/hostgroup.cfg
-------------------------------------------------
# 自己创建的监控远程Linux主机的配置文件
-------------------------------------------
touch /usr/local/nagios/etc/servers/test.cfg
-------------------------------------------------
下面引用配置文件中部分配置做说明:
---------------------------------------
vi /usr/local/nagios/etc/resource.cfg
--------------------------------------
内容如下
# 定义$USER1$变量,设置插件路径
$USER1$=/usr/local/nagios/libexec
---------------------------------------------------
vi /usr/local/nagios/etc/objects/commands.cfg
------------------------------------------------
内容如下
# 定义check-host-alive命令
define command{
command_name check-host-alive # 命令名称
command_line $USER1$/check_ping -H $HOSTADDRESS$ -w 3000.0,80% -c 5000.0,100% -p 5
}
# 上面的$USER1$和$HOSTADDRESS$引用自已定义的配置文件。变量不需现定义才能被引用。
########################################################################
#
# 2008.11.18 add by Stone
# NRPE COMMAND
# 自己定义check_nrpe命令,此命令后接必需接一个参数,用于告诉远程服务器上的NRPE daemon需要监控的内容,如check_swap参数为监控远程机器的交换分区。
########################################################################
# ‘check_nrpe ‘ command definition
define command{
command_name check_nrpe
command_line $USER1$/check_nrpe -H $HOSTADDRESS$ -c $ARG1$
}
---------------------------------------------------------
vi /usr/local/nagios/etc/objects/contacts.cfg  # 定义联系人
---------------------------------------------------------
内容如下:
define contact{
contact_name nagiosadmin ; Short name of user
use generic-contact ; Inherit default values from generic-contact template (defined above)
alias Nagios Admin ; Full name of user

email test@gmaile.com ; <<***** CHANGE THIS TO YOUR EMAIL ADDRESS ******
}
.....................................................................................
# 上面的generic-contact在templates.cfg中定义。
# 定义联系人组
define contactgroup{
contactgroup_name admins
alias Nagios Administrators
members nagiosadmin #在此可以加入多个联系人,中间用逗号隔开
}

vi /usr/local/nagios/etc/objects/timeperiods.cfg
# 定义监控的时间段
define timeperiod{
timeperiod_name 24×7 #监控所有时间段(7*24小时)
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
}

vi /usr/local/nagios/etc/objects/templates.cfg
# 定义generic-contact联系人模板,并非真正的联系人,真正的联系人在contacts.cfg中定义
define contact{
name generic-contact ; The name of this contact template
service_notification_period 24×7 ; service notifications can be sent anytime
host_notification_period 24×7 ; host notifications can be sent anytime
service_notification_options w,u,c,r,f,s ; send notifications for all service states, flapping events, and scheduled downtime events
host_notification_options d,u,r,f,s ; send notifications for all host states, flapping events, and scheduled downtime events
service_notification_commands notify-service-by-email ; send service notifications via email
host_notification_commands notify-host-by-email ; send host notifications via email
register 0 ; DONT REGISTER THIS DEFINITION – ITS NOT A REAL CONTACT, JUST A TEMPLATE!
}
——————————————————————————————————————
service_notification_period 24×7
服务出了状况通知的时间段,这个时间段就是上面在timeperiods.cfg中定义的.
host_notification_period 24×7
主机出了状况通知的时间段, 这个时间段就是上面在timeperiods.cfg中定义的
service_notification_options w,u,c,r
当服务出现w-报警(warning),u-未知(unkown),c-严重(critical),或者r-从异常情况恢复正常,在这四种情况下通知联系人.
host_notification_options d,u,r
当主机出现d—–当机(down),u-返回不可达(unreachable),r-从异常情况恢复正常,在这3种情况下通知联系人
service_notification_commands notify-service-by-email
服务出问题通知采用的命令notify-service-by-email,这个命令是在commands.cfg中定义的,作用是给联系人发邮件.
host_notification_commands notify-host-by-email
同上,主机出问题时采用的也是发邮件的方式通知联系人
——————————————————————————————————————

# 定义generic-host主机模板
define host{
name generic-host ; The name of this host template
notifications_enabled 1 ; Host notifications are enabled
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
retain_status_information 1 ; Retain status information across program restarts
retain_nonstatus_information 1 ; Retain non-status information across program restarts
notification_period 24×7 ; 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
check_period 24×7 ; By default, Linux hosts are checked round the clock
check_interval 5 ; Actively check the host every 5 minutes
retry_interval 1 ; Schedule host check retries at 1 minute intervals
max_check_attempts 10 ; Check each Linux host 10 times (max)
check_command check-host-alive ; Default command to check Linux hosts
notification_period workhours ; Linux admins hate to be woken up, so we only notify during the day
; Note that the notification_period variable is being overridden from
; the value that is inherited from the generic-host template!
notification_interval 120 ; Resend notifications every 2 hours
notification_options d,u,r ; Only send notifications for specific host states
contact_groups admins ; Notifications get sent to the admins by default
register 0 ; DONT REGISTER THIS DEFINITION – ITS NOT A REAL HOST, JUST A TEMPLATE!
}

========================================================================================
开始配置如何监控远程客户端
------------------------------------------------------------------------------------
# 在nagios.cfg配置文件中开启对/usr/local/nagios/etc/servers/中配置文件的引用。
编辑nagios.cfg加入以下内容
cfg_dir=/usr/local/nagios/etc/servers
# 远程Linux主机监控文件,如果监控多台主机只需简单复制修改即可

#我们应该牢记wiki-l-11.cfg用到的命令在commands.cfg中定义,在commands.cfg中定义的命令用到/usr /local/nagios/libexec下的插件(命令)。
vi /usr/local/nagios/etc/servers/test.cfg
------------------------------------------------------
加入以下内容
# 定义主机
define host{
use linux-server ; Name of host template to use
; This host definition will inherit all variables that are defined
; in (or inherited by) the linux-server host template definition.
host_name test
alias Docs
address 210.21.67.27
}
# 定义Ping远程Linux主机
define service{
use generic-service ; Name of service template to use
host_name test
service_description PING
check_command check_ping!100.0,20%!500.0,60% ;check_ping命令在commands.cfg中定义,后跟两个参数,命令及参数间用!分割。
}
#检查远程Linux主机根分区使用情况,check_nrpe命令必须在/usr/local/nagios/etc/objects /commands.cfg中定义(默认未定义)
define service{
use generic-service ; Name of service template to use
host_name test
service_description Root Partition
check_command check_nrpe!check_disk_root
}
# 检查远程Linux主机的登录人数
define service{
use generic-service ; Name of service template to use
host_name test
service_description Current Users
check_command check_nrpe!check_users
}
# 检查远程Linux的主机的负载
define service{
use generic-service ; Name of service template to use
host_name test
service_description Current Load
check_command check_nrpe!check_load
}
# 检查远程Linux主机swap分区使用情况
define service{
use generic-service ; Name of service template to use
host_name test
service_description Swap Usage
check_command check_nrpe!check_swap
}
# 检查远程Linux主机的SSH服务
define service{
use generic-service ; Name of service template to use
host_name test
service_description SSH
check_command check_ssh
notifications_enabled 0
}
# 检查远程Linux主机的HTTP服务
define service{
use generic-service ; Name of service template to use
host_name test
service_description HTTP
check_command check_http
notifications_enabled 0
}

----------------------------------难点
#监控mysql
define service{
use generic-service
host_name wiki
service_description mysql
check_command check_mysql
notifications_enabled 1
}
-------------------------------------------------------
在数据库创建一用户 用户权限最低 只能查询 并且指定主机登录,然后创建一空数据库

--------------------------------------------------------------------
编辑commadns.cfg文件 加入对mysql的监控
#test
define command{
    command_name check_mysql
    command_line /usr/local/nagios/libexec/check_mysql -H $HOSTADDRESS$ -u nagios -p aaa-bbbcc -d nagdb
}

#命令解释  -H 为主机地址,这里使用变量  -u为远程mysql数据库用户 -p为远程数据库密码  -d为远程数据库
测试远程监控 ./check_mysql -H 210.21.67.27 -u nagios -p aaa-bbbcc -d nagdb
出现以下内容则没问题
Uptime: 4761 Threads: 1 Questions: 168 Slow queries: 0 Opens: 139 Flush tables: 1 Open tables: 33 Queries per second avg: 0.35
开始之前mysqlysq只监听127.0.0.1这个地址! 我们需要修改mysql的配置文件
r让他对所有计算机开放注释          #bind-address= 127.0.0.1
然后使用防火墙关闭所有连接! 再开放指定连接
--------------------------------------------------------------------------
--------------------------------------------------------------------------
vi /usr/local/nagios/etc/servers/hostgroup.cfg
-----------------------------------------------------------------------------------
# 定义主机组(localhost.cfg中有类似的主机组设置,我已将其注释掉,否则可能会有冲突)
define hostgroup{
hostgroup_name linux-servers ; The name of the hostgroup
alias Linux Servers ; Long name of the group
members localhost,test ; Comma separated list of hosts that belong to this group
}

注释掉localhost.cfg的类似内容

# 检查配置文件

/usr/local/nagios/bin/nagios -v /usr/local/nagios/etc/nagios.cfg

# 确定无误后重启Nagios:
service nagios restart

监控安装完成!!

但是 我们还需要配置当主机出现down时 发送邮件通知管理员

首先我们下载一个邮件发送脚本!! 这样就无需安装SMTP服务 可给服务器带来一定的稳定与安全

wget http://caspian.dotconf.net/menu/Software/SendEmail/sendEmail-v1.56.tar.gz

解压。得到sendEmail 把这个文件方到之前创建的nagios目录

cp sendEmail /home/nagios

改变sendEmail权限  使nagios也有权限使用!
chomod 777 sendEmail
------------------------------------------
然后运行软件测试发送邮件

/usr/local/bin/sendEmail –f nagios@test.com  –t yahoon@test.com –s mail.test.com –u “from nagios” –xu nagios –xp p#3isoda –m happy
解释:
-f 表示发送者的邮箱
-t 表示接收者的邮箱
-s 表示SMTP服务器的域名或者ip
-u 表示邮件的主题
-xu 表示SMTP验证的用户名
-xp 表示SMTP验证的密码(注意,这个密码貌似有限制,例如我用d!5neyland就不能被正确 识别)
-m 表示邮件的内容
如果你不带-m参数的话,就会提示你自行输入
Reading message body from STDIN because the ‘-m’ option was not used.
If you are manually typing in a message:
- First line must be received within 60 seconds.
- End manual input with a CTRL-D on its own line
输入完成后使用CTRL-D来结束
当然我们也可以将一个文件的内容作为邮件的正文发出去的
那么就可以使用:
cat 文件名 | /usr/local/bin/sendEmail –f nagios@test.com –t yahoon@test.com –s mail.test.com –u “from nagios” –xu nagios –xp p#3isoda
有关sendEmail的用法就讲到这里
既然nagios要使用sendEmail来发警告邮件,那么就要修改commands.cfg中 关于发邮件的命令的定义,我们现在来修改notify-by-email这个命令,如下(注意其中粗体的部分)
# 'notify-by-email' command definition
define command{
    command_name notify-by-email
    command_line /usr/bin/printf "%b" "***** Nagios 2.9 *****/n/nNotification Type: $NOTIFICATIONTYPE$/n/nService: $SERVICEDESC$/nHost: $HOSTALIAS$/nAddress: $HOSTADDRESS$/nState: $SERVICESTATE$/n/nDate/Time: $LONGDATETIME$/n/nAdditional Info:/n/n$SERVICEOUTPUT$" | /home/nagios/sendEmail -f nagios@test.com -t $CONTACTEMAIL$ -s mail.test.com -u "** $NOTIFICATIONTYPE$ alert - $HOSTALIAS$/$SERVICEDESC$ is $SERVICESTATE$ **" -xu nagios -xp p#3isoda
    }

红色部分请根据自己情况修改!!  这个是在UBUNTU下的编译安装! 在其他系统下也应该如此! 配置文件基本相同。重要的是我们要了解每个配置文件以及配置选项的作用。这才能帮我们在不同环境下的独立安装调试.如果不行。请多看看nagioss的日记!!

建议使用139邮箱! 它可免费提供邮件到达短信通知! 这样可免配置短信通道! 一样可以使用手机时刻观察到主机状态

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值