目錄
1. 被監控主機環境準備
1.1 主機信息+IP地址
#一臺虛擬機
hostname:nagios-client
ip:ip_address
1.2 查看主機的系統版本信息
[root@nagios-client ~]# cat /etc/redhat-release
CentOS Linux release 7.2.1511 (Core)
1.3 暫時關掉Selinux和防火牆
[root@nagios-client ~]# sed -i "s#enforcing#disabled#g" /etc/sysconfig/selinux
[root@nagios-client ~]# cat /etc/sysconfig/selinux
# This file controls the state of SELinux on the system.
# SELINUX= can take one of these three values:
# disabled - SELinux security policy is enforced.
# permissive - SELinux prints warnings instead of disabled.
# disabled - No SELinux policy is loaded.
SELINUX=disabled
# SELINUXTYPE= can take one of three two values:
# targeted - Targeted processes are protected,
# minimum - Modification of targeted policy. Only selected processes are protected.
# mls - Multi Level Security protection.
SELINUXTYPE=targeted
[root@nagios-client ~]# setenforce 0 && getenforce
Permissive
---------------------------------------------------------------------------------------------------------------------------------------
[root@nagios-client ~]# firewall-cmd --state
running
[root@nagios-client ~]# systemctl stop firewalld.service
[root@nagios-client ~]# firewall-cmd --state
not running
1.4 安裝需要的依賴以及更新系統時間軟件
[root@nagios-client ~]# yum install -y gcc glibc glibc-common gd gd-devel xinetd openssl-devel ntpdate
[root@nagios-client ~]# ntpdate pool.ntp.org
1.5 創建nagios用戶和用戶組
[root@nagios-client ~]# useradd nagios&& echo "Aa123456" | passwd --stdin nagios
[root@nagios-client ~]# id nagios
uid=1004(nagios) gid=1004(nagios) 組=1004(nagios)
2. nagios被監控端環境搭建
說明:被監控端需要安裝nrpe軟件,但因爲nrpe又依賴於nagios-plugins,所以需要先下載安裝nagios-plugins,nagios-plugins最好和服務端的版本保持一致。
2.1 下載nrpe和nagios-plugins安裝包
2.1.1 nagios插件
[root@nagios-client ~]# cd /home/tools
[root@nagios-client tools]# wget https://nagios-plugins.org/download/nagios-plugins-2.3.3.tar.gz
[root@nagios-client tools]# ll
-rw-r--r--. 1 root root 2782610 3月 12 00:58 nagios-plugins-2.3.3.tar.gz
2.1.2 nagios客戶端代理檢測程序
[root@nagios-client ~]# cd /home/tools
[root@nagios-client tools]# wget https://github.com/NagiosEnterprises/nrpe/releases/download/nrpe-3.2.0/nrpe-3.2.0.tar.gz
[root@nagios-client tools]# ll
總用量 14072
-rw-r--r--. 1 root root 2782610 3月 12 00:58 nagios-plugins-2.3.3.tar.gz
-rw-r--r--. 1 root root 517743 6月 28 2017 nrpe-3.2.0.tar.gz
2.2 安裝部署nagios插件
2.2.1 解壓nagios插件安裝包
[root@nagios-client tools]# tar xf nagios-plugins-2.3.3.tar.gz
[root@nagios-client tools]# ll
總用量 10076
drwxr-xr-x. 15 root root 4096 3月 12 00:58 nagios-plugins-2.3.3
-rw-r--r--. 1 root root 2782610 3月 12 00:58 nagios-plugins-2.3.3.tar.gz
-rw-r--r--. 1 root root 517743 6月 28 2017 nrpe-3.2.0.tar.gz
2.2.2 配置編譯
[root@nagios-client tools]# cd nagios-plugins-2.3.3
[root@nagios-client nagios-plugins-2.3.3]# ll
總用量 3452
-rw-r--r--. 1 root root 80541 1月 17 2017 ABOUT-NLS
-rw-r--r--. 1 root root 2332 1月 17 2017 acinclude.m4
-rw-r--r--. 1 root root 1275 1月 17 2017 ACKNOWLEDGEMENTS
-rw-r--r--. 1 root root 42202 3月 12 00:57 aclocal.m4
-rw-r--r--. 1 root root 417 12月 5 05:53 AUTHORS
drwxr-xr-x. 3 root root 4096 3月 12 00:58 build-aux
-rw-r--r--. 1 root root 890609 3月 12 00:58 ChangeLog
-rw-r--r--. 1 root root 4918 12月 5 05:53 CODING
-rw-r--r--. 1 root root 78119 3月 12 00:58 config.h.in
-rwxr-xr-x. 1 root root 18163 1月 17 2017 config.rpath
drwxr-xr-x. 2 root root 56 3月 12 00:58 config_test
-rwxr-xr-x. 1 root root 2022791 3月 12 00:58 configure #編譯安裝腳本
-rw-r--r--. 1 root root 69121 3月 12 00:57 configure.ac
-rw-r--r--. 1 root root 35147 1月 17 2017 COPYING
-rw-r--r--. 1 root root 3716 1月 17 2017 FAQ
drwxr-xr-x. 4 root root 8192 3月 12 00:58 gl
-rw-r--r--. 1 root root 15578 3月 12 00:58 INSTALL
-rw-r--r--. 1 root root 443 1月 17 2017 LEGAL
drwxr-xr-x. 3 root root 4096 3月 12 00:58 lib
drwxr-xr-x. 2 root root 54 3月 12 00:58 m4
-rw-r--r--. 1 root root 2144 12月 5 05:53 Makefile.am
-rw-r--r--. 1 root root 67135 3月 12 00:58 Makefile.in
-rwxr-xr-x. 1 root root 3474 1月 17 2017 mkinstalldirs
-rw-r--r--. 1 root root 6471 3月 12 00:58 nagios-plugins.spec
-rw-r--r--. 1 root root 6491 12月 5 05:53 nagios-plugins.spec.in
-rw-r--r--. 1 root root 46403 3月 12 00:57 NEWS
-rw-r--r--. 1 root root 17504 12月 5 05:53 NPTest.pm
-rwxr-xr-x. 1 root root 1161 3月 12 00:57 NP-VERSION-GEN
drwxr-xr-x. 2 root root 4096 3月 12 00:58 perlmods
drwxr-xr-x. 5 root root 46 1月 17 2017 pkg
drwxr-xr-x. 4 root root 4096 3月 12 00:58 plugins
drwxr-xr-x. 3 root root 101 3月 12 00:58 plugins-root
drwxr-xr-x. 3 root root 4096 3月 12 00:58 plugins-scripts
drwxr-xr-x. 2 root root 4096 3月 12 00:58 po
-rw-r--r--. 1 root root 5440 12月 5 05:53 README
-rw-r--r--. 1 root root 6 3月 12 00:58 release
-rw-r--r--. 1 root root 3304 1月 17 2017 REQUIREMENTS
-rw-r--r--. 1 root root 3871 1月 17 2017 SUPPORT
drwxr-xr-x. 2 root root 79 3月 12 00:58 tap
-rwxr-xr-x. 1 root root 831 1月 17 2017 test.pl.in
-rw-r--r--. 1 root root 6265 12月 5 05:54 THANKS
drwxr-xr-x. 2 root root 81 3月 12 00:58 tools
指定安裝路徑以及其他需要的參數
[root@nagios-client nagios-plugins-2.3.3]# ./configure --prefix=/usr/local/nagios \
--with-mysql=/usr/local/mysql #如果需要監控客戶端的mysql的運行情況,必須在編譯安裝插件時加此選項
2.2.3 編譯安裝
[root@nagios-server nagios-plugins-2.3.3]# make && make install && echo $?
2.2.4 驗證nagios插件程序是否被正確安裝
切換目錄到安裝路徑(這裏是/usr/local/nagios/libexec) ,下看是否存在很多 check文件,若有則安裝插件成功,若無就需要重新安裝。
root@nagios-client nagios-plugins-2.3.3]# chown -R nagios.nagios /usr/local/nagios
[root@nagios-client nagios-plugins-2.3.3]# ll /usr/local/nagios/
總用量 4
drwxr-xr-x. 2 nagios nagios 6 5月 9 14:59 include
drwxr-xr-x. 2 nagios nagios 4096 5月 9 14:59 libexec ######
drwxr-xr-x. 3 nagios nagios 19 5月 9 14:59 share
[root@nagios-client nagios-plugins-2.3.3]# ll /usr/local/nagios/libexec/
2.3 安裝部署nrpe軟件
2.3.1 解壓安裝包
[root@nagios-client nagios-plugins-2.3.3]# cd /home/tools
[root@nagios-client tools]# tar xf nrpe-3.2.0.tar.gz
[root@nagios-client tools]# ll
總用量 10076
drwxr-xr-x. 15 root root 4096 3月 12 00:58 nagios-plugins-2.3.3
-rw-r--r--. 1 root root 2782610 3月 12 00:58 nagios-plugins-2.3.3.tar.gz
drwxr-xr-x. 15 root root 4096 3月 12 00:58 nrpe-3.2.0
-rw-r--r--. 1 root root 517743 6月 28 2017 nrpe-3.2.0.tar.gz
2.3.2 配置編譯
說明:nrpe3.0之前版本將nrpe作爲xinetd下獨立服務運行,nrpe3.0以後的版本不需要了,所以和以前版本的安裝步驟有一些不同。
[root@nagios-client tools]# cd nrpe-3.2.0
[root@nagios-client nrpe-3.2.0]# ll
總用量 392
-rwxrwxr-x. 1 root root 283 6月 28 2017 aclocal.m4
drwxrwxr-x. 2 root root 80 6月 28 2017 build-aux
-rw-rw-r--. 1 root root 18788 6月 28 2017 CHANGELOG.md
-rwxrwxr-x. 1 root root 249031 6月 28 2017 configure #編譯安裝腳本
-rw-rw-r--. 1 root root 11389 6月 28 2017 configure.ac
drwxrwxr-x. 2 root root 65 6月 28 2017 contrib
drwxrwxr-x. 2 root root 36 6月 28 2017 docs
drwxrwxr-x. 2 root root 81 6月 28 2017 include
-rw-rw-r--. 1 root root 441 6月 28 2017 LEGAL
-rw-rw-r--. 1 root root 15173 6月 28 2017 LICENSE.md
drwxrwxr-x. 2 root root 4096 6月 28 2017 macros
-rw-rw-r--. 1 root root 6915 6月 28 2017 Makefile.in
-rw-rw-r--. 1 root root 5879 6月 28 2017 nrpe.spec.in
-rwxrwxr-x. 1 root root 3248 6月 28 2017 paths.in
-rw-rw-r--. 1 root root 9807 6月 28 2017 README.md
-rw-rw-r--. 1 root root 11692 6月 28 2017 README.SSL.md
drwxrwxr-x. 2 root root 24 6月 28 2017 sample-config
-rw-rw-r--. 1 root root 3647 6月 28 2017 SECURITY.md
drwxrwxr-x. 2 root root 99 6月 28 2017 src
drwxrwxr-x. 2 root root 4096 6月 28 2017 startup
-rwxrwxr-x. 1 root root 74 6月 28 2017 test-wrapper
-rw-rw-r--. 1 root root 782 6月 28 2017 THANKS
-rwxrwxr-x. 1 root root 981 6月 28 2017 travis-test-1
-rwxrwxr-x. 1 root root 10637 6月 28 2017 uninstall.in
-rwxrwxr-x. 1 root root 4326 6月 28 2017 update-cfg.pl
-rwxrwxr-x. 1 root root 3067 6月 28 2017 update-version
不需要指定安裝路徑以及其他需要的參數
[root@nagios-client nrpe-3.2.0]# ./configure && echo $?
2.3.3 編譯安裝
make all 編譯所有
[root@nagios-client nrpe-3.2.0]# make all && echo $?
make install 編譯安裝
[root@nagios-client nrpe-3.2.0]# make install && echo $?
安裝check_nrpe 這個插件
[root@nagios-client nrpe-3.2.0]# make install-plugin && echo $?
安裝deamon
[root@nagios-client nrpe-3.2.0]# make install-daemon && echo $?
安裝配置文件
[root@nagios-client nrpe-3.2.0]# make install-daemon-config && echo $?
make: *** 沒有規則可以創建目標“install-xinetd”。 停止。
# nrpe3.0之前版本需要執行 make install-daemon-config
# nrpe3.0以上版本執行此命令時會報上面這個錯誤提示沒有這個參數。
# 現在應該執行以下命令:
[root@nagios-client nrpe-3.2.0]# make install-config && echo $?
這時可以看到安裝時修改了etc目錄的權限,把配置文件複製到了etc目錄下。
安裝啓動文件
[root@nagios-client nrpe-3.2.0]# make install-xinetd && echo $?
make: *** 沒有規則可以創建目標“install-xinetd”。 停止。
# nrpe3.0之前版本將nrpe作爲xinetd下獨立服務運行
# nrpe3.0之前版本需要執行 make install-xinetd
# nrpe3.0以上版本執行此命令時會報上面這個錯誤提示沒有這個參數。
[root@nagios-client nrpe-3.2.0]# ll Makefile
-rw-r--r--. 1 root root 6953 5月 9 15:10 Makefile
[root@nagios-client nrpe-3.2.0]# grep install-inetd Makefile
echo " install-inetd install the startup files for inetd, launchd, etc."; install-inetd:
[root@nagios-client nrpe-3.2.0]# grep install-init Makefile
echo " install-init install the startup files for init, systemd, etc."; install-init:
# 現在應該執行以下命令:
[root@nagios-client nrpe-3.2.0]# make install-inetd && echo $?
[root@nagios-client nrpe-3.2.0]# make install-init
[root@nagios-client nrpe-3.2.0]# echo $?
2.4 對服務端主機監控進行授權
將 /usr/local/nagios/etc/nrpe.cfg第106行
#allowed_hosts=127.0.0.1,::1
修改爲:
allowed_hosts=127.0.0.1,192.168.2.236 #添加nagios服務端的ip地址
[root@nagios-client nrpe-3.2.0]# sed -i "106s/::1/192.168.2.236/g" /usr/local/nagios/etc/nrpe.cfg
[root@nagios-client ~]# grep allowed_hosts /usr/local/nagios/etc/nrpe.cfg
allowed_hosts=127.0.0.1,192.168.2.236
2.5 設置nrpe服務開機自啓動
[root@nagios-client ~]# systemctl enable nrpe.service && echo $?
2.6 啓動重啓停止服務
啓動重啓服務
[root@nagios-client nrpe-3.2.0]# /bin/systemctl start nrpe.service && echo $?
[root@nagios-client nrpe-3.2.0]# /bin/systemctl restart nrpe.service && echo $?
[root@nagios-client nrpe-3.2.0]# /bin/systemctl stop nrpe.service && echo $?
驗證服務是否啓動成功
[root@nagios-client nrpe-3.2.0]# /bin/systemctl start nrpe.service && echo $?
[root@nagios-client nrpe-3.2.0]# ps -ef |grep nrpe
nagios 68784 1 0 15:32 ? 00:00:00 /usr/local/nagios/bin/nrpe -c /usr/local/nagios/etc/nrpe.cfg -f
root 68790 308 0 15:33 pts/0 00:00:00 grep --color=auto nrpe
[root@nagios-client nrpe-3.2.0]# lsof -i:5666
COMMAND PID USER FD TYPE DEVICE SIZE/OFF NODE NAME
nrpe 68784 nagios 4u IPv4 2908070 0t0 TCP *:nrpe (LISTEN)
nrpe 68784 nagios 5u IPv6 2908071 0t0 TCP *:nrpe (LISTEN)
[root@nagios-client nrpe-3.2.0]# /bin/systemctl status nrpe.service && echo $?
● nrpe.service - Nagios Remote Plugin Executor
Loaded: loaded (/usr/lib/systemd/system/nrpe.service; disabled; vendor preset: disabled)
Active: active (running) since 六 2020-05-09 15:32:18 CST; 2s ago
Docs: http://www.nagios.org/documentation
Main PID: 68784 (nrpe)
CGroup: /system.slice/nrpe.service
└─68784 /usr/local/nagios/bin/nrpe -c /usr/local/nagios/etc/nrpe.cfg -f
5月 09 15:32:18 nagios-client systemd[1]: Started Nagios Remote Plugin Executor.
5月 09 15:32:18 nagios-client nrpe[68784]: Starting up daemon
5月 09 15:32:18 nagios-client nrpe[68784]: Server listening on 0.0.0.0 port 5666.
5月 09 15:32:18 nagios-client nrpe[68784]: Server listening on :: port 5666.
5月 09 15:32:18 nagios-client nrpe[68784]: Listening for connections on port 5666
5月 09 15:32:18 nagios-client nrpe[68784]: Allowing connections from: 127.0.0.1,::1
0
3. 配置監控項以及告警指標
3.1 先在本地被監控主機上測試一下監控命令
監控主機上的登錄用戶
[root@nagios-client ~]# /usr/local/nagios/libexec/check_users -w 5 -c 10
USERS OK - 2 users currently logged in |users=2;5;10;0
監控主機負載
[root@nagios-client ~]# /usr/local/nagios/libexec/check_load -w 15,10,5 -c 30,25,20
OK - load average: 0.00, 0.01, 0.05|load1=0.000;15.000;30.000;0; load5=0.010;10.000;25.000;0; load15=0.050;5.000;20.000;0;
監控主機磁盤使用情況
[root@nagios-client ~]# /usr/local/nagios/libexec/check_disk -w 20% -c 10% -p /
DISK OK - free space: / 45597 MiB (89.10% inode=100%);| /=5577MiB;40940;46057;0;51175
監控主機上運行的進程
[root@nagios-client ~]# /usr/local/nagios/libexec/check_procs -w 250 -c 400
PROCS WARNING: 373 processes | procs=373;250;400;0;
監控主機交換分區使用情況
[root@nagios-client ~]# /usr/local/nagios/libexec/check_swap -w 20% -c 10%
SWAP OK - 100% free (7551 MB out of 7551 MB) |swap=7551MB;1510;755;0;7551
其他監控命令使用教程請參考手冊:http://nagios-plugins.org/doc/man/
3.2 將測試無誤監控命令寫入配置文件
[root@nagios-client ~]# cd /usr/local/nagios/etc/
[root@nagios-client etc]# ll
總用量 16
-rw-r--r--. 1 nagios nagios 13121 5月 9 15:49 nrpe.cfg
[root@nagios-client etc]# vi /usr/local/nagios/etc/nrpe.cfg +333
[root@nagios-client etc]# cat /usr/local/nagios/etc/nrpe.cfg |grep -v "^#"|grep -v "^$"
log_facility=daemon
debug=0
pid_file=/usr/local/nagios/var/nrpe.pid
server_port=5666
nrpe_user=nagios
nrpe_group=nagios
allowed_hosts=127.0.0.1,192.168.2.236
dont_blame_nrpe=0
allow_bash_command_substitution=0
command_timeout=60
connection_timeout=300
command[check_users]=/usr/local/nagios/libexec/check_users -w 5 -c 10
command[check_load]=/usr/local/nagios/libexec/check_load -r -w .15,.10,.05 -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_root_disk]=/usr/local/nagios/libexec/check_disk -w 20% -c 10% -p /
command[check_swap]=/usr/local/nagios/libexec/check_swap -w 20% -c 10%
#再次重啓nrpe服務
[root@nagios-client etc]# /bin/systemctl restart nrpe.service && echo $?
3.3 針對nrpe服務監聽端口進行配置
[root@nagios-client etc]# systemctl start firewalld.service
[root@nagios-client etc]# firewall-cmd --zone=public --permanent --add-port=5666/tcp && firewall-cmd --reload && firewall-cmd --list-ports