Chinaunix首页 | 论坛 | 博客
  • 博客访问: 536497
  • 博文数量: 142
  • 博客积分: 2966
  • 博客等级: 少校
  • 技术积分: 1477
  • 用 户 组: 普通用户
  • 注册时间: 2009-12-07 22:37
文章分类

全部博文(142)

文章存档

2013年(3)

2012年(21)

2011年(53)

2010年(33)

2009年(32)

分类:

2010-02-08 15:11:33

一、安装支持包

 

Bash代码
  1. #yum -y install gcc gcc-c++ gd gd-devel php httpd openssl-devel   

 

二、安装nagios

创建用户

 

Bash代码
  1. #useradd nagios #passwd nagios  

 

创建组

 

Bash代码
  1. #groupadd nagcmd #usermod -G nagcmd nagios #usermod -G nagcmd apache  

 

安装

Bash代码
  1. #tar xzf nagios-3.2.0.tar.gz    
  2. #cd nagios-3.2.0    
  3. #./configure --with-command-group=nagcmd --prefix=/usr/local/nagios    
  4. #make all    
  5. #make install # 使用make install来安装主程序,CGI和HTML文件    
  6. #make install-init # 使用make install-init在/etc/rc.d/init.d安装启动脚本 #make install-config # 使用make install-cofig来安装示例配置文件,安装的路径是/usr/local/nagios/etc.    
  7. #make install-commandmode # 使用make install-commandmode来配置目录权限  

 

nagios目录功能的简要说明:
bin    Nagios执行程序所在目录,nagios文件即为主程序
etc    Nagios配置文件位置
sbin    Nagios Cgi文件所在目录,也就是执行外部命令所需文件所在的目录
Share    Nagios网页文件所在的目录
var    Nagios日志文件、spid 等文件所在的目录
var/archives    日志归档目录
var/rw    用来存放外部命令文件

配置apache

 

 

Bash代码
  1. #vi /etc/httpd/conf/httpd.conf #加入末尾  

  

 

 

Bash代码
  1. ScriptAlias /nagios/cgi-bin "/usr/local/nagios/sbin"     
  2.      
  3. "/usr/local/nagios/sbin">      
  4. #  SSLRequireSSL      
  5.    Options ExecCGI      
  6.    AllowOverride None      
  7.    Order allow,deny      
  8.    Allow from all      
  9. #  Order deny,allow      
  10. #  Deny from all      
  11. #  Allow from 127.0.0.1     
  12.    AuthName "Nagios Access"     
  13.    AuthType Basic      
  14.    AuthUserFile /usr/local/nagios/etc/htpasswd.users      
  15.    Require valid-user      
  16.       
  17.      
  18. Alias /nagios "/usr/local/nagios/share"     
  19.      
  20. "/usr/local/nagios/share">      
  21. #  SSLRequireSSL      
  22.    Options None      
  23.    AllowOverride None      
  24.    Order allow,deny      
  25.    Allow from all      
  26. #  Order deny,allow      
  27. #  Deny from all      
  28. #  Allow from 127.0.0.1     
  29.    AuthName "Nagios Access"     
  30.    AuthType Basic      
  31.    AuthUserFile /usr/local/nagios/etc/htpasswd.users      
  32.    Require valid-user      
  33.     

 

创建apache目录验证文件

 

Bash代码
  1. #htpasswd -c /usr/local/nagios/etc/htpasswd.users nagiosadmin    
  2. New password: (输入密码)    
  3. Re-type new password: (再输入一次密码)    
  4. Adding password for user nagiosadmin    
  5.   
  6. #service httpd restart  

 

配置nagios配置文件

 

Bash代码
  1. #vi /usr/local/nagios/etc/objects/contacts.cfg # 将里面的email地址改为自己的email地址  

 

三、安装nagios插件

 

Bash代码
  1. #tar xzf nagios-plugins-1.4.13.tar.gz    
  2. #cd nagios-plugins-1.4.13    
  3. #./configure --with-nagios-user=nagios --with-nagios-group=nagios --prefix=/usr/local/nagios    
  4. #make    
  5. #make install  

 

