Nagios+pnp4nagios+rrdtool 安装配置nagios(一)

基于的软件版本

Apache-2.0.63  php-5.3.2 nagios-3.2.3  nagios-plugins-1.4.15  rrdtool-1.4.5 nrpe-2.12

pnp4nagios-0.4.13 check_mysql_health

 

服务器端

1、安装前准备

1)创建nagios用户和用户组
[root@localhost ~]#useradd -s /sbin/nologin nagios 

[root@localhost ~]#mkdir /usr/local/nagios

[root@localhost ~]# chown –R  nagios.nagios/usr/local/nagios

2)开启系统sendmail服务 (自行选择)
 
nagios监控服务器上开启sendmail服务的主要作用是让nagios在检测到故障时可以发送报警邮件,目前几乎所有的linux发行版本都默认自带了sendmail服务,所以,在安装系统时只需开启sendmail服务即可,并且不需要在sendmail上做任何配置。

2编译安装Nagios
[root@localhost ~]# tar -zxvf nagios-3.2.3.tar.gz
[root@localhost ~]# cd nagios-3.2.3
[root@localhost nagios-3.2.3]#./configure --prefix=/usr/local/nagios  


#
指定nagios的安装目录,这里指定nagios安装到/usr/local/nagios目录
[root@localhost nagios-3.2.3]#make all  
[root@localhost nagios-3.2.3]#make install   

    
# make install
用来安装nagios的主程序,CGIHTML文件
[root@localhost nagios-3.2.3]# make install-init   
#
通过make install-init命令可以在/etc/rc.d/init.d目录下创建nagios启动脚本
[root@localhost nagios-3.2.3]# make install-commandmode  
#
通过make install-commandmode命令来配置目录权限
[root@localhost nagios-3.2.3]# make install-config   
#make install-cofig
命令用来安装nagios示例配置文件,这里安装的路径是/usr/local/nagios/etc

3Nagios目录介绍

目录对应的功能说明如下:

 

bin:可执行程序所在目录

 

etc:配置文件所在目录

 

sbin:cgi文件所在目录,也就是执行外部命令所需文件所在目录

 

share:网页文件所在目录

 

libexec:外部插件所在目录

 

var:日志文件,lock等文件所在目录

 

var/archives:日志自动归档目录

 

var/rw:用来存放外部命令文件的目录

4、 安装Nagios插件


 
这里下载的版本是nagios-plugins-1.4.15
 
注意:插件版本与nagios版本的关联并不大。
[root@localhost nagios]#tar –zxvf  nagios-plugins-1.4.15.tar.gz
[root@localhost nagios]#cd  nagios-plugins-1.4.15
[root@localhost nagios-plugins-1.4.15]#./configure --prefix=/usr/local/nagios
[root@localhost nagios-plugins-1.4.15]# make 
[root@localhost nagios-plugins-1.4.15]# make install
 
安装完成,在/usr/local/nagios下的libexec目录下,生成很多可执行文件,这些正是nagios所需要的插件。

5、安装与配置apachephp
 apache
php不是安装nagios所必须的,但是nagios提供了web监控界面,通过web监控界面可以清晰的看到被监控主机、资源的运行状态,因此,安装一个web服务是很必要的。
 
需要注意的是,nagiosnagios3.1.x版本以后,配置web监控界面时需要php的支持。这里我们下载的nagios版本为nagios-3.2.3,因此在编译安装完成apache后,还需要编译php模块,这里选取的php版本为php5.3.2
1)安装apachephp
首先安装apache,步骤如下:
[root@nagiosserver ~]# tar zxvf httpd-2.0.63.tar.gz
[root@nagiosserver ~]#cd httpd-2.0.63

注意:在64位机器上安装需要加参数--witn-expat=bulitin,否则make时会报错 /usr/lib/libexpat.so: could not read symbols: File in wrong format  collect2: ld returned 1 exit status。在32位机器上直接./configure --prefix=/usr/local/apache2 就可以

 [root@nagiosserver ~]#./configure --prefix=/usr/local/apache2 --enable-modules=so--enable-rewrite --with-expat=builtin

 [root@nagiosserver ~]#make
[root@nagiosserver ~]#make install
接着安装php,步骤如下:
[root@nagiosserver ~]# tar zxvf php-5.3.2.tar.gz
[root@nagiosserver ~]#cd php-5.3.2

注意php的编译参数,如果需要pnp图表显示,请看pnp安装时的注意事项,编译参数不能用下面的;如果不用pnp图表直接用下面的就可以

(./configure  --prefix=/usr/local/php--with-apxs2=/usr/local/apache2/bin/apxs --with-zlib-dir=/usr/include/zlib.h--with-gd)
[root@nagiosserver~]#./configure  --prefix=/usr/local/php  --with-apxs2=/usr/local/apache2/bin/apxs

64位系统上编译php 如果如果出现如下错误

configure: error: libjpeg.(a|so) not found

解决方法:

来源 https://bugs.php.net/bug.php?id=33685

解决方式:

# ln -s /usr/lib64/libjpeg.so /usr/lib/

# ln -s /usr/lib64/libpng.so /usr/lib/

然后继续编绎安装即可!
[root@nagiosserver ~]#make
[root@nagiosserver ~]#make install
从安装步骤可知,apache安装路径为/usr/local/apache2,而php安装路径为/usr/local/php

