注册 登录
LinuxTone | 运维专家网论坛 - 最棒的Linux运维与开源架构技术交流社区! 返回首页

北京萍聚的个人空间 http://bbs.linuxtone.org/?12402 [收藏] [复制] [分享] [RSS]

日志

【Linux下nginx+nagios 监控平台搭建】

热度 1已有 2864 次阅读2011-3-30 15:46 |个人分类:【Linux 技术文章】

    【Linux下nginx+nagios 监控平台搭建】

随着linux服务器可用性、稳定性逐步增强,企业对线上服务也要求越来越严格,需要有专门的监控系统监控服务的正常运转,并有问题及时发邮件、报警给linux SA。
我们大多数nagios、cacti监控都基于apache搭建,今天我们讲一种新平台搭建nginx平台的nagios监控!

一、系统运行环境

服务器系统版本:Centos 5.3  x64

nginx版本:nginx-0.8.54

nagios版本:nagios-3.2.3

二、下载所需包:

所有安装所需的源码包:本站点集成下载:

http://blog.mgcrazy.com/download/nginx-0.8.54.tar.gz
http://blog.mgcrazy.com/download/pcre-8.01.tar.gz
http://blog.mgcrazy.com/download/nagios/FCGI-0.67.tar.gz
http://blog.mgcrazy.com/download/nagios/FCGI-ProcManager-0.18.tar.gz
http://blog.mgcrazy.com/download/nagios/nagios-3.2.3.tar.gz
http://blog.mgcrazy.com/download/nagios/nagios-plugins-1.4.13.tar.gz
http://blog.mgcrazy.com/download/nagios/nrpe-2.8.1.tar.gz

Nginx 安装请参考:http://blog.mgcrazy.com/?p=792 【nginx安装】

nginx安装完毕后。

三、正式安装nagios:

创建nagios用户和组,并把www加入nagios管理组:
useradd  nagios &&groupadd nagcmd && usermod -g nagcmd nagios && usermod -g nagcmd www

cd /usr/scr && tar xzf nagios-3.2.3.tar.gz && cd nagios-3.2.3 && ./configure --with-command-group=nagcmd --prefix=/usr/local/nagios && make all && make install &&make install-init && make install-config && make install-commandmode


nagios安装完毕:
验证程序是否被正确安装。切换目录到安装路径(这里是/usr/local/nagios),看是否存在 etc、bin、 sbin、 share、 var这五个目录,如果存在则可以表明程序被正确的安装到系统了。

接下来编译并安装nagios插件 nagios-plugins
cd /usr/src &&tar zxvf nagios-plugins-1.4.13.tar.gz &&cd nagios-plugins-1.4.13 &&./configure --with-nagios-user=nagios --with-nagios-group=nagios --perfix=/usr/local/nagios && make && make install
插件安装完毕!

ls /usr/local/nagios/libexec
会显示安装的插件文件,即所有的插件都安装在libexec这个目录下。

四、创建一个nagiosadmin用户

用于Nagios的WEB接口登录。

记下你所设置的登录口令,一会儿你会用到它
这里要借助于apche的htpasswd,在有apache机器上执行下列命令
/usr/local/apache2/bin/htpasswd -c /usr/local/nagios/etc/htpasswd nagiosadmin
输入两次一样的密码即可。
启动nagios ;chkconfig –add nagios 添加到service 中,chkconfig nagios –level 35 on 设置成开机启动。【把selinux、iptables关闭,或者更改iptables策略需要访问nagios】

五、安装perl fcgi模块:
(1)、安装FCGI
cd /usr/src && tar -zxvf FCGI-0.67.tar.gz &&cd FCGI-0.67 && perl Makefile.PL && make && make install
(2)、安装FCGI-ProcManager:
cd ../ && tar -xzxf FCGI-ProcManager-0.18.tar.gz &&cd FCGI-ProcManager-0.18 && perl Makefile.PL &&make && make install
安装完毕即可。
接下来配置cgi脚本、nginx配置文件:
cd
/usr/local/nagios/bin/ 下新建文件,并写入以下内容 vi perl-cgi.pl

