目錄
1. 寫在前面
nagois主要用於監控一臺或者多臺本地主機及遠程主機的各種信息,包括本機資源及對外的服務等。
默認的nagois配置沒有任何監控內容,僅是一些模板文件。若要讓nagois提供服務,就必須修改配置文件,增加要監控的主機和服務,下面將詳細介紹。
2. nagois服務端各配置文件之間的關係
在nagois服務端的配置過程中涉及到的幾個定義有:
主機、主機組,服務、服務組,聯繫人、聯繫人組,監控時間,監控命令等,
從這些定義可以看出,nagois服務端各個配置文件之間是互爲關聯,彼此引用的。
用nagios成功配置出一個監控系統,必須要弄清楚每個配置文件之間依賴與被依賴的關係,最重要的有四點:
- 第一:定義監控哪些主機、主機組、服務和服務組;
- 第二:定義這個監控要用什麼命令實現;
- 第三:定義監控的時間段;
- 第四:定義主機或服務出現問題時要通知的聯繫人和聯繫人組。
爲了能更清楚的說明問題,同時也爲了維護方便,建議將nagios服務端(/usr/local/nagios/etc/objects/)各個定義對象創建獨立的配置文件:
文件 | 默認存在自行創建 | 說明 |
---|---|---|
hosts.cfg | 需要自己創建 | 定義主機和主機組 |
services.cfg | 需要自己創建 | 定義服務和服務組 |
commands.cfg | 默認存在的 | 定義監控命令 |
timeperiods.cfg | 默認存在的 | 定義監控時間段 |
contacts.cfg | 默認存在的 | 定義聯繫人和聯繫人組 |
templates.cfg | 默認存在的 | 作爲資源引用文件 |
[root@nagios-server ~]# cd /usr/local/nagios/etc/
[root@nagios-server etc]# ll
總用量 72
-rw-rw-r--. 1 nagios nagios 13374 5月 8 15:16 cgi.cfg
-rw-r--r--. 1 root root 50 5月 8 15:13 htpasswd
-rw-rw-r--. 1 nagios nagios 44833 5月 8 14:41 nagios.cfg
drwxrwxr-x. 2 nagios nagios 4096 5月 8 14:41 objects
-rw-rw----. 1 nagios nagios 1312 5月 8 14:41 resource.cfg
[root@nagios-server ~]# cd /usr/local/nagios/etc/objects
[root@nagios-server objects]# ll
總用量 48
-rw-rw-r--. 1 nagios nagios 7696 5月 8 14:41 commands.cfg
-rw-rw-r--. 1 nagios nagios 2138 5月 8 14:41 contacts.cfg
-rw-r--r--. 1 root root 0 5月 8 15:42 hosts.cfg #新創建的
-rw-rw-r--. 1 nagios nagios 5379 5月 8 14:41 localhost.cfg
-rw-rw-r--. 1 nagios nagios 3069 5月 8 14:41 printer.cfg
-rw-r--r--. 1 root root 0 5月 8 15:42 services.cfg #新創建的
-rw-rw-r--. 1 nagios nagios 3252 5月 8 14:41 switch.cfg
-rw-rw-r--. 1 nagios nagios 10595 5月 8 14:41 templates.cfg
-rw-rw-r--. 1 nagios nagios 3178 5月 8 14:41 timeperiods.cfg
-rw-rw-r--. 1 nagios nagios 3991 5月 8 14:41 windows.cfg
3. 編輯/usr/local/nagios/etc/objects/hosts.cfg
[root@nagios-server objects]# cat /usr/local/nagios/etc/objects/hosts.cfg
define host{
use linux-server
host_name local-192.168.2.122
alias local-192.168.2.122
address 192.168.2.122
}
define host{
use linux-server
host_name local-192.168.2.124
alias local-192.168.2.124
address 192.168.2.124
}
define host{
use linux-server
host_name local-192.168.2.125
alias local-192.168.2.125
address 192.168.2.125
}
define host{
use linux-server
host_name local-192.168.2.167
alias local-192.168.2.167
address 192.168.2.167
}
define hostgroup{
hostgroup_name linux-servers
alias Linux Servers
members local-192.168.2.122,local-192.168.2.124,local-192.168.2.125,local-192.168.2.167
}
define hostgroup{
hostgroup_name Mysql-servers
alias Mysql Servers
members local-192.168.2.124,,local-192.168.2.167
}
define hostgroup{
hostgroup_name Tomcat-servers
alias Tomcat Servers
members local-192.168.2.122
}
define hostgroup{
hostgroup_name Tcp-servers
alias Tcp Servers
members local-192.168.2.122,local-192.168.2.124,local-192.168.2.125
}
4. 編輯/usr/local/nagios/etc/objects/services.cfg
[root@nagios-server objects]# cat /usr/local/nagios/etc/objects/services.cfg
#define service{
# use local-service ; Name of service template to use
# hostgroup_name linux-servers
# service_description SSH
# check_command check_ssh #不依賴於check_nrpe,即不依賴於客戶端代理檢測程序nrpe
# notifications_enabled 0
# }
define service{
use local-service ; Name of service template to use
hostgroup_name linux-servers
service_description PING
check_command check_ping!100.0,20%!500.0,60% #不依賴於check_nrpe,即不依賴於客戶端代理檢測程序nrpe
}
define service{
use local-service ; Name of service template to use
hostgroup_name linux-servers
service_description Root Partition
check_command check_nrpe!check_root_disk #依賴於check_nrpe,客戶端代理檢測程序nrpe檢測磁盤root分區
}
define service{
use local-service ; Name of service template to use
hostgroup_name linux-servers
service_description Current Users
check_command check_nrpe!check_users #依賴於check_nrpe,客戶端代理檢測程序nrpe檢測登錄用戶
}
define service{
use local-service ; Name of service template to use
hostgroup_name linux-servers
service_description Total Processes
check_command check_nrpe!check_total_procs #依賴於check_nrpe,客戶端代理檢測程序nrpe檢測主機進程
}
define service{
use local-service ; Name of service template to use
hostgroup_name linux-servers
service_description Current Load
check_command check_nrpe!check_load #依賴於check_nrpe,客戶端代理檢測程序nrpe檢測主機負責
}
define service{
use local-service ; Name of service template to use
hostgroup_name linux-servers
service_description Swap Usage
check_command check_nrpe!check_swap #依賴於check_nrpe,客戶端代理檢測程序nrpe檢測swap交換分區
}
define service{
use local-service ; Name of service template to use
hostgroup_name Mysql-servers
service_description Mysql Status
check_command check_nrpe!check_mysql #依賴於check_nrpe,客戶端代理檢測程序nrpe檢測mysql程序運行情況
}
define service{
use local-service ; Name of service template to use
hostgroup_name Tomcat-servers
service_description Tomcat Status
check_command check_nrpe!check_tomcat #依賴於check_nrpe,客戶端代理檢測程序nrpe檢測tomcat程序運行情況
}
define service{
use local-service ; Name of service template to use
hostgroup_name Tcp-servers
service_description Tcp Status
check_command check_nrpe!check_tcp #依賴於check_nrpe,客戶端代理檢測程序nrpe檢測tcp端口運行情況
}
5. 編輯/usr/local/nagios/etc/objects/commands.cfg
[root@nagios-server objects]# vi /usr/local/nagios/etc/objects/commands.cfg
在此配置文件最後添加以下內容,定義監控命令
################################################################################
#
# created by yuki on 2020.05.08
# related to the commands.cfg about check_nrpe
################################################################################
# 'check_nrpe' command definition
define command{
command_name check_nrpe
command_line $USER1$/check_nrpe -H $HOSTADDRESS$ -c $ARG1$
}
6. 編輯/usr/local/nagios/etc/objects/contacts.cfg
[root@nagios-server objects]# vi /usr/local/nagios/etc/objects/contacts.cfg
###############################################################################
###############################################################################
#
# CONTACTS
#
###############################################################################
###############################################################################
define contact{
contact_name nagiosadmin ; Short name of user
use generic-contact ; Inherit default values from generic-contact template (defined above)
alias Nagios Admin ; Full name of user
email nagios@localhost ; <<***** CHANGE THIS TO YOUR EMAIL ADDRESS ******
}
################################################################################
#
# created by yuki on 2020.05.08
#
################################################################################
define contact{
contact_name yuki
use generic-contact
alias yuki
email [email protected]
}
###############################################################################
###############################################################################
#
# CONTACT GROUPS
#
# modified by yuki on 2020.05.08
###############################################################################
###############################################################################
define contactgroup{
contactgroup_name admins
alias Nagios Administrators
members yuki
}
7. 配置/usr/local/nagios/etc/nagios.cfg
以上配置完成以後,然後配置/usr/local/nagios/etc/nagios.cfg來引用以上面幾個配置文件。
[root@nagios-server etc]# vi /usr/local/nagios/etc/nagios.cfg +36
在32行下面添加兩行如下配置,表示引用此配置文件:
cfg_file=/usr/local/nagios/etc/objects/hosts.cfg
cfg_file=/usr/local/nagios/etc/objects/services.cfg
8. 驗證配置文件的正確性
[root@nagios-server etc]# /usr/local/nagios/bin/nagios -v /usr/local/nagios/etc/nagios.cfg
Nagios Core 4.3.2
Copyright (c) 2009-present Nagios Core Development Team and Community Contributors
Copyright (c) 1999-2009 Ethan Galstad
Last Modified: 2017-05-09
License: GPL
Website: https://www.nagios.org
Reading configuration data...
Read main config file okay...
Warning: Duplicate definition found for hostgroup 'linux-servers' (config file '/usr/local/nagios/etc/objects/localhost.cfg', starting on line 45) #監控主機組名重複,隨便修改成其他的,如linux-servers-local
Error: Could not add object property in file '/usr/local/nagios/etc/objects/localhost.cfg' on line 46.
Error: Invalid max_check_attempts value for host 'local-192.168.2.122'
Error: Could not register host (config file '/usr/local/nagios/etc/objects/hosts.cfg', starting on line 1)
Error processing object config files!
***> One or more problems was encountered while processing the config files...
Check your configuration file(s) to ensure that they contain valid
directives and data definitions. If you are upgrading from a previous
version of Nagios, you should be aware that some variables/definitions
may have been removed or modified in this version. Make sure to read
the HTML documentation regarding the config files, as well as the
'Whats New' section to find out what has changed.
9. 重啓nagios-server
[root@dscq-236 objects]# chown -R nagios:nagios /usr/local/nagios/
[root@nagios-server objects]# systemctl start nagios.service && echo $?
0
[root@nagios-server objects]# systemctl stop nagios.service && echo $?
0
[root@nagios-server objects]# systemctl restart nagios.service && echo $?
0
[root@nagios-server objects]# systemctl status nagios.service && echo $?
0