Get Nagios to Work with Nginx (Gentoo)

今天装 Nagios,开源监控软件。

照例第一步:

emerge net-analyzer/nagios

这个包是个 meta 包,里面似乎有一个内核和一些插件,以后再研究,先搭起来再说。

跑起 Nagios:

/etc/init.d/nagios start
rc-update add nagios default

安装完成之后它会告诉你,要是 Apache 或者 Lighttpd 的话我会给你配好,不是的话自己看着办吧,于是我就自己来了。:-(

首先安装 spawn-fcgi 和 fcgiwrap,第一个是管理 fcgi 进程的,第二个是把 CGI 包装成 fcgi 的。fcgiwrap 这兄弟目前被 ~x86 mask 了,先 unmask。

echo www-misc/fcgiwrap ~x86 >> /etc/portage/package.keywords

然后安装:

emerge www-servers/spawn-fcgi www-misc/fcgiwrap

跑起 spawn-fcgi:

sudo spawn-fcgi -a 127.0.0.1 -p 22222 -P /var/run/fcgiwrap.pid -u nagios -g nagios -- /usr/sbin/fcgiwrap

-a 是主机,-p 是端口(用 unix domain 也行),-P 是 pid 文件,-u 是用户,-g 是组,-- 后面是要运行的命令,用户名和组一定要对,主机和端口要跟 nginx 对上。这条命令是起 CGI 部分的,Nagios 也用了 PHP,PHP 请通过 fcgi 自行跑起来 :-) ,php-fpm 或者 spawn-fcgi 均可,参考 WordPress 之类就行。

下面是 nginx 的配置,主要是抄的别人的,主体参考这里,另外也参考这里。尤其是fastcgi_param AUTH_USER $remote_user; fastcgi_param REMOTE_USER $remote_user; 两句,前一篇没有,害惨我了,nagios 一直说认证不通过……

自己发挥改改就好撒。

location / {
        auth_basic "Restricted";
        auth_basic_user_file /PATH/TO/HTPASSWD;
 
        root /usr/share/nagios/htdocs;
 
        location ~ \.php$ {
                fastcgi_pass   YOUR_PHP_UPSTREAM;
                fastcgi_index  index.php;
                fastcgi_param  SCRIPT_FILENAME  /usr/share/nagios/htdocs$fastcgi_script_name;
                include fastcgi_params;
        }
 
        location /nagios/ {
                location /nagios/images/ {
                        gzip off;
                }
                expires 30d;
                alias /usr/share/nagios/htdocs/;
        }
 
        location /docs/ {
                root /usr/share/nagios/htdocs/;
                index index.html;
        }
 
        location ~ \.cgi {
                fastcgi_pass 127.0.0.1:22222;
                fastcgi_index index.cgi;
                fastcgi_param SCRIPT_FILENAME /usr/lib$fastcgi_script_name;
                include fastcgi_params;
                fastcgi_param  AUTH_USER          $remote_user;
                fastcgi_param  REMOTE_USER        $remote_user;
        }
}

关于里面提到的 htpasswd,是 basic auth 用的,咋生成看文档吧。

Restart Nginx & enjoy!

参考:

  1. http://yemaosheng.com/?p=899
  2. http://www.matejunkie.com/howto-let-nginx-serve-the-nagios-web-interface/
  3. http://wiki.nginx.org/HttpAuthBasicModule
Tags:,,,,,,

This entry was posted in Information Technology and tagged , , , , , , . Bookmark the permalink.

2 Responses to Get Nagios to Work with Nginx (Gentoo)

  1. qq says:

    nginx安装路径下应该已有一个叫fastcgi_params的配置文件中有fastcgi_param AUTH_USER $remote_user; fastcgi_param REMOTE_USER $remote_user; 这两句了,所以nginx.conf里不写也对的.

  2. Leaf Duo says:

    @qq
    我这的 fastcgi_params 没有这两句……

Leave a Reply

Your email address will not be published. Required fields are marked *

*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong> <pre lang="" line="" escaped="" highlight="">