(2)配置apache(apache版本不同,修改可能会不同)
找到apache配置文件/usr/local/apache2/conf/httpd.conf 
找到:
User nobody
Group #-1
修改为
User nagios
Group nagios
然后找到
DirectoryIndex index.html index.html.var
修改为
DirectoryIndex index.html index.php
接着增加如下内容:注意下面的 .php,前面有空格
AddType application/x-httpd-php .php

为了安全其间,一般情况下要让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>

(3)创建apache目录验证文件
  在上面的配置中,指定了目录验证文件htpasswd,下面要创建这个文件:
 [root@localhost nagios]#/usr/local/apache2/bin/htpasswd -c/usr/local/nagios/etc/htpasswd nagios     
 New password: (输入密码)     
 Re-type new password: (再输入一次密码)     
 Adding password for user ixdba
 这样就在/usr/local/nagios/etc目录下创建了一个htpasswd验证文件,当通过http://ip/nagios/访问时就需要输入用户名和密码了。
 最后,启动服务: 
 [root@ nagiosserver ~]#/usr/local/apache2/bin/apachectl  start

启动服务可能会出现如下错误:

  httpd: Could notdetermine the server's fully qualified domain name, using 127.0.0.1 forServerName

解决很简单 在httpd.conf里有一段 #ServerName www.example.com:80去掉#号就可以 

一、nagios配置过程详解
1
nagios默认配置文件介绍
 nagios
安装完毕后,默认的配置文件在/usr/local/nagios/etc目录下

2、配置文件之间的关系

 nagios的配置过程中涉及到的几个定义有:主机、主机组,服务、服务组,联系人、联系人组,监控时间,监控命令等,从这些定义可以看出,nagios各个配置文件之间是互为关联,彼此引用的。
 
成功配置出一台nagios监控系统,必须要弄清楚每个配置文件之间依赖与被依赖的关系,最重要的有四点:
 
第一:定义监控哪些主机、主机组、服务和服务组
 
第二:定义这个监控要用什么命令实现,
 
第三:定义监控的时间段,
 
第四:定义主机或服务出现问题时要通知的联系人和联系人组。

 

3、开始配置nagios
 
为了能更清楚的说明问题,同时也为了维护方便,建议将nagios各个定义对象创建独立的配置文件:
 
即为:
 
创建hosts.cfg文件来定义主机和主机组
 
创建services.cfg文件来定义服务
 
用默认的contacts.cfg文件来定义联系人和联系人组
 
用默认的commands.cfg文件来定义命令
 
用默认的timeperiods.cfg来定义监控时间段
 
用默认的templates.cfg文件作为资源引用文件

  1templates.cfg文件
 
 nagios
主要用于监控主机资源以及服务,在nagios配置中称为对象,为了不必重复定义一些监控对象,Nagios引入了一个模板配置文件,将一些共性的属性定义成模板,以便于多次引用。这就是templates.cfg的作用。
 
