最新消息:其实接手服务器,最最最重要的还是要看下数据库、文件备份情况!!!非常重要!!!

nagios监控系统一:监控服务器nginx环境搭建、nagios编译

monitor shang 4471浏览 0评论

说明:
nagios监控系统的报警功能确实不错,经常半夜被那邮件告警给叫起来 – -,也不知道是好是坏。如果配合cacti,图像显示功能,这监控系统就很好了。当然这篇,并不会讲cacti的安装。
很多开源程序如mailman, nagios等等,都有web管理界面,都需要cgi支持。在Apache独大的年代,这个问题可以很好解决,因为apache本身就可以运行cgi程序。而nginx恰恰又没有cgi模块,特意去编个httpd也懒。
这时候,我们就需要配置nginx使其支持perl cgi从而保证nagios的运行。
本文的lnmp环境参考张晏老师的http://blog.s135.com/nginx_php_v6/搭建。我自己记录的lnmp编译安装。nginx采用php-fpm管理调用php处理正常的php请求,而nagios的cgi请求则通过转发给另行安装的perl cgi模块和FCGI-ProcManager来处理。

以下都是在监控服务器上操作

实现:
1.添加nagios运行用户

# groupadd nagios
# useradd -s /sbin/nologin -g nagios nagios  //不要加-M,不然会出现附录1:启动nagios出现warning:cannot change directory to /home/nagios的问题

 
2.编译安装nagios

# cd /usr/local/src/
# wget http://prdownloads.sourceforge.net/sourceforge/nagios/nagios-3.5.0.tar.gz
# tar zxvf nagios-3.5.0.tar.gz
# cd nagios
# ./configure --prefix=/usr/local/nagios -with-command-group=nagios
# make
# make install  //make install出现cannot stat `nagios': No such file or directory时,请make all下
# make install-init
# make install-config
# make install-commandmode
# vim /usr/local/nagios/etc/objects/contacts.cfg  //更改email地址nagiosadmin联系人定义的为接收报警的email。默认配置文件安装在/usr/local/nagios/etc,这些样例文件可以配置Nagios使之正常运行
# /usr/local/apache2/bin/htpasswd -c /usr/local/nginx/conf/htppasswd nagiosadmin  //创建一个nagiosadmin用户,用于登录nagios。这里借助apche的htpasswd。在有apache机器上执行,将生成的htppasswd拷贝到这台就可以了

 
3.接下来安装nagios插件

# wget http://prdownloads.sourceforge.net/sourceforge/nagiosplug/nagios-plugins-1.4.16.tar.gz
# tar  zxvf nagios-plugins-1.4.16.tar.gz
# cd nagios-plugins-1.4.16
# ./configure --with-nagios-user=nagios --with-nagios-group=nagios
# make & make install

 
4.启动nagios chkconfig –add nagios添加到service 中,chkconfig nagios on设置成开机启动

# /usr/local/nagios/bin/nagios -v /usr/local/nagios/etc/nagios.cfg  启动前,习惯性先检查下nagios.cfg配置文件是否正确
# service nagios start  //启动服务 这里要关掉selinux否则访问的时候会出错。

 
5.安装perl cgi模块,用于处理cgi请求

# wget http://www.cpan.org/modules/by-module/FCGI/FCGI-0.74.tar.gz
# tar zxvf FCGI-0.74.tar.gz
# cd FCGI-0.74
# perl Makefile.PL
# make && make install
# cd ..

 
6.安装FCGI-ProcManager,用于管理FCGI

# wget http://www.cpan.org/authors/id/G/GB/GBJK/FCGI-ProcManager-0.19.tar.gz
# tar zxvf FCGI-ProcManager-0.19.tar.gz
# cd FCGI-ProcManager-0.19
# perl Makefile.PL
# make && make install

 
7.下载nginx-fcgi脚本,后台守候进程,处理cgi进程

# cd /usr/local/src
# wget http://dl.gaingreat.com/nginx-fcgi
# mv nginx-fcgi /usr/sbin/nginx-fcgi
# chmod +x /usr/sbin/nginx-fcgi
# /usr/sbin/nginx-fcgi -l /var/log/nginx-fcgi.log -pid /var/run/nginx-fcgi.pid -S /var/run/nginx-fcgi.sock  //启动nginx-fcgi守护进程,用于处理cgi请求
# chmod 777 /var/run/nginx-fcgi.sock  //给nginx-fcgi.sock 777的权限
# perl -MCPAN -e 'install "IO::All"' //别人说要cpan安装FCGI;Getopt::Long;IO::All;Socket;但这里我就安装了IO::All,回车过去。如果IO::ALL无法cpan安装,可以到search.cpan.org下载IO::All包手动安装

 
8.到这里nagios基本上装完了,下来就是nginx的配置了!

