十年網(wǎng)站開(kāi)發(fā)經(jīng)驗(yàn) + 多家企業(yè)客戶 + 靠譜的建站團(tuán)隊(duì)
量身定制 + 運(yùn)營(yíng)維護(hù)+專業(yè)推廣+無(wú)憂售后,網(wǎng)站問(wèn)題一站解決
篇幅所限,看“Linux下Nagios的安裝與配置<< 二 >>”之前請(qǐng)先看“Linux下Nagios的安裝與配置<< 一 >>”
站在用戶的角度思考問(wèn)題,與客戶深入溝通,找到大廠網(wǎng)站設(shè)計(jì)與大廠網(wǎng)站推廣的解決方案,憑借多年的經(jīng)驗(yàn),讓設(shè)計(jì)與互聯(lián)網(wǎng)技術(shù)結(jié)合,創(chuàng)造個(gè)性化、用戶體驗(yàn)好的作品,建站類型包括:成都做網(wǎng)站、網(wǎng)站制作、企業(yè)官網(wǎng)、英文網(wǎng)站、手機(jī)端網(wǎng)站、網(wǎng)站推廣、國(guó)際域名空間、虛擬空間、企業(yè)郵箱。業(yè)務(wù)覆蓋大廠地區(qū)。
未完,還有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,本實(shí)驗(yàn)主要采用組進(jìn)行配置,此文檔不刪掉不使,下面配置供參考。
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文件
此文件默認(rèn)也不存在,需要手動(dòng)創(chuàng)建,services.cfg文件主要用于定義監(jiān)控的服務(wù)和主機(jī)資源,例如監(jiān)控http服務(wù)、ftp服務(wù)、主機(jī)磁盤空間、主機(jī)系統(tǒng)負(fù)載等等。Nagios-Server和Nagios-Windows相關(guān)服務(wù)已在相應(yīng)的配置文件中定義,所以這里只需要定義Nagios-Linux相關(guān)服務(wù)即可,這里只定義一個(gè)檢測(cè)是否存活的服務(wù)來(lái)驗(yàn)證配置文件的正確性,其他服務(wù)的定義將在后面講到。
define service{
use local-service ; #引用local-service服務(wù)的屬性值,local-service在templates.cfg文件中進(jìn)行了定義。
host_name Nagios-Linux ; #指定要監(jiān)控哪個(gè)主機(jī)上的服務(wù),“Nagios-Server”在hosts.cfg文件中進(jìn)行了定義。
service_description PING ; #對(duì)監(jiān)控服務(wù)內(nèi)容的描述,以供維護(hù)人員參考。
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文件,默認(rèn)不存在,需手動(dòng)創(chuàng)建!
contacts.cfg是一個(gè)定義聯(lián)系人和聯(lián)系人組的配置文件,當(dāng)監(jiān)控的主機(jī)或者服務(wù)出現(xiàn)故障,nagios會(huì)通過(guò)指定的通知方式(郵件或者短信)將信息發(fā)給這里指定的聯(lián)系人或者使用者。contactgroup.cfg定義聯(lián)系人組的配置文件,注意:members選項(xiàng)里面的聯(lián)系人在contacts.cfg里面要要定義,多個(gè)聯(lián)系從之間用逗號(hào)隔開(kāi)。
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文件
此文件只要用于定義監(jiān)控的時(shí)間段,下面是一個(gè)配置好的實(shí)例:也可以根據(jù)自己需求監(jiān)控情況進(jìn)行配置。
#下面是定義一個(gè)名為24x7的時(shí)間段,即監(jiān)控所有時(shí)間段
definetimeperiod{
timeperiod_name 24x7 #時(shí)間段的名稱,這個(gè)地方不要有空格
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
}
#下面是定義一個(gè)名為workhours的時(shí)間段,即工作時(shí)間段。
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文件
此文件用來(lái)控制相關(guān)cgi腳本,如果想在nagios的web監(jiān)控界面執(zhí)行cgi腳本,例如重啟nagios進(jìn)程、關(guān)閉nagios通知、停止nagios主機(jī)檢測(cè)等,這時(shí)就需要配置cgi.cfg文件了。
由于nagios的web監(jiān)控界面驗(yàn)證用戶為david,所以只需在cgi.cfg文件中添加此用戶的執(zhí)行權(quán)限就可以了,需要修改的配置信息如下:
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默認(rèn)的路徑為/usr/local/nagios/etc/nagios.cfg,是nagios的核心配置文件,所有的對(duì)象配置文件都必須在這個(gè)文件中進(jìn)行定義才能發(fā)揮其作用,這里只需將對(duì)象配置文件在Nagios.cfg文件中進(jìn)行引用即可。
log_file=/usr/local/nagios/var/nagios.log #定義nagios日志文件的路徑
#cfg_file=/usr/local/nagios/etc/objects/commands.cfg # “cfg_file”變量用來(lái)引用對(duì)象配置文件,如果有更多的對(duì)象配置文件,在這里依次添加即可。
#cfg_file=/usr/local/nagios/etc/objects/contacts.cfg#默認(rèn)開(kāi)啟,需注銷
#cfg_file=/usr/local/nagios/etc/objects/hosts.cfg#默認(rèn)開(kāi)啟,需注銷
#cfg_file=/usr/local/nagios/etc/objects/services.cfg #默認(rèn)開(kāi)啟,需注銷
#cfg_file=/usr/local/nagios/etc/objects/timeperiods.cfg#默認(rèn)開(kāi)啟,需注銷
#cfg_file=/usr/local/nagios/etc/objects/templates.cfg#默認(rèn)開(kāi)啟,需注銷
#cfg_file=/usr/local/nagios/etc/objects/localhost.cfg #本機(jī)配置文件,默認(rèn)開(kāi)啟,需注銷
#cfg_file=/usr/local/nagios/etc/objects/windows.cfg # windows主機(jī)配置文件默認(rèn)開(kāi)啟,需注銷
cfg_dir=/usr/local/nagios/etc/servers #servers文件默認(rèn)不存在,需手動(dòng)創(chuàng)建
#object_cache_file=/usr/local/nagios/var/objects.cache #該變量用于指定一個(gè)“所有對(duì)象配置文件”的副本文件,或者叫對(duì)象緩沖文件
precached_object_file=/usr/local/nagios/var/objects.precache
resource_file=/usr/local/nagios/etc/resource.cfg #該變量用于指定nagios資源文件的路徑,可以在nagios.cfg中定義多個(gè)資源文件。
status_file=/usr/local/nagios/var/status.dat #該變量用于定義一個(gè)狀態(tài)文件,此文件用于保存nagios的當(dāng)前狀態(tài)、注釋和宕機(jī)信息等。
status_update_interval=10 #該變量用于定義狀態(tài)文件(即status.dat)的更新時(shí)間間隔,單位是秒,最小更新間隔是1秒。
nagios_user=nagios #該變量指定了Nagios進(jìn)程使用哪個(gè)用戶運(yùn)行。
nagios_group=nagios #該變量用于指定Nagios使用哪個(gè)用戶組運(yùn)行。
check_external_commands=1 #該變量用于設(shè)置是否允許nagios在web監(jiān)控界面運(yùn)行cgi命令;
#也就是是否允許nagios在web界面下執(zhí)行重啟nagios、停止主機(jī)/服務(wù)檢查等操作;
# “1”為運(yùn)行,“0”為不允許。
command_check_interval=10s #該變量用于設(shè)置nagios對(duì)外部命令檢測(cè)的時(shí)間間隔,如果指定了一個(gè)數(shù)字加一個(gè)"s"(如10s);
#那么外部檢測(cè)命令的間隔是這個(gè)數(shù)值以秒為單位的時(shí)間間隔;
#如果沒(méi)有用"s",那么外部檢測(cè)命令的間隔是以這個(gè)數(shù)值的“時(shí)間單位”的時(shí)間間隔。
interval_length=60 #該變量指定了nagios的時(shí)間單位,默認(rèn)值是60秒,也就是1分鐘;
#即在nagios配置中所有的時(shí)間單位都是分鐘。
6.4驗(yàn)證Nagios配置文件的正確性
Nagios在驗(yàn)證配置文件方面做的非常到位,只需通過(guò)一個(gè)命令即可完成:
#/usr/local/nagios/bin/nagios -v /usr/local/nagios/etc/nagios.cfg
Nagios提供的這個(gè)驗(yàn)證功能非常有用,在錯(cuò)誤信息中通常會(huì)打印出錯(cuò)誤的配置文件以及文件中的哪一行,這使得nagios的配置變得非常容易,報(bào)警信息通常是可以忽略的,因?yàn)橐话隳切┲皇墙ㄗh性的。
看到上面這些信息就說(shuō)明沒(méi)問(wèn)題了,然后啟動(dòng)Nagios服務(wù)。
七、Nagios的啟動(dòng)與停止
7.1啟動(dòng)Nagios
a.通過(guò)初始化腳本啟動(dòng)nagios
#/etc/init.d/nagios start
or
# service nagios start
b.手工方式啟動(dòng)nagios
通過(guò)nagios命令的“-d”參數(shù)來(lái)啟動(dòng)nagios守護(hù)進(jìn)程:
#/usr/local/nagios/bin/nagios -d /usr/local/nagios/etc/nagios.cfg
7.2重啟Nagios
當(dāng)修改了配置文件讓其生效時(shí),需要重啟/重載Nagios服務(wù)。
a.通過(guò)初始化腳本來(lái)重啟nagios
#/etc/init.d/nagios reload
or
# /etc/init.d/nagios restart
or
# service nagios restart
b.通過(guò)web監(jiān)控頁(yè)重啟nagios
可以通過(guò)web監(jiān)控頁(yè)的 "ProcessInfo" -> "Restart the Nagios process"來(lái)重啟nagios
c.手工方式平滑重啟
#kill -HUP
7.3停止Nagios
a.通過(guò)初始化腳本關(guān)閉nagios服務(wù)
#/etc/init.d/nagios stop
or
# service nagios stop
b.通過(guò)web監(jiān)控頁(yè)停止nagios
可以通過(guò)web監(jiān)控頁(yè)的 "ProcessInfo" -> "Shutdown the Nagios process"來(lái)停止nagios
c.手工方式停止Nagios
#kill
八、查看初步配置情況
8.1啟動(dòng)完成之后,登錄Nagios Web監(jiān)控頁(yè)http://192.168.1.108/nagios/查看相關(guān)信息。
8.2點(diǎn)擊左面的Current Status -> Hosts可以看到所定義的三臺(tái)主機(jī)已經(jīng)全部UP了。
8.3點(diǎn)擊Current Status -> Services查看服務(wù)監(jiān)控情況。
看到Nagios-Linux和Nagios-Server的服務(wù)狀態(tài)已經(jīng)OK了,但是Nagios-Windows的服務(wù)狀態(tài)為CRITICAL,StatusInformation提示Connection refused。因?yàn)镹agios-Windows上還未安裝插件,內(nèi)部服務(wù)還無(wú)法查看,所以出現(xiàn)這種情況。將在下面具體講解。