Linux运维学习笔记之三十一:监控利器Nagios实战

118 阅读6分钟

a、存在位置

监控Windows主机。

b、作用

相当于Linux下的NRPE。

c、原理

(3)NDOUtils:不推荐使用

a、存在位置

Nagios服务器端。

b、作用

用于将Nagios的配置信息和各event产生的数据存入数据库以实现对这些数据的检索和处理。但是存入数据库还不如存放在磁盘上,因此不推荐使用。

c、原理

(4)NSCA:纯被动模式的监控

a、存在位置

同时安装在Nagios的服务器端和客户端。

b、作用

用于让被监控的远程Linux/Unix主机主动将监控到的信息发送给Nagios服务器。在分布式监控集群模式中要用到,300台服务器以内可以不考虑。

c、原理

5、Nagios的监控原理图
二、Nagios服务端安装
1、演示环境
HostOSroleremask
192.168.1.198RedHat6.4_32Nagios监控服务器服务端
192.168.1.218CentOS6.5_32LNMP_Web服务器被监控的客户端服务器
192.168.1.219CentOS6.5_32LNMP_Web服务器被监控的客户端服务器
2、安装前准备工作
(1)配置yum源

echo "------- Step 1 : Config yum -------"

cd /etc/yum.repos.d/

cp /etc/yum.repos.d/CentOS-Base.repo/etc/yum.repos.d/CentOS-Base.repo.bak

wget -O /etc/yum.repos.d/CentOS-Base.repohttp://mirrors.aliyum.com/repo/CentOS-6.repo

(2)配置字符集

echo "------- Step 2 : Config CharSet -------"

echo 'export LC_ALL=C' >> /etc/profile

source /etc/profile

(3)关闭防火墙和SELinux

echo "------- Step 3 : Stop iptables and SELinux -------"

a、关闭防火墙

/etc/init.d/iptables stop

/etc/init.d/ip6tables stop

chkconfig iptables off

chkconfig ip6tables off

b、关闭SELinux

setenforce 0

vi /etc/selinux/config  

SELINUX=disabled

c、脚本方式关闭SELinu

if [ if /etc/selinux/config ]; then

    sed -i's#SELINUX=enforcing#SELINUX=disable#g' /etc/selinux/config

    setenforce 0

fi

(4)配置时间同步任务(监控要求时间准确)

echo "------- Step 4 : Config CharSet -------"

/usr/sbin/ntpdate pool.ntp.org

echo "#time sync by my at date +%F " >> /var/spool/cron/root

echo '*/10 * * * * /usr/sbin/ntpdate pool.ntp.org >/dev/null2>&1' >> /var/spool/cron/root

crontab -l

(5)安装gcc和lamp环境(Nagios提供web界面查看,Nagios与httpd配合是官方推荐)

echo "------- Step 5 : Install gcc and lamp env etc-------"

yum install gcc glibc glibc-common -y   #编译环境

yum install gd gd-devel -y       #画图用

yum install httpd php php-gd -y #php环境

yum install mysql* -y   #非必须,但如不安装,nagios在安装时,就不会产生监控数据库的插件

yum install perl-devel -y #安装nagios插件时需要

3、安装
(1)增加nagios用户和组

echo "------- Step 6 : add nagios user and group -------"

/usr/sbin/useradd -m nagios

#/usr/sbin/useradd apache   #安装httpd时已安装

/usr/sbin/groupadd nagcmd

/usr/sbin/usermod -a -G nagcmd nagios

/usr/sbin/usermod -a -G nagcmd apache

(2)解压安装nagios软件包

echo "------- Step 7 : download and install nagios-------"

cd /tools

unzip oldboy_training_nagios_soft.zip

tar xzf nagios-3.5.1.tar.gz

cd nagios

./configure --with-command-group=nagcmd

make all

make install

make install-init   #This installs theinit script in /etc/rc.d/init.d

make install-config  # This installssample config files in /usr/local/nagios/etc

make install-commandmode  #installs andconfigures permissions the external command file

make install-webconf  #生成nagios在apache中的配置文件:/etc/httpd/conf.d/nagios.conf

cat /etc/httpd/conf.d/nagios.conf  #查看nagios.conf文件的内容

SAMPLE CONFIG SNIPPETS FOR APACHE WEB SERVER

Last Modified: 11-26-2005

This file contains examples of entries that need

to be incorporated into your Apache web server

configuration file. Customize the paths, etc. as

needed to fit your system.

 

ScriptAlias /nagios/cgi-bin "/usr/local/nagios/sbin"

 

<Directory "/usr/local/nagios/sbin">

#  SSLRequireSSL

   Options ExecCGI

   AllowOverride None

   Order allow,deny

   Allow from all

#  Order deny,allow

#  Deny from all