下面详细介绍下templates.cfg文件中每个参数的含义:

  1.  define contact{  
  2.         name                            generic-contact        #联系人名称,  
  3.         service_notification_period     24x7                   #当服务出现异常时,发送通知的时间段,这个时间段“7x24"在timeperiods.cfg文件中定义  
  4.         host_notification_period        24x7                   #当主机出现异常时,发送通知的时间段,这个时间段“7x24"在timeperiods.cfg文件中定义  
  5.         service_notification_options    w,u,c,r         #这个定义的是“通知可以被发出的情况”。w即warn,表示警告状态,u即unknown,表示不明状态,c即criticle,表示紧急状态,r即recover,表示恢复状态。也就是在服务出现警告状态、未知状态、紧急状态和重新恢复状态时都发送通知给使用者。  
  6.         host_notification_options       d,u,r         #定义主机在什么状态下需要发送通知给使用者,d即down,表示宕机状态,u即unreachable,表示不可到达状态,r即recovery,表示重新恢复状态。  
  7.         service_notification_commands   notify-service-by-email  #服务故障时,发送通知的方式,可以是邮件和短信,这里发送的方式是邮件,其中“notify-service-by-email”在commands.cfg文件中定义。  
  8.         host_notification_commands      notify-host-by-email     #主机故障时,发送通知的方式,可以是邮件和短信,这里发送的方式是邮件,其中“notify-host-by-email”在commands.cfg文件中定义。  
  9.         register                        0  
  10.         }  
  11.  define host{   
  12.         name                            generic-host    #主机名称,这里的主机名,并不是直接对应到真正机器的主机名,乃是对应到在主机配置文件里所设定的主机名。  
  13.         notifications_enabled           1              
  14.         event_handler_enabled           1               
  15.         flap_detection_enabled          1               
  16.         failure_prediction_enabled      1             
  17.         process_perf_data               1              
  18.         retain_status_information       1             
  19.         retain_nonstatus_information    1                
  20.         notification_period             24x7           #指定“发送通知”的时间段,也就是可以在什么时候发送通知给使用者。  
  21.         register                        0                
  22.         }  
  23.  
  24. 主机各参数意义
  25.  
  26. define host{  
  27.         name                            linux-server    #主机名称  
  28.         use                             generic-host    #use表示引用,也就是将主机generic-host的所有属性引用到linux-server中来,在nagios配置中,很多情况下会用到引用。  
  29.         check_period                    24x7            #这里的check_period告诉nagios检查主机的时间段  
  30.         check_interval                  5                #nagios对主机的检查时间间隔,这里是5分钟。  
  31.         retry_interval                  1               #重试检查时间间隔,单位是分钟。  
  32.         max_check_attempts              10               #nagios对主机的最大检查次数,也就是nagios在检查发现某主机异常时,并不马上判断为异常状况,而是多试几次,因为有可能只是一时网络太拥挤,或是一些其他原因,让主机受到了一点影响,这里的10就是至少试10次的意思。  
  33.         check_command                   check-host-alive  #指定检查主机状态的命令,其中“check-host-alive”在commands.cfg文件中定义。  
  34.         notification_period             workhours      #主机故障时,发送通知的时间范围,其中“workhours”在timeperiods.cfg中进行了定义,下面会陆续讲到。  
  35.                                                          
  36.         notification_interval           120            #在主机出现异常后,故障一直没有解决,nagios再次对使用者发出通知的时间。单位是分钟。如果你觉得,所有的事件只需要一次通知就够了,可以把这里的选项设为0  
  37.         notification_options            d,u,r          #定义主机在什么状态下可以发送通知给使用者,d即down,表示宕机状态,u即unreachable,表示不可到达状态,r即recovery,表示重新恢复状态。  
  38.         contact_groups                  admins         #指定联系人组,这个“admins”在contacts.cfg文件中定义。  
  39.         register                        0  
  40.         }  
  41.  
  42. 定义服务各参数意义
  43.  
  44. define service{  
  45.         name                            generic-service   #定义一个服务名称  
  46.         active_checks_enabled           1     
  47.         passive_checks_enabled          1      
  48.         parallelize_check                1      
  49.         obsess_over_service             1      
  50.         check_freshness                0  
  51.         notifications_enabled            1                 
  52.         event_handler_enabled           1                   
  53.         flap_detection_enabled           1                    
  54.         failure_prediction_enabled        1                   
  55.         process_perf_data               1                   
  56.         retain_status_information       1                     
  57.         retain_nonstatus_information    1                        
  58.         is_volatile                     0   
  59.         check_period                    24x7      #这里的check_period告诉nagios检查服务的时间段。              
  60.         max_check_attempts              3         #nagios对服务的最大检查次数。           
  61.         normal_check_interval           10       #此选项是用来设置服务检查时间间隔,也就是说,nagios这一次检查和下一次检查之间所隔的时间,这里是10分钟。   
  62.         retry_check_interval            2        #重试检查时间间隔,单位是分钟。            
  63.         contact_groups                  admins   #指定联系人组,同上。              
  64.         notification_options            w,u,c,r  #这个定义的是“通知可以被发出的情况”。w即warn,表示警告状态,u即unknown,表示不明状态,c即criticle,表示紧急状态,r即recover,表示恢复状态。也就是在服务出现警告状态、未知状态、紧急状态和重新恢复后都发送通知给使用者。  
  65.         notification_interval           60       #在服务出现异常后,故障一直没有解决,nagios再次对使用者发出通知的时间。单位是分钟。如果你认为,所有的事件只需要一次通知就够了,可以把这里的选项设为0。  
  66.         notification_period             24x7     #指定“发送通知”的时间段,也就是可以在什么时候发送通知给使用者。               
  67.          register                        0                       
  68.         }  
  69.  


(2)resource.cfg文件

 resource.cfg是nagios的变量定义文件,文件内容只有一行:
 

  1. $USER1$=/usr/local/nagios/libexec 


 其中,变量$USER1$指定了安装nagios插件的路径,如果把插件安装在了其它路径,只需在这里进行修改即可。需要注意的是,变量必须先定义,然后才能在其它配置文件中进行引用。

(3)理解Nagios宏及其工作机制

Nagios配置非常灵活,继承和引用是一大特征,另一个重要特征就是可以在命令行的定义里使用宏,通过定义宏,nagios可以灵活的获取主机、服务和其它对象的信息。
宏的工作机制
在执行命令之前,nagios将对命令里的每个宏替换成它们应当取得的值。这种宏替换发生在Nagios执行各种类型的宏时候。例如主机和服务的检测、通知、事件处理等。
 

宏的分类:
默认宏、按需而成的宏、用户自定制宏等。
 

默认宏
主机IP地址宏
当在命令定义中使用主机或服务宏时,宏将要执行所用的值指向主机或服务所带有值。看下面这个例子,假定在check_ping命令定义里使用了一个主机对象,例如这样:

  1. define host{  
  2.   host_name               ixdba  
  3.   address   192.168.12.246  
  4.   check_command               check_ping  
  5.   ...  
  6.   }  
  7. define command{  
  8.   command_name    check_ping  
  9.   command_line    /usr/local/nagios/libexec/check_ping -H $HOSTADDRESS$ -w 100.0,90% -c 200.0,60%  
  10.   } 


那么执行这个主机检测命令时展开并最终执行的将是这样的:

  1. /usr/local/nagios/libexec/check_ping -H 192.168.12.246 -w 100.0,90% -c 200.0,60% 


命令参数宏
同样你可以向命令传递参数,这样可以保证命令定义更具通用性。参数指定在对象(象主机或服务)中定义,用一个“!”来分隔,例如这样:

  1. define service{  
  2.   host_name          linuxbox  
  3.   service_description PING  
  4.   check_command      check_ping!200.0,80%!400.0,40%  
  5.   ...  
  6.   } 