#!/usr/bin/perl
use FCGI;
#perl -MCPAN -e ‘install FCGI’
use Socket;
#this keeps the program alive or something after exec’ing perl scripts
END()
{
}
BEGIN()
{
}
*CORE::GLOBAL::exit
=
sub
{
die
“fakeexit\nrc=”.shift().”\n”;
};
eval
q{exit};
if
($@)
{
exit
unless
$@
=~
/^fakeexit/;
}
;
&main;
sub main {
#$socket = FCGI::OpenSocket( “:3461″, 10 ); #use IP sockets
$socket
= FCGI::OpenSocket(
“/var/run/nagios.sock”,
10
);
#use UNIX sockets – user running this script must have w access to the ‘nginx’ folder!!
$request
= FCGI::Request(
\*STDIN,
\*STDOUT,
\*STDERR,
\%ENV,
$socket
);
if
($request)
{request_loop()};
FCGI::CloseSocket(
$socket
);
}
sub request_loop {
while(
$request->Accept()
>=
0
)
{
#processing any STDIN input from WebServer (for CGI-GET actions)
$env
=
$request->GetEnvironment();
$stdin_passthrough
=”;
$req_len
=
0
+
$ENV{CONTENT_LENGTH};
if
($ENV{REQUEST_METHOD}
eq
‘GET’){
$stdin_passthrough
.=
$ENV{‘QUERY_STRING’};
}
#running the cgi app
if
(
(-x $ENV{SCRIPT_FILENAME})
&&
#can I execute this?
(-s
$ENV{SCRIPT_FILENAME})
&&
#Is this file empty?
(-r $ENV{SCRIPT_FILENAME})
#can I read this file?
){
#http://perldoc.perl.org/perlipc.html#Safe-Pipe-Opens
open
$cgi_app,
‘-|’,
$ENV{SCRIPT_FILENAME},
$stdin_passthrough
or
print(“Content-type: text/plain\r\n\r\n”);
print
“Error: CGI app returned no output – Executing $ENV{SCRIPT_FILENAME} failed !\n”;
if
($cgi_app)
{print
<$cgi_app>;
close
$cgi_app;}
}
else
{
print(“Content-type: text/plain\r\n\r\n”);
print
“Error: No such CGI app – $req_len – $ENV{CONTENT_LENGTH} – $ENV{REQUEST_METHOD} – $ENV{SCRIPT_FILENAME} may not exist or is not executable by this process.\n”;
     }
  }
}
设置权限chmod +x perl-cgi.pl 即可!

以下是我的nginx.conf文件server内容:

