nagios 监控平台搭建
nagios搭建配置
一、需要的軟件包
1、nagios-3.2.3.tar.gz ?nagios主監控包
tar -zxvf nagios-3.2.3.tar.gz
cd nagios-3.2.3
./configure --prefix=/usr/local/nagios
make all
make install
命令對主程序,cgi及html進行安裝
make install-init
命令在/etc/rc.d/init.d/目錄中安裝啟動腳本
make install-commandmode
命令來安裝和配置外部命令對Nagios主路徑操作的權限。(這里所謂的外部命令主要是指Apache服務通過CGI來對
Nagios的進行的操作。用戶將通過Web以執行CGI程序腳本的方式來對Nagios的檢測結果進行讀取和調用。)
make install-config
命令在/usr/local/nagios/etc/下安裝nagios配置文件示例模版
chkconfig --add nagios on 開機啟動nagios
chkconfig --level 35 nagios on
useradd nagios
創建nagios用戶
chown -R ?nagios.nagios /usr/local/nagios
權限設置
nagios-plugins-1.4.15.tar.gz 監控插件
2、tar -zxvf nagios-plugins-1.4.15.tar.gz
cd nagios-plugins-1.4.15
./configure --prefix=/usr/local/nagios
make && make install
nagios-cn-3.2.3.tar.bz2 ?漢化包
tar -jxvf nagios-cn-3.2.3.tar.bz2
cd nagios-cn-3.2.3
./configure
make && make install
3、httpd-2.2.14.tar.gz 安裝apache
tar -zxvf httpd-2.2.14.tar.gz
cd httpd-2.2.14
./configure --prefix=/usr/local/apache
make
make install
4、php-5.4.0.tar.gz 安裝php
tar -zxvf php-5.4.0.tar.gz
cd php-5.4.0
./configure --prefix=/usr/local/php --with-apxs2=/usr/local/aache/bin/apxs(apache可以調用php)
make
make install
二、修改apache配置文件
1、修改:
User ?nagios
Group nagios
修改為:
DirectoryIndex index.html index.php
增加:
AddType application/x-httpd-php .php
設置apache授權訪問在配置文件最后添加:
#setting for nagios
ScriptAlias /nagios/cgi-bin "/usr/local/nagios/sbin"
<Directory "/usr/local/nagios/sbin">
AuthType ? Basic
Options ? ?ExecCGI
AllowOverride AuthConfig
Order allow,deny
Allow from all
AuthName "Welcome to user nagios"
AuthUserFile ?/usr/local/apache/conf/htpasswd
Require ?valid-user
</Directory>
Alias /nagios ?"/usr/local/nagios/share"
<Directory ?"/usr/local/nagios/share">
AuthType ? Basic
Options ? ?ExecCGI
AllowOverride AuthConfig
Order allow,deny
Allow from all
AuthName "Welcome to user nagios"
AuthUserFile ?/usr/local/apache/conf/htpasswd
Require ?valid-user
</Directory>
2、創建驗證文件
/usr/local/apache/bin/htasswd -c /usr/local/apache/conf/htpasswd nagiosadmin
注意-c 為創建文件用,nagiosadmin為所創建的用戶,下次使用該命令創建用戶,不能用-c這樣會覆蓋之前的用戶
chown -R nagios.nagios /usr/local/apache
權限設置,不然驗證不通過
啟動nagios:
service nagios start
啟動apache
/usr/local/apache/bin/httpd -k start
瀏覽器訪問 http://ip/nagios/ 要求輸入用戶名密碼,顯示nagios監控界面恭喜你基本安裝成功
三、nagios 配置文件分析
1、/usr/local/nagios/nagios/etc/nagios.cfg ?nagios的主配置文件
cfg_file=/usr/local/nagios/etc/objects/commands.cfg ? ? ? 定義nagios命令的配置文件
cfg_file=/usr/local/nagios/etc/objects/contacts.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/windows.cfg ? ? ? ?定義windons主機的模版配置文件
cfg_dir=/usr/local/nagios/etc/nagiosconf在主配置文件中可以通過這條命令指定,nagios配置文件目錄nagiosconf這
個目錄可以按類型嵌套目錄,nagios執行中尋找這個目錄中后綴為.cfg的所有文件
2、cgi.cfg 控制CGI訪問的配置文件
use_authentication=1
設定啟用身份驗證。
authorized_for_system_information=nagiosadmin
設定系統信息的授權用戶。
authorized_for_configuration_information=nagiosadmin
設定配置信息的授權用戶。
authorized_for_system_commands=nagiosadmin
設定系統命令的授權用戶。
authorized_for_all_services=nagiosadmin
設定全部監測服務的授權用戶。
authorized_for_all_hosts=nagiosadmin
設定全部被監測主機的授權用戶。
authorized_for_all_service_commands=nagiosadmin
設定全部監測服務命令的授權用戶。
authorized_for_all_host_commands=nagiosadmin
設定全部主機命令的授權用戶。
3、resource.cfg ?資源文件,主要是定義命令所在地,以便其他配置文件引用
3、object目錄,存放很多模版配置文件,被其他配置文件引用
nagios中的配置文件是這樣一個邏輯應用結構:
當一個任務被觸發要有這樣的工作,任務可以是主機或服務,這個任務什么時候被檢查(需要timeperiods.cfg模版定義),
檢查這個任務的命令是什么(需要commands.cfg模版定義),報警什么時候發送(由timeperiods.cfg模版定義),聯系人是
誰(由contact.cfg模版定義)
templates.cfg 文件分析
define contact{
? ? ? ?name ? ? ? ? ? ? ? ? ? ? ? ? ? ?generic-contact ? ? ? name用來定義一個類名,應為這是個difine contact段所以這是個聯系人類名,可被contact引用引用 ? ?
? ? ? ?service_notification_period ? ? 24x7 ? ? ? ? ? ? ? ? ?服務故障通知時間
? ? ? ?host_notification_period ? ? ? ?24x7 ? ? ? ? ? ? ? ? ?主句故障通知時間
? ? ? ?service_notification_options ? ?w,u,c,r,f,s ? ? ? ? ? 服務報警觸發參數
? ? ? ?host_notification_options ? ? ? d,u,r,f,s ? ? ? ? ? ? 主機報警觸發參數
? ? ? ?service_notification_commands ? notify-service-by-email ? 服務報警通知方式
? ? ? ?host_notification_commands ? ? ?notify-host-by-email ? ? ?主機報警通知方式
? ? ? ?register ? ? ? ? ? ? ? ? ? ? ? ?0 ? ? ? ? ? ? ? ? ? ? ?用來說明它是個類
? ? ? ?}
看這個配置文件“24x7”這是一個時間段名,是在timeeriods.cfg 定義。 ?notify-host-by-email命令是在commadns.cfg定義,涉及到了模版配置文件的引用
define host{
? ? ? ?name ? ? ? ? ? ? ? ? ? ? ? ? ? ?generic-host ? ? ? ? 命名一個主機類 generic-host可被應用
? ? ? ?notifications_enabled ? ? ? ? ? 1 ? ? ? ? ? ? ? ? ? ?設定啟用主機事件通知
? ? ? ?event_handler_enabled ? ? ? ? ? 1 ? ? ? ? ? ? ? ? ? ?設定啟用主機事件處理程
flap_detection_enabled ? ? ? ? ?1 ? ? ? ? ? ? ? ? ? ?設定啟用狀態抖動監測
? ? ? ?failure_prediction_enabled ? ? ?1 ? ? ? ? ? ? ? ? ? ?設定啟用故障預測
? ? ? ?process_perf_data ? ? ? ? ? ? ? 1 ? ? ? ? ? ? ? ? ? ?設定啟用進程性能數據記錄
? ? ? ?retain_status_information ? ? ? 1 ? ? ? ? ? ? ? ? ? ?設定啟用狀態信息保存功能
? ? ? ?retain_nonstatus_information ? ?1 ? ? ? ? ? ? ? ? ? ?設定啟用非狀態信息保存功能
? ? ? ?notification_period ? ? ? ? ? ? 24x7 ? ? ? ? ? ? ? ? 設定事件通知的工作時間段
? ? ? ?register ? ? ? ? ? ? ? ? ? ? ? ?0 ? ? ? ? ? ? ?
? ? ? ?}
define host{
? ? ? ?name ? ? ? ? ? ? ? ? ? ? ? ? ? ?linux-server ? ? ? ? 定義了一個linux主機類
? ? ? ?use ? ? ? ? ? ? ? ? ? ? ? ? ? ? generic-host ? ? ? ? 通過use引用之前定義過的類
? ? ? ?check_period ? ? ? ? ? ? ? ? ? ?24x7 ? ? ? ? ? ? ? ? 服務的檢查時間
? ? ? ?check_interval ? ? ? ? ? ? ? ? ?5 ? ? ? ? ? ? ? ? ? ?檢查間隔5分
? ? ? ?retry_interval ? ? ? ? ? ? ? ? ?1 ? ? ? ? ? ? ? ? ? ?主機失敗檢查重試時間間隔
? ? ? ?max_check_attempts ? ? ? ? ? ? ?10 ? ? ? ? ? ? ? ? ? 主機失敗最大重檢次數
? ? ? ?check_command ? ? ? ? ? ? ? ? ? check-host-alive ? ? 檢查命令在commands.cfg中定義
? ? ? ?notification_period ? ? ? ? ? ? workhours ? ? ? ? ? ?發送報警的時間
? ? ? ?notification_interval ? ? ? ? ? 120 ? ? ? ? ? ? ? ? ?主機未回復重發報警的時間間隔120分
? ? ? ?notification_options ? ? ? ? ? ?d,u,r ? ? ? ? ? ? ? ?觸發參數
? ? ? ?contact_groups ? ? ? ? ? ? ? ? ?admins ? ? ? ? ? ? ? 聯系人
? ? ? ?register ? ? ? ? ? ? ? ? ? ? ? ?0 ? ? ? ? ? ? ? ? ? ?類
? ? ? ?}
define service{
? ? ? ?name ? ? ? ? ? ? ? ? ? ? ? ? ? ?generic-service ? ? ? ?
? ? ? ?active_checks_enabled ? ? ? ? ? 1 ? ? ? ? ? ? ? ? ? ? ?
? ? ? ?passive_checks_enabled ? ? ? ? ?1 ? ? ? ? ? ? ? ? ? ? ?
? ? ? ?parallelize_check ? ? ? ? ? ? ? 1 ? ? ? ? ? ? ? ? ? ? ?
? ? ? ?obsess_over_service ? ? ? ? ? ? 1 ? ? ? ? ? ? ? ? ? ? ?
? ? ? ?check_freshness ? ? ? ? ? ? ? ? 0 ? ? ? ? ? ? ? ? ? ? ?
? ? ? ?notifications_enabled ? ? ? ? ? 1 ? ? ? ? ? ? ? ? ? ? ?
? ? ? ?event_handler_enabled ? ? ? ? ? 1 ? ? ? ? ? ? ? ? ? ? ?
? ? ? ?flap_detection_enabled ? ? ? ? ?1 ? ? ? ? ? ? ? ? ? ? ?
? ? ? ?failure_prediction_enabled ? ? ?1 ? ? ? ? ? ? ? ? ? ? ?
? ? ? ?process_perf_data ? ? ? ? ? ? ? 1 ? ? ? ? ? ? ? ? ? ? ?
? ? ? ?retain_status_information ? ? ? 1 ? ? ? ? ? ? ? ? ? ? ?
? ? ? ?retain_nonstatus_information ? ?1 ? ? ? ? ? ? ? ? ? ? ?
? ? ? ?is_volatile ? ? ? ? ? ? ? ? ? ? 0 ? ? ? ? ? ? ? ? ? ? ?
? ? ? ?check_period ? ? ? ? ? ? ? ? ? ?24x7 ? ? ? ? ? ? ? ? ? ?
? ? ? ?max_check_attempts ? ? ? ? ? ? ?3 ? ? ? ? ? ? ? ? ? ? ?
? ? ? ?normal_check_interval ? ? ? ? ? 10 ? ? ? ? ? ? ? ? ? ?
? ? ? ?retry_check_interval ? ? ? ? ? ?2 ? ? ? ? ? ? ? ? ? ? ?
? ? ? ?contact_groups ? ? ? ? ? ? ? ? ?admins ? ? ? ? ? ? ? ? ?
? ? ? ?notification_options ? ? ? ? ? ?w,u,c,r ? ? ? ? ? ? ? ?
? ? ? ?notification_interval ? ? ? ? ? 60 ? ? ? ? ? ? ? ? ? ?
? ? ? ?notification_period ? ? ? ? ? ? 24x7 ? ? ? ? ? ? ? ? ? ?
? ? ? ? register ? ? ? ? ? ? ? ? ? ? ? ?0 ? ? ? ? ? ? ? ? ? ?
? ? ? ?}
此為定義一個服務基本類,就是說基本上服務檢查需要指定的內容,若有特殊需要可以在此基礎上引用重定義,如下工作,一個監測工
作就是一個服務。服務定義段就是要定義Nagios將進行什么樣的監測工作的配置段。而且服務當中也能夠定義服務的類,并且類也能繼承類,
運用比較靈活。
define service{
? ? ? ?name ? ? ? ? ? ? ? ? ? ? ? ? ? ?local-service ? ? ? ? ?
? ? ? ?use ? ? ? ? ? ? ? ? ? ? ? ? ? ? generic-service ? ? ? 通過use引用generic-service類 ?
? ? ? ?max_check_attempts ? ? ? ? ? ? ?4 ? ? ? ? ? ? ? ? ? ? ?
? ? ? ?normal_check_interval ? ? ? ? ? 5 ? ? ? ? ? ? ? ? ? ?重定義后覆蓋generic-service 中的定義 ? ? ? ? ? ? ? ?
? ? ? ?retry_check_interval ? ? ? ? ? ?1 ? ? ? ? ? ? ? ? ? ? ?
? ? ? ?register ? ? ? ? ? ? ? ? ? ? ? ?0 ? ? ? ? ? ? ? ? ? ? ?
? ? ? ?}
這里的服務并不是指被監測主機上運行的服務,而是指Nagios所進行的“各種不同功能的監測
contact.cfg文件分析
define contact{
? ? ? ?contact_name ? ? ? ? ? ? ? ? ? ?nagiosadmin ? ? ? ? ? 定義一個聯系人 ? ? ? ?
? ? ? ?use ? ? ? ? ? ? ? ? ? ? ? ? ? ? generic-contact ? ? ? 引用templates.cfg模版中的generic-contact類 ?
? ? ? ?alias ? ? ? ? ? ? ? ? ? ? ? ? ? Nagios Admin ? ? ? ? ?聯系人描述
? ? ? ?email ? ? ? ? ? ? ? ? ? ? ? ? ? nagios@localhost ? ? ?聯系人郵件地址
? ? ? ?}
define contactgroup{
? ? ? ?contactgroup_name ? ? ? admins ? ? ? ? ? ? ? ? ? ? ? ?定義一個聯系人組
? ? ? ?alias ? ? ? ? ? ? ? ? ? Nagios Administrators ? ? ? ? 描述
? ? ? ?members ? ? ? ? ? ? ? ? nagiosadmin ? ? ? ? ? ? ? ? ? 組成員,組成員必須是define contact{}段定義的,成員之間用逗號隔開
? ? ? }
timeperiods.cfg 文件分析
define timeperiod{
? ? ? ?timeperiod_name 24x7 ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?timeperiod_name 定義時間段名,這個時間段名可被引用
? ? ? ?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' timeperiod definition
define timeperiod{
? ? ? ?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
? ? ? ?}
commamnds.cfg 文件分析
define command{
? ? ? ?command_name ? ?notify-host-by-email
? ? ? ?command_line ? ?/usr/bin/printf "%b" "***** Nagios *****\n\nNotificationType: $NOTIFICATIONTYPE$\nHost:
$HOSTNAME$\nState: $HOSTSTATE$\nAddress: $HOSTADDRESS$\nInfo: $HOSTOUTPUT$\n\nDate/Time:
$LONGDATETIME$\n" | /bin/mail -s "** $NOTIFICATIONTYPE$ Host Alert: $HOSTNAME$ is $HOSTSTATE$ **" $CONTACTEMAIL$
? ? ? ?}
define command{
? ? ? ?command_name ? ?check-host-alive
? ? ? ?command_line ? ?$USER1$/check_ping -H $HOSTADDRESS$ -w 3000.0,80% -c 5000.0,100% -p 5
? ? ? ?}
通過define關鍵字command來表示一個命令定義段,定義內用在兩個大括號內,并且一個屬性必須占一行。如果要定義多個命令的話要寫多個
define command {}段
commnad_name ?來命令一個命令名,在服務的定義當中,只能引用在commands.cfg中定義過的命令名
通過command_line來定義該命令的所使用到的插件程序以及參數配置格式,也可使用系統程序或自己安裝的程序,
如notify-host-by-email
命令的定義它通過command_line 調用了/usr/bin/printf;、/bin/mial 命令,后面是它們的參數
如check-host-alive
命令的定義它通過command_line 調用了$USER1$下的check_ping 命令,而$USER1$其實是個目錄在resource.cfg配置文件中定義了nagios的
資源在哪里,
resource.cfg配置文件:
$USER1$=/usr/local/nagios/libexec
localhost.cfg 具體應用實例配置分析
define host{
? ? ? ?use ? ? ? ? ? ? ? ? ? ? linux-server ? ? ? ?引用linux-server類 ? ? ? ? ?
? ? ? ?host_name ? ? ? ? ? ? ? localhost ? ? ? ? ? 被監控主機名,報警時會提示,根據業務定
? ? ? ?alias ? ? ? ? ? ? ? ? ? localhost ? ? ? ? ? 描述主機應用
? ? ? ?address ? ? ? ? ? ? ? ? 127.0.0.1 ? ? ? ? ? 主機ip
? ? ? ?}
define hostgroup{
? ? ? ?hostgroup_name ?linux-servers
? ? ? ?alias ? ? ? ? ? Linux Servers
? ? ? ?members ? ? ? ? localhost ? ?
? ? ? ?}
define service{
? ? ? ?use ? ? ? ? ? ? ? ? ? ? ? ? ? ? local-service ? ? 引用locak-server類 ? ?
? ? ? ?host_name ? ? ? ? ? ? ? ? ? ? ? localhost ? ? ? ?
? ? ? ?service_description ? ? ? ? ? ? Root Partition
? ? ? ?check_command ? ? ? ? ? ? ? ? ? check_local_disk!20%!10%!/
? ? ? ?}
重點說check_command ? ? ?check_local_disk!20%!10%!/
在命令配置文件中check_local_disk命令的command_line行是這樣
command_line ? $USER1$/check_disk -w $ARG1$ -c $ARG2$ -p $ARG3$
對比之下我們知道check_local_disk 就相當于$USER1$/check_disk -w ?-c ?-p 那么
20%這個參數就是$ARG1$的實際值
10%這個參數就是$ARG2$的實際值
/這個參數就是$ARG3$的實際值
多個參數之間用!!隔開
至于check_disk 這個命令的用法可以執行check_disk --help 來查看,其他命令也一樣,舉例來說:
check_local_disk!20%!10%!/
-p選項指定要監控的分區為/
-w選項觸發warn報警當/空間小于20%
-c選項觸發crit報警當/空間小于10%
配置文件終于說完了,不知是否啰嗦了,我覺得啰嗦點沒關系如果大家能更好的理解,說的不對的地方大家指正這里先謝謝了!
四、nrpe包的安裝
1、nagios監控主程序可以理解為一個監控平臺,Nagios監測服務只能是本地系統監測以及對遠程主機的連通性監測。為了使Nagios
的監測服務器能夠遠程對被監測主機系統上的信息進行獲取,比如遠程系統上的進程數、磁盤空間使用狀況、所運行的服務等等
這些必須要登錄遠程主機系統上才能了解的信息的話,就必須要依靠NRPE這個核心擴展插件程序,NRPE作為中間的代理程序,扮
演著一手接受著Nagios監測服務器發來的請求,另一手在遠程主機系統上獲取指定的信息的中間人角色。
如以上提到的實現Nagios對遠程系統的監測,那么首先必須要在被監測的遠程主機上除了安裝Nagios-plugins插件程序之外還
必須安裝 NRPE核心擴展插件程序,并將NRPE在被監測的遠程主機系統上以守護進程的方式運行起來,開放指定的NRPE監聽端口
監聽著Nagios監測服務器發送過來的所有監測請求。另外,在Nagios監測服務器上,在必須要安裝Nagios-plugins插件程序和
NRPE核心擴展插件程序。唯一不同的是,Nagios監測服務器不需要將NRPE作為守護進程運行著,因為它本身一般不需要被別人
監測著而是去監測別人,對Nagios監測服務器而言,它只需要使用到Nagio-plugins插件程序和NRPE擴展插件程序就足夠了。
在被監控主機上安裝Nagios-plugins插件程序
首先要添加用戶useradd nagios 這是必須的
tar -zxvf nagios-plugins-1.4.15.tar.gz
cd nagios-plugins-1.4.15
./configure --prefix=/usr/local/nagios
make && make install
chown -R nagios.nagios /usr/local/nagios
安裝nrpe包
tar -zxvf nrpe-2.13.tar.gz
cd nrpe-2.13
./configure
make all
make install-plugin ?安裝nrpe插件程序
make isntall-daemon ?安裝nrpe守護程序
make install-daemon-config ?安裝nrpe守護進程配置文件
2、vi /usr/local/nagios/etc/nrpe.cfg
pid_file=/var/run/nrpe.pid
設定NRPE運行的PID文件,這個默認就設定好了,不用更改。
server_port=5666
這個是NRPE守護進程占用的系統端口。
#server_address=0.0.0.0
server_address=192.168.1.9
設定系統監聽NRPE的網絡接口。一般設定具體的IP地址,如果要多個網卡的話就用逗號“,”來分隔多個IP地址。如果要監聽系統
全部的網絡接口的話,可以使用“0.0.0.0”這個表示全部網絡接口的特殊地址,但是不可以用通配符“*”。
nrpe_user=nagios
nrpe_group=nagios
設定NRPE的宿主用戶。
allowed_hosts=192.168.1.12
這里是設定允許與本機進行NRPE交互的主機的IP地址,也就是Nagios監測服務器的地址,如果要指定多個Nagios服務器的話,那么用逗號“,”來分隔多個IP地址。
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
以上是NRPE默認就有的擴展插件命令,這是至關重要的地方了。當被監測主機運行NRPE守護進程的時候,同時就運行起來了這些在NRPE配置文件中定義的NRPE
擴展命令了。可以看出這些擴展命令都已經指定了好了命令名、插件路徑以及選項參數了。格式如下:
? ? ? ? ?command[NRPE命令名]=插件路徑/插件 選項1 參數1 選項2 參數2 ...
注意,如果要指定該被監測主機需要被監控的服務的話,就必須在這里都配置好NRPE命令并運行在NRPE守護進程里。這里沒有配置的NRPE命令則Nagios監測服
務器是無法監控到的。
3、了解NRPE的執行程序的使用方式
/usr/local/nagios/bin/nrpe -h
-------------------------------------------------------
Usage: nrpe [-n] -c <config_file> <mode>
運行方法:nrpe -c NRPE配置文件路徑 運行模式
Options:
可用選項
? ?-n ? ? ? ? ? = Do not use SSL
? ?不使用SSL方式,一般都使用SSL。
<config_file> = Name of config file to use
? ?指定NRPE配置文件路徑,這個NRPE主配置文件就是nrpe.cfg。
<mode> ? ? ? ? = One of the following two operating modes:
? ?指定NRPE的運行方式,一共有2種可用的NRPE運行方式:
? ?-i ? ? ? ? ? = ? ? Run as a service under inetd or xinetd
? ?以超級守護進程inetd或xinetd方式運行NRPE,要通過這種方式運行的話還要安裝和配置xinetd,一般不用。
? ?-d ? ? ? ? ? = ? ? Run as a standalone daemon
? ?以獨立守護進程方式運行NRPE,一般常用這種運行方式。
4、運行NRPE守護進程
/usr/local/nagios/bin/nrpe -c /usr/local/nagios/etc/nrpe.cfg -d
-c 指定配置文件
-d 以獨立守護進程方式運行nrpe
檢查是否啟動
ps aux | grep nrpe
netstat -anlp | grep nrpe
5、將NRPE運行添加加入系統啟動腳本中
echo "/usr/local/nagios/bin/nrpe -c /usr/local/nagios/etc/nrpe.cfg -d" >> /etc/rc.d/rc.local
到此,被監控主機要做的工作已經完成了。
五、在監控主機上安裝nrpe程序
tar -zxvf nrpe-2.13.tar.gz
cd nrpe-2.13
./configure
make all
安裝NRPE擴展插件程序
make install-plugin
Nagios只要擁有NRPE的擴展插件功能就可以了,所以在Nagios監測服務器安裝NRPE工作到這步就可以了。接下來要
做的就是在Nagios監測服務器上配置NRPE的擴展插件命令,使得Nagios監測服務器能夠定義nrpe命令,從而可以定
義出“遠程系統監測服務”。
在監控主機上配置NRPE的服務
1.查看新擴展插件check_nrpe的使用方法
/usr/local/nagios/libexec/check_nrpe -h|less
-------------------------------------------------------
Usage: check_nrpe -H <host> [-n] [-u] [-p <port>] [-t <timeout>] [-c <command>]
使用方式:check_nrpe -H 主機名 -p NRPE端口 -c NRPE命令名
Options:
選項:
<host> ? ? ?= The address of the host running the NRPE daemon
主機,運行著NRPE守護進程的遠程被監測主機名,并且該主機名必須在host里定義過。
[port] ? ? ?= The port on which the daemon is running (default=5666)
端口,被監測的遠程主機上運行NRPE的端口,默認是5666,如果是默認就不用指定。
[command] ? = The name of the command that the remote daemon should run
命令,這些命令名必須是被監測主機上NRPE守護進程運行著的。
-------------------------------------------------------
2.在command.cfg命令定義文件中添加NRPE命令。
vi /usr/local/nagios/etc/commands.cfg
-------------------------------------------------------
# NRPE Command
添加NRPE功能命令。
define command(
? command_name ? nrpe
? command_line ? $USER1$/check_nrpe -H $HOSTADDRESS$ -c $ARG1$
? }
-------------------------------------------------------
這里要說明幾點:
(1)這里定義的命令名就叫作nrpe。
(2)$USER1$/check_nrpe會通過引用resource.cfg獲得/usr/local/nagios/libexec/check_nrpe這個絕對路徑。
(3)-H $HOSTADDRESS$ 用來獲得指定被監測主機的IP地址,$HOSTADDRESS$變量會通過定義主機名查找到host段中的IP地址。
(4)-c $ARG1$ 用來指定被監測主機上NRPE守護進程運行著的NRPE命令名。
3.添加NRPE監控服務
-------------------------------------------------------
#Define Remote Current Users
定義監測遠程系統當前登錄用戶數量服務。
define service{
? ? ? ? use ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?remote-service
? ? ? ? host_name ? ? ? ? ? ? ? ? ? ? ? ?KCentOS5A
? ? ? ? service_description ? ? ? ? ? ? ?Current Users On Remote System
? ? ? ? check_command ? ? ? ? ? ? ? ? ? ?nrpe!check_users
? ? ? ? }
#Define Remote System Loads
定義監測遠程系統當前負載服務。
define service{
? ? ? ? use ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?remote-service
? ? ? ? host_name ? ? ? ? ? ? ? ? ? ? ? ?KCentOS5A
? ? ? ? service_description ? ? ? ? ? ? ?Current System Loads
? ? ? ? check_command ? ? ? ? ? ? ? ? ? ?nrpe!check_load
? ? ? ? }
#Define Remote Zombie Processes
定義監測遠程系統僵尸進程數服務。
define service{
? ? ? ? use ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?remote-service
? ? ? ? host_name ? ? ? ? ? ? ? ? ? ? ? ?KCentOS5A
? ? ? ? service_description ? ? ? ? ? ? ?Zombie Processes On Remote System
? ? ? ? check_command ? ? ? ? ? ? ? ? ? ?nrpe!check_zombie_procs
? ? ? ? }
#Define Remote System Total Processes
定義監測遠程系統進程總數服務。
define service{
? ? ? ? use ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?remote-service
? ? ? ? host_name ? ? ? ? ? ? ? ? ? ? ? ?KCentOS5A
? ? ? ? service_description ? ? ? ? ? ? ?Total Processes On Remote System
? ? ? ? check_command ? ? ? ? ? ? ? ? ? ?nrpe!check_total_procs
? ? ? ? }
-------------------------------------------------------
再強調一遍:
(1)這里check_command后面的nrpe是commands.cfg里定義的命令名。
(2)而nrpe命令名后用感嘆號“!”分隔的是NRPE命令名,這些NRPE命令是在遠端被監測主機上nrpe.cfg中command后方括號“[]”
中定義的命令名,并且被遠程NRPE守護進程運行著。
3.在修改配置并確認完畢后,重新啟動Nagios主程序。
service nagios restart
Running configuration check...done
Stopping nagios: done.
Starting nagios: done.
這里特別提醒一點: 如果配置文件當中不小心出現偏差的話,那么Nagios程序將啟動失敗,而且通過service nagios restart
這種方式的話,還可能不會出現配置文件出錯的任何提示信息,即使在日志中也不會記錄,就是單單地告訴你不能啟動,這樣
會給排查錯誤帶來巨大的困擾。如果發生了配置文件錯誤而導致Nagios啟動失敗的話,那么請不要使用service nagios restart/start
這樣的方式啟動。而是使用命令的方式啟動它:/usr/local/nagios/bin/nagios -d /usr/local/nagios/etc/nagios.cfg 這個
命令來啟動Nagios主程序,這樣的方式下,如果Nagios配置錯誤的話,它還會提示出錯信息,以方便于故障的排查。
六、定制個性化的NRPE遠程監控服務:
默認情況下被檢測主機上的NRPE配置文件nrpe.cfg中只給出了5條NRPE命令名,如下
-------------------------------------------------------
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
-------------------------------------------------------
其實在方括號“[]”里面寫的才是真正的NRPE命令名,而其后的部分則是指定了所使用到的插件路徑以及相應的選項和參數,也就
是說,命令名和其使用到的插件以及相應的選項和參數是在被監測端定義的,而Nagios主監測服務器上只要把被監控端上定義的
NRPE命令名拿過來再加入到服務定義中就可以了。
比如我要增加兩個遠程監測服務,分別是:
(1)監測遠程主機上的sda1磁盤空間使用狀況。
(2)監測遠程主機上的swap交換分區使用狀況。
1.首先要找到相應的插件,以及使用方法。
根據要求,找到check_disk和check_swap這兩個插件。
首先看看check_disk的用法。
/usr/local/nagios/libexec/check_disk -h|less
-------------------------------------------------------
Usage: check_disk -w limit -c limit {-p path | -x device}
格式:check_disk -w 低限% -c 低限% -p 磁盤設備文件或則分區文件的絕對路徑
Options:
選項
-w, --warning=PERCENT%
? ? 設定告警通知百分比數,空間低于該百分比則發出告警通知。
-c, --critical=PERCENT%
? ? 設定嚴重告警通知百分比數,空間低于該百分比則發出嚴重告警通知。
-p, --path=PATH, --partition=PARTITION
? ? 指定磁盤設備文件或則分區文件的絕對路徑。
-------------------------------------------------------
再來看看check_swap的用法。
/usr/local/nagios/libexec/check_swap -h|less
-------------------------------------------------------
Usage:check_swap [-av] -w <percent_free>% -c <percent_free>%
? ? ? ? ?check_swap -w 剩余空間% -c 剩余空間%
? ? ? ? ?check_swap [-av] -w <bytes_free> -c <bytes_free>
? ? ? ? ?check_swap -w 剩余字節數 -c 剩余字節數
Options:
選項
-w, --warning=INTEGER
? ? 設定告警通知剩余字節數,剩余字節低于該值則發出告警通知,只可整數。
-w, --warning=PERCENT%%
? ? 設定告警通知百分比數,剩余空間低于該百分比則發出告警通知,雙百分號。
-c, --critical=INTEGER
? ? 設定嚴重告警通知剩余字節數,剩余字節低于該值則發出嚴重告警通知,只可整數。
-c, --critical=PERCENT%%
? ? 設定嚴重告警通知百分比數,剩余空間低于該百分比則發出嚴重告警通知,雙百分號。
-------------------------------------------------------
2、在被監控主機上編輯nrpe.cfg配置文件:
vi /usr/local/nagios/etc/nrpe.cfg
-------------------------------------------------------
# Added NRPE Commands
下面兩條是自己添加的NRPE命令名。
command[disk_observer]=/usr/local/nagios/libexec/check_disk -w 20% -c 10% -p /dev/sda
設定監測/dev/sda塊設備文件,空間小于20%產生告警,小于10%產生嚴重告警。
command[swap_observer]=/usr/local/nagios/libexec/check_swap -w 45%% -c 25%%
設定監測Swap交換分區,占用率大于55%則產生告警,大于75%則產生嚴重告警。
-------------------------------------------------------
3、重新啟動被監控主機的NRPE守護進程:
killall nrpe
/usr/local/nagios/bin/nrpe -c /usr/local/nagios/etc/nrpe.cfg -d
4、檢查被監測主機的NRPE守護進程:
ps aux | grep nrpe
nagios ? ?27504 ? 0.0 ? 0.3 ? 4396 ? 868 ? ? ? Ss 03:35 0:00 /usr/local/nagios/bin/nrpe -c /usr/local/nagios/etc/nrpe.cfg -d
5、在Nagios監控服務器上首先確認NRPE的命令定義:
vi /usr/local/nagios/etc/commands.cfg
-------------------------------------------------------
define command{
? ? ? ? command_name nrpe
? ? ? ? command_line $USER1$/check_nrpe -H $HOSTADDRESS$ -c $ARG1$
? ? ? ? }
-------------------------------------------------------
6.在Nagios監控服務器上然后按照NRPE命令定義來添加NRPE遠程監控服務:
vi /usr/local/nagios/etc/localhost.cfg
-------------------------------------------------------
#Define Remote System Disk Using
定義監測遠程系統磁盤使用狀況。
define service{
? ? ? ? use ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?remote-service
? ? ? ? host_name ? ? ? ? ? ? ? ? ? ? ? ?KCentOS5A
? ? ? ? service_description ? ? ? ? ? ? ?Disk Using Of Remote System
? ? ? ? check_command ? ? ? ? ? ? ? ? ? ?nrpe!disk_observer
? ? ? ? }
#Define Remote System Swap Using
定義監測遠程系統交換分區使用狀況。
define service{
? ? ? ? use ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?remote-service
? ? ? ? host_name ? ? ? ? ? ? ? ? ? ? ? ?KCentOS5A
? ? ? ? service_description ? ? ? ? ? ? ?Swap Using Of Remote System
? ? ? ? check_command ? ? ? ? ? ? ? ? ? ?nrpe!swap_observer
? ? ? ? }
-------------------------------------------------------
7、重新啟動Nagios監測服務器的監測程序:
service nagios restart
Running configuration check...done
Stopping nagios: .done.
Starting nagios: done.
8、查看Naigos服務運行狀況:
ps aux | grep nagios
nagios ? ? 7367 ? 0.0 ? 0.3 ? 22888 ? ?904 ? ? ? ? ? Ssl ? 21:24 ? ?0:00 /usr/local/nagios/bin/nagios -d
/usr/local/nagios/etc/nagios.cfg
root ? ? ? 7376 ? 0.0 ? 0.2 ? ?3884 ? ?660 pts/2 ? ? R+ ? ?21:25 ? ?0:00 grep nagios
轉載于:https://blog.51cto.com/buguoruci/1354812
總結
以上是生活随笔為你收集整理的nagios 监控平台搭建的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: C# 各版本更新简介
- 下一篇: 提高PHP代码质量需要注意的地方三