nagios-初探

一、nagios简介

nagios是一款流行的开源监控软件,它能监测主机和服务,当异常发生和解除时能通过用户自定义的方式通知用户。
Nagios可以识别4种状态返回信息,即0(OK)表示状态正常/绿色,1(WARNING)表示出现警告/×××,2(CRITTCAL)表示出现 非常严重 的错误/红色,3(UNKNOWN)表示未知错误 、深×××。Nagios根据插件返回来的值,来判断监控对象的状态,并在web显示来告知管理员。

二、主要功能

网络服务监控(SMTP、POP3、HTTP、NNTP、ICMP、SNMP、FTP、SSH)
主机资源监控(CPU load、disk usage、system logs),也包括Windows主机(使用NSClient++ plugin)
可以指定自己编写的Plugin通过网络收集数据来监控任何情况(温度、警告……)
可以通过配置Nagios远程执行插件远程执行脚本
远程监控支持SSH或SSL加通道方式进行监控
简单的plugin设计允许用户很容易的开发自己需要的检查服务,支持很多开发语言(shell scripts、C++、Perl、ruby、Python、PHP、C#等)
包含很多图形化数据Plugins(Nagiosgraph、Nagiosgrapher、PNP4Nagios等)
可并行服务检查
能够定义网络主机的层次, 允许逐级检查, 就是从父主机开始向下检查
当服务或主机出现问题时发出通告,可通过email, pager, sms 或任意用户自定义的plugin进行通知
能够自定义事件处理机制重新激活出问题的服务或主机
自动日志循环
支持冗余监控
包括Web界面可以查看当前网络状态,通知,问题历史,日志文件等

(引自维基百科)

三、工作原理

Nagios工作原理
1、Nagios的功能是监控服务和主机,但自身并没有该功能,所有的监控、检测功能都是通过插件来完成。
2、Nagios周期性调用插件检测服务器状态,并维持一个队列,所有插件返回状态信息都进入队列,Nagios每次从队首开始读取信息,并把状态通过web显示。

Nagios提供了许多插件,利用这些插件可以监控服务状态。安装完成后,在nagios主目录下/libexec放有nagios自带所有插件,如check_disk检查磁盘空间插件,check_load检查CPU负载等等。每一个插件可运行./check_xxx –h 查看其使用方法和功能

四、前戏

1、环境

软件版本
nagios-4.3.4.tar.gz
nagios-cn.4.3.4.tar.gz
nagios-plugins-2.2.1.tar.gz
nrpe-3.1.0.tar.gz
pnp4nagios-0.6.26.tar.gz
rrdtool-1.4.5.tar.gz
系统版本
CentOS Linux release 7.4.1708 (Core)
Nagios-Server IP:192.168.118.130
Nagios-Client IP:192.168.118.129

2、安装依赖包

yum install -y gcc glibc glibc-common wget unzip httpd php gd gd-devel perl postfix php-devel make net-snmp

3、安装FTP+本地YUM源

FTP方式本地yum源

4、配置互免密

ssh-keygen -t rsa
ssh-copy-id [email protected]

5、配置时间同步

列出所有时区

timedatectl list-timezones

设置所在时区

timedatectl set-timezone Asia/Almaty

配置ntp服务

yum install ntp -y
echo "*/5 * * * * /usr/sbin/ntpdate time.nist.gov > /dev/null 2>&1" >> /var/spool/cron/root

Note:一般数据中心网络都配置有时间同步与原子钟服务器,一台时间同步服务器大概辐射500台左右设备,一台原子钟可以管理4到6台时钟同步服务器。

6、关闭selinux和firewall

vim /etc/selinux/config
SELINUX=disabled

Systemctl stop firewall
Systemctl disable firewall

7、安装邮件服务

yum install mailx -y
yum install sendmail -y

五、nagiso-server配置

1、添加nagios用户与组,并创建nagios安装目录

useradd -s /sbin/nologin nagios
mkdir /usr/local/nagios

2、编译安装nagios

2.1、解压缩

tar zxf /root/nagios/nagios-4.3.4.tar.gz -C /usr/src
cd /usr/src/nagios-4.3.4/

2.2、编译安装

./configure --prefix=/usr/local/nagios --with-command-group
=nagios --with-nagios-group=nagios
 make all
 make install
 make install-init
 make install-config
 make install-commandmode
 make install-webconf

2.3、检查配置文件语法

/usr/local/nagios/bin/nagios -v /usr/local/nagios/etc/nagios.cfg
Total Warnings: 0
Total Errors:   0

2.4、把案例脚本复制至nagios目录

cd /usr/src/nagios-4.3.4/contrib/eventhandlers/ /usr/local/nagios
/libexec/

2.5、改变目录权限

chown -R nagios:nagios /usr/local/nagios

3、配置httpd

3.1、创建用户(超级用户为nagiosadmin这里用jym)

/bin/htpasswd -c /usr/local/nagios/etc/htpasswd jym

3.2、修改httpd配置文件

修改/etc/httpd/conf/httpd.conf 
User nagios
Group nagios

加入以下内容

AddType application/x-httpd-php .php
#setting for nagios 
ScriptAlias /nagios/cgi-bin "/usr/local/nagios/sbin"
<Directory "/usr/local/nagios/sbin">
     AuthType Basic
     Options ExecCGI
     AllowOverride None
     Order allow,deny 
     Allow from all 
     AuthName "Nagios Access"
     AuthUserFile /usr/local/nagios/etc/htpasswd             
     Require valid-user
</Directory>
Alias /nagios "/usr/local/nagios/share"
<Directory "/usr/local/nagios/share">
     AuthType Basic
     Options None
     AllowOverride None
     Order allow,deny 
     Allow from all 
     AuthName "nagios Access"
     AuthUserFile /usr/local/nagios/etc/htpasswd
     Require valid-user
</Directory>

3、编译安装nagios-plugins

tar zxf nagios-plugins-2.2.1.tar.gz -C /usr/src/
cd /usr/src/nagios-plugins-2.2.1/
./configure --prefix=/usr/local/nagios --with-nagios-user=nagios --with-nagios-group=nagios
make 
make install

4、编译安装nrpe

tar -zxf nrpe-3.1.0.tar.gz -C /usr/src/nrpe-3.1.0
cd /usr/src/nrpe-3.1.0/
./configure --with-nrpe-user=nagios --with-nr
pe-group=nagios --with-nagios-user=nagios --with-nagios-group=nagios --enable-command-args --enable-ssl

出现如下信息说明编译成功
Configuration summary for nrpe 3.1.0-rc1 2017-04-06 :

General Options:

NRPE port: 5666
NRPE user: nagios
NRPE group: nagios
Nagios user: nagios
Nagios group: nagios

Review the options above for accuracy. If they look okay,
type 'make all' to compile the NRPE daemon and client
or type 'make' to get a list of make options.

安装

make all 
make install-plugin
make install-daemon

检查插件是否已安装

ll /usr/local/nagios/libexec/ | grep check_nrpe

5、设置开机自动启动

/sbin/chkconfig nagios on
systemctl enable httpd.service

6、启动nagios,httpd,nrp

systemctl start httpd
/usr/local/nagios/bin/nagios -d /usr/local/nagios/etc/nagios.cfg 
/usr/local/nagios/bin/nrpe -c /usr/local/nagios/etc/nrpe.cfg -d 

7、启动httpd报错

日志信息如下:cat /var/log/httpd/error_log

[Mon Nov 19 12:00:48.171796 2018] [suexec:notice] [pid 7389] AH01232: 
suEXEC mechanism enabled (wrapper: /usr/sbin/suexec)[Mon Nov 19 12:00:48.171914 2018] [:warn] [pid 7389] NSSSessionCacheTi
meout is deprecated. Ignoring.[Mon Nov 19 12:00:48.187131 2018] [:error] [pid 7389] Server user nagi
os lacks read access to NSS key database /etc/httpd/alias/key3.db.

在redhat官网查询解决方案为
https://bugzilla.redhat.com/show_bug.cgi?id=1395300

yum install httpd mod_nss
usermod -a -G nagios nagios
chown :nagios /etc/httpd/alias/*.db

猜你喜欢

转载自blog.51cto.com/jiayimeng/2318832