server
{
listen 80;
server_name 192.168.2.79;
root /usr/local/nagios/share;
index index.php;
auth_basic “You Name”;

auth_basic_user_file /usr/local/nagios/etc/htpasswd;
log_format nagios ‘$remote_addr – $remote_user [$time_local] “$request” ‘
‘$status $body_bytes_sent “$http_referer” ‘
‘”$http_user_agent” $http_x_forwarded_for’;
access_log /usr/local/nginx/nagios.log nagios;
location ~ .*\.(php|php5)?$
{
#fastcgi_pass unix:/tmp/php-cgi.sock;
fastcgi_pass 127.0.0.1:9000;

fastcgi_index index.php;
include fcgi.conf;
}
location ~ \.cgi$ {
root /usr/local/nagios/sbin;
rewrite ^/nagios/cgi-bin/(.*)\.cgi /$1.cgi break;
fastcgi_index index.cgi;
fastcgi_pass unix:/var/run/nagios.sock;

fastcgi_param SCRIPT_FILENAME /usr/local/nagios/sbin$fastcgi_script_name;
fastcgi_param QUERY_STRING $query_string;

fastcgi_param REMOTE_ADDR $remote_addr;
fastcgi_param REMOTE_PORT $remote_port;
fastcgi_param REQUEST_METHOD $request_method;
fastcgi_param REQUEST_URI $request_uri;
fastcgi_param REMOTE_USER $remote_user;

#默认没有红色这一句,如果没有,后面nagios页面会报错,具体错误后面给出。

#fastcgi_param SCRIPT_NAME $fastcgi_script_name;
fastcgi_param SERVER_ADDR $server_addr;
fastcgi_param SERVER_NAME $server_name;
fastcgi_param SERVER_PORT $server_port;
fastcgi_param SERVER_PROTOCOL $server_protocol;
fastcgi_param SERVER_SOFTWARE nginx;
fastcgi_param CONTENT_LENGTH $content_length;
fastcgi_param CONTENT_TYPE $content_type;
fastcgi_param GATEWAY_INTERFACE CGI/1.1;
fastcgi_param HTTP_ACCEPT_ENCODING gzip,deflate;
fastcgi_param HTTP_ACCEPT_LANGUAGE zh-cn;
}

fcgi.conf文件内容如下:

fastcgi_param  GATEWAY_INTERFACE  CGI/1.1;
fastcgi_param  SERVER_SOFTWARE    nginx;

fastcgi_param  QUERY_STRING       $query_string;
fastcgi_param  REQUEST_METHOD     $request_method;
fastcgi_param  CONTENT_TYPE       $content_type;
fastcgi_param  CONTENT_LENGTH     $content_length;

fastcgi_param  SCRIPT_FILENAME    $document_root$fastcgi_script_name;
fastcgi_param  SCRIPT_NAME        $fastcgi_script_name;
fastcgi_param  REQUEST_URI        $request_uri;
fastcgi_param  DOCUMENT_URI       $document_uri;
fastcgi_param  DOCUMENT_ROOT      $document_root;
fastcgi_param  SERVER_PROTOCOL    $server_protocol;

fastcgi_param  REMOTE_ADDR        $remote_addr;
fastcgi_param  REMOTE_PORT        $remote_port;
fastcgi_param  SERVER_ADDR        $server_addr;
fastcgi_param  SERVER_PORT        $server_port;
fastcgi_param  SERVER_NAME        $server_name;

# PHP only, required if PHP was built with –enable-force-cgi-redirect
fastcgi_param  REDIRECT_STATUS    200;

以上可以把nginx中fastcGI部分整合到fcgi.conf中。

启动nginx,确定有fastcGI 9000端口。 后台启动screen   ./perl-cgi.pl >/dev/null  ;ctrl +a +d 退出screen。

然后chmod  777   /var/run/nagios.sock  ;并且重启service nagios restart

六。安装完毕,然后访问:

直接访问 http://192.168.2.79   会弹出登录框 ,输入用户名nagiosadmin 和密码,会看到nagios默认监控localhost!

页面如下:而且点击详细服务关闭报警,也不报错:

这样的话,nginx+nagios 环境搭建完毕!剩下的就是具体监控客户端的配置啦!文章后续给出!

本文环境已经测试通过,搭建的时候,也遇到了好多例如403、502、504等等系列问题,一点一滴的解决。一件事只有你真正做过,你才会感觉到学到很多东西。如下的报错:

(1)、It seems that you have chosen to not use the authentication functionality of the CGIs.

I don't want to be personally responsible for what may happen as a result of allowing

unauthorized users to issue commands to Nagios,so you'll have to disable this safeguard if you

are really stubborn and want to invite trouble.

(2)、It appears as though you do not have permission to view information for any of the hosts you requested...


If you believe this is an error, check the HTTP server authentication requirements for accessing this CGI
and check the authorization options in your CGI configuration file

可参照修改:http://hi.baidu.com/shengit/blog/item/b21b770965c6e8de62d986c0.html

 本文参考文章链接:
http://bbs.linuxtone.org/thread-4441-1-1.html

http://bbs.linuxtone.org/thread-7404-1-1.html

http://www.comeonsa.com/

本文出自 “【烟雨楼台】” 博客,请务必保留此出处http://wgkgood.blog.51cto.com/1192594/523150


路过

鸡蛋

鲜花

握手

雷人

发表评论 评论 (3 个评论)

回复 kevin_chu 2011-5-16 08:55
学习学习,需要中。
nagiosql能用nginx搭建么?
回复 北京萍聚 2011-5-16 11:13
这个还没弄过呢 呵呵 有时间一起研究一下
回复 北京萍聚 2011-5-16 11:13
应该可以!

facelist

您需要登录后才可以评论 登录 | 注册

Archiver|手机版|感谢所有关心和支持过LinuxTone的朋友们 转载本站内容请注明原作者名及出处 ( 京ICP备08103151 )   |

GMT+8, 2019-8-17 21:01 , Processed in 0.009429 second(s), 10 queries , Apc On.

Powered by Discuz! X2 Licensed

© 2001-2011 Comsenz Inc.

回顶部