CentOS 6.2+Nginx+Nagios,手机短信和qq邮箱提醒

分类:CentOS运维 阅读:3291 次

CentOS 6.2+Nginx+Nagios,手机短信和qq邮箱提醒


注:192.168.0.21 服务端

192.168.0.22 客户端

环境:两台centos6.0 64位系统,都已经搭建好了源码的lnmp平台

结尾附上所需的软件包


1.nagios安装(中文版)

123456789101112tarxvf nagios-cn-3.2.3.tar.bz2cdnagios-cn-3.2.3useradd-m -s /bin/bashnagiosusermod-a -G nagcmd nagios./configure--prefix=/usr/local/nagios--with-command-group=nagcmdmakemakeallmakeinstallmakeinstall-init # 生成init启动脚本makeinstall-config # 安装示例配置文件makeinstall-commandmode # 设置相应的目录权限chmodo+rwx /usr/local/nagios/var/rw

2.nagios-plugins安装

12345678wget http://prdownloads.sourceforge.net/sourceforge/nagiosplug/nagios-pluginstarzxvf nagios-plugins-1.4.16.tar.gzcdnagios-plugins-1.4.16yum installmakeapr* autoconf automake curl curl-devel gcc gcc-c++ zlib-devel \openssl openssl-devel pcre-devel gd gd-devel kernel keyutils patch perl perl-devel \kernel keyutils kernel-headers compat* mpfr cpp glibc libgomp libstdc++-devel ppl \cloog-ppl keyutils-libs-devel libcom_err-devel libsepol-devel libselinux-devel \krb5-devel zlib-devel libXpm* freetype libjpeg* libpng* php-common php-gd ncurses* libtool* libxml2 libxml2-devel patch -y
123./configure--prefix=/usr/local/nagios--with-mysql=/home/mysql/makemakeinstall


3.nrpe安装

123456789101112tarxzvf nrpe-2.12.tar.gzcdnrpe-2.12./configuremake./configuremakeallmakeinstall-pluginmakeinstall-daemonmakeinstall-daemon-config\cpsrc/check_nrpe/usr/local/nagios/libexec//usr/local/nagios/bin/nrpe-c /usr/local/nagios/etc/nrpe.cfg -decho'/usr/local/nagios/bin/nrpe -c /usr/local/nagios/etc/nrpe.cfg -d'>> /etc/rc.local
12345要重启nrpe进行就先杀掉进行,然后重启kill`psaux |grepnrpe |grep-vgrep|awk'{print $2}'`/usr/local/nagios/bin/nrpe-c /usr/local/nagios/etc/nrpe.cfg -d本机测试下:/usr/local/nagios/libexec/check_nrpe-H localhost -c check_users


加入系统服务

123456加入系统服务并设为开机自动chkconfig --add nagioschkconfig nagios onchownnagios.nagios /usr/local/nagios/var/rw# 测试配置文件是否正确/usr/local/nagios/bin/nagios-v/usr/local/nagios/etc/nagios.cfg


添加别名命令,方便测试配置文件

12345vi~/.bashrc在里面用alias来自定义一个命令来代替,这里我用checkaliascheck='/usr/local/nagios/bin/nagios -v /usr/local/nagios/etc/nagios.cfg'source~/.bashrc此时可以用check命令来检测配置文件了


修改联系人邮箱,修改为用于报警接收的邮件地址

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051vi/usr/local/nagios/etc/objects/contacts.cfg################################################################################ CONTACTS.CFG - SAMPLE CONTACT/CONTACTGROUP DEFINITIONS## Last Modified: 05-31-2007## NOTES: This config file provides you with some example contact and contact# group definitions that you can reference in host and service# definitions.## You don't need to keep these definitions in a separate file from your# other object definitions. This has been done just to make things# easier to understand.################################################################################################################################################################################################################################################ CONTACTS################################################################################################################################################################ Just one contact defined by default - the Nagios admin (that's you)# This contact definition inherits a lot of default values from the 'generic-contact'# template which is defined elsewhere.define contact{contact_name nagiosadmin ; Short name of useruse generic-contact ; Inherit default values from generic-contact template (defined above)aliasNagios Admin ; Full name of useremail nagios@localhost ; <<***** CHANGE THIS TO YOUR EMAIL ADDRESS ******}################################################################################################################################################################ CONTACT GROUPS################################################################################################################################################################ We only have one contact in this simple configuration file, so there is# no need to create more than one contact group.define contactgroup{contactgroup_name adminsaliasNagios Administratorsmembers nagiosadmin}定义check_nrpe命令vi/usr/local/nagios/etc/objects/commands.cfgdefine command{command_name check_nrpecommand_line /usr/local/nagios/libexec/check_nrpe-H $HOSTADDRESS$ -c $ARG1$}