# cat /usr/local/nginx/conf/vhost/nagios.conf   //内容如下,可以看到php|php5语法转给127.0.0.1:9000;cgi请求则给/var/run/nginx-fcgi.sock处理
server
{
listen 80;
server_name nagios.coolnull.com;
root /usr/local/nagios/share;
index index.php;
location ~ .*\.(php|php5)?$
{
        fastcgi_pass  127.0.0.1:9000;
        fastcgi_index index.php;
        include fastcgi.conf;
        auth_basic "Nagios Login pain";
        auth_basic_user_file /usr/local/nginx/conf/htppasswd;
}

location ~ .*\.cgi$ {
        root /usr/local/nagios/sbin;
        rewrite ^/nagios/cgi-bin/(.*)\.cgi /$1.cgi break;
        fastcgi_pass unix:/var/run/nginx-fcgi.sock;
        fastcgi_index index.cgi;
        fastcgi_param SCRIPT_FILENAME /usr/local/nagios/sbin/$fastcgi_script_name;
        fastcgi_param HTTP_ACCEPT_LANGUAGE zh-cn;
        include fcgi.conf;
        auth_basic "Nagios Login pain";
        auth_basic_user_file /usr/local/nginx/conf/htppasswd;
}

location /nagios {
        alias /usr/local/nagios/share/;
        #index index.html index.htm index.php;
        #auth_basic "Nagios Login pain";
        #auth_basic_user_file /usr/local/nginx/conf/htppasswd;
}

error_log /tmp/error.log;
access_log  /tmp/access.log  access;

}

 
9:添加开机启动项

# vim /etc/rc.loal  //内容如下
#for nagios monitor
/usr/sbin/nginx-fcgi -l /var/log/nginx-fcgi.log -pid /var/run/nginx-fcgi.pid -S /var/run/nginx-fcgi.sock
/bin/chmod 777 /var/run/nginx-fcgi.sock
/etc/init.d/nagios start

 
测试:
以上步骤完成,基本就可以监控服务器自身的监控信息。


附录
附录1:启动nagios出现warning:cannot change directory to /home/nagios….或Starting nagios:This account is currently not available.
这个是因为当时安装nagios没有设好权限,给予nagios登陆权限usermod -s /bin/bash
或创建目录/home/nagios,我这里用的是mkdir /home/nagios

附录2:Can’t locate IO/All.pm
perl -MCPAN -e ‘install “IO::All”‘

Can’t locate FCGI.pm in @INC
perl -MCPAN -e ‘install FCGI’
perl -MCPAN -e ‘install FCGI::ProcManager’
cpan -i FCGI
cpan -i FCGI::ProcManager

//身份认证、数据库连接
cpan Authen::SASL Filesys::Df DBI DBD::mysql
perl-fcgi.pl(fcgi socket):

附录3.nagios搭建好后nagios右侧具体内容为乱码。左侧导航部分全英文,都正常
这是因为nagios没办法执行cgi请求,直接输出乱码出来。只要安装perl cgi模块和FCGI-ProcManager,并在nginx上添加fastcgi_pass unix:/var/run/nginx-fcgi.sock;等,让nginx能正确处理cgi请求即可!

附录4.fastcgi.conf

# cat /usr/local/nginx/conf/fastcgi.conf  //内容如下
fastcgi_param  SCRIPT_FILENAME    $document_root$fastcgi_script_name;
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_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  HTTPS              $https if_not_empty;

fastcgi_param  GATEWAY_INTERFACE  CGI/1.1;
fastcgi_param  SERVER_SOFTWARE    nginx/$nginx_version;

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;

 
参考:
主要参考了这篇:http://liuyu.blog.51cto.com/183345/241867
nginx官网关于cgi的处理:http://wiki.nginx.org/NginxSimpleCGI,没仔细看!

转载请注明:酷喃|coolnull| » nagios监控系统一:监控服务器nginx环境搭建、nagios编译

发表我的评论
取消评论

表情

Hi,您需要填写昵称和邮箱!

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址