天天看點

手把手打造開源新監控利器check_mk

Check_MK是一款通用的Nagios/Icinga資料采集插件,主要由三個子項目組成。它采用了新的方法從作業系統和網絡元件中收集資料和淘汰老式的NRPE、check_by_ssh、NSClient和check_snmp。其優點能夠自動檢測主機上的監控項目,并且在監控端的CPU使用率也顯著減少。其遵循CPL v2,是開源的免費軟體。

工作原理,摘自其官網上的一幅原理圖像

<a href="http://blog.51cto.com/attachment/201209/145154917.png" target="_blank"></a>

1、Nagios每一次對所監控的主機每進行一次檢查的時間間隔觸發一個活動檢查。這種主動檢查将會觸發check_mk插件。

2、check_mk通過TCP連接配接到目标主機。目标主機上的check_mk_agent檢索有關該主機的所有相關資料, 并将其以ASCII文本形式傳回給伺服器端(這樣的模式類似于zabbix哈~)。

3、check_mk提取performanca資料直接傳回給rrd。

4、check_mk提取相關資料,比較所設定的warning/critical 閥值,然後傳回這台主機通過Nagios的被動的服務檢查的檢查結果。

由于check_mk的圖比較炫而且其監控主機也比較友善,類似cacti一樣可以直接在web頁面進行添加,個人覺得很不錯,先上一些我配置完成的check_mk監控的圖。

<a href="http://blog.51cto.com/attachment/201209/161947151.jpg" target="_blank"></a>

<a href="http://blog.51cto.com/attachment/201209/162742998.jpg" target="_blank"></a>

<a href="http://blog.51cto.com/attachment/201209/160350846.jpg" target="_blank"></a>

由于check_mk是nagios的一個插件,是以必須先配置安裝好nagios,關于nagios的安裝與配置在51cto上已經有很多大神給出各種平台的詳細配置文檔,需要的朋友可以讀一下,這裡我就不多介紹nagios.

以下安裝配置check_mk,由于網上關于check_mk的資料比較少,筆者隻好照着官方文檔學習了下,無奈筆者連CECT-4都沒過,想必各位也能估摸出筆者的英語水準,以下内容若有了解偏差或者出入很大,還請各位諒解。

開篇:

環境配置:

安裝pnp4nagios

2.pnp4nagios是一個輸出插件,用來建立和顯示由nagios收集的資料,rrdtool繪畫出來的圖像是以還需要安裝rrdtool-perl php-gd rrdtool rrdtool-php rrdtool-devel包。

#yum localinstall -y --nogpgcheck rrdtool-*

#yum install php-gd

#tar xzf pnp4nagios-0.6.2.tar.gz

#cd pnp4nagios-0.6.2

#./configure --prefix=/usr/local/pnp4nagios --with-nagios-user=nagios --with-nagios-group=nagios --with-httpd-conf=/etc/httpd/conf.d

#make all

#make fullinstall(也可根據提示一步一步執行make install, install-webconf, install-init, and install-config.)

3.編輯nagios.cfg開啟以下幾項:

process_performance_data=1

host_perfdata_command=process-host-perfdata

service_perfdata_command=process-service-perfdata

host_perfdata_file=/tmp/host-perfdata

service_perfdata_file=/tmp/service-perfdata

host_perfdata_file_template=[HOSTPERFDATA]\t$TIMET$\t$HOSTNAME$\t$HOSTEXECUTIONTIME$\t$HOSTOUTPUT$\t$HOSTPERFDATA$

service_perfdata_file_template=[SERVICEPERFDATA]\t$TIMET$\t$HOSTNAME$\t$SERVICEDESC$\t$SERVICEEXECUTIONTIME$\t$SERVICELATENCY$\t$SERVICEOUTPUT$\t$SERVICEPERFDATA$

host_perfdata_file_mode=a

service_perfdata_file_mode=a

host_perfdata_file_processing_interval=0

service_perfdata_file_processing_interval=0

host_perfdata_file_processing_command=process-host-perfdata-file

service_perfdata_file_processing_command=process-service-perfdata-file

4.增加以下内容:

event_broker_options=-1

broker_module=/usr/local/pnp4nagios/lib/npcdmod.o config_file=/usr/local/pnp4nagios/etc/npcd.cfg'

這裡要安裝自己目前的環境設定子產品和配置檔案的目錄路徑

5.啟動npcd

#service npcd start

6.重新啟動nagios

#service nagios restart

7.重新啟動apache.讓pnp4nagios.conf加載進來

#killall -9 httpd

#apachectl start

通路:http://localhost/pnp4nagios

安裝check_mk