#  Allow from 127.0.0.1

   AuthName "NagiosAccess"

   AuthType Basic

   AuthUserFile /usr/local/nagios/etc/htpasswd.users

   Require valid-user

 

Alias /nagios "/usr/local/nagios/share"

 

<Directory "/usr/local/nagios/share">

#  SSLRequireSSL

   Options None

   AllowOverride None

   Order allow,deny

   Allow from all

#  Order deny,allow

#  Deny from all

#  Allow from 127.0.0.1

   AuthName "NagiosAccess"

   AuthType Basic

   AuthUserFile /usr/local/nagios/etc/htpasswd.users

   Require valid-user

(3)配置apache的web认证(也就是登陆web的用户和密码:test/123456)

echo "------- Step 8 : config web auth -------"

#一定要用/etc/httpd/conf.d/nagios.conf文件中指定的AuthUserFile的值一样,否则不能登陆

htpasswd -cb /usr/local/nagios/etc/htpasswd.users test 123456

cd ..

(4)安装nagios插件

echo "------- Step 9 : install nagios-plusins -------"

#yum install perl-devel -y #安装nagios插件时需要,需确认一下是否安装

tar zxf nagios-plugins-1.4.16.tar.gz

cd nagios-plugins-1.4.16

./configure --with-nagios-user=nagios --with-nagios-group=nagios--enable-perl-modules

make && make install

ls /usr/local/nagios/libexec/ | wc -l  #查看安装的插件的数量

61

(5)安装nrpe(因为服务端需要chek_nrpe插件)

echo "------- Step 10 : install nrpe -------"

tar zxf nrpe-2.12.tar.gz

cd nrpe-2.12

./configure

make all

make install-plugin

make install-daemon

make install-daemon-config

cd ..

(6)启动服务并检查

echo "------- Step 11 : startup service and check -------"

/etc/init.d/nagios start

/etc/init.d/httpd start

lsof -i tcp:80

ps -ef | grep nagios

(7)浏览器登陆验证

http://192.168.1.198

出现登陆框,用户输入test,密码输入123456后提示用户名密码错误,经检查原因如下:

配置文件/etc/httpd/conf.d/nagios.conf文件中指定的AuthUserFile的值为:

AuthUserFile /usr/local/nagios/etc/htpasswd.users

但创建密码文件时的文件为:

htpasswd -cb /usr/local/nagios/etc/htpasswd.user test 123456

重新创建密码文件后,成功登陆

htpasswd -cb /usr/local/nagios/etc/htpasswd.users test 123456

三、Nagios客户端安装
1、客户端需安装的软件

(1)不需要安装的软件

a、无需lamp环境。

不需要安装gd、gd-devel、mysql*、httpd、php、php-gd

b、无需nagios服务端软件包

不需要安装nagios-3.5.1.tar.gz

c、无需gcc环境

不需要安装gcc glibc glibc-common

(2)需要安装的软件

a、客户端软件:

nrpe-2.12

b、插件:

Class-Accessor-0.31.tar.gz

Config-Tiny-2.12.tar.gz

Math-Calc-Units-1.07.tar.gz

Nagios-Plugin-0.34.tar.gz

Params-Validate-0.91.tar.gz

Regexp-Common-2010010201.tar.gz

check_iostat

check_memory.pl

2、安装前准备工作
(1)配置yum源

echo "------- Step 1 : Config yum -------"

cd /etc/yum.repos.d/

cp /etc/yum.repos.d/CentOS-Base.repo/etc/yum.repos.d/CentOS-Base.repo.bak

wget -O /etc/yum.repos.d/CentOS-Base.repohttp://mirrors.aliyum.com/repo/CentOS-6.repo

(2)配置字符集

echo "------- Step 2 : Config CharSet -------"

echo 'export LC_ALL=C' >> /etc/profile

source /etc/profile

(3)关闭防火墙和SELinux

echo "------- Step 3 : Stop iptables and SELinux -------"

a、关闭防火墙

/etc/init.d/iptables stop

/etc/init.d/ip6tables stop

chkconfig iptables off

chkconfig ip6tables off

b、关闭SELinux

setenforce 0

vi /etc/selinux/config  

SELINUX=disabled

c、脚本方式关闭SELinu

if [ if /etc/selinux/config ]; then

    sed -i's#SELINUX=enforcing#SELINUX=disable#g' /etc/selinux/config

    setenforce 0

fi

(4)配置时间同步任务(监控要求时间准确)

echo "------- Step 4 : Config CharSet -------"

/usr/sbin/ntpdate pool.ntp.org

echo "#time sync by my at date +%F " >>/var/spool/cron/root

echo '*/10 * * * * /usr/sbin/ntpdate pool.ntp.org >/dev/null2>&1' >> /var/spool/cron/root