在上例中,服务的检测命令中含有两个参数(请参考$ARGn$宏),而$ARG1$宏将是"200.0,80%",同时$ARG2$将是"400.0,40%"(都不带引号)。假定使用之前的主机定义并这样来定义你的check_ping命令:

  1. define command{  
  2.   command_name    check_ping  
  3.   command_line    /usr/local/nagios/libexec/check_ping -H $HOSTADDRESS$ -w $ARG1$ -c $ARG2$  
  4.   } 


那么对于服务的检测命令最终将是这样子的:

  1. /usr/local/nagios/libexec/check_ping -H 192.168.12.246 -w 200.0,80% -c 400.0,40% 

 

Nagios可用的全部的宏

主机宏
$HOSTNAME$  主机简称(如"web"),取自于主机定义里的host_name域
$HOSTADDRESS$  主机地址。取自于主机定义里的address域
服务宏
$SERVICESTATE$ 服务状态描述,有w,u,c
$SERVICEDESC$   对当前服务的描述  
联系人宏
$CONTACTNAME$   表示联系人,在联系人文件中定义
通知宏
$NOTIFICATIONTYPE$    返回下面信息:("PROBLEM","RECOVERY", "ACKNOWLEDGEMENT", "FLAPPINGSTART","FLAPPINGSTOP", "FLAPPINGDISABLED","DOWNTIMESTART", "DOWNTIMEEND", or"DOWNTIMECANCELLED").
日期/时间宏
$LONGDATETIME$  当前的日期/时间戳
文件宏
$LOGFILE$  日志文件的保存位置。
$MAINCONFIGFILE$  主配置文件的保存位置。
其他宏
$ADMINEMAIL$  全局的管理员EMail地址
$ARGn$  指向第n个命令传递参数(通知、事件处理、服务检测等)。Nagios支持最多32个参数宏

 

(4)commands.cfg文件
 此文件默认是存在的,无需修改即可使用,当然如果有新的命令需要加入时,在此文件进行添加即可。这里并未列出文件的所有内容,仅仅介绍了配置中用到的一些命令。 

  1. #下面是notify-host-by-email命令的定义  
  2. define command{  
  3.         command_name    notify-host-by-email    #命令名称,即定义了一个主机异常时发送邮件的命令。  
  4.         command_line    /usr/bin/printf "%b" "***** Nagios *****nnNotification Type: $NOTIFICATIONTYPE$nHost: $HOSTNAME$nState: $HOSTSTATE$nAddress: $HOSTADDRESS$nInfo: $HOSTOUTPUT$nnDate/Time: $LONGDATETIME$n" | /bin/mail -s "** $NOTIFICATIONTYPE$ Host Alert: $HOSTNAME$ is $HOSTSTATE$ **" $CONTACTEMAIL$    #命令具体的执行方式,“-H $HOSTADDRESS$” 是定义目标主机的地址,这个地址在hosts.cfg文件中定义了。  
  5.         }  
  6. #下面是notify-host-by-email命令的定义  
  7. define command{  
  8.         command_name    notify-service-by-email   #命令名称,即定义了一个服务异常时发送邮件的命令  
  9.         command_line    /usr/bin/printf "%b" "***** Nagios *****nnNotification Type: $NOTIFICATIONTYPE$nnService: $SERVICEDESC$nHost: $HOSTALIAS$nAddress: $HOSTADDRESS$nState: $SERVICESTATE$nnDate/Time: $LONGDATETIME$nnAdditional Info:nn$SERVICEOUTPUT$" | /bin/mail -s "** $NOTIFICATIONTYPE$ Service Alert: $HOSTALIAS$/$SERVICEDESC$ is $SERVICESTATE$ **" $CONTACTEMAIL$  
  10.         }  
  11.  
  12. #下面是notify-host-by-email命令的定义  
  13.  define command{  
  14.         command_name    check-host-alive   
  15.  #命令名称,用来检测主机状态。  
  16.        command_line    $USER1$/check_ping -H $HOSTADDRESS$ -w 3000.0,80% -c 5000.0,100% -p 5     
  17. #这里的变量$USER1$在resource.cfg文件中进行定义,即$USER1$=/usr/local/nagios/libexec  
  18. ,那么check_ping的完整路径为/usr/local/nagios/libexec/check_ping。 “-w 3000.0,80%”中“-w”说明后面的一对值对应的是“WARNING”状态,“80%”是其临界值。“-c 5000.0,100%”中“-c”说明后面的一对值对应的是“CRITICAL”,“100%”是其临界值。“-p 1”说明每次探测发送一个包。  
  19.         }  
  20. #下面是notify-host-by-email命令的定义  
  21.  define command{  
  22.         command_name    check_ftp  
  23.         command_line    $USER1$/check_ftp -H $HOSTADDRESS$ $ARG1$    #$ARG1$是指在调用这个命令的时候,命令后面的第一个参数。  
  24.         }  
  25. #下面是check_http命令的定义  
  26.  define command{  
  27.         command_name    check_http  
  28.         command_line    $USER1$/check_http -I $HOSTADDRESS$ $ARG1$  
  29.         }  
  30. #下面是check_ssh命令的定义  
  31.  define command{  
  32.         command_name    check_ssh  
  33.         command_line    $USER1$/check_ssh $ARG1$ $HOSTADDRESS$  
  34.         }  
    (5) hosts.cfg文件
     此文件默认不存在,需要手动创建,hosts.cfg主要用来指定被监控的主机地址以及相关属性信息,一个配置好的实例如下:
  1. define host{  
  2.         use    linux-server  #引用主机linux-server的属性信息,linux-server主机在templates.cfg文件中进行了定义。  
  3.         host_name     web    #主机名  
  4.         alias       ixdba-web  #主机别名  
  5.         address     192.168.12.251  #被监控的主机地址,这个地址可以是ip,也可以是域名。  
  6.         }  
  7.  define host{  
  8.         use           linux-server  
  9.         host_name        mysql  
  10.         alias          ixdba-mysql  
  11.         address         192.168.12.237  
  12.         }  
  13.  
  14.  define hostgroup{      #定义一个主机组  
  15.  hostgroup_name  sa-servers  #主机组名称,可以随意指定。  
  16.  alias       sa servers    #主机组别名  
  17.  members      web,mysql   #主机组成员,其中“web”、“mysql”就是上面定义的两个主机。  
  18.  }  
  19.  