四、启动nagios

添加服务

 

Bash代码
  1. #chkconfig --add nagios    
  2. #chkconfig nagios on  

 

检查配置文件

 

Bash代码
  1. #/usr/local/nagios/bin/nagios -v /usr/local/nagios/etc/nagios.cfg  

 

启动nagios

 

Bash代码
  1. #service nagios start  

 

启动apache

 

Bash代码
  1. #service httpd start  

 

访问nagios 输入用户名密码

 

五、安装nrpe

Bash代码
  1. #tar xzvf nrpe-2.12.tar.gz    
  2. #cd nrpe-2.12    
  3. #./configure    
  4. #make all    
  5. #make install-plugin #服务器端只要安装nrpe监控插件就行  

 

在/usr/local/nagios/etc/objects/commands.cfg中定义check_nrpe命令

 

Bash代码
  1. #vi /usr/local/nagios/etc/objects/commands.cfg  

 

 

 

Commands.cfg代码
  1. 'check_nrpe ' command definition   
  2. define command{   
  3.         command_name check_nrpe   
  4.         command_line $USER1$/check_nrpe -H $HOSTADDRESS$ -c $ARG1$   
  5.         }  

  

六、在被监控机上安装nagios插件和nrpe

 

Bash代码
  1. #useradd nagios      
  2. #tar xzvf nagios-plugins-1.4.13.tar.gz      
  3. #cd nagios-plugins-1.4.13     
  4. #./configure      
  5. #make      
  6. #make install      
  7. #chown nagios.nagios /usr/local/nagios/      
  8. #chown -R nagios.nagios /usr/local/nagios/libexec/      
  9.      
  10.        
  11.      
  12. #tar xzvf nrpe-2.12.tar.gz      
  13. #cd nrpe-2.12     
  14. #./configure      
  15. #make all             
  16. #make install-plugin         #安装nrpe插件,本监控端可以不装            
  17. #make install-daemon        # 安装nrpe守护进程             
  18. #make install-daemon-config         #安装nrpe配置文件    

 

Bash代码
  1. #vi /usr/local/nagios/etc/nrpe.cfg        #修改nrpe配置文件,允许Nagios监控服务器(192.168.0.19)监控  

 

Nrpe.cfg代码
  1. allowed_hosts=127.0.0.1,192.168.0.19        #多台机器用逗号隔开  

  

Bash代码
  1. #/usr/local/nagios/bin/nrpe -c /usr/local/nagios/etc/nrpe.cfg -d        以独立守护进程启动nrpe,也可以使用xinetd启动nrpe,具体清查看nrpe官方文档。      
  2.      
  3. #cat >> /etc/rc.d/rc.local << EOF               #开机自动启动nrpe      
  4. /usr/local/nagios/bin/nrpe -c /usr/local/nagios/etc/nrpe.cfg -d      
  5.      
  6. EOF      
  7.      
  8. # /usr/local/nagios/libexec/check_nrpe -H localhost        #检查nrpe是否安装正常      
  9. NRPE v2.12     
  10.      
  11. #netstat -tln        #查看端口      
  12.      
  13. tcp        0      0 0.0.0.0:5666                0.0.0.0:*   

 

我们需要在/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%时为严重状态

 

Nrpe.cfg代码
  1. command[check_swap]=/usr/local/nagios/libexec/check_swap -w 20% -c 10%  


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

 

 

Java代码
  1. command[check_disk_root]=/usr/local/nagios/libexec/check_disk -w 20% -c 10% -p /  

  

七、nagios监控流程

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不能做任何的监控.

八、nagios配置文件

#控制cgi访问的配置文件
cgi.cfg
#Nagios主配置文件
nagios.cfg
#resource.cfg定义了一些变量,以便被其他文件引用,如$USER1$
resource.cfg
# objects是一个目录,用于定义Nagios对象
objects
# servers是自己创建的一个目录,Nagios可以加载一个目录下面的所有配置文件(需要在nagios.cfg中配置)
servers

