用Nagios来监控网络服务器-nagios配置

运维 系统运维
什么是nagios?如何用Nagios来监控网络服务器?nagios怎样安装?nagios怎样配置?Nagios是一个用来监控主机、服务和网络的开放源码软件,很多大的公司或组织都在使用它。本文讲述的是用Nagios来监控网络服务器,nagios配置

  现在开始配置:

  1:配置web接口

  假设你已经运行了apache,如果没有,请参考:apache的安装

  1.   vi /usr/local/apache2/conf/httpd.conf  
  2.  

  添加如下内容:

  1.   ScriptAlias /nagios/cgi-bin /usr/local/nagios/sbin  
  2.  
  3.     
  4.  
  5.   Options Execcgi  
  6.  
  7.   AllowOverride None  
  8.  
  9.   Order allow,deny  
  10.  
  11.   Allow from all  
  12.  
  13.   AuthName "Nagios Access"  
  14.  
  15.   AuthType Basic  
  16.  
  17.   AuthUserFile /usr/local/nagios/etc/htpasswd.users  
  18.  
  19.   Require valid-user  
  20.  
  21.     
  22.  
  23.   Alias /nagios /usr/local/nagios/share  
  24.  
  25.     
  26.  
  27.   Options None  
  28.  
  29.   AllowOverride None  
  30.  
  31.   Order allow,deny  
  32.  
  33.   Allow from all  
  34.  
  35.   AuthName "Nagios Access"  
  36.  
  37.   AuthType Basic  
  38.  
  39.   AuthUserFile /usr/local/nagios/etc/htpasswd.users  
  40.  
  41.   Require valid-user  
  42.  

  

  修改完毕,保存文件,并重启apache:

  1.   /usr/local/apahce2/bin/apachectl restart  
  2.  

  2:配置apache的BASIC认证:

  生成认证密码:

  1.   /usr/local/apache2/bin/htpasswd –c /usr/local/nagios/etc/htpasswd.users nagios nagios  
  2.  

  apache接口配置完成。

#p#

  开始配置nagios:

  1.   cd /usr/local/nagios/etc/  
  2.  

  在/usr/local/nagios/etc下是nagios的配置模板文件-sample,把.cfg-sample文件全部拷贝成.cfg

  例如:cp nagios.cfg-sample nagios.cfg

  全部拷贝完成即可.

  1.   vi minimal.cfg  
  2.  

  注释所有command:

  注释的方法是在每一个定义语句前面添加”#“

  修改cgi.cfg

  修改use_authentication=1为use_authentication=0,即不用验证.不然有一些页面不会显示。

  现在检查配置文件是否有语法错误:

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

  如果正确,会显示以下结果:

  1.   Total Warnings: 0  
  2.  
  3.   Total Errors: 0  
  4.  

  否则,需要根据提示进行修改配置文件。

  配置文件等会再弄。现在启动nagios

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

  为了使nagios异常中断,我们使用daemontools启动:

#p#

  安装daemontool:

  1.   mkdir -p /package  
  2.  
  3.   chmod 1755 /package  
  4.  
  5.   cd /package  
  6.  
  7.   fetch http://cr.yp.to/daemontools/daemontools-0.76.tar.gz  
  8.  
  9.   cd admin/daemontools-0.76/  
  10.  
  11.   package/install  
  12.  

  检查svscan进程是否启动:

  1.   ps aux | grep svscan  
  2.  
  3.   root 376 0.0 0.0 1636 0 con- IW - 0:00.00 /bin/sh /command/svscanboot  
  4.  
  5.   root 411 0.0 0.0 1224 208 con- S 8Jul06 0:42.50 svscan /service  
  6.  

  ok,启动正常了。

  1.   cd /service  
  2.  
  3.   mkdir nagios  
  4.  
  5.   chmod 1755 nagios  
  6.  
  7.   touch ./run  
  8.  
  9.   chmod 755 ./run  
  10.  
  11.   vi run  
  12.  
  13.   PATH=/usr/local/bin:/usr/bin:/bin  
  14.  
  15.   export PATH  
  16.  
  17.   exec env - PATH=$PATH \  
  18.  
  19.   /usr/local/nagios/bin/nagios /usr/local/nagios/etc/nagios.cfg  
  20.  
  21.   mkdir log  
  22.  
  23.   cd log  
  24.  
  25.   touch ./run  
  26.  
  27.   chmod 755 ./run  
  28.  
  29.   vi ./run  
  30.  
  31.   #!/bin/sh  
  32.  
  33.   exec setuidgid logadmin multilog t s1000000 n100 ./main  
  34.  
  35.   mkdir main  
  36.  
  37.   chmod 777 main  
  38.  
  39.   chown nagios.nagios main  
  40.  
  41.   touch status  
  42.  
  43.   chown nagios.nagios status  
  44.  
  45.   svc -u /service/nagios/  
  46.  
  47.   svstat /service/nagios/  
  48.  
  49.   root@## ps auxww | grep nagios  
  50.  
  51.   root 23276 0.0 0.1 1176 488 ?? I 5:00PM 0:01.71 supervise nagios  
  52.  
  53.   nagios 34251 0.0 0.3 2316 1552 ?? S 6:06PM 0:00.10 /usr/local/nagios/bin/nagios /usr/local/nagios/etc/nagios.cfg  
  54.  
  55.   root@##  
  56.  

  ok,现在把nagios服务做成自动启动的服务了。

  通过svc命令可以启动或者停止服务。

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

  1.   svc opts services  
  2.  
  3.   opts is a series of getopt-style options. services consists of any number of arguments, each argument naming a directory used by supervise.  
  4.  
  5.   -u: Up. If the service is not running, start it. If the service stops, restart it.  
  6.  
  7.   -d: Down. If the service is running, send it a TERM signal and then a CONT signal. After it stops, do not restart it.  
  8.  
  9.   -o: Once. If the service is not running, start it. Do not restart it if it stops.  
  10.  
  11.   -p: Pause. Send the service a STOP signal.  
  12.  
  13.   -c: Continue. Send the service a CONT signal.  
  14.  
  15.   -h: Hangup. Send the service a HUP signal.  
  16.  
  17.   -a: Alarm. Send the service an ALRM signal.  
  18.  
  19.   -i: Interrupt. Send the service an INT signal.  
  20.  
  21.   -t: Terminate. Send the service a TERM signal.  
  22.  
  23.   -k: Kill. Send the service a KILL signal.  
  24.  
  25.   -x: Exit. supervise will exit as soon as the service is down. If you use this option on a stable system, you're doing something wrong; supervise is designed to run forever.  
  26.  

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

  比如:

  停止nagios--svc -d /service/nagios/

  重启nagios--svc -t /service/nagios/

  启动nagios--svc -u /service/nagios/

  当然,你也可以使用inited的方式进行:

  /usr/local/etc/rc.d/nagios start/stop

  好了,反正daemontools很强大,以后慢慢熟悉,转入正题。

  现在打开网页:http://localhost/nagios/

  一定会让你大吃一惊,呵呵,我的服务器和服务状态都清楚的看到了。

  现在我们的nagios中只有一个,那就是它自己,localhost,呵呵,等会我们添加别的主机和主机服务,ok,我们认识一下nagios的庐山真面目:

#p#

  配置nagios:

  1)为主机添加服务

  2)添加主机并添加服务

  3)停止一个服务

  4)删除一台主机和服务

  5)查看所有主机的故障

  6)查看一台特定的主机状态

  7)改变报警的时间间隔

  8)改变发现故障的重试次数

  9)如何在nagios中使用外部命令

  1)为主机添加一个服务

  为localhost主机添加qmail服务的监控,方法如下:

  1.   vi minimal.cfg  
  2.  
  3.   define service{  
  4.  
  5.   use generic-service ; Name of service template to use  
  6.  
  7.   host_name localhost  
  8.  
  9.   service_description qmail_smtp  
  10.  
  11.   is_volatile 0  
  12.  
  13.   check_period 24x7  
  14.  
  15.   max_check_attempts 1  
  16.  
  17.   normal_check_interval 1  
  18.  
  19.   retry_check_interval 1  
  20.  
  21.   contact_groups admins  
  22.  
  23.   notification_options w,u,c,r  
  24.  
  25.   notification_interval 960  
  26.  
  27.   notification_period 24x7  
  28.  
  29.   check_command check_smtp!20%!10%!/  
  30.  
  31.   }  
  32.  

  可以直接拷贝原有的进行修改,我这个就是拷贝的原有的check_local_disk进行的。

  修改host_name,service_description,check_command等

  1.   define service{  
  2.  
  3.   use generic-service ; Name of service template to use  
  4.  
  5.   host_name localhost  
  6.  
  7.   service_description qmail_pop3  
  8.  
  9.   is_volatile 0  
  10.  
  11.   check_period 24x7  
  12.  
  13.   max_check_attempts 1  
  14.  
  15.   normal_check_interval 1  
  16.  
  17.   retry_check_interval 1  
  18.  
  19.   contact_groups admins  
  20.  
  21.   notification_options w,u,c,r  
  22.  
  23.   notification_interval 960  
  24.  
  25.   notification_period 24x7  
  26.  
  27.   check_command check_pop!20%!10%!/  
  28.  
  29.   }  
  30.  

  照猫画虎的进行修改,然后去修改:

  1.   vi checkcommands.cfg  
  2.  
  3.   #'check_qmail' command definition  
  4.  
  5.   define command{  
  6.  
  7.   command_name check_qmail  
  8.  
  9.   command_line $USER1$/check_smtp -H 127.0.0.1  
  10.  
  11.   }  
  12.  
  13.   define command{  
  14.  
  15.   command_name check_pop3  
  16.  
  17.   command_line $USER1$/check_pop -H 127.0.0.1  
  18.  
  19.   }  
  20.  

  保存,然后检查配置文件:

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

  如果没有错误会显示:

  1.   Total Warnings: 0  
  2.  
  3.   Total Errors: 0  
  4.  

  如果有错误,请根据提示进行错误的修正。

  重启nagios

  1.   svc -d /service/nagios/ && svc -u /service/nagios/  
  2.  

  通过web页面检查nagios的结果:

  http://10.5.1.153/nagios/

  点击“Service Detail”

  会出现:

  2)添加主机并添加服务

  我们会监控这台主机的负载、磁盘等一些没有通过端口方式启动的服务器状态,以及它的服务,比如:apache、mysql、qmail和ntp等等吧。那么没有端口的nagios直接能监控到吗?答案是不行。所以我们必须在两台主机上安装nrpe,nrpe可以启动5666端口,把检测的信息源源不断的传给监控中心的主机。

  ok,我们把apache、mysql、qmail和ntp先加上,这回我们把监控的主机和服务新建一个文件:

  1.   cd /usr/local/nagios/etc/  
  2.  
  3.   touch 10_5_1_156.cfg  
  4.  
  5.   vi nagios.cfg  
  6.  
  7.   cfg_file=/usr/local/nagios/etc/10_5_1_156.cfg  
  8.  
  9.   vi 10_5_1_156.cfg  
  10.  

  定义一个主机:

  1.   define host{  
  2.  
  3.   use generic-host ; Name of host template to use  
  4.  
  5.   host_name test_nrpe  
  6.  
  7.   alias client  
  8.  
  9.   address 10.5.1.156  
  10.  
  11.   check_command check-host-alive  
  12.  
  13.   max_check_attempts 1  
  14.  
  15.   check_period 24x7  
  16.  
  17.   notification_interval 120  
  18.  
  19.   notification_period 24x7  
  20.  
  21.   notification_options d,r  
  22.  
  23.   contact_groups admins  
  24.  
  25.   }  
  26.  

  定义主机需要检查的服务:

  1.   define service{  
  2.  
  3.   use generic-service ; Name of service template to use  
  4.  
  5.   host_name test_nrpe  
  6.  
  7.   service_description PING  
  8.  
  9.   is_volatile 0  
  10.  
  11.   check_period 24x7  
  12.  
  13.   max_check_attempts 1  
  14.  
  15.   normal_check_interval 1  
  16.  
  17.   retry_check_interval 1  
  18.  
  19.   contact_groups admins  
  20.  
  21.   notification_options w,u,c,r  
  22.  
  23.   notification_interval 960  
  24.  
  25.   notification_period 24x7  
  26.  
  27.   check_command check_ping!100.0,20%!500.0,60%  
  28.  
  29.   }  
  30.  
  31.   define service{  
  32.  
  33.   use generic-service ; Name of service template to use  
  34.  
  35.   host_name test_nrpe  
  36.  
  37.   service_description apache  
  38.  
  39.   is_volatile 0  
  40.  
  41.   check_period 24x7  
  42.  
  43.   max_check_attempts 1  
  44.  
  45.   normal_check_interval 1  
  46.  
  47.   retry_check_interval 1  
  48.  
  49.   contact_groups admins  
  50.  
  51.   notification_options w,u,c,r  
  52.  
  53.   notification_interval 960  
  54.  
  55.   notification_period 24x7  
  56.  
  57.   check_command check_http!100.0,20%!500.0,60%  
  58.  
  59.   }  
  60.  
  61.   define service{  
  62.  
  63.   use generic-service ; Name of service template to use  
  64.  
  65.   host_name test_nrpe  
  66.  
  67.   service_description mysql  
  68.  
  69.   is_volatile 0  
  70.  
  71.   check_period 24x7  
  72.  
  73.   max_check_attempts 1  
  74.  
  75.   normal_check_interval 1  
  76.  
  77.   retry_check_interval 1  
  78.  
  79.   contact_groups admins  
  80.  
  81.   notification_options w,u,c,r  
  82.  
  83.   notification_interval 960  
  84.  
  85.   notification_period 24x7  
  86.  
  87.   check_command check_mysql!100.0,20%!500.0,60%  
  88.  
  89.   }  
  90.  
  91.   define service{  
  92.  
  93.   use generic-service ; Name of service template to use  
  94.  
  95.   host_name test_nrpe  
  96.  
  97.   service_description ntp  
  98.  
  99.   is_volatile 0  
  100.  
  101.   check_period 24x7  
  102.  
  103.   max_check_attempts 1  
  104.  
  105.   normal_check_interval 1  
  106.  
  107.   retry_check_interval 1  
  108.  
  109.   contact_groups admins  
  110.  
  111.   notification_options w,u,c,r  
  112.  
  113.   notification_interval 960  
  114.  
  115.   notification_period 24x7  
  116.  
  117.   check_command check_ntp!100.0,20%!500.0,60%  
  118.  
  119.   }  
  120.  
  121.   define service{  
  122.  
  123.   use generic-service ; Name of service template to use  
  124.  
  125.   host_name test_nrpe  
  126.  
  127.   service_description qmail_smtp  
  128.  
  129.   is_volatile 0  
  130.  
  131.   check_period 24x7  
  132.  
  133.   max_check_attempts 1  
  134.  
  135.   normal_check_interval 1  
  136.  
  137.   retry_check_interval 1  
  138.  
  139.   contact_groups admins  
  140.  
  141.   notification_options w,u,c,r  
  142.  
  143.   notification_interval 960  
  144.  
  145.   notification_period 24x7  
  146.  
  147.   check_command check_smtp!100.0,20%!500.0,60%  
  148.  
  149.   }  
  150.  
  151.   define service{  
  152.  
  153.   use generic-service ; Name of service template to use  
  154.  
  155.   host_name test_nrpe  
  156.  
  157.   service_description qmail_pop3  
  158.  
  159.   is_volatile 0  
  160.  
  161.   check_period 24x7  
  162.  
  163.   max_check_attempts 1  
  164.  
  165.   normal_check_interval 1  
  166.  
  167.   retry_check_interval 1  
  168.  
  169.   contact_groups admins  
  170.  
  171.   notification_options w,u,c,r  
  172.  
  173.   notification_interval 960  
  174.  
  175.   notification_period 24x7  
  176.  
  177.   check_command check_pop!100.0,20%!500.0,60%  
  178.  
  179.   }  
  180.  

  现在我们象上次一样把服务也定义完了。

【编辑推荐】

使用Nagios的建议技巧

使用Nagios实现网络监控

Cents下Nagios的安装-Apache服务的安装

责任编辑:zhaolei 来源: hackervip
相关推荐

2011-03-22 15:17:13

Nagios监控

2011-03-22 15:17:14

Nagios安装

2011-08-22 11:00:17

nagios

2011-08-22 11:00:14

nagios

2011-08-22 11:00:10

nagios

2011-08-22 10:30:29

nagios

2011-03-21 11:21:04

LinuxNagios

2011-03-25 14:40:33

Nagios监控

2011-03-22 09:03:47

Nagios配置

2011-03-23 15:13:08

Nagios监控Oracle

2011-07-14 15:28:11

服务器

2014-06-26 14:10:44

2011-07-14 14:17:33

网络服务器配置DNS服务器

2011-07-14 14:45:01

网络服务器配置DHCP服务器

2011-03-23 10:17:26

2011-03-21 14:53:27

Nagios监控Linux

2011-04-06 14:24:21

Nagios监控Linux

2011-07-14 14:58:19

网络服务器配置服务器

2010-03-24 11:39:01

2011-07-14 14:01:29

网络服务器配置服务器
点赞
收藏

51CTO技术栈公众号