crontab -l

3、安装
(1)增加nagios用户和组

echo "------- Step 5 : add nagios user and group -------"

/usr/sbin/useradd -m nagios -s /sbin/nologin

(2)安装nagios插件

echo "------- Step 6 : install nagios-plusins -------"

#yum install perl-devel -y #安装nagios插件时需要,需确认一下是否安装

scp /wddg/tools/oldboy_training_nagios_soft.ziproot@192.168.1.218:/wddg/tools/

cd /wddg/tools/

unzip oldboy_training_nagios_soft.zip

tar zxf nagios-plugins-1.4.16.tar.gz

cd nagios-plugins-1.4.16

./configure --prefix=/application/nagios--enable-perl-modules --enable-redhat-pthread-workaround

make && make install

ls /application/nagios/libexec/ | wc -l  #查看安装的插件的数量

64

(3)安装nrpe

echo "------- Step 7 : install nrpe -------"

tar zxf nrpe-2.12.tar.gz

cd nrpe-2.12

./configure --prefix=/application/nagios  #目录必须与nagios-plugins目录一致

make all

make install-plugin

make install-daemon

make install-daemon-config

cd ..

(4)安装iostat(监控磁盘IO的插件)

echo "------- Step 8 : install iostat -------"

cd /wddg/tools/

 

echo "------- Step 8.1 : install Params-Validate -------"

tar zxvf Params-Validate-0.91.tar.gz

cd Params-Validate-0.91

perl Makefile.PL

make

make install

cd -

echo "------- Step 8.2 : install Class-Accessor -------"

tar zxvf Class-Accessor-0.31.tar.gz

cd Class-Accessor-0.31

perl Makefile.PL

make

make install

cd -

 

echo "------- Step 8.3 : install Config-Tiny -------"

tar zxvf Config-Tiny-2.12.tar.gz

cd Config-Tiny-2.12

perl Makefile.PL

make

make install

cd -

 

echo "------- Step 8.4 : install Math-Calc-Units -------"

tar zxvf Math-Calc-Units-1.07.tar.gz

cd Math-Calc-Units-1.07

perl Makefile.PL

make

make install

cd -

 

echo "------- Step 8.5 : install Regexp-Common -------"

tar zxvf Regexp-Common-2010010201.tar.gz

cd Regexp-Common-2010010201

perl Makefile.PL

make

make install

cd -

 

echo "------- Step 8.6 : install Nagios-Plugin -------"

tar zxvf Nagios-Plugin-0.34.tar.gz

cd Nagios-Plugin-0.34

perl Makefile.PL

make

make install

cd -

 

echo "------- Step 8.7 : install sysstat -------"

#for monitor iostat

yum install sysstat -y

 

echo "------- Step 8.8 : copy script to nagios -------"

/bin/cp /wddg/tools/check_memory.pl /application/nagios/libexec/

/bin/cp /wddg/tools/check_iostat /application/nagios/libexec/

 

echo "------- Step 8.9 : chmod 755 script-------"

chmod 755 /application/nagios/libexec/check_memory.pl

chmod 755 /application/nagios/libexec/check_iostat

 

echo "------- Step 8.10 : dos2unix script -------"

dos2unix /application/nagios/libexec/check_memory.pl

dos2unix /application/nagios/libexec/check_iostat

(5)修改nrpe配置文件nrpe.cfg

cp /application/nagios/etc/nrpe.cfg/application/nagios/etc/nrpe.cfg.bak

vi /application/nagios/etc/nrpe.cfg

a、指定nagios服务端IP

#第79行修改前:

allowed_hosts=127.0.0.1

#第79行修改后:

allowed_hosts=127.0.0.1,192.168.1.198

b、删除第199行到203行

sed -i '199,203d' /application/nagios/etc/nrpe.cfg

下面是删除的内容:

command[check_users]=/application/nagios/libexec/check_users -w 5 -c10

command[check_load]=/application/nagios/libexec/check_load -w15,10,5 -c 30,25,20

command[check_hda1]=/application/nagios/libexec/check_disk -w 20% -c10% -p /dev/hda1

command[check_zombie_procs]=/application/nagios/libexec/check_procs-w 5 -c 10 -s Z

command[check_total_procs]=/application/nagios/libexec/check_procs-w 150 -c 200

c、在文件未尾加上如下内容

command[check_load]=/application/nagios/libexec/check_load -w15,10,6 -c 30,25,20

command[check_mem]=/application/nagios/libexec/check_memory.pl -w 6%-c 3%

command[check_disk]=/application/nagios/libexec/check_disk -w 20% -c8% -p /

command[check_swap]=/application/nagios/libexec/check_swap -w 20% -c10%"

command[check_iostat]=/application/nagios/libexec/check_iostat -w 6-c 10