检测配置文件是否有误

check

CentOS 6.2+Nginx+Nagios,手机短信和qq邮箱提醒

CentOS 6.2+Nginx+Nagios,手机短信和qq邮箱提醒

12345678910111213141516171819202122232425nginx 配置,Nginx fastcgi perl (pl、cgi)支持安装FCGI模块cdtarzxvf FCGI-0.70.tar.gzcdFCGI-0.70perl Makefile.PLmakemakeinstallcd安装 IO 和 IO::ALL模块tarzxvf IO-1.25.tar.gzcdIO-1.25perl Makefile.PLmakemakeinstallcdtarzxvf IO-All-0.41.tar.gzcdIO-All-0.41perl Makefile.PLmakemakeinstallcdunzip perl-fcgi.zipcpperl-fcgi.pl /usr/local/nginx/chmod755 /usr/local/nginx/perl-fcgi.pl


12345678910111213141516171819202122232425262728293031323334vi/usr/local/nginx/start_perl_cgi.sh#!/bin/bash#set -xdir=/usr/local/nginx/stop (){#pkill -f $dir/perl-fcgi.plkill$(cat$dir/logs/perl-fcgi.pid)rm$dir/logs/perl-fcgi.pid 2>/dev/nullrm$dir/logs/perl-fcgi.sock 2>/dev/nullecho"stop perl-fcgi done"}start (){rm$dir/now_start_perl_fcgi.sh 2>/dev/nullchownnobody.root $dir/logsecho"$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.shchownnobody.nobody $dir/now_start_perl_fcgi.shchmodu+x $dir/now_start_perl_fcgi.shsudo-u nobody $dir/now_start_perl_fcgi.shecho"start perl-fcgi done"}case$1 instop)stop;;start)start;;restart)stopstart;;esac


把start_perl_cgi.sh文件中的nobody全部用nagios替换,nginx 目录上的用户


123sed-i 's@nobody@nagios@g'/usr/local/nginx/start_perl_cgi.shchmod755 /usr/local/nginx/start_perl_cgi.sh/usr/local/nginx/start_perl_cgi.sh start

CentOS 6.2+Nginx+Nagios,手机短信和qq邮箱提醒

12345# 取消用户认证(方便调试)vi/usr/local/nagios/etc/cgi.cfg找到use_authentication=1并把值改为0修改联系人邮箱,修改为用于报警接收的邮件地址vi/usr/local/nagios/etc/objects/contacts.cfg

CentOS 6.2+Nginx+Nagios,手机短信和qq邮箱提醒


CentOS 6.2+Nginx+Nagios,手机短信和qq邮箱提醒

到这一步就是正常的

下面nginx 配置

我把监听改成80的了

CentOS 6.2+Nginx+Nagios,手机短信和qq邮箱提醒

然后开启服务

就可以访问了,然后继续安装客户端,最后给大家截图看效果

service nagios start



nagios被控端安装

12345678910111213141516yum installopenssl-devel -y1. nagios-plugins安装groupadd nagiosuseraddnagios -M -s /sbin/nologin-g nagiostarxvf nagios-plugins-1.4.16.tar.gzcdnagios-plugins-1.4.16./configure--prefix=/usr/local/nagios--with-nagios-user=nagios --with-nagios-gourp=nagios --with-mysql=/usr/local/mysql&& make&& makeinstallcd2. nrpe安装tarzxvf nrpe-2.13.tar.gzcdnrpe-2.13./configuremakeallmakeinstall-pluginmakeinstall-daemonmakeinstall-daemon-config

