前几天本地测试服务器硬盘idoe满了,导致文件无法上传,花了一上午才找到这个原因!那时候我就想如果有一款监控那就好了.经过查找资料和询问运维,他们推荐nagios监控,那我就搞一搞学习一下nagios,以便在以后能自动化监控服务器!
-
Nagios安装环境所需
#PHP环境 #perl环境 #nginx环境
-
常用配置目录
/usr/local/nginx #nginx 安装目录 /usr/local/nagios #nagios 安装目录 /home/www/htdocs #www 目录
-
PHP环境在本博客有完整的lnmp配置,本文中将沿用以前的lnmp配置来进行Nagios的安装。
由于Nginx不支持Perl的CGI,这里就需要先来搭建Perl环境。 -
-
创建nagios相关的用户及用户组和安装路径
/usr/sbin/useradd -m -s /sbin/nologin nagios mkdir -p /usr/local/nagios chmod a+w /usr/local/nagios chown -R nagios:nagios /usr/local/nagios #创建一个名为nagcmd的用户组,用于从web接口执行外部命令。将Nagios用户和Nginx用户加入组中 groupadd nagcmd usermod -a -G nagcmd nagios usermod -a -G nagcmd www
-
让Nginx支持Perl的CGI方法有好几种,基本原理都是通过Perl的FCGI模块实现
FCGI模块前必须先安装Perlyum -y install perl-devel
#可能的错误 错:Can't locate ExtUtils/MakeMaker.pm in @INC (@INC contains: /usr/local/lib/perl5 /usr/local/share/perl5 /usr/lib/perl5/vendor_perl /usr/share/perl5/vendor_perl /usr/lib/perl5 /usr/share/perl5 .) at Makefile.PL line 1. BEGIN failed--compilation aborted at Makefile.PL line 1. 解决:yum -y install perl-devel
-
安装FCGI模块
wget http://search.cpan.org/CPAN/authors/id/F/FL/FLORA/FCGI-0.73.tar.gz tar xvzf FCGI-0.73.tar.gz cd FCGI-0.73 perl Makefile.PL make && make install cd ../
-
安装FCGI-ProcManager模块
wget http://search.cpan.org/CPAN/authors/id/G/GB/GBJK/FCGI-ProcManager-0.19.tar.gz tar xvzf FCGI-ProcManager-0.19.tar.gz cd FCGI-ProcManager-0.19 perl Makefile.PL make && make install cd ../
-
安装IO perl-IO-String IO::ALL模块
#安装IO wget http://search.cpan.org/CPAN/authors/id/G/GB/GBARR/IO-1.25.tar.gz tar zxvf IO-1.25.tar.gz cd IO-1.25 perl Makefile.PL make && make install cd ../ #安装 perl-IO-String yum install perl-IO-String #安装IO::ALL模块 wget http://search.cpan.org/CPAN/authors/id/I/IN/INGY/IO-All-0.41.tar.gz tar zxvf IO-All-0.41.tar.gz cd IO-All-0.41 perl Makefile.PL make && make install cd ../
-
安装 unzip
yum -y install unzip
-
下载Perl脚本
wget http://www.mike.org.cn/wp-content/uploads/2011/07/perl-fcgi.zip unzip perl-fcgi.zip cp perl-fcgi.pl /usr/local/nginx/ #修改脚本权限 chmod 755 /usr/local/nginx/perl-fcgi.pl
-
建立一个CGI启动/停止脚本
这个SHELL脚本只是为了方便管理上面的Perl脚本。脚本中的www为nginx的运行用户,请据自己的实际情况调整。
注意事项:不能用root用户执行(会提示). 要用与Nginx相同身份的用户执行。否则可能会在Nginx Log中提示 Permision Denied。vim /usr/local/nginx/start_perl_cgi.sh #!/bin/bash #set -x dir=/usr/local/nginx/ stop () { #pkill -f $dir/perl-fcgi.pl kill $(cat $dir/logs/perl-fcgi.pid) rm $dir/logs/perl-fcgi.pid 2>/dev/null rm $dir/logs/perl-fcgi.sock 2>/dev/null echo "stop perl-fcgi done" } start () { rm $dir/now_start_perl_fcgi.sh 2>/dev/null chown www.www $dir/logs echo "$dir/perl-fcgi.pl -l $dir/logs/perl-fcgi.log -pid $dir/logs/perl-fcgi.pid -S $dir/logs/perl-fcgi.sock" >>$dir/now_start_perl_fcgi.sh chown www.www $dir/now_start_perl_fcgi.sh chmod u+x $dir/now_start_perl_fcgi.sh sudo -u www $dir/now_start_perl_fcgi.sh echo "start perl-fcgi done" } case $1 in stop) stop ;; start) start ;; restart) stop start ;; esac #修改脚本权限 chmod 755 /usr/local/nginx/start_perl_cgi.sh #启动脚本 /usr/local/nginx/start_perl_cgi.sh start 正常情况下在/usr/local/webserver/nginx/logs下生成perl-fcgi.sock这个文件,如果没有生成,请检查下上面的步聚
错误:xxx is not in the sudoers file. This incident will be reported 解决方法如下: # yum -y install sudo 1>、进入超级用户模式。也就是输入"su -",系统会让你输入超级用户密码,输入密码后就进入了超级用户模式。 2>、添加文件的写权限。也就是输入命令"chmod u+w /etc/sudoers"。 3>、编辑/etc/sudoers文件。也就是输入命令"vim /etc/sudoers",输入"i"进入编辑模式,找到这一行:"root ALL=(ALL) ALL"在起下面添加"xxx ALL=(ALL) ALL"(这里的xxx是你的用户名),然后保存(就是先摁一下Esc键,然后输入":wq")退出。 4>、撤销文件的写权限。也就是输入命令"chmod u-w /etc/sudoers"。 至此,问题解决。
#启动 /usr/local/nginx/start_perl_cgi.sh start
-
创建nagios相关的用户及用户组和安装路径
- 安装Nagios
-
编译安装Nagios
wget http://prdownloads.sourceforge.net/sourceforge/nagios/nagios-3.4.1.tar.gz tar -zxvf nagios-3.4.1.tar.gz cd nagios ./configure --prefix=/usr/local/nagios --with-command-group=nagcmd make all make install - This installs the main program CGIs and HTML files --安装主程序的CGIs and HTML文件 make install-init - This installs the init script in /etc/rc.d/init.d --创建自动启动脚本 make install-commandmode - This installs and configures permissions on the directory for holding the external command file - 配置目录权限 make install-config # 安装示例配置文件到/usr/local/nagios/etc - This installs *SAMPLE* config files in /usr/local/nagios/etc You'll have to modify these sample files before you can use Nagios. Read the HTML documentation for more info on doing this. Pay particular attention to the docs on object configuration files, as they determine what/how things get monitored! #这里是在Nginx下运行Nagios,下面就不用做了 make install-webconf - This installs the Apache config file for the Nagios web interface make install-exfoliation - This installs the Exfoliation theme for the Nagios web interface make install-classicui - This installs the classic theme for the Nagios web interface
注:
make install 用于安装主要的程序、CGI及HTML文件
make install-init 用于生成init启动脚本
make install-config 用于安装示例配置文件
make install-commandmode 用于设置相应的目录权限
make install-webconf 用于安装Apache配置文件 -
验证程序是否被正确安装
切换目录到安装路径,这里是/usr/local/nagios,看是否存在etc、bin、 sbin、 share、 var这五个目录,如果存在则可以表明程序被正确的安装到系统了。ls /usr/local/nagios/ bin/ etc/ sbin/ share/ var/
注:
bin–Nagios执行程序所在目录,其中的nagios文件即为主程序。
etc–Nagios配置文件位置
sbin–Nagios cgi文件所在目录,也就是执行外部命令所需文件所在的目录
Share–Nagios网页文件所在的目录
var–Nagios日志文件、spid 等文件所在的目录
var/archives–日志归档目录
var/rw–用来存放外部命令文件 -
编译并安装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 --prefix=/usr/local/nagios --with-nagios-user=nagios --with-nagios-group=nagios make && make install
-
安装中文语言插件
wget http://sourceforge.net/projects/nagios-cn/files/sourcecode/zh_CN%203.2.3/nagios-cn-3.2.3.tar.bz2/download tar -jxvf nagios-cn-3.2.3.tar.bz2 cd nagios-cn-3.2.3 ./configure --prefix=/usr/local/nagios make all && make install
- 配置NGINX
-
配置Nagios Web界面登陆帐号及密码
htpasswd -c /usr/local/nagios/etc/nagiospasswd mike
如果你没有htpasswd(这个工具由Apache安装包所提供),可在线生成需要加密数据
-
访问http://www.4webhelp.net/us/password.php生成需要加密数据
创建加密验证文件vi /usr/local/nagios/etc/nagiospasswd #加入生成的加密数据,冒号前是用户名,后面是加密后的密码 chenai:78S3w.vy2HtE2
-
修改Nagios配置文件,给新增的用户增加访问权限
vi /usr/local/nagios/etc/cgi.cfg #以下几项中分别加入新增的用户,多用户用逗号分隔。 authorized_for_system_information=nagiosadmin,chenai authorized_for_configuration_information=nagiosadmin,chenai authorized_for_system_commands=nagiosadmin,chenai authorized_for_all_services=nagiosadmin,chenai authorized_for_all_hosts=nagiosadmin,chenai authorized_for_all_service_commands=nagiosadmin,chenai authorized_for_all_host_commands=nagiosadmin,chenai
-
修改NGINX配置,以支持WEB方式访问Nagios
方法一:以http://ip方式访问server { listen 80; server_name 192.168.1.108; index index.html index.htm index.php; root /usr/local/nagios/share; #权限 auth_basic "Nagios Access"; auth_basic_user_file /usr/local/nagios/etc/nagiospasswd; 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|pl)?$ { gzip off; root /usr/local/nagios/sbin; rewrite ^/nagios/cgi-bin/(.*)\.cgi /$1.cgi break; fastcgi_pass unix:/usr/local/webserver/nginx/logs/perl-fcgi.sock; fastcgi_param SCRIPT_FILENAME /usr/local/nagios/sbin$fastcgi_script_name; fastcgi_index index.cgi; fastcgi_read_timeout 60; fastcgi_param REMOTE_USER $remote_user; include fcgi.conf; auth_basic "Nagios Access"; auth_basic_user_file /usr/local/nagios/etc/nagiospasswd; } location /nagios { alias /usr/local/nagios/share; auth_basic "Nagios Access"; auth_basic_user_file /usr/local/nagios/etc/nagiospasswd; } log_format wwwlogs '$remote_addr - $remote_user [$time_local] "$request" ' '$status $body_bytes_sent "$http_referer" ' '"$http_user_agent" $http_x_forwarded_for'; access_log /data1/logs/access_www.log wwwlogs; }
方法二:以http://ip/nagios方式访问
在WEB主目录下创建一个软链ln -s /usr/local/nagios/share/ ln -s /usr/local/nagios/share/ /home/www/htdocs/nagios
server { listen 80; server_name localhost; root /home/www/htdocs; index index.php index.html index.htm; location ~ \.php$ { fastcgi_pass 127.0.0.1:9000; fastcgi_index index.php; fastcgi_param SCRIPT_FILENAME /home/www/blog$fastcgi_script_name; include fastcgi_params; } location ~ .*\.(cgi|pl)?$ { gzip off; root /usr/local/nagios/sbin; rewrite ^/nagios/cgi-bin/(.*)\.cgi /$1.cgi break; fastcgi_pass unix:/usr/local/nginx/logs/perl-fcgi.sock; fastcgi_param SCRIPT_FILENAME /usr/local/nagios/sbin$fastcgi_script_name; fastcgi_index index.cgi; fastcgi_read_timeout 60; fastcgi_param REMOTE_USER $remote_user; include fcgi.conf; auth_basic "Nagios Access"; auth_basic_user_file /usr/local/nagios/etc/nagiospasswd; } }
- 启动服务
-
启动前先检查下配置文件是否正确
/usr/local/nagios/bin/nagios -v /usr/local/nagios/etc/nagios.cfg
-
如果没有报错,可以启动Nagios服务
/usr/local/nagios/bin/nagios -d /usr/local/nagios/etc/nagios.cfg
-
查看Nagios运行状态
/usr/local/nagios/bin/nagiostats
- 启动服务
由于Nagios只能监测自己所在的主机的一些本地情况,例如,cpu负载、内存使用、硬盘使用等等。如果想要监测被监控的服务器上的这些本地情况,就要用到NRPE。NRPE(Nagios Remote Plugin Executor)是Nagios的一个扩展,它被用于被监控的服务器上,向Nagios监控平台提供该服务器的一些本地的情况。NRPE可以称为Nagios的Linux客户端。
apt-get install libssl-dev libssl0.9.8
tar zxvf nrpe-2.12.tar.gz cd nrpe-2.12 ./configure make all make install-plugin make install-daemon make install-daemon-config
/usr/local/nagios/bin/nrpe -c /usr/local/nagios/etc/nrpe.cfg -d
/usr/local/nagios/libexec/check_nrpe -H localhost
注:如果成功,会返回NRPE的版本号