您好,登錄后才能下訂單哦!
篇幅所限,看“Linux下Nagios的安裝與配置<< 二 >>”之前請先看“Linux下Nagios的安裝與配置<< 一 >>”
未完,還有Linux下Nagios的安裝與配置<< 三 >>
localhost.cfg
Definehost{
Use linux-server ; Name of host template to use
; This host definition will inherit all variables that are defined
; in (or inherited by) the linux-server host template definition.
Host_name Nagios-Server
Alias Nagios-Server
Address 127.0.0.1
}
definehostgroup{
hostgroup_name linux-servers ; The name of the hostgroup
Alias Linux Servers ; Long name of thegroup
members Nagios-Server ; Comma separated listof hosts that belong to this group
}
defineservice{
Use local-service ; Name of service template to use
Host_name Nagios-Server
service_description PING
check_command check_ping!100.0,20%!500.0,60%
}
Defineservice{
use local-service ; Name of service template to use
host_name Nagios-Server
service_description Root Partition
check_command check_local_disk!20%!10%!/
}
defineservice{
use local-service ; Name of service template to use
host_name Nagios-Server
service_description Current Users
check_command check_local_users!20!50
}
defineservice{
use local-service ; Name of service template to use
host_name Nagios-Server
service_description Total Processes
check_command check_local_procs!250!400!RSZDT
}
Defineservice{
Use local-service ; Name of service template to use
host_name Nagios-Server
service_description Current Load
check_command check_local_load!5.0,4.0,3.0!10.0,6.0,4.0
}
defineservice{
use local-service ; Name of service template to use
host_name Nagios-Server
service_description Swap Usage
check_command check_local_swap!20!10
}
defineservice{
Use local-service ; Name of service template to use
host_name Nagios-Server
service_description SSH
check_command check_ssh
notifications_enabled 0
}
defineservice{
Use local-service ; Name of service template to use
host_name Nagios-Server
service_description HTTP
check_command check_http
notifications_enabled 0
}
windows.cfg,本實驗主要采用組進行配置,此文檔不刪掉不使,下面配置供參考。
definehost{
use windows-server ; Inherit default values from a template
host_name Nagios-Windows ; The name we're giving to this host
alias My Windows Server ; A longer name associated with the host
address 192.168.1.113 ; IP address of the host
}
definehostgroup{
hostgroup_name windows-servers ; The name of the hostgroup
alias Windows Servers ; Long name of thegroup
}
defineservice{
use generic-service
host_name Nagios-Windows
service_description NSClient++ Version
check_command check_nt!CLIENTVERSION
}
defineservice{
use generic-service
host_name Nagios-Windows
service_description Uptime
check_command check_nt!UPTIME
}
defineservice{
use generic-service
host_name Nagios-Windows
service_description CPU Load
check_command check_nt!CPULOAD!-l 5,80,90
}
defineservice{
use generic-service
host_name Nagios-Windows
service_description Memory Usage
check_command check_nt!MEMUSE!-w 80 -c 90
}
defineservice{
use generic-service
host_name Nagios-Windows
service_description C:\ Drive Space
check_command check_nt!USEDDISKSPACE!-l c -w 80 -c90
}
defineservice{
use generic-service
host_name Nagios-Windows
service_description W3SVC
check_command check_nt!SERVICESTATE!-d SHOWALL -lW3SVC
}
defineservice{
use generic-service
host_name Nagios-Windows
service_description Explorer
check_command check_nt!PROCSTATE!-d SHOWALL -lExplorer.exe
}
e. services.cfg文件
此文件默認也不存在,需要手動創建,services.cfg文件主要用于定義監控的服務和主機資源,例如監控http服務、ftp服務、主機磁盤空間、主機系統負載等等。Nagios-Server 和Nagios-Windows 相關服務已在相應的配置文件中定義,所以這里只需要定義Nagios-Linux 相關服務即可,這里只定義一個檢測是否存活的服務來驗證配置文件的正確性,其他服務的定義將在后面講到。
define service{
use local-service ; #引用local-service服務的屬性值,local-service在templates.cfg文件中進行了定義。
host_name Nagios-Linux ; #指定要監控哪個主機上的服務,“Nagios-Server”在hosts.cfg文件中進行了定義。
service_description PING ; #對監控服務內容的描述,以供維護人員參考。
contact_groups linuxcontact
check_command check-host-alive ; #指定檢查的命令。
}
define service{
use local-service
host_name Nagios-Linux
service_description CurrentLoad
contact_groups linuxcontact
check_command check_nrpe!check_load
}
define service{
use local-service
host_name Nagios-Linux
service_description TotalProcesses
contact_groups linuxcontact
check_command check_nrpe!check_total_procs
}
define service{
use local-service
host_name Nagios-Linux
service_description CurrentUsers
contact_groups linux contact
check_command check_nrpe!check_users
}
define service{
use local-service
host_name Nagios-Linux
service_description CheckZombie Procs
contact_groups linuxcontact
check_command check_nrpe!check_zombie_procs
}
define service{
use local-service
host_name Nagios-Linux
service_description Check Swap
contact_groups linux contact
check_command check_nrpe!check_swap
}
# SERVICE DEFINITIONS
#
###############################################################################
# Create a service for monitoring the versionof NSCLient++ that is installed
# Change the host_name to match the name ofthe host you defined above
define service{
use generic-service
host_name Nagios-Windows
service_description NSClient++Version
contact_groups windowscontact
check_command check_nt!CLIENTVERSION
}
# Create a service for monitoring the uptimeof the server
# Change the host_name to match the name ofthe host you defined above
define service{
use generic-service
host_name Nagios-Windows
service_description Uptime
contact_groups windowscontact
check_command check_nt!UPTIME
}
# Create a service for monitoring CPU load
# Change the host_name to match the name ofthe host you defined above
define service{
use generic-service
host_name Nagios-Windows
service_description CPU Load
contact_groups windowscontact
check_command check_nt!CPULOAD!-l 5,80,90
}
# Create a service for monitoring memoryusage
# Change the host_name to match the name ofthe host you defined above
define service{
use generic-service
host_name Nagios-Windows
service_description MemoryUsage
contact_groups windowscontact
check_command check_nt!MEMUSE!-w 80 -c 90
}
# Create a service for monitoring C:\ diskusage
# Change the host_name to match the name ofthe host you defined above
define service{
use generic-service
host_name Nagios-Windows
service_description C:\ DriveSpace
contact_groups windowscontact
check_command check_nt!USEDDISKSPACE!-l c -w 80 -c 90
}
# Create a service for monitoring the W3SVCservice
# Change the host_name to match the name ofthe host you defined above
define service{
use generic-service
host_name Nagios-Windows
service_description W3SVC
contact_groups windowscontact
check_command check_nt!SERVICESTATE!-d SHOWALL -l W3SVC
}
# Create a service for monitoring theExplorer.exe process
# Change the host_name to match the name ofthe host you defined above
define service{
use generic-service
host_name Nagios-Windows
service_description Explorer
contact_groups windowscontact
check_command check_nt!PROCSTATE!-d SHOWALL -l Explorer.exe
}
f. contacts.cfg,contactgroup.cfg文件,默認不存在,需手動創建!
contacts.cfg是一個定義聯系人和聯系人組的配置文件,當監控的主機或者服務出現故障,nagios會通過指定的通知方式(郵件或者短信)將信息發給這里指定的聯系人或者使用者。contactgroup.cfg定義聯系人組的配置文件,注意:members選項里面的聯系人在contacts.cfg里面要要定義,多個聯系從之間用逗號隔開。
contacts.cfg下面的配置文檔
definecontact{
contact_name echo ; Short name of user
use generic-contact ; Inheritdefault values from generic-contact template (defined above)
alias linux contact ; Full name of user
email test1@163.com ; <<***** CHANGE THIS TO YOUREMAIL ADDRESS ******
}
definecontact{
contact_name jason ; Short name of user
use generic-contact ; Inheritdefault values from generic-contact template (defined above)
alias windows contact ; Full name of user
email test2@qq.com ; <<***** CHANGE THIS TO YOUREMAIL ADDRESS ******
}
definecontact{
contact_name johnson ; Short name of user
use generic-contact ; Inheritdefault values from generic-contact template (defined above)
alias windows and linuxcontact ; Full name of user
email test3@qq.com ; <<***** CHANGE THIS TOYOUR EMAIL ADDRESS ******
}
contactgroup.cfg下面的配置文檔
# We only have one contact in this simpleconfiguration file, so there is
# no need to create more than one contactgroup.
define contactgroup{
contactgroup_name linuxcontact
alias linuxserver contact
members echo,johnson
}
define contactgroup{
contactgroup_name windows contact
alias windowsserver contact
members jason,johnson
}
define contactgroup{
contactgroup_name allcontact
alias linux andwindows server contact
members jason,echo,johnson
}
g. timeperiods.cfg文件
此文件只要用于定義監控的時間段,下面是一個配置好的實例:也可以根據自己需求監控情況進行配置。
#下面是定義一個名為24x7的時間段,即監控所有時間段
definetimeperiod{
timeperiod_name 24x7 #時間段的名稱,這個地方不要有空格
alias 24 Hours A Day, 7 Days A Week
sunday 00:00-24:00
monday 00:00-24:00
tuesday 00:00-24:00
wednesday 00:00-24:00
thursday 00:00-24:00
friday 00:00-24:00
saturday 00:00-24:00
}
#下面是定義一個名為workhours的時間段,即工作時間段。
definetimeperiod{
timeperiod_name workhours
alias Normal Work Hours
monday 09:00-17:00
tuesday 09:00-17:00
wednesday 09:00-17:00
thursday 09:00-17:00
friday 09:00-17:00
}
h. cgi.cfg文件
此文件用來控制相關cgi腳本,如果想在nagios的web監控界面執行cgi腳本,例如重啟nagios進程、關閉nagios通知、停止nagios主機檢測等,這時就需要配置cgi.cfg文件了。
由于nagios的web監控界面驗證用戶為david,所以只需在cgi.cfg文件中添加此用戶的執行權限就可以了,需要修改的配置信息如下:
default_user_name=david
authorized_for_system_information=nagiosadmin,david
authorized_for_configuration_information=nagiosadmin,david
authorized_for_system_commands=david
authorized_for_all_services=nagiosadmin,david
authorized_for_all_hosts=nagiosadmin,david
authorized_for_all_service_commands=nagiosadmin,david
authorized_for_all_host_commands=nagiosadmin,david
i. nagios.cfg文件
nagios.cfg默認的路徑為/usr/local/nagios/etc/nagios.cfg,是nagios的核心配置文件,所有的對象配置文件都必須在這個文件中進行定義才能發揮其作用,這里只需將對象配置文件在Nagios.cfg文件中進行引用即可。
log_file=/usr/local/nagios/var/nagios.log # 定義nagios日志文件的路徑
#cfg_file=/usr/local/nagios/etc/objects/commands.cfg # “cfg_file”變量用來引用對象配置文件,如果有更多的對象配置文件,在這里依次添加即可。
#cfg_file=/usr/local/nagios/etc/objects/contacts.cfg # 默認開啟,需注銷
#cfg_file=/usr/local/nagios/etc/objects/hosts.cfg # 默認開啟,需注銷
#cfg_file=/usr/local/nagios/etc/objects/services.cfg # 默認開啟,需注銷
#cfg_file=/usr/local/nagios/etc/objects/timeperiods.cfg # 默認開啟,需注銷
#cfg_file=/usr/local/nagios/etc/objects/templates.cfg # 默認開啟,需注銷
#cfg_file=/usr/local/nagios/etc/objects/localhost.cfg # 本機配置文件,默認開啟,需注銷
#cfg_file=/usr/local/nagios/etc/objects/windows.cfg # windows 主機配置文件默認開啟,需注銷
cfg_dir=/usr/local/nagios/etc/servers #servers文件默認不存在,需手動創建
#object_cache_file=/usr/local/nagios/var/objects.cache # 該變量用于指定一個“所有對象配置文件”的副本文件,或者叫對象緩沖文件
precached_object_file=/usr/local/nagios/var/objects.precache
resource_file=/usr/local/nagios/etc/resource.cfg # 該變量用于指定nagios資源文件的路徑,可以在nagios.cfg中定義多個資源文件。
status_file=/usr/local/nagios/var/status.dat # 該變量用于定義一個狀態文件,此文件用于保存nagios的當前狀態、注釋和宕機信息等。
status_update_interval=10 # 該變量用于定義狀態文件(即status.dat)的更新時間間隔,單位是秒,最小更新間隔是1秒。
nagios_user=nagios # 該變量指定了Nagios進程使用哪個用戶運行。
nagios_group=nagios # 該變量用于指定Nagios使用哪個用戶組運行。
check_external_commands=1 # 該變量用于設置是否允許nagios在web監控界面運行cgi命令;
# 也就是是否允許nagios在web界面下執行重啟nagios、停止主機/服務檢查等操作;
# “1”為運行,“0”為不允許。
command_check_interval=10s # 該變量用于設置nagios對外部命令檢測的時間間隔,如果指定了一個數字加一個"s"(如10s);
# 那么外部檢測命令的間隔是這個數值以秒為單位的時間間隔;
# 如果沒有用"s",那么外部檢測命令的間隔是以這個數值的“時間單位”的時間間隔。
interval_length=60 # 該變量指定了nagios的時間單位,默認值是60秒,也就是1分鐘;
# 即在nagios配置中所有的時間單位都是分鐘。
6.4 驗證Nagios 配置文件的正確性
Nagios 在驗證配置文件方面做的非常到位,只需通過一個命令即可完成:
#/usr/local/nagios/bin/nagios -v /usr/local/nagios/etc/nagios.cfg
Nagios提供的這個驗證功能非常有用,在錯誤信息中通常會打印出錯誤的配置文件以及文件中的哪一行,這使得nagios的配置變得非常容易,報警信息通常是可以忽略的,因為一般那些只是建議性的。
看到上面這些信息就說明沒問題了,然后啟動Nagios 服務。
七、Nagios的啟動與停止
7.1 啟動Nagios
a. 通過初始化腳本啟動nagios
#/etc/init.d/nagios start
or
# service nagios start
b. 手工方式啟動nagios
通過nagios命令的“-d”參數來啟動nagios守護進程:
#/usr/local/nagios/bin/nagios -d /usr/local/nagios/etc/nagios.cfg
7.2 重啟Nagios
當修改了配置文件讓其生效時,需要重啟/重載Nagios服務。
a. 通過初始化腳本來重啟nagios
#/etc/init.d/nagios reload
or
# /etc/init.d/nagios restart
or
# service nagios restart
b. 通過web監控頁重啟nagios
可以通過web監控頁的 "ProcessInfo" -> "Restart the Nagios process"來重啟nagios
c. 手工方式平滑重啟
#kill -HUP <nagios_pid>
7.3 停止Nagios
a. 通過初始化腳本關閉nagios服務
#/etc/init.d/nagios stop
or
# service nagios stop
b. 通過web監控頁停止nagios
可以通過web監控頁的 "ProcessInfo" -> "Shutdown the Nagios process"來停止nagios
c. 手工方式停止Nagios
#kill <nagios_pid>
八、查看初步配置情況
8.1 啟動完成之后,登錄Nagios Web監控頁http://192.168.1.108/nagios/查看相關信息。
8.2 點擊左面的Current Status -> Hosts 可以看到所定義的三臺主機已經全部UP了。
8.3 點擊Current Status -> Services 查看服務監控情況。
看到Nagios-Linux和Nagios-Server的服務狀態已經OK了,但是Nagios-Windows的服務狀態為CRITICAL,StatusInformation 提示Connection refused。因為Nagios-Windows上還未安裝插件,內部服務還無法查看,所以出現這種情況。將在下面具體講解。
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。