1. 配置nagios.cfg

   
   
   
   
  1. vi /usr/local/nagios/etc/nagios.cfg
    # 目录名称自己定义,等下需要建立这个目录  
  2. cfg_dir=/usr/local/nagios/etc/linuxjcq 

将以上添加在cfg_dir段落,注释所有的cfg_file

   
   
   
   
  1. cfg_file=/usr/local/nagios/etc/objects/localhost.cfg 
  2. ......

2. 创建以上配置文件制定的目录

   
   
   
   
  1. cp -a /usr/local/nagios/etc/ojbects /usr/local/nagios/etc/linuxjcq 
  2. cd /usr/local/nagios/etc/linuxjcq
  3. rm -rf localhost.cfg printer.cfg switch.cfg windows.cfg

3. 编辑目录下得commands.cfg文件,添加nrpe支持

   
   
   
   
  1. define command{  
  2.         command_name    check_nrpe  
  3.         command_line    $USER1$/check_nrpe -H $HOSTADDRESS$ -c $ARG1$  
  4.         } 

可以添加到文件的末尾

4. 在目录下添加hosts.cfg、hostgroups.cfg、services.cfg

我习惯将内容分成这个三个文件,合并成一个文件也可以

a. 主机

   
   
   
   
  1. vi hosts.cfg  
  2. define host{  
  3.     use                     linux-server  
  4.     host_name               linuxjcq01  
  5.     address                 192.168.2.11  
  6.     }  
  7. ...... 

有多少台服务器需要监控,就需要添加多个host段落

说明:

use        制定使用的模板

host       主机名

address 主机的ip地址

b. 主机组

可以将服务器分成各种类型的组

   
   
   
   
  1. define hostgroup{  
  2.         hostgroup_name  linuxjcq_servers  
  3.         members         linuxjcq01,......  
  4.         }
  5.  
  6. ......

说明:

hostgroup_name 组名

members            组的成员,名字为刚在在host设置的名字

c. 服务

   
   
   
   
  1. define service{  
  2.         use                             local-service  
  3.         hostgroup_name                  linuxjcq_servers  
  4.         service_description             01. System Load  
  5.         check_command                   check_nrpe!check_load  
  6.         }  
  7.      
  8. define service{  
  9.         use                             local-service  
  10.         hostgroup_name                  linuxjcq_servers  
  11.         service_description             02. System Users  
  12.         check_command                   check_nrpe!check_users  
  13.         }  
  14.  
  15. define service{  
  16.         use                             local-service  
  17.         hostgroup_name                  linuxjcq_servers  
  18.         service_description             03. Check Zombie Procs  
  19.         check_command                   check_nrpe!check_zombie_procs  
  20.         }    
  21. define service{  
  22.         use                             local-service  
  23.         hostgroup_name                  linuxjcq_servers  
  24.         service_description             04. The Disk Usage  
  25.         check_command                   check_nrpe!check_disk  
  26.         }  
  27.  
  28. define service{  
  29.         use                             local-service  
  30.         host_name                       linuxjcq01  
  31.         service_description             05. The Nginx Status  
  32.         check_command                   check_nrpe!check_nginx  
  33.         }  
  34.      
  35. define service{  
  36.         use                             local-service  
  37.         host_name                       linuxjcq01  
  38.         service_description             06. The Mysql Status  
  39.         check_command                   check_nrpe!check_mysql  
  40.         }  

说明:

use                          使用的模板名称

host_name              主机名

hostgroup_name      组名

service_description  服务的描述

check_command      检查服务时制定的命令,这里的命令和nrpe配置文件中得command[check_xxxx]对应

每台主机都需要监控的服务,比如负载监控,用户监控,磁盘监控,僵死进程监控,使用主机组hostgroupt_name就好了,可以添加多个组,使用逗号分隔

如果不是所有主机都有的服务,比如监控nginx,监控mysql,使用host_name,后面添加需要的主机名(在hosts.cfg文件中的名字),可以添加多个主机,以逗号作为分隔

5. 检测配置文件是否正确

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

如果有报错,根据错误进行调整

6. 启动nagios

   
   
   
   
  1. service nagios start 

7. 配置nginx

   
   
   
   
  1. vi /usr/local/nginx/etc/conf/vhosts/nagios.cfg  
  2. server  
  3. {  
  4.   listen       port;  
  5.   server_name  x.x.x.x;  
  6.   index index.html index.htm index.php;  
  7.   root  /usr/local/nagios/share;  
  8.  
  9.   location ~ ^(.*)\/\.svn\/  
  10.   {  
  11.     deny all;  
  12.   }  
  13.  
  14.   location ~ ^(.*)\/phpinfo.php  
  15.   {  
  16.     deny all;  
  17.   }  
  18.  
  19.   location ~ .*\.(php|php5)?$  
  20.   {  
  21.     # fastcgi_pass  unix:/tmp/php-cgi.sock;  
  22.     fastcgi_pass  192.168.2.11:9000;  
  23.     fastcgi_index index.php;  
  24.     include fcgi.conf;  
  25.     auth_basic "Nagios Access";  
  26.     auth_basic_user_file /usr/local/nagios/etc/htpasswd.users;  
  27.   }  
  28.  
  29.   location ~ .*\.cgi$  
  30.   {  
  31.     root /usr/local/nagios/sbin;  
  32.     rewrite ^/nagios/cgi-bin/(.*)\.cgi /$1.cgi break;  
  33.     fastcgi_pass 192.168.2.11:10000;  
  34.     fastcgi_param REMOTE_USER $remote_user;  
  35.     fastcgi_index index.cgi;  
  36.     include fcgi.conf;  
  37.     auth_basic "Nagios Access";  
  38.     auth_basic_user_file /usr/local/nagios/etc/htpasswd.users;  
  39.   }  
  40.  
  41.   location ~ .*\.(gif|jpg|jpeg|png|bmp|swf)$  
  42.   {  
  43.     root /usr/local/nagios/share/p_w_picpaths;  
  44.     rewrite ^(/nagios)?/p_w_picpaths/(.*)$ /$2 break;  
  45.     expires      30d;  
  46.     access_log   off;  
  47.   }  
  48.  
  49.   location ~ .*\.(js|css)?$  
  50.   {  
  51.     root /usr/local/nagios/share/stylesheets;  
  52.     rewrite ^(/nagios)?/stylesheets/(.*)$ /$2 break;  
  53.     expires      1h;   
  54.     access_log   off;  
  55.   }  
  56.  
  57.   log_format  nagios  '$remote_addr - $remote_user [$time_local] [$request_time] "$request"'  
  58.             '$status $body_bytes_sent "$http_referer"'  
  59.             '"$http_user_agent" $http_x_forwarded_for';  
  60.   access_log  off;  

10000端口的fcgiwrap配置见:http://linuxjcq.blog.51cto.com/3042600/718002

因为nagios为C语言编写的,要运行fastcgi,需要进行以上配置

htpasswd.users为第一节安装nagios时生成的文件,用于Web的认证

重写规则:见上文件

重载nginx使其生效:

   
   
   
   
  1. nginx -t  
  2. nginx -s reload 

9. 登录http://x.x.x.x/,输入用户名密码可以看到界面