./objects:
#命令定义配置文件,里面定义的命令可以被其他文件引用
commands.cfg
#联系人和联系人组配置文件
contacts.cfg
#监控本地机器的配置文件
localhost.cfg
#监控打印机的一个事例配置文件(默认未启用)
printer.cfg
#监控路由器的一个事例配置文件(默认未启用)
switch.cfg
#模板配置文件,在此可以定义模板,在其他文件中引用
templates.cfg
#定义监控时间段的配置文件
timeperiods.cfg
#监控Windows的一个事例配置文件(默认未启用)
windows.cfg

./servers:
#自己创建的主机群组配置文件
hostgroup.cfg
#自己创建的监控远程Linux主机的配置文件
sectop.cfg

配置文件是怎样引用的?
 

nagios主要是监控一台主机的各种信息,包括本机资源以及对外的服务等等.这些在nagios里面都是被定义为一个个的项目(nagios称之为服务,为了与主机提供的服务相区别,我这里用项目这个词),而实现每个监控项目,则需要通过commands.cfg文件中定义的命令。
为了不必重复定义一些项目,Nagios引入了一个模板配置文件(templates.cfg),将一些共性的属性定义成模板,以便于多次引用。
我们现在有一个监控项目是监控一台机器的web服务是否正常, 我们需要哪些元素呢?最重要的有下面三点:首先是监控哪台机器,然后是这个监控要用什么命令实现,最后就是出了问题的时候要通知哪个联系人。


我们首先应该在commands.cfg中定义监控远程服务和资源的命令,以及如何发送邮件的命令。大部分监控远程服务和资源的命令的命令通过/usr/local/nagios/libexec下的脚本实现,如ping命令为check_ping。
/usr/local/nagios/libexec下的脚本命令的使用发法可以通过-h参数查看

然后我们在contacts.cfg文件中定义联系人和联系人组,在timeperiods.cfg中定义监控时间段。

最后我们在服务器监控配置文件中引用前面定义的元素来监控服务器状态。

下面引用配置文件中部分配置做说明:

 

Bash代码
  1. #vi /usr/local/nagios/etc/resource.cfg  

 

 

Bash代码
  1. #定义$USER1$变量,设置插件路径 $USER1$=/usr/local/nagios/libexec  

 

 

Bash代码
  1. #vi /usr/local/nagios/etc/objects/commands.cfg  

 

 

Command.cfg代码
  1. #定义check-host-alive命令      
  2. define command{      
  3.         command_name    check-host-alive  #命令名称      
  4.      command_line    $USER1$/check_ping -H $HOSTADDRESS$ -w 3000.0,80% -c 5000.0,100% -p 5     
  5.         }      
  6. #上面的$USER1$和$HOSTADDRESS$引用自已定义的配置文件。变量不需现定义才能被引用。      
  7.      
  8. 'check_nrpe ' command definition      
  9. define command{      
  10.         command_name check_nrpe      
  11.         command_line $USER1$/check_nrpe -H $HOSTADDRESS$ -c $ARG1$      
  12.         }    

 

 

Bash代码
  1. #vi /usr/local/nagios/etc/objects/contacts.cfg  

 

 

Contacts.cfg代码
  1. #定义联系人      
  2. define contact{      
  3.         contact_name                    nagiosadmin             ; Short name of user      
  4.         use                             generic-contact         ; Inherit default values from generic-contact template (defined above)      
  5.         alias                           Nagios Admin            ; Full name of user      
  6.      
  7.         email                           test@gmaile.com         ; <<***** CHANGE THIS TO YOUR EMAIL ADDRESS ******      
  8.         }      
  9. #上面的generic-contact在templates.cfg中定义。      
  10. #定义联系人组      
  11. define contactgroup{      
  12.         contactgroup_name       admins      
  13.         alias                   Nagios Administrators      
  14.         members                 nagiosadmin   #在此可以加入多个联系人,中间用逗号隔开      
  15.         }    

 

 

Bash代码
  1. #vi /usr/local/nagios/etc/objects/timeperiods.cfg  

 

 

