nagios 4.08配置步骤

2014-09-16

Nagios是一款开源的免费网络监视工具,能有效监控Windows、Linux和Unix的主机状态,交换机路由器等网络设置,打印机等。在系统或服务状态异常时发出邮件或短信报警第一时间通知运维人员,在状态恢复后发出正常的邮件或短信通知。

主要步骤,安装nginx,安装php,安装nagios,安装nagios-plugins,安装oracle插件check_oracle_health,安装perl-cgi,安装飞信fetion,安装sendmail,错误实例,首页注释,监测文件/var/log/message生命实例,oracle监测实例,进程监测实例。

1、安装nginx
################################################################################################
#!/bin/bash

yum -y update

export DEFAULT=/usr/local/nginx/conf/default
export LNMP=/opt/LNMP
export CONF=/usr/local/nginx/conf
LOG=/data/logs/nginx
cd $LNMP

#wget -c http://nginx.org/download/nginx-1.6.1.tar.gz -P ${LNMP}/untar
#wget -c ftp://61.135.158.199/pub/openssl-1.0.1i.tar.gz -P ${LNMP}/untar
#pcre-8.13.tar.gz
#zlib-1.2.5.tar.bz2

tar zxf untar/pcre-8.13.tar.gz
pushd pcre-8.13
./configure;make;make install
popd
tar zxf untar/nginx-1.6.1.tar.gz
tar jxf untar/zlib-1.2.5.tar.bz2
tar zxf untar/openssl-1.0.1i.tar.gz

pushd nginx-1.6.1
./configure \
--prefix=/usr/local/nginx \
--sbin-path=/usr/sbin \
--with-http_ssl_module \
--with-http_sub_module \
--with-http_flv_module \
--with-http_stub_status_module \
--with-zlib=../zlib-1.2.5 \
--with-openssl=../openssl-1.0.1i \
--with-pcre=../pcre-8.13
make;make install
popd
groupadd www
useradd -g www -d /dev/null -s /sbin/nologin www