2.check_mk的安裝包解壓安裝後,跟其他的軟體不一樣安裝後就可以删除

#tar xvf check_mk-1.2.0p2.tar.gz

#cd check_mk-1.2.0p2

#./setup.sh

根據提示進行符合自己的設定,絕大部分的選項它都會檢測你目前的環境配置

               ____ _               _        __  __ _  __               

              / ___| |__   ___  ___| | __   |  \/  | |/ /               

             | |   | '_ \ / _ \/ __| |/ /   | |\/| | ' /                

             | |___| | | |  __/ (__|   &lt;    | |  | | . \                

              \____|_| |_|\___|\___|_|\_\___|_|  |_|_|\_\               

                                       |_____|                          

   Check_MK setup                                  Version: 1.2.0p2     

Welcome to Check_MK. This setup will install Check_MK into user defined directories. If you run this script as root, installation paths below /usr will be suggested. If you run this script as non-root user paths in your home directory will be suggested. You may override the default values or just hit enter to accept them. 

Your answers will be saved to /root/.check_mk_setup.conf and will be reused when you run the setup of this or a later version again. Please delete that file if you want to delete your previous answers.

 * Found running Nagios process, autodetected 20 settings.                                                                                  

  1) Installation directories of check_mk                                                                                               

Executable programs

Otherwise youalways have to specify the installation path when calling check_mk:

( default  --&gt; /usr/bin): 

安裝check_mk指令的路徑,預設即可

Check_MK configuration 

( default  --&gt; /etc/check_mk):

安裝check_mk的配置檔案路徑,預設即可

Check_MK software

( default  --&gt; /usr/share/check_mk): 

指定安裝check_mk軟體的安裝,這裡我繼續使用預設

documentation 

( default  --&gt; /usr/share/doc/check_mk): 

指定check_mk的文檔安裝目錄,如果上一個選項你更改了安裝路徑,建議這裡也作出修改,盡量保持父目錄一緻,我使用預設

check manuals

( default  --&gt; /usr/share/doc/check_mk/checks): 

指定檢測手冊的安裝路徑,這裡我使用預設

working directory of check_mk 

( default  --&gt; /var/lib/check_mk): 

指定check_mk的工作目錄,以建立緩存檔案等                                                                 

2) Configuration of Linux/UNIX Agents                                                                                                        

extensions for agents 

( default  --&gt; /usr/lib/check_mk_agent): 

指定check_mk的用戶端的目錄

configuration dir for agents 

( default  --&gt; /etc/check_mk): 

指定check_mk用戶端的配置檔案目錄                         

3) Integration with Nagios                                                                                                             

Name of Nagios user

( default  --&gt; nagios): 

指定你的nagios的運作使用者。

User of Apache process

 ( default  --&gt; apache): 

指定你的apache的運作使用者,這裡我的是apache

Common group of Nagios+Apache

指定你的apache的運作使用者和nagios運作使用者的共同組名,預設是nagios

Nagios binary

( autodetected  --&gt; /usr/local/apache/htdocs/nagios/bin/nagios): 

指定nagios指令的目錄,這裡該安裝程式會自動檢測出你的目前環境的配置,是以基本上預設即可

Nagios main configuration file

( autodetected  --&gt; /usr/local/apache/htdocs/nagios/etc/nagios.cfg): 

指定nagios的主配置檔案

Nagios object directory

( autodetected  --&gt; /usr/local/apache/htdocs/nagios/etc/service): 

指定nagios定義的對象目錄,即nagios中配置的cfg_dir

Nagios startskript

( autodetected  --&gt; /etc/init.d/nagios): 

nagios的SysV風格的啟動腳本

Nagios command pipe

( autodetected  --&gt; /usr/local/apache/htdocs/nagios/var/rw/nagios.cmd): 

指定nagios啟動後生成的指令接口檔案目錄

Check results directory

( autodetected  --&gt; /usr/local/apache/htdocs/nagios/var/spool/checkresults): 

指定nagios的檢測結果目錄

Nagios status file

( autodetected  --&gt; /usr/local/apache/htdocs/nagios/var/status.dat): 

指定nagios的狀态檔案的路徑

Path to check_icmp

( autodetected  --&gt; /usr/local/nagios/libexec/check_icmp): 

指定check_mk附帶的check_icmp的指令的安裝目錄,伺服器端通過該指令來檢測多個監控主機                                                                  

  4) Integration with Apache                                                                                                                 

URL Prefix for Web addons

( default  --&gt; /): 

指定通過web通路時的别名。

Apache config dir

( autodetected  --&gt; /etc/httpd/conf.d): 

指定apache的擴充配置檔案目錄,check_mk會在該目錄下生成适用于apache的一個配置檔案。

