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

本文详细介绍了Nagios监控利器的使用,从Nagios的基本概念、服务端安装配置到客户端的安装,以及Nagios监控原理、图形监控显示和报警策略。针对Nagios的安装过程,包括配置yum源、关闭防火墙和SELinux、安装Nagios等步骤,并提供了详细的命令行操作示例。此外,文章还探讨了Nagios的排错方法和短信网关报警的实战操作,对于理解Nagios的监控机制及其在实际运维中的应用非常有帮助。
摘要由CSDN通过智能技术生成

第四十二章 监控利器Nagios实战

一、Nagios介绍
1、哪些内容需要监控呢?
(1)本地资源

a、负载:uptime;

b、CPU:top,sar,cpu温度;

c、磁盘:df;

d、内存:free;

e、IO:iostat;

f、RAID

g、passwd文件的变化(本地所有文件指纹识别)。

(2)网络服务

端口、URL、ping丢包、进程数、IDC网络流量

(3)其他设备

路由器、交换机端口流量、打印机、windows等

(4)业务数据

用户登录失败次数,用户登录网站次数,输入验证码失败次数、某个API接口流量并发,电商网站订单,支付交易的数量等。这个获取的过程可能是开发或者架构师完成的,但添加的过程就是运维;

2、Nagios(难够死)监控工具介绍与优势

Nagios是一款开源的网络及服务监控工具,功能强大,灵活性强。能有效监控Windows、Linux、Unix等系统主机的各种状态信息、交换机、路由器等网络设备、主机端口和URL服务等。根据不同业务故障级另发出告警信息(邮件、微信、短信、语音报警、飞信、MSN)给管理员,当故障恢复时也会发出恢复消息给管理员。

Nagios服务端可以在Linux系统和类Unix系统上运行,目前无法在Windows上运行,但Windows可以作为被监控的主机。

官方网站地址:https://www.nagios.org/

官方快速安装说明:https://support.nagios.com/kb/article.php?id=96#CentOS

 

3、Nagios的特点

(1)监控网络服务(SMTP、POP3、HTTP、NNTP、PING等)

(2)监控主机资源(处理器负荷、磁盘利用率等)

(3)简单地插件设计使得用户可以方便地扩展自己服务的检测方法

(4)并行服务检查机制

(5)具备定义网络分层结构的能力,用"parent"主机定义来表达网络主机间的关系,这种关系可被用来发现和明晰主机宕机或不可达状态

(6)当服务或主机问题产生与解决时将告警发送给联系人(通过EMail、短信、用户定义方式)

(7)具备定义事件句柄功能,它可以在主机或服务的事件发生时获取更多问题定位

(8)自动的日志回滚

(9)可以支持并实现对主机的冗余监控

(10)可选的WEB界面用于查看当前的网络状态、通知和故障历史、日志文件等

4、Nagios的构成

Nagios不好的地方在于它只做核心,很多其他功能都是通过插件来实现的。Nagios一般由一个主程序(Nagios),一个插件程序(Nagios-plugins)和一些可选的附加程序(NRPE,NSClient++,NSCA,NDOUtils)等。Nagios本身就是一个监控的平台而已,其具体的监控工作都是通过插件(Nagios-plugins,也可自己编写)来实现的。因此,Nagios主程序和Nagios-plugins插件都是Nagios服务端必须安装的程序组件,并且Nagios-plugins一般也要安装于被监控端。几个附加程序的描述如下:

(1)NRPE:半被动模式 (用于Linux服务器,主要用于监控本地资源)

a、存在位置

工作在被监控端,操作系统为Linux/Unix。

b、作用

用于在被监控的远程Linux/Unix主机上执行脚本插件获取数据回传给服务器端,以实现对这些主机资源的监控。主要用于监控本地资源。

c、存在形式

守护进程(agent)模式,开启的端口为5666.

d、原理:

相当于领导分配工作,下属做完回报工作。

(2)NSClient++:半被动模式 (用于windows服务器)

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、演示环境

Host

OS

role

remask

192.168.1.198

RedHat6.4_32

Nagios监控服务器

服务端

192.168.1.218

CentOS6.5_32

LNMP_Web服务器

被监控的客户端服务器

192.168.1.219

CentOS6.5_32

LNMP_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  #生成nagiosapache中的配置文件:/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

</Directory>

 

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

</Directory>

(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

(6)启动nrpe

/application/nagios/bin/nrpe -c /application/nagios/etc/nrpe.cfg -d

netstat -lntup | grep 5666

echo "/application/nagios/bin/nrpe-c /application/nagios/etc/nrpe.cfg -d">>/etc/rc.local

四、配置Nagios服务端监控服务
1、Nagios服务端目录结构
(1)目录结构

ll /usr/local/nagios/

bin:Nagios执行程序所在目录,包括nagiosnpcnrpe等;

etc:存放nagios配置文件

include:存放nagios的包含文件

libexec:存放nagios的插件

perl:

sbin:NagiosCgi文件所在目录,也就是执行外部命令所需文件所在的目录

share:存放nagios的web程序。主要是nagios界面展示的php程序

var:存放nagios的日志和数据

(2)etc目录

cd /usr/local/nagios/etc

ll

-rw-rw-r--.  cgi.cfg

-rw-r--r--. htpasswd.users

-rw-rw-r--. nagios.cfg

-rw-r--r--. nrpe.cfg

drwxrwxr-x. objects

-rw-rw----. resource.cfg

其中,nagios.cfg是nagios的主配置文件,包含(include)了cgi.cfg、resource.cfg文件和objects目录下的所有文件。nrpe.cfg是客户端配置文件,如果要把nagios服务端也当成一个客户端时,就需要配置,否则就不需要配置。htpasswd.users是nagios的web密码验证文件。

(3)etc/objects目录

ll /usr/local/nagios/etc/objects

commands.cfg:定义命令执行的文件,比如check_tcp、check_local_disk等,由后面定义服务的配置文件来引用;

contacts.cfg:定义联系人的文件,比如服务down了通知的对象;

localhost.cfg:定义本机的监控条目,默认生成;

printer.cfg:定义打印机的文件,默认未启用,在生产环境中意义不大;

switch.cfg:定义监控路由器和交换机的配置文件,默认未启用;

templates.cfg:定义服务类型的模版配置文件,类似shell中的函数功能;

timeperiods.cfg:定义要监控时间段(

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值