(6) services.cfg文件
 此文件默认也不存在,需要手动创建,services.cfg文件主要用于定义监控的服务和主机资源,例如监控http服务、ftp服务、主机磁盘空间、主机系统负载等等。

  1. #################     web  #####################  
  2.  define service{  
  3.         use     local-service  #引用local-service服务的属性值,local-service在templates.cfg文件中进行了定义。  
  4.         host_name web  #指定要监控哪个主机上的服务,“web”在hosts.cfg文件中进行了定义。  
  5.         service_description   PING  #对监控服务内容的描述,以供维护人员参考。  
  6.         check_command check_ping!100.0,20%!500.0,60% #指定检查的命令,check_ping命令在commands.cfg中定义,后跟两个参数,命令与参数间用!分割。         
  7.         }  
  8.  
  9.  define service{  
  10.         use         local-service  
  11.         host_name      web  
  12.         service_description SSH  
  13.         check_command    check_ssh    
  14. # check_ssh命令也在commands.cfg中定义。 
  15.         }  
  16.  
  17.  define service{  
  18.         use            local-service  
  19.         host_name         web  
  20.         service_description    SSHD  
  21.         check_command       check_tcp!22  
  22.         }  
  23.  


(7) contacts.cfg文件
 contacts.cfg是一个定义联系人和联系人组的配置文件,当监控的主机或者服务出现故障,nagios会通过指定的通知方式(邮件或者短信)将信息发给这里指定的联系人或者使用者。

  1. define contact{  
  2.         contact_name  sasystem  #联系人名称  
  3.         use       generic-contact #引用generic-contact的属性信息,其中“generic-contact”在templates.cfg文件中进行定义  
  4.         alias      sa-system  #联系人别名  
  5.         email      ixdba@126.com  #联系人的邮件地址  
  6.         }  
  7. define contactgroup {  
  8.       contactgroup_name    admins #联系人组名称  
  9.       alias       system administrator group #联系人组描述  
  10.       members     sasystem  #联系人组成员,其中“sasystem”就是上面定义的联系人  
  11. }  
  12.  


(8)timeperiods.cfg文件
 此文件只要用于定义监控的时间段,下面是一个配置好的实例:

  1. #下面是定义一个名为24x7的时间段,即监控所有时间段  
  2. define timeperiod{  
  3.         timeperiod_name 24x7     
  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.         }  
  13. #下面是定义一个名为workhours的时间段,即工作时间段。  
  14. define timeperiod{  
  15.         timeperiod_name workhours   
  16.         alias           Normal Work Hours  
  17.         monday          09:00-17:00  
  18.         tuesday         09:00-17:00  
  19.         wednesday       09:00-17:00  
  20.         thursday        09:00-17:00  
  21.         friday          09:00-17:00  
  22.         }  
  23.  


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

  1. default_user_name=ixdba 
  2. authorized_for_system_information=nagiosadmin,ixdba  
  3. authorized_for_configuration_information=nagiosadmin,ixdba  
  4. authorized_for_system_commands=ixdba 
  5. authorized_for_all_services=nagiosadmin,ixdba  
  6. authorized_for_all_hosts=nagiosadmin,ixdba  
  7. authorized_for_all_service_commands=nagiosadmin,ixdba  
  8. authorized_for_all_host_commands=nagiosadmin,ixdba  
  9.  

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

  1. log_file=/usr/local/nagios/var/nagios.log  
  2. “log_file”变量用来定义nagios日志文件的路径。  
  3. cfg_file=/usr/local/nagios/etc/hosts.cfg  
  4. cfg_file=/usr/local/nagios/etc/services.cfg  
  5. cfg_file=/usr/local/nagios/etc/commands.cfg  
  6. cfg_file=/usr/local/nagios/etc/contacts.cfg  
  7. cfg_file=/usr/local/nagios/etc/timeperiods.cfg  
  8. cfg_file=/usr/local/nagios/etc/templates.cfg  
  9.  “cfg_file”变量用来引用对象配置文件,如果有更多的对象配置文件,在这里依次添加即可。  
  10. object_cache_file=/usr/local/nagios/var/objects.cache  
  11. 该变量用于指定一个“所有对象配置文件”的副本文件,或者叫对象缓冲文件  
  12.  
  13. resource_file=/usr/local/nagios/etc/resource.cfg  
  14. 该变量用于指定nagios资源文件的路径,可以在Nagios.cfg中定义多个资源文件。  
  15. status_file=/usr/local/nagios/var/status.dat  
  16. 该变量用于定义一个状态文件,此文件用于保存nagios的当前状态、注释和宕机信息等。  
  17. status_update_interval=10 
  18.  该变量用于定义状态文件(即status.dat)的更新时间间隔,单位是秒,最小更新间隔是1秒。  
  19. nagios_user=nagios  
  20. 该变量指定了Nagios进程使用哪个用户运行。  
  21. nagios_group=nagios  
  22.  该变量用于指定Nagios使用哪个用户组运行。  
  23. check_external_commands=1 
  24.  该变量用于设置是否允许nagios在web监控界面运行cgi命令,也就是是否允许nagios在web界面下执行重启nagios、停止主机/服务检查等操作。“1”为运行,“0”为不允许。  
  25. command_check_interval=2 
  26.  该变量用于设置nagios对外部命令检测的时间间隔,如果指定了一个数字加一个"s"(如10s),那么外部检测命令的间隔是这个数值以秒为单位的时间间隔。如果没有用"s",那么外部检测命令的间隔是以这个数值的“时间单位”的时间间隔。  
  27. interval_length=60 
  28.  该变量指定了nagios的时间单位,默认值是60秒,也就是1分钟,即在nagios配置中所有的时间单位都是分钟。  