HTTP authentication file

( autodetected  --&gt; /usr/local/apache/htdocs/nagios/etc/htpasswd.users): 

指定通路check_mk時的身份驗證檔案,這裡它會使用nagios的使用者驗證檔案

HTTP AuthName

( autodetected  --&gt; Nagios Access): 

用于指定AuthName的提示資訊。                                            

5) Integration with PNP4Nagios 0.6                                     

PNP4Nagios templates

( autodetected  --&gt; /usr/local/pnp4nagios/share/templates): 

用于指定pnp4Nagios的模版目錄                                           

6) Check_MK Livestatus Module                                             

compile livestatus module

( default  --&gt; yes): 

這裡詢問是否要編譯livestatus子產品到nagios,具體的livestatus我還不了解,隻是知道在執行此操作後,當nagios啟動時會生成一個名為live的socket套接字檔案,用于check_mk通信用的。

check_mk's binary modules

( default  --&gt; /usr/lib/check_mk): 

指定之前check_mk指令的安裝目錄

Unix socket for Livestatus

( default  --&gt; /usr/local/apache/htdocs/nagios/var/rw/live): 

指定livestatus的Unix的套接字檔案生成的目錄以及名字

Backends for other systems

( default  --&gt; /usr/share/check_mk/livestatus): 

指定與其他作業系統通信的livestatus的目錄

----------------------------------------------------------------------

You have chosen the following directories: 

 Executable programs             /usr/bin                          

 Check_MK configuration          /etc/check_mk                          

 Check_MK software               /usr/share/check_mk                    

 documentation                   /usr/share/doc/check_mk                

 check manuals                   /usr/share/doc/check_mk/checks        

 working directory of check_mk   /var/lib/check_mk                      

 extensions for agents           /usr/lib/check_mk_agent                

 configuration dir for agents    /etc/check_mk                          

 Name of Nagios user             nagios                                

 User of Apache process          apache                                

 Common group of Nagios+Apache   nagios                                  

 Nagios binary                    /usr/local/apache/htdocs/nagios/bin/nagios 

 Nagios main configuration file  /usr/local/apache/htdocs/nagios/etc/nagios.cfg 

 Nagios object directory         /usr/local/apache/htdocs/nagios/etc/service 

 Nagios startskript              /etc/init.d/nagios                      

 Nagios command pipe             /usr/local/apache/htdocs/nagios/var/rw/nagios.cmd 

 Check results directory         /usr/local/apache/htdocs/nagios/var/spool/checkresults 

 Nagios status file              /usr/local/apache/htdocs/nagios/var/status.dat 

 Path to check_icmp              /usr/local/nagios/libexec/check_icmp    

 URL Prefix for Web addons       /                                    

 Apache config dir               /etc/httpd/conf.d                    

 HTTP authentication file        /usr/local/apache/htdocs/nagios/etc/htpasswd.users 

 HTTP AuthName                   Nagios Access                          

 PNP4Nagios templates            /usr/local/pnp4nagios/share/templates   

 compile livestatus module       yes                                     

 check_mk's binary modules       /usr/lib/check_mk                       

 Unix socket for Livestatus      /usr/local/apache/htdocs/nagios/var/rw/live 

 Backends for other systems      /usr/share/check_mk/livestatus          

Proceed with installation (y/n)? y

此刻将會将之前的所有配置列出來,作出一個最後的确認。

(Compiling MK Livestatus.......................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................)

Installation completed successfully.

Please restart Nagios and Apache in order to update/active check_mk's web pages.

You can access the new Multisite GUI at http://localhost/check_mk/ 

3.重新開機apache和nagios

4.通路http://localhost/check_mk/

錯誤1:

提示沒有mod_python子產品

解決方法1:

#wget http://archive.apache.org/dist/httpd/modpython/mod_python-3.3.1.tgz

#tar zxvf mod_python-3.3.1.gz

#cd mod_python-3.3.1

#./configure --with-apxs=/usr/local/apache/bin/apxs --with-python=/usr/bin/python

