站长视角
用户至上

搬瓦工Centos7.8系统部署Nagios监控系统教程

Nagios 是电脑系统和网络监控程序,用于检测主机和服务,当异常发生和解除时能提醒用户;是基于 GPLv2 开发的开源软件,可免费获得及使用。

Nagios 原名 NetSaint,由 Ethan Galstad 开发并维护至今。NAGIOS 是简称,全写“Nagios Ain’t Gonna Insist On Sainthood”,Sainthood 意思是“圣者”,而“Agios”是“saint”的希腊文。Nagios 在 Linux 运作,但也能用于 Unix。

主要功能

  • 网络服务监控(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 界面可以查看当前网络状态,通知,问题历史,日志文件等

基础环境准备

2.1系统及软件
主机 IP地址 软件
192.168.250.236 Nagios服务端 Apache、Php、Nagios、nagios-plugins、nrpe
192.168.250.197 client被监控端(客户端) nagios-plugins、nrpe
2.2主要插件介绍
(1)NRPE

当Nagios服务端需要监控某个远程Linux 客户端的服务或者资源情况时:1.Nagios服务端会运行checknrpe 这个插件,告诉它要检查什么;2.checknrpe 插件会连接到远程客户端上的NRPE daemon,所用的方式是SSL;3.NRPE daemon 会运行相应的Nagios 插件来执行检查;4.NRPE daemon 将检查的结果返回给check_nrpe 插件,插件将其递交给nagios做处理

注意:NRPE daemon 需要Nagios 插件安装在远程的Linux主机上,否则,daemon不能做任何的监控。

(2)Nagios-plugins

nagios core是没有内置任何检查机制进行主机服务或者网络的监控。对于这些工作,都是交给额外的程序,就是nagios plugins程序来完成。Nagios只是一个平台,本身啥都不是,需要通过各种小弟(nagios plugins)才能干活。

安装Nagios服务端

3.1安装前准备
(1)关闭防火墙
[root@mynagios ~]# systemctl stop firewalld

[root@mynagios ~]# systemctl disable firewalld

Removed symlink /etc/systemd/system/multi-user.target.wants/firewalld.service.

Removed symlink /etc/systemd/system/dbus-org.fedoraproject.FirewallD1.service.

[root@mynagios ~]# iptables -F

[root@mynagios ~]#
(2)关闭Selinux
[root@mynagios ~]# setenforce 0
[root@mynagios ~]# sed -ri 's#^(SELINUX=).*#\1disabled#g'/etc/selinux/config
[root@mynagios ~]#
(3)调整字符集

如果不安装后面安装一些插件会有错误。

[root@mynagios ~]# echo 'export LC_ALL=C'>>/etc/profile
[root@mynagios ~]# source /etc/profile
[root@mynagios ~]#
(4)时间同步

监控的时间同步很重要,所以时间必须同步一致。

[root@mynagios ~]# echo '# time sync'>>/var/spool/cron/root
[root@mynagios ~]# echo '*/10 * * * * /usr/sbin/ntpdate pool.ntp.org &>/dev/null'>>/var/spool/cron/root
[root@mynagios ~]#
(5)安装web展示依赖软件

Nagios服务端需要安装web展示依赖软件。

[root@mynagios ~]# yum install -y gcc glibc glibc-common gd gd-devel httpd php php-gd mysql*
Loaded plugins: fastestmirror, langpacks
Determining fastest mirrors
* base: mirrors.aliyun.com
* extras: mirrors.aliyun.com
* updates: mirrors.aliyun.com
base| 3.6 kB 00:00:00
extras | 2.9 kB 00:00:00
updates | 2.9 kB 00:00:00
(1/2): updates/7/x86_64/primary_db | 15 MB 00:00:04
(2/2): extras/7/x86_64/primary_db | 246 kB 00:00:05
ResolvingDependencies
--> Running transaction check
---> Package gcc.x86_64 0:4.8.5-44.el7 will be installed

说明:上面可以看到yum 一键安装这些软件,不仅包含了编译器,图画等软件还包含了LAMP坏境。

安装的软件说明:

gcc glibc glibc-common gcc 编译器 gd gd-devel Nagios服务端Web界面中的Map画拓扑图用的 httpd php \php-gd php环境,LAMP坏境 mysql* 生成MySQL的插件,MySQL不需要启动

(6)创建所需用户

这里将将apache和nagios同属于一个组,方便一起管理。

[root@mynagios ~]# useradd -m nagios
[root@mynagios ~]# groupadd nagcmd
[root@mynagios ~]# usermod -a -G nagcmd nagios
[root@mynagios ~]# usermod -a -G nagcmd apache
[root@mynagios ~]#
3.2安装 Nagios平台
(1)下载Nagios
[root@mynagios ~]# cd /usr/local/src
[root@mynagios src]# wget https://assets.nagios.com/downloads/nagioscore/releases/nagios-4.3.1.tar.gz
--2022-05-1111:07:53-- https://assets.nagios.com/downloads/nagioscore/releases/nagios-4.3.1.tar.gz
Resolving assets.nagios.com (assets.nagios.com)... 45.79.49.120
Connecting to assets.nagios.com (assets.nagios.com)|45.79.49.120|:443... connected.
HTTP request sent, awaiting response... 200 OK
Length: 11095797(11M) [application/x-gzip]
Saving to: 'nagios-4.3.1.tar.gz'

100%[=================>] 11,095,7973.07MB/s in3.4s

2022-05-1111:08:02(3.07 MB/s) - 'nagios-4.3.1.tar.gz' saved [11095797/11095797]

[root@mynagios src]#
(2)解压编译安装
[root@mynagios src]# tar -zxf nagios-4.3.1.tar.gz
[root@mynagios src]# ls
nagios-4.3.1 nagios-4.3.1.tar.gz
[root@mynagios src]# cd nagios-4.3.1/
[root@mynagios nagios-4.3.1]# ./configure --with-command-group=nagcmd
checking for a BSD-compatible install... /usr/bin/install -c
checking build system type... x86_64-pc-linux-gnu
checking host system type... x86_64-pc-linux-gnu
checking for gcc... gcc
checking whether the C compiler works... yes
checking for C compiler default output file name... a.out
[root@mynagios nagios-4.3.1]# make all
[root@mynagios nagios-4.3.1]#
[root@mynagios nagios-4.3.1]# make install
cd ./base&& make install

[root@mynagios nagios-4.3.1]#
========================================================
[root@mynagios nagios-4.3.1]# make install-init

[root@mynagios nagios-4.3.1]# make install-commandmode
/usr/bin/install -c -m 775-o nagios -g nagcmd -d /usr/local/nagios/var/rw
chmod g+s /usr/local/nagios/var/rw

*** External command directory configured ***

[root@mynagios nagios-4.3.1]# make install-config

[root@mynagios nagios-4.3.1]# make install-webconf
[root@mynagios nagios-4.3.1]#
(3)Nagios目录说明

安装完成后,在以下目录下生成文件

[root@mynagios nagios]# pwd
/usr/local/nagios
[root@mynagios nagios]# ll
total 8
drwxrwxr-x. 2 nagios nagios 38May1111:23 bin
drwxrwxr-x. 3 nagios nagios 74May1111:25 etc
drwxrwxr-x. 2 nagios nagios 6May1111:23 libexec
drwxrwxr-x. 2 nagios nagios 4096May1111:23 sbin
drwxrwxr-x. 14 nagios nagios 4096May1111:23 share
drwxrwxr-x. 5 nagios nagios 45May1111:24var
[root@mynagios nagios]#

它们的作用如下:

文件夹 用途
bin Nagios 可执行程序所在目录
etc Nagios 配置文件所在目录
sbin Nagios CGI 文件所在目录,也就是执行外部命令所需文件所在的目录
share Nagios网页文件所在的目录
libexec Nagios 外部插件所在目录
var Nagios 日志文件、lock 等文件所在的目录
var/archives Nagios 日志自动归档目录
var/rw 用来存放外部命令文件的目录

Nagios 安装完毕后,默认的配置文件在 /usr/local/nagios/etc目录下,文件用途解释说明:

(4)配置登录账户

Nagios服务端Web界面的用户账号:moonrong密码:123456

[root@mynagios nagios-4.3.1]# htpasswd -cb /usr/local/nagios/etc/htpasswd.users moonrong 123456
Adding password for user moonrong
[root@mynagios nagios-4.3.1]#
(5)给moonrong用户授权

因为nagios默认把全部的权限给 nagiosadmin,所以可以通过修改 cgi.cfg文件授权给刚刚新建的 moonrong这个用户。如果不授权给 moonrong,后面完成 Nagios配置之后,使用账号 moonrong来登录 Nagios服务端的 Web界面,将会看不到你配置好的需要监控的主机的所有信息。

[root@mynagios nagios-4.3.1]# cd /usr/local/nagios/etc
[root@mynagios etc]# pwd
/usr/local/nagios/etc
[root@mynagios etc]# ll
total 68
-rw-rw-r--. 1 nagios nagios 12999May1111:25 cgi.cfg
-rw-r--r--. 1 root root 47May1111:40 htpasswd.users
-rw-rw-r--. 1 nagios nagios 44831May1111:25 nagios.cfg
drwxrwxr-x. 2 nagios nagios 167May1111:25 objects
-rw-rw----. 1 nagios nagios 1312May1111:25 resource.cfg
[root@mynagios etc]# cp cgi.cfg cgi.cfg.ori.20220511
[root@mynagios etc]# sed -i 's/nagiosadmin/nagiosadmin,moonrong/g' cgi.cfg
[root@mynagios etc]#

可以看到 cgi.cfg文件内的已经授权给用户 moonrong

[root@mynagios etc]# grep nagiosadmin cgi.cfg
authorized_for_system_information=nagiosadmin,moonrong
authorized_for_configuration_information=nagiosadmin,moonrong
authorized_for_system_commands=nagiosadmin,moonrong
authorized_for_all_services=nagiosadmin,moonrong
authorized_for_all_hosts=nagiosadmin,moonrong
authorized_for_all_service_commands=nagiosadmin,moonrong
authorized_for_all_host_commands=nagiosadmin,moonrong
[root@mynagios etc]#
(6)登录到nagios的WEB端

这里登录之前,先重启一下 Apache和 Nagios服务。

[root@mynagios bin]# systemctl restart httpd
[root@mynagios bin]#

[root@mynagios ~]# systemctl start nagios
[root@mynagios ~]# systemctl status nagios
* nagios.service - LSB: Startsand stops the Nagios monitoring server
Loaded: loaded (/etc/rc.d/init.d/nagios; bad; vendor preset: disabled)
Active: active (running) since Wed2022-05-1113:50:41 CST; 21s ago
Docs: man:systemd-sysv-generator(8)
Process: 2142ExecStart=/etc/rc.d/init.d/nagios start (code=exited, status=0/SUCCESS)
Tasks: 8
CGroup: /system.slice/nagios.service
|-2165/usr/local/nagios/bin/nagios -d /usr/local/nagios/etc/nagios.cfg
|-2167/usr/local/nagios/bin/nagios --worker /usr/local/nagios/var/rw/nagios.qh
|-2168/usr/local/nagios/bin/nagios --worker /usr/local/nagios/var/rw/nagios.qh
|-2169/usr/local/nagios/bin/nagios --worker /usr/local/nagios/var/rw/nagios.qh
|-2170/usr/local/nagios/bin/nagios --worker /usr/local/nagios/var/rw/nagios.qh
|-2171/usr/local/nagios/bin/nagios --worker /usr/local/nagios/var/rw/nagios.qh
|-2172/usr/local/nagios/bin/nagios --worker /usr/local/nagios/var/rw/nagios.qh
`-2175 /usr/local/nagios/bin/nagios -d /usr/local/nagios/etc/nagios.cfg

May 11 13:50:41 mynagios nagios[2165]: nerd: Channel opathchecks registered successfully
May 11 13:50:41 mynagios nagios[2165]: nerd: Fully initialized and ready to rock!
May 11 13:50:41 mynagios nagios[2165]: wproc: Successfully registered manager as @wproc with query handler
May 11 13:50:41 mynagios nagios[2165]: wproc: Registry request: name=Core Worker 2167;pid=2167
May 11 13:50:41 mynagios nagios[2165]: wproc: Registry request: name=Core Worker 2168;pid=2168
May 11 13:50:41 mynagios nagios[2165]: wproc: Registry request: name=Core Worker 2169;pid=2169
May 11 13:50:41 mynagios nagios[2165]: wproc: Registry request: name=Core Worker 2170;pid=2170
May 11 13:50:41 mynagios nagios[2165]: wproc: Registry request: name=Core Worker 2171;pid=2171
May 11 13:50:41 mynagios nagios[2165]: wproc: Registry request: name=Core Worker 2172;pid=2172
May 11 13:50:43 mynagios nagios[2165]: Successfully launched command file worker with pid 2175
[root@mynagios ~]#

登录地址:

http://192.168.250.236/nagios/

用户名和密码:moonrong/123456

错误提示:

打开页面后,提示:

Error: Couldnot read object configuration data! Here are some things you should check in order to

这个是因为我们没有启动Nagios的后台进程所导致的,只要执行下面的命令即可解决:

[root@mynagios ~]# /usr/local/nagios/bin/nagios -d /usr/local/nagios/etc/nagios.cfg
[root@mynagios ~]# systemctl nagios restart
[root@mynagios ~]#

接着查看插件目录,空的,如下:

[root@mynagios ~]# ls /usr/local/nagios/libexec
[root@mynagios ~]#
3.3安装 Nagios-plugins插件
(1)安装前准备

安装依赖软件 perl-devel

[root@mynagios ~]# yum install -y perl-devel
Loaded plugins: fastestmirror, langpacks
Loading mirror speeds from cached hostfile
* base: mirrors.aliyun.com
* extras: mirrors.aliyun.com
* updates: mirrors.aliyun.com
ResolvingDependencies
--> Running transaction check
(2)下载 Nagios-plugins
[root@mynagios ~]# cd /usr/local/src/
[root@mynagios src]#

[root@mynagios src]# wget https://nagios-plugins.org/download/nagios-plugins-2.3.3.tar.gz --no-check-certificate
--2022-05-1114:20:21-- https://nagios-plugins.org/download/nagios-plugins-2.3.3.tar.gz
Resolving nagios-plugins.org (nagios-plugins.org)... 45.56.123.251
(3)解压编译安装
[root@mynagios src]# tar -zxf nagios-plugins-2.3.3.tar.gz
[root@mynagios src]#
[root@mynagios src]# cd nagios-plugins-2.3.3/
[root@mynagios nagios-plugins-2.3.3]# ./configure --with-nagios-user=nagios --with-nagios-group=nagios --enable-perl-modules
checking for a BSD-compatible install... /usr/bin/install -c
checking whether build environment is sane... yes
checking for a thread-safe mkdir -p... /usr/bin/mkdir -p
checking for gawk... gawk
checking whether make sets $(MAKE)... yes

[root@mynagios nagios-plugins-2.3.3]# make && make install
[root@mynagios nagios-plugins-2.3.3]#

安装完成后,然后查看,一共安装了57个插件,这个插件越好,说明可监控的东西更多:

[root@mynagios nagios-plugins-2.3.3]# ls /usr/local/nagios/libexec/|wc -l
57
[root@mynagios nagios-plugins-2.3.3]#
3.4.安装 NRPE插件

服务端和被监控端都要安装这个插件。

原因是:Nagios服务端需要checknrpe这个插件,才能实现与客户端的NRPE插件联系,它们之间是通过SSL协议通信。如果Nagios服务端不安装NRPE插件,在/usr/local/nagios/libexec/这个路径下,将找不到checknrpe这个插件。所以Nagios 服务端需要装NRPE插件。Nagios服务端的NRPE不一定需要运行,但被监控的客户端的NRPE一定得运行起来。NRPE总共由两部分组成:

check_nrpe 插件,位于监控主机上
NRPE daemon 运行在远程的Linux客户端上(通常就是被监控机)
(1)NRPE下载
[root@mynagios ~]# wget http://prdownloads.sourceforge.net/sourceforge/nagios/nrpe-2.13.tar.gz
--2022-05-1114:51:49-- http://prdownloads.sourceforge.net/sourceforge/nagios/nrpe-2.13.tar.gz
Resolving prdownloads.sourceforge.net (prdownloads.sourceforge.net)... 204.68.111.105
Connecting to prdownloads.sourceforge.net (prdownloads.sourceforge.net)|204.68.111.105|:80... connected.
HTTP request sent, awaiting response... 301MovedPermanently
Location: http://downloads.sourceforge.net/project/nagios/nrpe-2.x/nrpe-2.13/nrpe-2.13.tar.gz [following]
--2022-05-1114:51:49-- http://downloads.sourceforge.net/project/nagios/nrpe-2.x/nrpe-2.13/nrpe-2.13.tar.gz
Resolving downloads.sourceforge.net (downloads.sourceforge.net)... 204.68.111.105
Reusing existing connection to prdownloads.sourceforge.net:80.
HTTP request sent, awaiting response... 302Found
Location: http://jaist.dl.sourceforge.net/project/nagios/nrpe-2.x/nrpe-2.13/nrpe-2.13.tar.gz [following]
--2022-05-1114:51:49-- http://jaist.dl.sourceforge.net/project/nagios/nrpe-2.x/nrpe-2.13/nrpe-2.13.tar.gz
Resolving jaist.dl.sourceforge.net (jaist.dl.sourceforge.net)... 150.65.7.130, 2001:df0:2ed:feed::feed
Connecting to jaist.dl.sourceforge.net (jaist.dl.sourceforge.net)|150.65.7.130|:80... connected.
HTTP request sent, awaiting response... 200 OK
Length: 411768(402K) [application/x-gzip]
Saving to: 'nrpe-2.13.tar.gz'

100%[=================================================>] 411,768136KB/s in3.0s

2022-05-1114:51:54(136 KB/s) - 'nrpe-2.13.tar.gz' saved [411768/411768]

[root@mynagios ~]#
(2)NRPE解压编译安装
[root@mynagios ~]# mv nrpe-2.13.tar.gz /usr/local/src/
mv: overwrite '/usr/local/src/nrpe-2.13.tar.gz'? y
[root@mynagios ~]# cd /usr/local/src
[root@mynagios src]# ls
nagios-4.3.1 nagios-4.3.1.tar.gz nagios-plugins-2.3.3 nagios-plugins-2.3.3.tar.gz nrpe-2.13.tar.gz wget-log
[root@mynagios src]# tar zxf nrpe-2.13.tar.gz
[root@mynagios src]# ll
total 13976
drwxrwxr-x. 18 root root 4096May1111:10 nagios-4.3.1
-rw-r--r--. 1 root root 11095797Feb242017 nagios-4.3.1.tar.gz
drwxr-xr-x 15 root root 4096May1114:29 nagios-plugins-2.3.3
-rw-r--r-- 1 root root 2782610May1114:25 nagios-plugins-2.3.3.tar.gz
drwxr-xr-x 8 nagios nagios 4096Nov122011 nrpe-2.13
-rw-r--r-- 1 root root 411768Nov122011 nrpe-2.13.tar.gz
-rw-r--r-- 1 root root 2298May1114:50 wget-log
[root@mynagios src]# cd nrpe-2.13/
[root@mynagios nrpe-2.13]# yum install -y openssl-devel #安装NRPE依赖
Loaded plugins: fastestmirror, langpacks
Loading mirror speeds from cached hostfile
[root@mynagios nrpe-2.13]# ./configure
[root@mynagios nrpe-2.13]#
[root@mynagios nrpe-2.13]# make all

[root@mynagios nrpe-2.13]# make install-plugin
cd ./src/ && make install-plugin
make[1]: Entering directory `/usr/local/src/nrpe-2.13/src'
/usr/bin/install -c -m 775 -o nagios -g nagios -d /usr/local/nagios/libexec
/usr/bin/install -c -m 775 -o nagios -g nagios check_nrpe /usr/local/nagios/libexec
make[1]: Leaving directory `/usr/local/src/nrpe-2.13/src'
[root@mynagios nrpe-2.13]#

被监控客户端安装

客户端只要安装nagios-plugins和NRPE就行了。

4.1安装前准备

关闭防火墙、关闭selinux过程略。

(1)调整字符集
[root@mailserver ~]# echo 'export LC_ALL=C'>>/etc/profile
[root@mailserver ~]# source /etc/profile
[root@mailserver ~]#
(2)时间同步
[root@mailserver ~]# echo '# time sync'>>/var/spool/cron/root
[root@mailserver ~]# echo '*/10 * * * * /usr/sbin/ntpdate pool.ntp.org &>/dev/null'>>/var/spool/cron/root
[root@mailserver ~]#
(3)安装nagios依赖软件
[root@mailserver ~]# yum install -y gcc glibc glibc-common openssl-devel perl-devel
Loaded plugins: fastestmirror, langpacks
Determining fastest mirrors
* base: mirrors.dgut.edu.cn
* extras: mirrors.dgut.edu.cn
* updates: mirrors.dgut.edu.cn

gcc glibc glibc-common :这些是编译器所需,没有就无法编译源码,无法进行源码安装软件。openssl-devel :这个是待会安装NRPE所需的依赖软件。

(4)创建所需用户
[root@mailserver ~]# useradd -m nagios -s /sbin/nologin
[root@mailserver ~]#
4.2安装Nagios-plugins插件

同3.3节,操作过程略。

4.3安装NRPE插件
(1)NRPE下载
[root@mailserver src]# cd /usr/local/src/
[root@mailserver src]# wget http://prdownloads.sourceforge.net/sourceforge/nagios/nrpe-2.13.tar.gz
--2022-05-1115:38:13-- http://prdownloads.sourceforge.net/sourceforge/nagios/nrpe-2.13.tar.gz
(2)NRPE解压编译安装
[root@mailserver src]# tar zxf nrpe-2.13.tar.gz
[root@mailserver src]# cd nrpe-2.13/
[root@mailserver nrpe-2.13]# ./configure
[root@mailserver nrpe-2.13]# make all

[root@mailserver nrpe-2.13]# make install-plugin
cd ./src/ && make install-plugin
make[1]: Entering directory `/usr/local/src/nrpe-2.13/src'
/usr/bin/install -c -m 775 -o nagios -g nagios -d /usr/local/nagios/libexec
/usr/bin/install -c -m 775 -o nagios -g nagios check_nrpe /usr/local/nagios/libexec
make[1]: Leaving directory `/usr/local/src/nrpe-2.13/src'
[root@mailserver nrpe-2.13]#

[root@mailserver nrpe-2.13]# make install-daemon
cd ./src/ && make install-daemon
make[1]: Entering directory `/usr/local/src/nrpe-2.13/src'
/usr/bin/install -c -m 775-o nagios -g nagios -d /usr/local/nagios/bin
/usr/bin/install -c -m 775-o nagios -g nagios nrpe /usr/local/nagios/bin
make[1]: Leaving directory `/usr/local/src/nrpe-2.13/src'
[root@mailserver nrpe-2.13]# make install-daemon-config
/usr/bin/install -c -m 775 -o nagios -g nagios -d /usr/local/nagios/etc
/usr/bin/install -c -m 644 -o nagios -g nagios sample-config/nrpe.cfg /usr/local/nagios/etc
[root@mailserver nrpe-2.13]#

客户端NRPE配置与启动

5.1允许被服务端监控
[root@mailserver /]# cd /usr/local/nagios/etc
[root@mailserver etc]# ls
nrpe.cfg
[root@mailserver etc]# cp nrpe.cfg nrpe.cfg.ori.20220511
[root@mailserver etc]# vim nrpe.cfg
[root@mailserver etc]# grep allowed_hosts nrpe.cfg
allowed_hosts=127.0.0.1,::1,192.168.250.236
[root@mailserver etc]#
5.2修改配置文件

告诉客户机的NRPE,该守护进程怎么去监控。

# The following examples use hardcoded command arguments...

#command[check_users]=/usr/local/nagios/libexec/check_users -w 5 -c 10
#command[check_load]=/usr/local/nagios/libexec/check_load -w 15,10,5 -c 30,25,20
#command[check_hda1]=/usr/local/nagios/libexec/check_disk -w 20% -c 10% -p /dev/hda1
#command[check_zombie_procs]=/usr/local/nagios/libexec/check_procs -w 5 -c 10 -s Z
#command[check_total_procs]=/usr/local/nagios/libexec/check_procs -w 150 -c 200

command[check_load]=/usr/local/nagios/libexec/check_load -w 15,10,4-c 30,25,20
command[check_disk]=/usr/local/nagios/libexec/check_disk -w 20% -c 6% -p /

注释掉样例,然后新增两行,含义如下:

command[checkload]=/usr/local/nagios/libexec/checkload -w 15,10,4 -c 30,25,20 命令解释:

当1分钟多于15个进程等待,5分钟多于10个,15分钟多于4个则为warning状态 当1分钟多于30个进程等待,5分钟多于25个,15分钟多于20个则为critical状态 [checkload]:相当于模块名 /usr/local/nagios/libexec/checkload:获取资源的命令 也就是说通过调用checkload就相当于执行/usr/local/nagios/libexec/checkload这个命令,下同。

command[checkdisk]=/usr/local/nagios/libexec/checkdisk -w 20% -c 6% -p / 命令解释:-w 20% -c 6% -p /(根分区剩余空间为总大小的20%为warning状态,剩余6%为critical状态,-p后是根分区)

5.3启动NRPE以及测试
[root@mailserver etc]# /usr/local/nagios/bin/nrpe -c /usr/local/nagios/etc/nrpe.cfg -d
[root@mailserver etc]# netstat -ptln | grep 5666
tcp 000.0.0.0:56660.0.0.0:* LISTEN 31099/nrpe
[root@mailserver etc]# /usr/local/nagios/libexec/check_nrpe -H localhost
NRPE v2.13
[root@mailserver etc]# /usr/local/nagios/libexec/check_nrpe -H localhost -c check_disk
DISK OK - free space: / 46539 MiB (90.94% inode=99%);| /=4635MiB;40940;48104;0;51175
[root@mailserver etc]# /usr/local/nagios/libexec/check_nrpe -H localhost -c check_load
OK - load average: 0.08, 0.03, 0.05|load1=0.080;15.000;30.000;0; load5=0.030;10.000;25.000;0; load15=0.050;4.000;20.000;0;
[root@mailserver etc]#

Nagios服务端配置与测试

6.1修改Nagios 主配置文件
[root@mynagios /]# cd /usr/local/nagios/etc/
[root@mynagios etc]# ls
cgi.cfg cgi.cfg.ori.20220511 htpasswd.users nagios.cfg objects resource.cfg
[root@mynagios etc]# cp nagios.cfg nagios.cfg.ori.20220511
[root@mynagios etc]# vim nagios.cfg
[root@mynagios etc]#

修改变动如下:

[root@mynagios etc]# diff nagios.cfg nagios.cfg.ori.20220511
33,35d32
< cfg_file=/usr/local/nagios/etc/objects/services.cfg
< cfg_file=/usr/local/nagios/etc/objects/hosts.cfg
<
38c35
< #cfg_file=/usr/local/nagios/etc/objects/localhost.cfg --- > cfg_file=/usr/local/nagios/etc/objects/localhost.cfg
[root@mynagios etc]#

新增的两个文件:hosts.cfg是即将新建的文件用来定义主机和主机组;services.cfg是即将新建的文件用来定义这些主机要监控的服务条目。

6.2新建配置文件

根据上面配置里文件名,新建两个配置文件 hosts.cfg和 services.cfg

[root@mynagios etc]# cd objects/
[root@mynagios objects]# pwd
/usr/local/nagios/etc/objects
[root@mynagios objects]#

[root@mynagios objects]# head -20 localhost.cfg >hosts.cfg
[root@mynagios objects]# touch services.cfg
[root@mynagios objects]# ll
total 52
-rw-rw-r--. 1 nagios nagios 7696May1111:25 commands.cfg
-rw-rw-r--. 1 nagios nagios 2138May1111:25 contacts.cfg
-rw-r--r-- 1 root root 762May1116:24 hosts.cfg
-rw-rw-r--. 1 nagios nagios 5379May1111:25 localhost.cfg
-rw-rw-r--. 1 nagios nagios 3070May1111:25 printer.cfg
-rw-r--r-- 1 root root 0May1116:24 services.cfg
-rw-rw-r--. 1 nagios nagios 3252May1111:25switch.cfg
-rw-rw-r--. 1 nagios nagios 10595May1111:25 templates.cfg
-rw-rw-r--. 1 nagios nagios 3180May1111:25 timeperiods.cfg
-rw-rw-r--. 1 nagios nagios 3991May1111:25 windows.cfg
[root@mynagios objects]# chown -R nagios.nagios services.cfg hosts.cfg
[root@mynagios objects]# ll
total 52
-rw-rw-r--. 1 nagios nagios 7696May1111:25 commands.cfg
-rw-rw-r--. 1 nagios nagios 2138May1111:25 contacts.cfg
-rw-r--r-- 1 nagios nagios 762May1116:24 hosts.cfg
-rw-rw-r--. 1 nagios nagios 5379May1111:25 localhost.cfg
-rw-rw-r--. 1 nagios nagios 3070May1111:25 printer.cfg
-rw-r--r-- 1 nagios nagios 0May1116:24 services.cfg
-rw-rw-r--. 1 nagios nagios 3252May1111:25switch.cfg
-rw-rw-r--. 1 nagios nagios 10595May1111:25 templates.cfg
-rw-rw-r--. 1 nagios nagios 3180May1111:25 timeperiods.cfg
-rw-rw-r--. 1 nagios nagios 3991May1111:25 windows.cfg
[root@mynagios objects]#
6.3 hosts.cfg和 services.cfg文件的配置
(1)hosts.cfg配置
[root@mynagios objects]# pwd
/usr/local/nagios/etc/objects
[root@mynagios objects]#
[root@mynagios objects]# vim hosts.cfg
==============================================
define host{
use linux-server
host_name client
alias client
address 192.168.250.197
}
define hostgroup{
hostgroup_name linux-servers
aliasLinuxServers
members client
}
(2)services.cfg配置
define service{
usegeneric-service
host_name client
service_description DiskPartition
check_command check_nrpe!check_disk
}

define service{
usegeneric-service
host_name client
service_description Load
check_command check_nrpe!check_load
}
6.4commands.cfg配置
[root@mynagios objects]# pwd
/usr/local/nagios/etc/objects
[root@mynagios objects]# cp commands.cfg commands.cfg.ori.20220511
[root@mynagios objects]# vim commands.cfg

#在最后加入:

#'check_nrpe' command definition
define command{
command_name check_nrpe
command_line $USER1$/check_nrpe -H $HOSTADDRESS$ -c $ARG1$
}

接着测试命令的可用性

[root@mynagios etc]# /usr/local/nagios/libexec/check_nrpe -H 192.168.250.197-c check_disk
DISK OK - free space: / 46539 MiB (90.94% inode=99%);| /=4635MiB;40940;48104;0;51175
[root@mynagios etc]#

说明:# checknrpe:Nagios服务端的命令,需要在commands.cfg文件中定义。#checkdisk:调用客户端配置文件nrpe.cfg中[checkdisk]标签后面的命令。可以查看到客户端的返回的磁盘占用情况,表明成功通过NRPE实现远程监控。如果获取不到被监控端的数据,第一:请检查客户端的NRPE是否正常运行,可通过netstat -ptln | grep 5666命令查看端口正常是否打开。第二:请检查服务端与客户端之间的防火墙是否关闭。第三:查看commands.cfg文件内新增的命令是否有错,第四:查看客户端的nrpe.cfg文件内的allowedhosts=127.0.0.1语句后面是否添加了Nagios服务端的ip。

6.5检查配置文件是否有误
[root@mynagios etc]# /usr/local/nagios/bin/nagios -v /usr/local/nagios/etc/nagios.cfg

NagiosCore4.3.1
Copyright(c) 2009-present NagiosCoreDevelopmentTeamandCommunityContributors
Copyright(c) 1999-2009EthanGalstad
LastModified: 02-23-2017
License: GPL

Website: https://www.nagios.org
Reading configuration data...
Read main config file okay...
Readobject config files okay...

Running pre-flight check on configuration data...

Checking objects...
Checked2 services.
Checked1 hosts.
Checked1 host groups.
Checked0 service groups.
Checked1 contacts.
Checked1 contact groups.
Checked25 commands.
Checked5 time periods.
Checked0 host escalations.
Checked0 service escalations.
Checkingfor circular paths...
Checked1 hosts
Checked0 service dependencies
Checked0 host dependencies
Checked5 timeperiods
Checkingglobalevent handlers...
Checking obsessive compulsive processor commands...
Checking misc settings...

TotalWarnings: 0
TotalErrors: 0

Things look okay - No serious problems were detected during the pre-flight check
[root@mynagios etc]#

Total Warnings: 0 Total Errors: 0,说明配置文件没有错误可以启动Nagios服务端进行测试了。

测试

  1. http://192.168.250.236/nagios/

搬瓦工推荐方案

温馨提醒 如果您有选择困难症,直接选中间的 CN2 GIA-E方案,季付 $49.99,多达 12 个机房任意切换
方案 内存 CPU 硬盘 流量/月 带宽 机房 价格 购买
CN2
(最便宜)
1GB 1核 20GB 1TB 1Gbps DC3 CN2
DC8 ZNET
$49.99/年 直达
CN2 2GB 1核 40GB 2TB 1Gbps $52.99/半年
$99.99/年
直达
CN2 GIA-E
(最推荐)
1GB 2核 20GB 1TB 2.5Gbps DC6 CN2 GIA-E
DC9 CN2 GIA
日本软银 JPOS_1
荷兰 EUNL_9
$49.99/季度
$169.99/年
直达
CN2 GIA-E 2GB 3核 40GB 2TB 2.5Gbps $89.99/季度
$299.99/年
直达
HK 2GB 2核 40GB 0.5TB 1Gbps 中国香港 CN2 GIA $89.99/月
$899.99/年
直达
HK 4GB 4核 80GB 1TB 1Gbps $155.99/月
$1559.99/年
直达
TOKYO 2GB 2核 40GB 0.5TB 1.2Gbps 日本东京 CN2 GIA $89.99/月
$899.99/年
直达
TOKYO 4GB 4核 80GB 1TB 1.2Gbps $155.99/月
$1559.99/年
直达
搬瓦工优惠码:BWH3HYATVBJW 搬瓦工实时库存
赞(0)
版权声明:本文采用知识共享 署名4.0国际许可协议 [BY-NC-SA] 进行授权, 转载请注明出处。
文章名称:《搬瓦工Centos7.8系统部署Nagios监控系统教程》
文章链接:https://www.veidc.com/31365.html
【声明】:国外主机测评仅分享信息,不参与任何交易,也非中介,所有内容仅代表个人观点,均不作直接、间接、法定、约定的保证,读者购买风险自担。一旦您访问国外主机测评,即表示您已经知晓并接受了此声明通告。
【关于安全】:任何 IDC商家都有倒闭和跑路的可能,备份永远是最佳选择,服务器也是机器,不勤备份是对自己极不负责的表现,请保持良好的备份习惯。