1、验证Nagios配置文件的正确性

 

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

 

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

 

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

2、 启动与停止nagios

(1)启动nagios

在启动nagios 前 修改/etc/passwd,文件最后nagios:x:501:501::/home/nagios:/sbin/nlogin

 改为

 nagios:x:501:501::/home/nagios:/bin/bash

否则报如下错误

 Startingnagios:This account is currently not available.

 done.

 

 修改/etc/cgi.cfg
vi    /usr/local/nagios/etc/cgi.cfg

里面有配置


use_authentication=1   #把1修改为0,保存

 为了保障系统的安全性,nagios设置了这个参数,默认为1,改为0即可

 通过初始化脚本启动nagios

/etc/init.d/nagios start

 或者

Service nagios start

     手工方式启动nagios

 通过nagios命令的“-d”参数来启动nagios守护进程:

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

 

(2)关闭nagios

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

 /etc/init.d/nagios stop

或者

 Service nagios stop

     通过kill方式关闭nagios

 kill <nagios_pid>

2启动与停止nagios

   通过初始化脚本来重启nagios

/etc/rc.d/init.d/nagios reload

/etc/rc.d/init.d/nagios restart

4 nagios性能分析图表

 

1nagios性能分析图表的作用

    Nagios对服务或主机监控的是一个瞬时状态,有时候系统管理员如果需要了解主机在一段时间内的性能以及服务的响应状态,并且形成图表时,就需要通过查看日志数据来分析,但是这种方式不但繁琐,而且抽象,不过幸运的是,PNP可以帮助我们来完成这个工作。

 

(2) PNP的概念与安装环境

     PNP是一个小巧的开源软件包,它基于PHP和PERL,PNP可以利用rrdtool工具将Nagios采集的数据绘制成相关的图表,然后显示主机或者服务在一段时间内的运行状况。

 PNP官方网站为:http://www.pnp4nagios.org

 如果要安装PNP,首先需要安装如下环境:

 1:整合后的apache和PHP环境,需支持GD、zlib

  不然当点击太阳小图标时,会遇到如下错误,

  1 PHP zlib Support not found 

安装php 时未指定zlib库,或者是未安装zlib (whereis zlib 判断是否安装了zlib)

2  PHP gd support not found 

 如果 gd库是rpm安装的 用参数 --with-gd

在上面安装PHP 时应该这样指定

./configure  --prefix=/usr/local/php--with-apxs2=/usr/local/apache2/bin/apxs --with-zlib-dir=/usr/include/zlib.h--with-gd

如果没有性能分析图表,就不用参数 --with-zlib-dir  --with-gd

 2:安装rrdtool工具

 3:安装perl

 

(3)安装PNP

 

RRDtool是一个图表生成工具,可以从http://www.mrtg.org/rrdtool/获得信息。

 

这里下载的版本是rrdtool-1.4.5.tar.gz,安装过程如下:

 

[root@nagios rrdtool]# tar zxvf rrdtool-1.4.5.tar.gz  

[root@nagios rrdtool]# cdrrdtool-1.4.5 

[root@nagios rrdtool-1.4.5]# ./configure--prefix=/usr/local/rrdtool 

[root@nagios rrdtool-1.4.5]#make 

[root@nagios rrdtool-1.4.5]# make install 

 

接着安装PNP,这里下载的版本是pnp-0.4.13.tar.gz,安装过程如下:

[root@nagios pnp]#tar -xvzfpnp-0.4.13.tar.gz 

[root@nagios pnp]#cd pnp-0.4.13 

[root@nagios pnp-0.4.13]#./configure--with-nagios-user=nagios  --with-nagios-group-nagios --with-rrdtool=/usr/local/rrdtool/bin/rrdtool --with-perfdata-dir=/usr/local/nagios/share/perfdata 