Timeperiods.cfg代码
  1. #定义监控的时间段      
  2. define timeperiod{      
  3.         timeperiod_name 24x7   #监控所有时间段(724小时)      
  4.         alias           24 Hours A Day, 7 Days A Week      
  5.         sunday          00:00-24:00     
  6.         monday          00:00-24:00     
  7.         tuesday         00:00-24:00     
  8.         wednesday       00:00-24:00     
  9.         thursday        00:00-24:00     
  10.         friday          00:00-24:00     
  11.         saturday        00:00-24:00     
  12.         }    

 

 

Bash代码
  1. #vi /usr/local/nagios/etc/objects/templates.cfg  

 

 

Templates.cfg代码
  1. #定义generic-contact联系人模板,并非真正的联系人,真正的联系人在contacts.cfg中定义      
  2. define contact{      
  3.         name                            generic-contact               
  4.         service_notification_period     24x7                         
  5.         host_notification_period        24x7                         
  6.         service_notification_options    w,u,c,r,f,s                   
  7.         host_notification_options       d,u,r,f,s                    
  8.         service_notification_commands   notify-service-by-email       
  9.         host_notification_commands      notify-host-by-email          
  10.         register                        0                             
  11.         }    


------------------------------------------------------------------------------------------------------------------
service_notification_period     24x7
服务出了状况通知的时间段,这个时间段就是上面在timeperiods.cfg中定义的.
host_notification_period        24x7
主机出了状况通知的时间段, 这个时间段就是上面在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
同上,主机出问题时采用的也是发邮件的方式通知联系人
------------------------------------------------------------------------------------------------------------------

 

Templates.cfg代码
  1. #定义generic-host主机模板      
  2. define host{      
  3.         name                            generic-host    ; The name of this host template      
  4.         notifications_enabled           1               ; Host notifications are enabled      
  5.         event_handler_enabled           1               ; Host event handler is enabled      
  6.         flap_detection_enabled          1               ; Flap detection is enabled      
  7.         failure_prediction_enabled      1               ; Failure prediction is enabled      
  8.         process_perf_data               1               ; Process performance data      
  9.         retain_status_information       1               ; Retain status information across program restarts      
  10.         retain_nonstatus_information    1               ; Retain non-status information across program restarts      
  11.         notification_period             24x7            ; Send host notifications at any time      
  12.         register                        0               ; DONT REGISTER THIS DEFINITION - ITS NOT A REAL HOST, JUST A TEMPLATE!      
  13.         }      
  14.      
  15. #定义Linux主机模板      
  16. define host{      
  17.         name                            linux-server    ; The name of this host template      
  18.         use                             generic-host    ; This template inherits other values from the generic-host template      
  19.         check_period                    24x7            ; By default, Linux hosts are checked round the clock      
  20.         check_interval                  5               ; Actively check the host every 5 minutes      
  21.         retry_interval                  1               ; Schedule host check retries at 1 minute intervals      
  22.         max_check_attempts              10              ; Check each Linux host 10 times (max)      
  23.         check_command                   check-host-alive ; Default command to check Linux hosts      
  24.         notification_period             workhours       ; Linux admins hate to be woken up, so we only notify during the day      
  25.                                                         ; Note that the notification_period variable is being overridden from      
  26.                                                         ; the value that is inherited from the generic-host template!      
  27.         notification_interval           120             ; Resend notifications every 2 hours      
  28.         notification_options            d,u,r           ; Only send notifications for specific host states      
  29.         contact_groups                  admins          ; Notifications get sent to the admins by default     
  30.         register                        0               ; DONT REGISTER THIS DEFINITION - ITS NOT A REAL HOST, JUST A TEMPLATE!      
  31.         }    


#在nagios.cfg配置文件中开启对/usr/local/nagios/etc/servers/中配置文件的引用。

 

Nagios.cfg代码
  1. cfg_dir=/usr/local/nagios/etc/servers  


#远程Linux主机监控文件,如果监控多台主机只需简单复制修改即可。
#我们应该牢记sectop.cfg用到的命令在commands.cfg中定义,在commands.cfg中定义的命令用到/usr/local/nagios/libexec下的插件(命令)。

 