#make

 apxs:Error: Command failed with rc=65536

 make[1]: *** [mod_python.so] Error 1

 make[1]: Leaving directory `/root/ndo/src/mod_python-3.3.1/src'

 make: *** [do_dso] Error 2

 原因是 Apache 的版本還太高 mod_python 未支援

 #vim src/connobject.c

 把!(b == APR_BRIGADE_SENTINEL(b) || 改為!(b ==  APR_BRIGADE_SENTINEL(bb) ||

#make clean

#make install

在httpd.conf中增加

LoadModule python_module modules/mod_python.so

重新開機apache,重新通路正常。

錯誤2:

Your web server cannot create the directory ,or cannot set the group to or cannot set the permissions to.Please make sure that:Reason:[Error 13]Permission denied:'/var/lib/check_mk/web/admin'

解決方法2:

#chown apache:apache /var/lib/check_mk/web

錯誤3:

Livestatus problem:Cannot connect to 'unix:/usr/local/apache/htdocs/nagios/var/rw/live':[Error 13]Permission denied

解決方法3:

#vim /usr/local/apache/htdocs/nagios/etc/nagios

添加如下内容:

broker_module=/usr/lib/check_mk/livestatus.o /usr/local/apache/htdocs/nagios/var/rw/live

#usermod -G apache,nagios nagios

#chown nagios:nagios /usr/local/apache/htdocs/nagios/var/rw/live

#tail /usr/local/apache/htdocs/nagios/var/nagios.log 

出現如下内容,則表示正常了,重新整理重試。錯誤消失

[1347850110] Event broker module '/usr/lib/check_mk/livestatus.o' initialized successfully.

[1347850110] livestatus: Timeperiod cache not updated, there are no timeperiods (yet)

[1347850110] livestatus: Livestatus 1.2.0p2 by Mathias Kettner. Socket: '/usr/local/apache/htdocs/nagios/var/rw/live'

[1347850110] livestatus: Please visit us at http://mathias-kettner.de/

[1347850110] livestatus: Hint: please try out OMD - the Open Monitoring Distribution

[1347850110] livestatus: Please visit OMD at http://omdistro.org

[1347850110] livestatus: Removed old left over socket file /usr/local/apache/htdocs/nagios/var/rw/live

[1347850110] livestatus: Finished initialization. Further log messages go to /usr/local/apache/htdocs/nagios/var/livestatus.log

5.一切通路正常,安裝check_mk用戶端:

Linux主機

#wget http://mathias-kettner.de/download/check_mk-agent-1.2.0p2-1.noarch.rpm

#rpm -ivh check_mk-agent-1.2.0p2-1.noarch.rpm

指定被監控端的IP

#vim /etc/xinetd.d/check_mk

修改only_from = “監控端IP”

重新開機xinetd

#service xinedt restart

windows主機

下載下傳http://mathias-kettner.de/download/check-mk-agent-1.2.0p2.exe

安裝即可

編輯安裝目錄下的check_mk.example.ini

重新開機Check_MK_Agent服務即可。

6.監控Linux主機

#vim /etc/check_mk/main.mk

修改all_hosts,填寫每一個被監控機的IP位址,中間用,隔開,這裡的IP位址如果在nagios中你作為hostname使用的話,請修改nagios中的hostname或者将其注釋掉,否則兩者将會沖突報錯。

all_hosts = [ "172.23.136.139","172.23.136.149","172.23.136.155" ]

儲存退出後:

執行以下指令:

#check_mk -I

檢查所有可檢測的項目 

df                8 new checks

logwatch          14 new checks

mem.win           2 new checks

systemtime        2 new checks

uptime            2 new checks

winperf_phydisk   2 new checks

winperf_processor.util 2 new checks

#check_mk -O

建立或更新Nagios配置,并且重新加載Nagios

Generating Nagios configuration...OK

Validating Nagios configuration...OK

Precompiling host checks...OK

Reloading Nagios...OK

7、通路http://localhost/check_mk

發現并未出現定義的那些主機

#vim /etc/check_mk/multisite.mk

将admin_users = [ "nagiosadmin" ]   修改為登陸到nagios的使用者名,即htpasswd定義的使用者名,我這裡是admin

admin_users = [ "admin" ]

儲存退出,重新重新整理,即可看到剛才定義的主機了

在長時間的觀察發現,這些定義的主機的伺服器基本上已正常,但是主機确實down的狀态

錯誤提示:

Waring:This plugin must be either run as root or setuid root"

解決方法:

找到當初安裝check_mk時附帶的check_icmp指令

#chown root:nagios check_icmp

#chmod u+s check_icmp

發現主機馬上就處于up狀态了

對于從web界面直接添加主機的方法,點選左下角的一個Hosts &amp; Folders按鈕,點選New host即可

<a href="http://blog.51cto.com/attachment/201209/160110475.jpg" target="_blank"></a>

其他功能筆者這在琢磨,例如監控cluster還有那個wato等,由于界面是全英的,是以對于筆者這樣的英語水準的确吃力,有興趣的朋友可以自行研究下。同樣歡迎朋友補充check_mk的其他功能。

本文轉自 向陽草米奇 51CTO部落格,原文連結:http://blog.51cto.com/grass51/994819,如需轉載請自行聯系原作者

繼續閱讀