[root@nagios pnp-0.4.13]#make all 

[root@nagios pnp-0.4.13]#make install 

[root@nagios pnp-0.4.13]#make install-config 

[root@nagios pnp-0.4.13]#make install-init

 

安装完成,PNP默认文件放置情况如下:

General Options:

-----------------------------       -------------------

  Nagios user/group:               nagios nagios

  Install directory:               /usr/local/nagios

  HTML Dir:                        /usr/local/nagios/share/pnp

  Config Dir:                      /usr/local/nagios/etc/pnp

  Path to rrdtool:                 /usr/local/bin/rrdtool (Version 1.4.5)

  RRDs Perl Modules:               *** NOT FOUND ***

  RRD Files stored in:             /usr/local/nagios/share/perfdata

  process_perfdata.pl Logfile:     /usr/local/nagios/var/perfdata.log

  Perfdata files (NPCD) stored in: /usr/local/nagios/var/spool/perfdata/

-------------------------------------------------------------------------

 

(4)设置PNP

① 创建默认配置文件 

cd  /usr/local/nagios/etc/pnp/

cp process_perfdata.cfg-sample process_perfdata.cfg 

cp npcd.cfg-sample npcd.cfg

cp rra.cfg-sample rra.cfg 

chown -R nagios:nagios *

② 修改process_perfdata.cfg文件

vi /usr/local/nagios/etc/pnp/process_perfdata.cfg

LOG_FILE =/usr/local/nagios/var/perfdata.log 

# Loglevel 0=silent 1=normal 2=debug 

LOG_LEVEL = 2 

这里将日志级别改为2,即为debug模式。

 

(5)修改nagios配置文件

①增加小太阳标示

修改templates.cfg,增加一个定义PNP的hosts和services

define host {

        name      hosts-pnp

        register   0

        action_url/nagios/pnp/index.php?host=$HOSTNAME$

        process_perf_data              1

 

}

define service {

        name      services-pnp

        register   0

        action_url/nagios/pnp/index.php?host=$HOSTNAME$&srv=$SERVICEDESC$

        process_perf_data              1

 

}

②修改nagios.cfg

找到如下几项,去掉注释,修改后信息如下:

process_performance_data=1 

host_perfdata_command=process-host-perfdata 

service_perfdata_command=process-service-perfdata

 

③修改commands.cfg,把文件最后定义的注释掉,添加如下信息,用process_perfdata.pl读取host、service信息

 

# 'process-host-perfdata' command definition

define command{

 

        command_name   process-host-perfdata

        command_line/usr/local/nagios/libexec/process_perfdata.pl

}

 

# 'process-service-perfdata' command definition

 

define command{

        command_name   process-service-perfdata

        command_line /usr/local/nagios/libexec/process_perfdata.pl

}

 

 

④修改hosts.cfg文件和services.cfg文件

 

define host{

        use                  linux-server,hosts-pnp

        host_name              web

        alias                 ixdba-web

        address               192.168.12.251

        }

 

define host{

        use                  linux-server,hosts-pnp

        host_name              mysql

        alias                  ixdba-mysql

        address                192.168.12.237

        }

 

define service{

        use                  local-service,services-pnp

        host_name                mysql

        service_description          SSH

        check_command              check_ssh

        }

define service{

        use                local-service,services-pnp

        host_name                  web

        service_description            http

        check_command                check_http

        }

 

(5)测试PNP功能

 

所有配置完成之后,重新检查nagios配置文件是否正确,然后重启nagios

/etc/init.d/nagios restart

如果配置正确,此时就会生成响应主机的pnp文件

[root@nagios web]# pwd

/usr/local/nagios/share/perfdata/web

[root@gaojf web]# ls

http.rrd  http.xml  PING.rrd  PING.xml SSHD.rrd  SSHD.xml

最后打开网页:

打开网页http://IP/nagios,选择主机选项,然后点击小太阳就可以看到图表,或者访问http://ip/nagios/pnp也可以直接访问图表信息。如下图所示:

 点击小太阳报错

Deprecated:Assigning the return value of new by reference is deprecated in /usr/local/nagios/share/pnp/include/function.inc.phpon line 1026

Warning
: date() [function.date]:It is not safe to rely on the system's timezone settings. You are *required* touse the date.timezone setting or the date_default_timezone_set() function. Incase you used any of those methods and you are still getting this warning, youmost likely misspelled the timezone identifier. We selected 'Asia/Chongqing'for 'CST/8.0/no DST' instead in /usr/local/nagios/share/pnp/include/function.inc.phpon line 557

Warning
: date() [function.date]:It is not safe to rely on the system's timezone settings. You are *required* touse the date.timezone setting or the date_default_timezone_set() function. Incase you used any of those methods and you are still getting this warning, youmost likely misspelled the timezone identifier. We selected 'Asia/Chongqing'for 'CST/8.0/no DST' instead in /usr/local/nagios/share/pnp/include/function.inc.phpon line 559

Warning
: date() [function.date]:It is not safe to rely on the system's timezone settings. You are *required* touse the date.timezone setting or the date_default_timezone_set() function. Incase you used any of those methods and you are still getting this warning, youmost likely misspelled the timezone identifier. We selected 'Asia/Chongqing'for 'CST/8.0/no DST' instead in /usr/local/nagios/share/pnp/include/function.inc.phpon line 563