Bash代码
  1. #vi /usr/local/nagios/etc/servers/sectop.cfg  

 

 

Sectop.cfg代码
  1. #定义主机      
  2. define host{      
  3.         use                     linux-server            ; Name of host template to use      
  4.                                                         ; This host definition will inherit all variables that are defined      
  5.                                                         ; in (or inherited by) the linux-server host template definition.      
  6.         host_name               sectop      
  7.         alias                   Docs      
  8.         address                 192.168.99.200     
  9.         }      
  10. #定义Ping远程Linux主机      
  11. define service{      
  12.         use                             generic-service         ; Name of service template to use      
  13.         host_name                       sectop      
  14.         service_description             PING      
  15.         check_command                   check_ping!100.0,20%!500.0,60%   ;check_ping命令在commands.cfg中定义,后跟两个参数,命令及参数间用!分割。      
  16.         }      
  17. #检查远程Linux主机根分区使用情况,check_nrpe命令必须在/usr/local/nagios/etc/objects/commands.cfg中定义(默认未定义)      
  18. define service{      
  19.         use                             generic-service         ; Name of service template to use      
  20.         host_name                       sectop      
  21.         service_description             Root Partition      
  22.         check_command                   check_nrpe!check_disk_root      
  23.         }      
  24. #检查远程Linux主机的登录人数      
  25. define service{      
  26.         use                             generic-service         ; Name of service template to use      
  27.         host_name                       sectop      
  28.         service_description             Current Users      
  29.         check_command                   check_nrpe!check_users      
  30.         }      
  31. #检查远程Linux的主机的负载      
  32. define service{      
  33.         use                             generic-service         ; Name of service template to use      
  34.         host_name                       sectop      
  35.         service_description             Current Load      
  36.         check_command                   check_nrpe!check_load      
  37.         }      
  38. #检查远程Linux主机swap分区使用情况      
  39. define service{      
  40.         use                             generic-service         ; Name of service template to use      
  41.         host_name                       sectop      
  42.         service_description             Swap Usage      
  43.         check_command                   check_nrpe!check_swap      
  44.         }      
  45. #检查远程Linux主机的SSH服务      
  46. define service{      
  47.         use                             generic-service         ; Name of service template to use      
  48.         host_name                       sectop      
  49.         service_description             SSH      
  50.         check_command                   check_ssh      
  51.         notifications_enabled           0     
  52.         }      
  53. #检查远程Linux主机的HTTP服务      
  54. define service{      
  55.         use                             generic-service         ; Name of service template to use      
  56.         host_name                       sectop      
  57.         service_description             HTTP      
  58.         check_command                   check_http      
  59.         notifications_enabled           0     
  60.         }    

 

 

Bash代码
  1. #vi /usr/local/nagios/etc/servers/hostgroup.cfg  

 

 

Hostgroup.cfg代码
  1. #定义主机组(localhost.cfg中有类似的主机组设置,我已将其注释掉,否则可能会有冲突)      
  2. define hostgroup{      
  3.         hostgroup_name  linux-servers ; The name of the hostgroup      
  4.         alias           Linux Servers ; Long name of the group      
  5.         members         localhost,sectop     ; Comma separated list of hosts that belong to this group      
  6.         }      
  7.      
  8. #define hostgroup{      
  9. #        hostgroup_name  windows-servers ; The name of the hostgroup      
  10. #        alias           Windows Servers ; Long name of the group      
  11. #        members         print     ; Comma separated list of hosts that belong to this group      
  12. #        }    


===============================================================================
#完成监控主机配置文件的配置后使用下面命令检查配置文件的正确性:

Bash代码
  1. #/usr/local/nagios/bin/nagios -v /usr/local/nagios/etc/nagios.cfg  

 

 

#确定无误后重启Nagios

Bash代码
  1. #service nagios restart  
阅读(1173) | 评论(0) | 转发(0) |
0

上一篇:mysql常用管理命令

下一篇:MySQL-python安装

给主人留下些什么吧!~~