123启动nrpe/usr/local/nagios/bin/nrpe-c /usr/local/nagios/etc/nrpe.cfg -decho'/usr/local/nagios/bin/nrpe -c /usr/local/nagios/etc/nrpe.cfg -d'>> /etc/rc.local


监控服务端本机:自己监控自己不需要配置nrpe,服务端的nrpe只用于获取客户端的nrpe传送过来的数据,在这里因为中文版的nagios已经默认有些配置,等会儿修改下直接用了

监控客户端:监控的服务有:mysql、nginx、memory、ip连接数、僵死的进程、磁盘空间、磁盘IO、登录用户数、进程总数、cpu负载、PING、SSH

123unzip libexec.zip\cplibexec/* /usr/local/nagios/libexecchmod-R +x /usr/local/nagios/libexec

装插件

12345创建一个空的数据库nagios,授权nagios这个用户从任何地方访问nagios这个数据库,刷新授权设置,查询下nagios这个用户是否创建成功create database nagios;grant selecton nagios.* to nagios @'%'identified by '123456';flush privileges;selectUser,Password,Host from mysql.user;


CentOS 6.2+Nginx+Nagios,手机短信和qq邮箱提醒

12345678添加mysql库到系统搜索库vim /etc/ld.so.conf/usr/local/mysql/libldconfig要监控磁盘io,还得安装sysstat这个工具包yum installsysstat -y配置客户端上面的nrpevim /usr/local/nagios/etc/nrpe.cfg

CentOS 6.2+Nginx+Nagios,手机短信和qq邮箱提醒


CentOS 6.2+Nginx+Nagios,手机短信和qq邮箱提醒

CentOS 6.2+Nginx+Nagios,手机短信和qq邮箱提醒

CentOS 6.2+Nginx+Nagios,手机短信和qq邮箱提醒

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184配置客户端上面的nrpevim /usr/local/nagios/etc/nrpe.cfgcommand[check_users]=/usr/local/nagios/libexec/check_users-w 5 -c 10command[check_load]=/usr/local/nagios/libexec/check_cpu.sh -w 80% -c 90%command[check_sda1]=/usr/local/nagios/libexec/check_disk-w 20% -c 10% -p /dev/sda1command[check_sda2]=/usr/local/nagios/libexec/check_disk-w 20% -c 10% -p /dev/sda2command[check_zombie_procs]=/usr/local/nagios/libexec/check_procs-w 5 -c 10 -s Zcommand[check_total_procs]=/usr/local/nagios/libexec/check_procs-w 150 -c 200command[check_swap]=/usr/local/nagios/libexec/check_swap-w 20% -c 10%command[check_iostat]=/usr/local/nagios/libexec/check_iostat.sh -d sda -w 6 -c 10command[check_mysql]=/usr/local/nagios/libexec/check_mysql-H 192.168.0.22 -u nagios -p 123456 -d nagioscommand[check_nginx]=/usr/local/nagios/libexec/check_nginx.sh -u 192.168.0.22 -p /status-w 4000 -c 5000command[check_mem]=/usr/local/nagios/libexec/check_memory.pl -f -w 20 -c 10command[check_ip_conn]=/usr/local/nagios/libexec/ip_conn.sh 200 250command[check_ssh]=/usr/local/nagios/libexec/check_tcp-p 22 -w 1.0 -c 10.0配置完成后,重启nrpekill`psaux |grepnrpe |grep-vgrep|awk'{print $2}'`/usr/local/nagios/bin/nrpe-c /usr/local/nagios/etc/nrpe.cfg -d服务端配置:监控服务端本机的配置:vim /usr/local/nagios/etc/objects/localhost.cfg修改里面的配置,最后修改完成的配置如下define host{use linux-serverhost_name localhostaliaslocalhostaddress 127.0.0.1icon_image server.gifstatusmap_image server.gd22d_coords 500,2003d_coords 500,200,100}define hostgroup{hostgroup_name linux-servers ; The name of the hostgroupaliasLinux Servers ; Long name of the groupmembers * ; Comma separated list of hosts that belong to this group}define servicegroup{servicegroup_name 全部联通性检查alias联通性检查members localhost,PING,nagios-client,PING}define service{