Warning
: date() [function.date]:It is not safe to rely on the system's timezone settings. You are *required* touse the date.timezone setting or the date_default_timezone_set() function. Incase you used any of those methods and you are still getting this warning, youmost likely misspelled the timezone identifier. We selected 'Asia/Chongqing'for 'CST/8.0/no DST' instead in /usr/local/nagios/share/pnp/include/function.inc.phpon line 565

Warning
: date() [function.date]:It is not safe to rely on the system's timezone settings. You are *required* touse the date.timezone setting or the date_default_timezone_set() function. Incase you used any of those methods and you are still getting this warning, youmost likely misspelled the timezone identifier. We selected 'Asia/Chongqing'for 'CST/8.0/no DST' instead in /usr/local/nagios/share/pnp/include/function.inc.phpon line 563

Warning
: date() [function.date]:It is not safe to rely on the system's timezone settings. You are *required* touse the date.timezone setting or the date_default_timezone_set() function. Incase you used any of those methods and you are still getting this warning, youmost likely misspelled the timezone identifier. We selected 'Asia/Chongqing'for 'CST/8.0/no DST' instead in /usr/local/nagios/share/pnp/include/function.inc.phpon line 565

Warning
: date() [function.date]:It is not safe to rely on the system's timezone settings. You are *required* touse the date.timezone setting or the date_default_timezone_set() function. Incase you used any of those methods and you are still getting this warning, youmost likely misspelled the timezone identifier. We selected 'Asia/Chongqing'for 'CST/8.0/no DST' instead in /usr/local/nagios/share/pnp/include/function.inc.phpon line 563

Warning
: date() [function.date]:It is not safe to rely on the system's timezone settings. You are *required* touse the date.timezone setting or the date_default_timezone_set() function. Incase you used any of those methods and you are still getting this warning, youmost likely misspelled the timezone identifier. We selected 'Asia/Chongqing'for 'CST/8.0/no DST' instead in /usr/local/nagios/share/pnp/include/function.inc.phpon line 565

Warning
: date() [function.date]:It is not safe to rely on the system's timezone settings. You are *required* touse the date.timezone setting or the date_default_timezone_set() function. Incase you used any of those methods and you are still getting this warning, youmost likely misspelled the timezone identifier. We selected 'Asia/Chongqing'for 'CST/8.0/no DST' instead in /usr/local/nagios/share/pnp/include/function.inc.phpon line 563

Warning
: date() [function.date]:It is not safe to rely on the system's timezone settings. You are *required* touse the date.timezone setting or the date_default_timezone_set() function. Incase you used any of those methods and you are still getting this warning, youmost likely misspelled the timezone identifier. We selected 'Asia/Chongqing'for 'CST/8.0/no DST' instead in /usr/local/nagios/share/pnp/include/function.inc.phpon line 565

Warning
: date() [function.date]:It is not safe to rely on the system's timezone settings. You are *required* touse the date.timezone setting or the date_default_timezone_set() function. Incase you used any of those methods and you are still getting this warning, youmost likely misspelled the timezone identifier. We selected 'Asia/Chongqing'for 'CST/8.0/no DST' instead in /usr/local/nagios/share/pnp/include/function.inc.phpon line 563

Warning
: date() [function.date]:It is not safe to rely on the system's timezone settings. You are *required* touse the date.timezone setting or the date_default_timezone_set() function. Incase you used any of those methods and you are still getting this warning, youmost likely misspelled the timezone identifier. We selected 'Asia/Chongqing'for 'CST/8.0/no DST' instead in /usr/local/nagios/share/pnp/include/function.inc.phpon line 565

 

 

这是因为pnp-0.4.14对PHP 5.3.2的支持还不是很好,其中有几个bug需要修改:

根据错误提示打开

vi /usr/local/nagios/share/pnp/include/function.inc.php



根据提示找到对应行进行修改,提示1026行错误,其他是date错误 从557开始
 根据报错信息 添加
line 556: 添加:date_default_timezone_set('UTC');
line 1026:修改为$pdf = new PDF('P', 'mm', 'A4'); 删除&

 

改完之后 重启nagios  又出现如下错误
Deprecated:Function eregi() is deprecated in /usr/local/nagios/share/pnp/include/function.inc.phpon line 1504

Deprecated
: Function eregi() is deprecated in /usr/local/nagios/share/pnp/include/function.inc.phpon line 1504

Deprecated
: Function eregi() is deprecated in /usr/local/nagios/share/pnp/include/function.inc.phpon line 1504

Deprecated
: Function eregi() is deprecated in /usr/local/nagios/share/pnp/include/function.inc.phpon line 1504

Deprecated
: Function eregi() is deprecated in /usr/local/nagios/share/pnp/include/function.inc.phpon line 1504

Deprecated
: Function eregi() is deprecated in /usr/local/nagios/share/pnp/include/function.inc.phpon line 1504

Deprecated
: Function eregi() is deprecated in /usr/local/nagios/share/pnp/include/function.inc.phpon line 1504

Deprecated
: Function eregi() is deprecated in /usr/local/nagios/share/pnp/include/function.inc.phpon line 1504

提示eregi()函数有误  

找到行1504附近修改为

if($level == 2 && $type == "complete" &&preg_match("/^NAGIOS_/",$tag)){

 

再重新重启nagios,应该就能看到图表了

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值