/bin/cp $LNMP/conf/init.nginx /etc/init.d/nginx
mkdir ${CONF}/default
mv ${CONF}/*.default ${CONF}/default
/bin/cp $LNMP/conf/*.conf ${CONF}
/bin/cp $LNMP/conf/star.* ${CONF}
/bin/cp $LNMP/conf/root /var/spool/cron/
mkdir -p ${LOG}
/bin/cp conf/iftop /usr/local/sbin/
/bin/cp conf/netstat2 /bin
/bin/cp conf/nmon /bin

chmod 755 /etc/init.d/nginx
/etc/init.d/nginx start
chkconfig nginx on
netstat -ntl |grep 80
################################################################################################
2、安装PHP
#!/bin/bash
####################php##################3
export LNMP=/opt/LNMP
cd $LNMP
pushd zlib-1.2.5
./configure && make && make instal/
popd

tar xf untar/libpng-1.2.46.tar
pushd libpng-1.2.46
./configure;make;make install
popd

tar zxf untar/jpegsrc.v8c.tar.gz
pushd jpeg-8c
./configure -enable-shared -enable-static
make && make install
popd

tar zxf untar/freetype-2.4.6.tar.gz
pushd freetype-2.4.6
./configure;make;make install
popd

tar zxf untar/gd-2.0.35.tar.gz
pushd gd-2.0.35
./configure;make;make install
popd

tar -zxf untar/libxml2-2.7.7.tar.gz
pushd libxml2-2.7.7
./configure;make;make install
popd

#if get this err
#./.libs/libxml2.so: undefined reference to `gzopen64'
#ldd ./.libs/libxml2.so
#cd zlib-1.2.5
#make uninstall
#make install

tar zxf untar/libiconv-1.14.tar.gz
pushd libiconv-1.14
./configure --prefix=/usr/local
make && make install
popd

tar xzf untar/libxslt-1.1.26.tar.gz && cd libxslt-1.1.26
./configure && make && make install && cd ../

tar zxf untar/autoconf-2.13.tar.gz && cd autoconf-2.13
./configure --prefix=/usr/local/autoconf-2.13;make;make install && cd ..

tar zxf untar/libmcrypt-2.5.8.tar.gz
cd libmcrypt-2.5.8/ && ./configure && make && make install
ln -s /usr/local/bin/libmcrypt_config /usr/bin/libmcrypt_config
/sbin/ldconfig

cd libltdl/
./configure --enable-ltdl-install && make && make install
cd /opt/LNMP

tar zxf untar/mhash-0.9.9.9.tar.gz
cd mhash-0.9.9.9/ && ./configure && make && make install && cd ../
ln -s /usr/local/lib/libmcrypt.la /usr/lib/libmcrypt.la
ln -s /usr/local/lib/libmcrypt.so /usr/lib/libmcrypt.so
ln -s /usr/local/lib/libmcrypt.so.4 /usr/lib/libmcrypt.so.4
ln -s /usr/local/lib/libmcrypt.so.4.4.8 /usr/lib/libmcrypt.so.4.4.8
ln -s /usr/local/lib/libmhash.a /usr/lib/libmhash.a
ln -s /usr/local/lib/libmhash.la /usr/lib/libmhash.la
ln -s /usr/local/lib/libmhash.so /usr/lib/libmhash.so
ln -s /usr/local/lib/libmhash.so.2 /usr/lib/libmhash.so.2
ln -s /usr/local/lib/libmhash.so.2.0.1 /usr/lib/libmhash.so.2.0.1
ln -s /usr/local/bin/libmcrypt-config /usr/bin/libmcrypt-config

tar zxf untar/mcrypt-2.6.8.tar.gz
cd mcrypt-2.6.8/
/sbin/ldconfig
./configure ; make ; make install ; cd ../

#tar zxf untar/libevent-1.4.14b-stable.tar.gz
#cd libevent-1.4.14b-stable && ./configure && make && make install && cd ..
#/sbin/ldconfig

tar zxf untar/libevent-2.0.16-stable.tar.gz
pushd libevent-2.0.16-stable
./configure && make && make install
popd
/sbin/ldconfig

chown -R root:root *

#!/bin/bash
LNMP=/opt/LNMP
cd $LNMP
# php 5.3.25
export PHP_AUTOCONF=/usr/local/autoconf-2.13/bin/autoconf
export PHP_AUTOHEADER=/usr/local/autoconf-2.13/bin/autoheader

tar jxf untar/php-5.3.25.tar.bz2
#tar jxf untar/openssl-1.0.1c.tar.gz
pushd php-5.3.25
./buildconf --force
./configure \
--prefix=/usr/local/php \
--with-config-file-path=/usr/local/php/etc \
--with-mysql=mysqlnd \
--with-mysqli=mysqlnd \
--with-iconv-dir \
--with-freetype-dir \
--with-jpeg-dir \
--with-png-dir \
--with-zlib \
--with-libxml-dir=/usr \
--enable-xml \
--enable-magic-quotes \
--enable-safe-mode \
--enable-bcmath \
--enable-shmop \
--enable-sysvsem \
--enable-inline-optimization \
--with-curl \
--with-curlwrappers \
--enable-mbregex \
--enable-fpm \
--enable-mbstring \
--with-mcrypt \
--enable-ftp \
--with-gd \
--enable-gd-native-ttf \
--with-openssl \
--with-mhash \
--enable-pcntl \
--enable-sockets \
--with-xmlrpc \
--enable-zip \
--enable-soap \
--without-pear \
--with-gettext
#--with-imap=/usr/local/imap-2007f \
#--with-imap-ssl=/usr/local/openssl

make ZEND_EXTRA_LIBS='-liconv'
make test
make install
popd

mkdir -p /usr/local/php/etc

cp untar/conf/php.ini /usr/local/php/etc
cp untar/conf/php-fpm.conf /usr/local/php/etc
cp untar/conf/init.php-fpm /etc/init.d/php-fpm
chmod +x /etc/init.d/php-fpm
chkconfig php-fpm on
cd ../
ln -sf /usr/local/php/bin/php /usr/bin/php
ln -sf /usr/local/php/bin/phpize /usr/bin/phpize
ln -sf /usr/local/php/sbin/php-fpm /usr/bin/php-fpm

#start php-fpm 9002
#/etc/init.d/php-fpm restart
#netstat -ntl |grep 9002

#!/bin/bash
##################php#######addtional##################1
LNMP=/data/LNMP

#extension eaccelerator
cd /opt/LNMP
tar jxf untar/eaccelerator-0.9.6.1.tar.bz2
pushd eaccelerator-0.9.6.1/
/usr/local/php/bin/phpize
./configure --enable-eaccelerator=shared --with-php-config=/usr/local/php/bin/php-config
make;make install
popd
mkdir -p /usr/local/php/eaccelerator_cache && chmod 777 /usr/local/php/eaccelerator_cache
#/usr/local/php/bin/php -v

#extension memcache
cd /opt/LNMP
tar zxf untar/memcache-3.0.6.tgz && cd memcache-3.0.6/
/usr/local/php/bin/phpize
./configure --with-php-config=/usr/local/php/bin/php-config
make && make install && cd ../

tar zxf untar/PDO_MYSQL-1.0.2.tgz && cd PDO_MYSQL-1.0.2/
/usr/local/php/bin/phpize
./configure --with-php-config=/usr/local/php/bin/php-config --with-pdo-mysql=/usr/local/mysql
make && make install && cd ../

tar zxf untar/ImageMagick-6.7.2-10.tar.gz
pushd ImageMagick-6.7.2-10
./configure
make && make install
popd

tar zxf untar/imagick-3.0.1.tgz
pushd imagick-3.0.1
/usr/local/php/bin/phpize
./configure --with-php-config=/usr/local/php/bin/php-config
make;make install
popd

cd /opt/LNMP
x86=`uname -m`
printf "$x86 \n"
zend_32()
{
tar zxf untar/ZendGuardLoader-php-5.3-linux-glibc23-i386.tar.gz
rm -rf /usr/local/php/lib/php/extensions/no-debug-non-zts-20090626/ZendGuardLoader.so
cp /opt/LNMP/ZendGuardLoader-php-5.3-linux-glibc23-i386/php-5.3.x/ZendGuardLoader.so \
/usr/local/php/lib/php/extensions/no-debug-non-zts-20090626
}
zend_64()
{
tar zxf untar/ZendGuardLoader-php-5.3-linux-glibc23-x86_64.tar.gz
rm -rf /usr/local/php/lib/php/extensions/no-debug-non-zts-20090626/ZendGuardLoader.so
cp /opt/LNMP/ZendGuardLoader-php-5.3-linux-glibc23-x86_64/php-5.3.x/ZendGuardLoader.so \
/usr/local/php/lib/php/extensions/no-debug-non-zts-20090626
}

case "$x86" in

i686|i386|i586)
zend_32 && exit 0
;;
x86_64)
zend_64 || exit 0
;;
*)
exit 2
esac
#######php-fpm########3
/etc/init.d/php-fpm restart
#netstat -ntl |grep 9002

pushd /usr/local/nginx/html
cat > p.php <<EOF
<? phpinfo(); ?>
EOF
popd
#########################end##################315

3、安装nagios 4.0.8
#!/bin/bash

/usr/sbin/useradd -m nagios
/usr/sbin/groupadd nagcmd
/usr/sbin/usermod -a -G nagcmd nagios
/usr/sbin/usermod -a -G nagcmd www

export NAGIOS=/opt/nagios
cd $NAGIOS

# wget https://assets.nagios.com/downloads/nagioscore/releases/nagios-4.3.4.tar.gz
# wget https://nagios-plugins.org/download/nagios-plugins-2.2.1.tar.gz
#wget -c ftp://61.135.158.199/pub/nagios-4.0.8.tar.gz -P ${NAGIOS}/untar
#wget -c ftp://61.135.158.199/pub/nagios-plugins-2.0.3.tar.gz -P ${NAGIOS}/untar

tar zxf untar/nagios-4.0.8.tar.gz
pushd nagios-4.0.8
./configure --prefix=/usr/local/nagios --with-command-group=nagcmd
make all;
make install
make install-init
make install-commandmode
make install-config
popd

4、安装nagios-plugins
tar zxf untar/nagios-plugins-2.0.3.tar.gz
pushd nagios-plugins-2.0.3
make;make install
popd

tar zxf untar/check_oracle_health-1.9.3.4.tar.gz
pushd check_oracle_health-1.9.3.4
make;make install;
popd

5、安装CGI
#!/bin/bash

cd /opt/nagios

tar -zxvf untar/FCGI-0.74.tar.gz
pushd FCGI-0.74
perl Makefile.PL
make && make install
popd

#http://search.cpan.org/CPAN/authors/id/B/BO/BOBTFISH/FCGI-ProcManager-0.24.tar.gz
tar zxf untar/FCGI-ProcManager-0.24.tar.gz
pushd FCGI-ProcManager-0.24
perl Makefile.PL
make; make install
popd

tar zxvf untar/IO-All-0.39.tar.gz
pushd IO-All-0.39
perl Makefile.PL
make && make install
popd

tar zxf untar/Class-Accessor-Tiny-0.10.tar.gz
pushd Class-Accessor-Tiny-0.10
perl Makefile.PL
make;make install
popd

tar zxf untar/FCGI-Restarter-0.12.tar.gz
pushd FCGI-Restarter-0.12
perl Makefile.PL
make;make install
popd

保存3个脚本到/usr/local/nagios/bin
perl-cgi.pl太长忽略
fcgi.pl太长忽略
start_nginx_cgi.sh
#!/bin/bash

## start_nginx_cgi.sh: start nginx cgi mode

PERL="/usr/bin/perl"
NGINX_CGI_FILE="/usr/local/nagios/bin/fcgi.pl"
export FCGI_SOCKET_PATH=/var/run/nagios.sock

#bg_num=`jobs -l |grep "NGINX_CGI_FILE"`
PID=`ps aux|grep "perl-fcgi"|cut -c10-14|xargs kill -9`
#PID=`ps aux|grep 'perl-fcgi'|cut -c10-14|sed -n "1P"`
echo $PID
sockfiles="/var/run/nagios.sock"
kill -9 $PID

$PERL $NGINX_CGI_FILE &

sleep 3

`chown www.www $sockfiles`

6、安装fetion
下载fetion,解压到/usr/local/fetion
相关链接:
http://bbs.it-adv.net/viewthread.php?tid=1081

http://www.it-adv.net/fetion/cenos54X64_20101113.rar
http://www.it-adv.net/fetion/linuxso_20101113.rar
http://www.it-adv.net/fetion/win32dll_20101113.rar

创建fetion.sh
#!/bin/sh

receiver=$1
messages=$2
echo "send sms[$receiver]:$messages">>/usr/local/fetion/log/info.log
/usr/local/fetion/fetion --config=/usr/local/fetion/conf/id.conf --index=1 --to="$receiver" --msg-utf8=
"$messages" --exit-on-verifycode >> /usr/local/fetion/log/info.log
#################################################################################################################

vi /etc/ld.so.conf.d/fetion.conf
/usr/local/fetion/lib
ldconfig
测试
ldd /usr/local/fetion/fetion

7、配置sendmail
yum -y install sendmail sendmail-cf
vi /etc/mail/sendmail.cf
DSmail.test.com
vi /etc/submit.mc
添加
MASQUERADE_AS(`abc.com')dnl
FEATURE(`masquerade_envelope')dnl
m4 submit.mc > submit.cf

8、配置邮件或飞信
编辑/usr/local/nagios/etc/nagios.cfg
cfg_file=/usr/local/nagios/etc/objects/commands.cfg
cfg_file=/usr/local/nagios/etc/objects/templates.cfg
cfg_file=/usr/local/nagios/etc/objects/timeperiods.cfg
cfg_file=/usr/local/nagios/etc/objects/contacts.cfg

commands.cfg增加
# 'notify-host-by-email' command definition
define command{
command_name notify-host-by-email
command_line /usr/bin/printf "%b" "***** Nagios *****\n\nNotification Type: $NOTIFICATIONTYP
E$\nHost: $HOSTNAME$\nState: $HOSTSTATE$\nAddress: $HOSTADDRESS$\nInfo: $HOSTOUTPUT$\n\nDate/Time: $LON
GDATETIME$\n" | /bin/mail -s "** $NOTIFICATIONTYPE$ Host Alert: $HOSTNAME$ is $HOSTSTATE$ **" $CONTACTE
MAIL$
}
# 'notify-service-by-email' command definition
define command{
command_name notify-service-by-email
command_line /usr/bin/printf "%b" "***** Nagios *****\n\nNotification Type: $NOTIFICATIONTYP
E$\n\nService: $SERVICEDESC$\nHost: $HOSTALIAS$\nAddress: $HOSTADDRESS$\nState: $SERVICESTATE$\n\nDate/
Time: $LONGDATETIME$\n\nAdditional Info:\n\n$SERVICEOUTPUT$\n" | /bin/mail -s "** $NOTIFICATIONTYPE$ Se
rvice Alert: $HOSTALIAS$/$SERVICEDESC$ is $SERVICESTATE$ **" $CONTACTEMAIL$
}
define command{
command_name notify-service-by-sms
command_line /usr/local/fetion/fetion.sh $CONTACTPAGER$ "$NOTIFICATIONTYPE$: $HOSTNAME$/$SERVIC
EDESC$ is $SERVICESTATE$"
}

contact.cfg内容
define contactgroup{
contactgroup_name testadmin
members user01,user02
}
define contact{
contact_name user01
use generic-contact
email user01@abc.com
pager 手机号
}
define contact{
contact_name user02
use generic-contact
email user02@abc.com
pager 手机号
}

9、创建htpasswd.users,配置Nginx
htpasswd.users
user01:adfjalsjdfsd
user02:adjfajsdlfjl

vi /usr/local/nginx/conf/nginx.conf
server {
listen 8088;
root /data/deploy/www;
index index.php index.html index.cgi;
location /nagios{
auth_basic "auther";
auth_basic_user_file /usr/local/nagios/etc/htpasswd.users;
}
location ~ \.php$ {
fastcgi_pass 127.0.0.1:9002;
fastcgi_index index.php;
include fastcgi.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;
include fastcgi.conf;
}

10、访问nagios
在/data/deploy/www里面创建链接
ln -s /usr/local/nagios/share /data/deploy/www/nagios
访问http://ip:8088/nagios

11、在需要监测的服务器安装nrpe
more ins_nrpe.sh
yum -y install xinetd && /usr/sbin/useradd -s /sbin/nologin -d /usr/local/nagios nagios && tar zxf nagi
os-plugins-1.4.15.tar.gz && cd nagios-plugins-1.4.15 && ./configure && make && make install && chown na
gios.nagios /usr/local/nagios && chown -R nagios.nagios /usr/local/nagios/libexec && cd .. && tar zxf n
rpe-2.13.tar.gz && cd nrpe-2.13 && ./configure && make all && make install-plugin && make install-daemo
n && make install-daemon-config && make install-xinetd && cd .. && cp -f nrpe.cfg /usr/local/nagios/etc
/ && cat services >> /etc/services && cp -f nrpe /etc/xinetd.d && /etc/init.d/xinetd restart

编辑添加nagios服务器ip
/etc/xinet.d/nrpe
disable = no
only_from = 127.0.0.1 172.16.1.68
/etc/init.d/xinetd restart
netstat -ntlp检查5666端口

12、在/usr/local/nagios/etc/nrpe.cfg增加监测配置
command[check_sda1]=/usr/local/nagios/libexec/check_disk -w 10% -c 5% -p /dev/sda1
command[check_tomcat]=/usr/local/nagios/libexec/check_procs -w 2:4 -a tomcat
command[check_msg]=/usr/local/nagios/libexec/check_file_age -w 43200 -c 86400 -f "/var/log/messages"

13、服务器配置
/usr/local/nagios/etc/config增加文件
more host-zw-172.16.1.70.cfg
define host{
host_name zw-172.10.1.70
use linux-server
address 172.10.1.70
contact_groups looyuadmin
hostgroups allhosts,dbserver
}
define service{
use generic-service
host_name zw-172.10.1.70
service_description oracle1521
check_command check_tcp!1521!0.0020!0.0050!10
}
define service{
use generic-service
host_name zw-172.10.1.70
service_description server load
check_command check_nrpe!check_load
}
define service{
use generic-service
host_name zw-172.10.1.70
service_description disk sda1
check_command check_nrpe!check_sda1
}

14、监测oracle端口及session数量
#!/bin/sh

ID=$1
if [[ -z "$1" ]];then
echo "Usage: $0 ID"
exit 0;
fi

sed -i '/ORACLE/d' ~/.bash_profile
grep ORACLE ~oracle/.bash_profile >> ~/.bash_profile
. ~/.bash_profile

chcon -t texrel_shlib_t $ORACLE_HOME/lib/libnnz10.so
chcon -t texrel_shlib_t $ORACLE_HOME/lib/libclntsh.so.10.1

sed -i '/^DB/,$d' $ORACLE_HOME/network/admin/tnsnames.ora
cat >> $ORACLE_HOME/network/admin/tnsnames.ora << EOF DB${ID} = (DESCRIPTION = (ADDRESS = (PROTOCOL = TCP)(HOST = 172.16.1.${ID})(PORT = 1521)) (CONNECT_DATA = (SERVER = DEDICATED) (SERVICE_NAME = mydb) ) ) EOF wget -c http://wallcopper.com/tar/ora_health.gz -P /opt cd /opt tar zxf ora_health.gz tar zxf DBI-1.631.tar.gz tar zxf DBD-Oracle-1.70.tar.gz tar zxf check_oracle_health-1.9.3.4.tar.gz pushd DBI-1.631; perl Makefile.PL;make;make install popd pushd DBD-Oracle-1.70; perl Makefile.PL;make;make install popd pushd check_oracle_health-1.9.3.4; ./configure;make;make install popd sed -i '/check_tnsping/d' /usr/local/nagios/etc/nrpe.cfg sed -i '/check_session/d' /usr/local/nagios/etc/nrpe.cfg cat > ora.txt << EOF
command[check_tnsping]=/usr/local/nagios/libexec/check_oracle_health --connect=DB${ID} --mode=tnsping
command[check_session]=/usr/local/nagios/libexec/check_oracle_health --connect=DB${ID} --username=system --password=123456 --mode=sql --name='select count(1) from v\$session' --name2=current_sessions --warning 100 --critical 300
EOF

sed -i '/check_total_procs/r ora.txt' /usr/local/nagios/etc/nrpe.cfg

/etc/init.d/xinetd restart
usermod -G oinstall nagios
source ~/.bash_profile

/usr/local/nagios/libexec/check_oracle_health --connect=DB${ID} --mode=tnsping
/usr/local/nagios/libexec/check_oracle_health --connect=DB${ID} --username=system --password=123456 --mode=sql --name='select c
ount(1) from v$session' --name2=current_sessions --warning 100 --critical 300

15、进程检查实例
被监测服务器(例如172.16.1.70)nrpe.cfg增加
command[check_ipconvert]=/usr/local/nagios/libexec/check_procs -w 1:1 -a IPConvert
nagios服务器(例如172.16.1.68)增加
/usr/local/nagios/etc/config/host-remote-172.16.1.70.cfg
define service{
use generic-service
host_name remote-172.16.1.70
service_description IPConvert
check_command check_nrpe!check_ipconvert
}

16、限制显示条数
vi /usr/local/nagios/etc/cgi.cfg
refresh_rate=90
result_limit=250/100/1000

result_limit=All

分类:Linux网络 | 标签: |

相关日志

评论被关闭!