Nagios透過check_http監控一臺web應用伺服器上多個tomcat服務

dbasdk發表於2017-04-24

如何在nagios監控tomcat,是一個比較簡單又複雜的事情,簡單是因為如果只監控web應用伺服器的一個tomcat服務是否正常執行,那麼比較簡單;如果要監控tomcat的其他比如連線數比如jvm記憶體使用率等就比較複雜,google沒有適合的監控指令碼;如果要監控web應用上面的多個tomcat伺服器,而且很多tomcat服務都是跳轉式的,那就需要多做很多事情。

 

一般通常都使用tcp tomcat埠的方式,不過這有一個bug就是tomcat假死的情況下,tcp 埠是OK的,但是tomcat裡面部署的web應用其實已經不能正常訪問,這個時候需要使用http方式來監控tomcat的狀態。

 

所以本文就記錄瞭如何採用http方式來監控一臺web伺服器上多個tomcat應用伺服器。

 

1tomcat web伺服器上安裝nrpe客戶端:

Rpm包下載地址為:

1.1rpm方式安裝nrpe客戶端


  1. [root@localhost nagios]# ll
  2. 總計 768
  3. -rw-r--r-- 1 root root 713389 12-16 12:08 nagios-plugins-1.4.11-1.x86_64.rpm
  4. -rw-r--r-- 1 root root 32706 12-16 12:09 nrpe-2.12-1.x86_64.rpm
  5. -rw-r--r-- 1 root root 18997 12-16 12:08 nrpe-plugin-2.12-1.x86_64.rpm
  6. [root@localhost nagios]# rpm -ivh *.rpm --nodeps --force
  7. Preparing... ########################################### [100%]
  8.    1:nagios-plugins ########################################### [ 33%]
  9. id: nagios:無此使用者
  10.    2:nrpe ########################################### [ 67%]
  11.    3:nrpe-plugin ########################################### [100%]
  12. [root@cache-1 ~]#


1.2 在配置檔案最末尾,新增配置資訊以及監控主機伺服器ip地址

[root@ localhost nagios]# vim /etc/nagios/nrpe.cfg


  1. # add by tim on 2014-06-11
  2. command[check_users]=/usr/local/nagios/libexec/check_users -w 8 -c 15
  3. command[check_load]=/usr/local/nagios/libexec/check_load -w 15,10,5 -c 30,25,20
  4. command[check_sda1]=/usr/local/nagios/libexec/check_disk -w 20% -c 10% -p /dev/sda
  5. command[check_zombie_procs]=/usr/local/nagios/libexec/check_procs -w 5 -c 10 -s Z
  6. #command[check_total_procs]=/usr/local/nagios/libexec/check_procs -w 50 -c 80
  7. command[check_total_procs]=/usr/local/nagios/libexec/check_procs -w 750 -c 800
  8. command[check-host-alive]=/usr/local/nagios/libexec/check_ping -H 10.xx.xx.10 -w 3000.0,80% -c 5000.0,100% -p 5
  9. allowed_hosts = 127.0.0.1,10.xx.xxx.xx1


check下命令是否生效:


  1. [root@webserver nrpe-2.15]# /usr/local/nagios/libexec/check_users -w 8 -c 15
  2. USERS OK - 2 users currently logged in |users=2;8;15;0
  3. [root@webserver nrpe-2.15]#


看到已經USERS OK -….命令已經生效。

 

1.3 啟動nrpe報錯如下:


  1. [root@webserver ~]# service nrpe restart
  2. Shutting down nrpe: [失敗]
  3. Starting nrpe: /usr/sbin/nrpe: error while loading shared libraries: libssl.so.6: cannot open shared object file: No such file or directory
  4.                                                            [失敗]
  5. [root@webserver ~]#
  6. [root@db-m2-slave-1 nagios_client]# service nrpe start
  7. Starting nrpe: /usr/sbin/nrpe: error while loading shared libraries: libssl.so.6: cannot open shared object file: No such file or directory
  8.                                                            [失敗]
  9. [root@db-m2-slave-1 nagios_client]#


建立軟連線

[root@db-m2-slave-1 nagios_client]# ln -s /usr/lib64/libssl.so /usr/lib64/libssl.so.6

 (如果沒有libssl.so,就採用別的libssl.so.10來做軟連線,ln -s /usr/lib64/libssl.so.10 /usr/lib64/libssl.so.6)

[root@db-m2-slave-1 nagios_client]#

再重新啟動如下:

[root@webserver nagios_client]# service nrpe start

Starting nrpe: /usr/sbin/nrpe: error while loading shared libraries: libcrypto.so.6: cannot open shared object file: No such file or directory

                                                           [失敗]

[root@web-10 ~]# ll /usr/lib64/libcrypto.so

lrwxrwxrwx. 1 root root 18 10 13 2013 /usr/lib64/libcrypto.so -> libcrypto.so.1.0.0

[root@webserver nagios_client]#

再建軟連結:
[root@webserver nagios_client]# ln -s /usr/lib64/libcrypto.so /usr/lib64/libcrypto.so.6

(或者如果沒有libcrypto.so,就採用libcrypto.so.10做軟連線, ln -s /usr/lib64/libcrypto.so.10 /usr/lib64/libcrypto.so.6)

[root@webserver nagios_client]# service nrpe start

Starting nrpe:                                             [確定]

[root@webserver nagios_client]#

 

1.4 檢測下nrpe是否正常執行:

nagios伺服器端check

[root@cache-2 ~]#  /usr/local/nagios/libexec/check_nrpe -H 10.xx.xx.10

NRPE v2.12

[root@cache-2 ~]#

看到返回NRPE v2.15表示已經連線成功。

 

1.5 web應用下新增檢測jsp檔案

(1) 建立測試檔案

vim ./webapps/nagios_test_0611/nagios_test_0611.jsp


  1. <%@ page language=\"java\" contentType=\"text/html; charset=gb2312\"
  2. pageEncoding=\"gb2312\"%>
  3. <!DOCTYPE html PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\" \"\">
  4. <html>
  5. <head>
  6. <meta http-equiv=\"Content-Type\" content=\"text/html; charset=gb2312\">
  7. <title>nagios test here</title>
  8. </head>
  9. <body>
  10.  <center>Now time is: <%=new java.util.Date()%></center>
  11. </body>
  12. </html>


(2) check下check_http命令

[root@webserver~]# /usr/local/nagios/libexec/check_http -I 10.xx.xx.10 -p 8300 -u /nagios_test_0611/nagios_test_0611.jsp -e 200

HTTP CRITICAL - Invalid HTTP response received from host on port 8300: HTTP/1.1 404 Not Found

需要重啟一下tomcat,使新新增的jsp生效能開啟,執行如下stop start命令:

/usr/local/app/apache-tomcat-6.0.37_8300/bin/shutdown.sh

/usr/local/app/apache-tomcat-6.0.37_8300/bin/startup.sh

[root@webserver~]# /usr/local/nagios/libexec/check_http -I 10.xx.xx.10 -p 8300 -u /nagios_test_0611/nagios_test_0611.jsp -e 200

HTTP OK: Status line output matched "200" - 571 bytes in 0.882 second response time |time=0.882479s;;;0.000000 size=571B;;;0

[root@ webserver ~]#

 

1.6檢視NRPE的監控命令


  1. [root@webserver nrpe-2.15]# cat /etc/nagios/nrpe.cfg |grep -v \"^#\"|grep -v \"^$\"
  2. log_facility=daemon
  3. pid_file=/var/run/nrpe.pid
  4. server_port=5666
  5. nrpe_user=nagios
  6. nrpe_group=nagios
  7.  
  8. dont_blame_nrpe=0
  9. debug=0
  10. command_timeout=60
  11. connection_timeout=300
  12. command[check_users]=/usr/local/nagios/libexec/check_users -w 8 -c 15
  13. command[check_load]=/usr/local/nagios/libexec/check_load -w 15,10,5 -c 30,25,20
  14. command[check_sda1]=/usr/local/nagios/libexec/check_disk -w 20% -c 10% -p /dev/sda
  15. command[check_zombie_procs]=/usr/local/nagios/libexec/check_procs -w 5 -c 10 -s Z
  16. command[check_total_procs]=/usr/local/nagios/libexec/check_procs -w 750 -c 800
  17. command[check-host-alive]=/usr/local/nagios/libexec/check_ping -H 10.xx.xx.10 -w 3000.0,80% -c 5000.0,100% -p 5
  18. allowed_hosts=127.0.0.1,10.xx.xxx.xx1
  19. [root@webserver nrpe-2.15]#


2,去nagios伺服器端新增host等監控資訊。

2.1 hosts.cfg裡面新增主機資訊


  1. define host{
  2.         use linux-server
  3.         host_name webserver
  4.         alias webserver
  5.         address 10.xx.xx.10
  6.         check_command check-host-alive
  7.         max_check_attempts 5
  8.         check_period 24x7
  9.         contact_groups ops
  10.         notification_interval 60
  11.         notification_period 24x7
  12.         notification_options d,u,r
  13.         }


2.2 service.cfg裡面新增web機器監控的命令資訊


  1. # No.007 webserver
  2. # service definition
  3. define service{
  4.         host_name webserver
  5.         service_description check_load
  6.         check_command check_nrpe!check_load
  7.         max_check_attempts 5
  8.         normal_check_interval 3
  9.         retry_check_interval 2
  10.         check_period 24x7
  11.         notification_interval 10
  12.         notification_period 24x7
  13.         notification_options w,u,c,r
  14.         contact_groups opsweb
  15.         }
  16.  
  17. define service{
  18.         host_name webserver
  19.         service_description check-host-alive
  20.         check_command check-host-alive
  21.         max_check_attempts 5
  22.         normal_check_interval 3
  23.         retry_check_interval 2
  24.         check_period 24x7
  25.         notification_interval 10
  26.         notification_period 24x7
  27.         notification_options w,u,c,r
  28.         contact_groups opsweb
  29.         }
  30.  
  31. define service{
  32.         host_name webserver
  33.         service_description Check Disk sda1
  34.         check_command check_nrpe!check_sda1
  35.         max_check_attempts 5
  36.         normal_check_interval 3
  37.         retry_check_interval 2
  38.         check_period 24x7
  39.         notification_interval 10
  40.         notification_period 24x7
  41.         notification_options w,u,c,r
  42.         contact_groups opsweb
  43.         }
  44.  
  45. define service{
  46.         host_name webserver
  47.         service_description Total Processes
  48.         check_command check_nrpe!check_total_procs
  49.         max_check_attempts 5
  50.         normal_check_interval 3
  51.         retry_check_interval 2
  52.         check_period 24x7
  53.         notification_interval 10
  54.         notification_period 24x7
  55.         notification_options w,u,c,r
  56.         contact_groups opsweb
  57.         }
  58.  
  59. define service{
  60.         host_name webserver
  61.         service_description Current Users
  62.         check_command check_nrpe!check_users
  63.         max_check_attempts 5
  64.         normal_check_interval 3
  65.         retry_check_interval 2
  66.         check_period 24x7
  67.         notification_interval 10
  68.         notification_period 24x7
  69.         notification_options w,u,c,r
  70.         contact_groups opsweb
  71.         }
  72.  
  73. define service{
  74.         host_name webserver
  75.         service_description Check Zombie Procs
  76.         check_command check_nrpe!check_zombie_procs
  77.         max_check_attempts 5
  78.         normal_check_interval 3
  79.         retry_check_interval 2
  80.         check_period 24x7
  81.         notification_interval 10
  82.         notification_period 24x7
  83.         notification_options w,u,c,r
  84.         contact_groups opsweb
  85.         }

  86. define service{
  87.         host_name webserver
  88.         service_description Check Tomcat 9300 Status
  89.         check_command check_nrpe!check_tomcat_9300_status
  90.         max_check_attempts 5
  91.         normal_check_interval 3
  92.         retry_check_interval 2
  93.         check_period 24x7
  94.         notification_interval 10
  95.         notification_period 24x7
  96.         notification_options w,u,c,r
  97.         contact_groups opsweb
  98.         }


2.3 vim contacts.cfg新增新的opsweb郵件組資訊


  1. define contactgroup{
  2.         contactgroup_name opsweb
  3.         alias pl ops team
  4.         members tim,mch,nagiosadmin
  5.         }


 

2.4 新增新的監控tomcat的命令,check_tomcat_9300_status

這裡不採用check_tcp!8080埠的方式,是因為在實際中tomcat服務假死之後,jsp的網頁都是打不開的,但是這個監控埠8080都是正常的,不會報警出來;所以採用check_http的方式,新建立一個通用的/nagios_test_0611/nagios_test_0611.jsp檔案,來檢測這個jsp的訪問情況,如下所示:

vim commands.cfg


  1. # add by tim on 20140611
  2. define command{
  3.         command_name check_tomcat_9300_status
  4.         command_line $USER1$/check_http -I $HOSTADDRESS$ -p $PORT$ -u $URL$ -e $N200$ -w $Warning$ -c$Cri$
  5.         }


Jsp檔案內容如下:


  1. [root@webserver webapps]# vim . /nagios_test_0611/nagios_test_0611.jsp
  2. <%@ page language=\"java\" contentType=\"text/html; charset=gb2312\"
  3. pageEncoding=\"gb2312\"%>
  4. <!DOCTYPE html PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\" \"\">
  5. <html>
  6. <head>
  7. <meta http-equiv=\"Content-Type\" content=\"text/html; charset=gb2312\">
  8. <title>nagios test here</title>
  9. </head>
  10. <body>
  11.  <center>Now time is: <%=new java.util.Date()%></center>
  12. </body>
  13. </html>



2.5
在被監控客戶端的nrpe.cfg配置檔案裡面新增tomcat埠配置資訊:

command[check_tomcat_9300_status]=/usr/local/nagios/libexec/check_http -I 10.xx.xx.10 -p 9444 -u /nagios_test_0611/nagios_test_0611.jsp -e 200 -w 5 -c 10

command[check_tomcat_8300_status]=/usr/local/nagios/libexec/check_http -I 10.xx.xx.10 -p 8300 -u /nagios_test_0611/nagios_test_0611.jsp -e 200 -w 5 -c 10

 

2.6 測試報錯

[root@cache-2 objects]# /usr/local/nagios/libexec/check_nrpe -H 10.xx.xx.10  -c check_load

NRPE: Unable to read output

[root@cache-2 objects]#

已經新增了tomcat930埠,現在再新增一個tomcat8300

 

去伺服器端shell命令列裡面check

/usr/local/nagios/libexec/check_nrpe -H 192.168.15.178 -c check_mysql_myisam_lock

[root@cache-2 etc]# /usr/local/nagios/libexec/check_nrpe -H 10.xx.xx.10  -c check_load

NRPE: Unable to read output

[root@cache-2 etc]#

同樣報錯,那麼可能就是nagios被監控端的問題。

 

最終檢查是nrpe.cfg裡面路徑有誤,原始碼安裝預設路徑是:/usr/local/nagios/libexec/check_httprpm安裝預設路徑是:/usr/lib/nagios/plugins/。這裡是rpm安裝,所以nrpe.cfg配置檔案裡面用後面rpm的路徑/usr/lib/nagios/plugins/,替換下service nrpe restart之後,問題解決,如下圖所示:

3 tomcat多埠監控報警

已經新增了tomcat930埠,現在再新增一個tomcat8300


3.1 客戶端的nrpe.cfg裡面新增配置

[root@webserver root]# vim /etc/nagios/nrpe.cfg

command[check_tomcat_8300_status]=/usr/lib/nagios/plugins/check_http -I 10.xx.xx.10 -p 8300 -u /xx_xx_xx/index.html -e 200 -w 5 -c 10

 

  1. define service{
  2.         host_name webserver
  3.         service_description Tomcat_8300_Status
  4.         check_command check_nrpe!check_tomcat_8300_status
  5.         max_check_attempts 5
  6.         normal_check_interval 3
  7.         retry_check_interval 2
  8.         check_period 24x7
  9.         notification_interval 10
  10.         notification_period 24x7
  11.         notification_options w,u,c,r
  12.         contact_groups opsweb
  13.         }


3.2 nagios伺服器端
新增command命令


  1. [root@cache-2 etc]# vim ./objects/commands.cfg
  2. define command{
  3.         command_name check_tomcat_8300_status
  4.         command_line $USER1$/check_http -I $HOSTADDRESS$ -p $PORT$ -u $URL$ -e $N200$ -w $Warning$ -c$Cri$
  5.         }


新增service服務


  1. define service{
  2.         host_name webserver
  3.         service_description Tomcat_8300_Status
  4.         check_command check_nrpe!check_tomcat_8300_status
  5.         max_check_attempts 5
  6.         normal_check_interval 3
  7.         retry_check_interval 2
  8.         check_period 24x7
  9.         notification_interval 10
  10.         notification_period 24x7
  11.         notification_options w,u,c,r
  12.         contact_groups opsweb
  13.         }


3.3 nagios伺服器上check下新新增的命令是否生效

[root@cache-2 etc]# /usr/local/nagios/libexec/check_nrpe -H 10.xx.xx.10  -c check_tomcat_8300_status

HTTP OK HTTP/1.1 200 OK - 611 bytes in 0.003 seconds |time=0.003152s;5.000000;10.000000;0.000000 size=611B;;;0

[root@cache-2 etc]#

看到命令已經生效。

 

3.4 重啟nagios伺服器,檢視結果

[root@cache-2 etc]# service nagios reload

Running configuration check...

Reloading nagios configuration...

done

[root@cache-2 etc]#

重啟後,過3分鐘,新的tomcat8300已經監控起來了,如下圖所示:

 

為了驗證tomcat的監控效果,在web伺服器客戶端,停掉tomcat9300埠,一會就會收到報警email,也會在nagios頁面看到紅色報警提示,如下所示:

這標示2nagios選項監控的是2個埠,一個9300,一個8300

 

新增新埠8200檢測-e 200報錯問題解決

[root@webserver OCC_MANAGER_Web]#  /usr/lib/nagios/plugins/check_http -I 10.xx.xx.10 -p 8200 -u /OCC_REPORT_Web/index.html -e 200 -w 5 -c 10

HTTP CRITICAL - Invalid HTTP response received from host on port 8200

[root@webserver OCC_MANAGER_Web]#

 

4.1 直接訪問tomcat服務以及indexhtml

是可以訪問的,但是會跳轉到

的頁面,證明web應用都是正常的,只是已經被跳轉到別的域名頁面而已。

 

4.2 –v詳細分析

這個時候tomcat伺服器是正常running的,而且web應用也是正常返回的,只是執行 看到這裡大概意思是從8200埠獲取無效的HTTP響應,因為這條命令最重要的是監控/OCC_REPORT_Web/index.html獲取http資訊並透過-e 200來判斷http正常響應的OK狀態,所以去掉報警的-w 5 –c 10引數,去掉-e 200的字元比對資訊,看下check的返回資訊。

[root@webserver OCC_MANAGER_Web]# /usr/lib/nagios/plugins/check_http -I 10.xx.xx.10 -p 8200 -u /OCC_REPORT_Web/index.html

HTTP OK - HTTP/1.1 302 Found - 0.003 second response time |time=0.003367s;;;0.000000 size=317B;;;0

看到返回的是HTTP/1.1 302 Found 檢視Tomcat錯誤程式碼知道是產生了新的URL資訊

……

301  Moved Permanently  客戶請求的文件在其他地方,新的URL在Location頭中給出,瀏覽器應該自動地訪問新的URL。
302  Found  類似於301,但新的URL應該被視為臨時性的替代,而不是永久性的。注意,在HTTP1.0中對應的狀態資訊是“Moved Temporatily”。

……

 

最後加入-v引數除錯看詳細的獲取資訊:

[root@webserver OCC_MANAGER_Web]# /usr/lib/nagios/plugins/check_http -H .com -I 10.xx.xx.10 -p 8200 -u /OCC_REPORT_Web/index.html -v

GET /OCC_REPORT_Web/index.html HTTP/1.0

User-Agent: check_http/v1861 (nagios-plugins 1.4.11)

Connection: close

Host: .com

 

is 323 characters

STATUS: HTTP/1.1 302 Found

**** HEADER ****

Server: Apache-Coyote/1.1

Set-Cookie: ploccSessionId=45CD9C9921A5B89C59FCB2E34FE52734; Path=/

Location: .com/OCC_SSO_Web/login.htm?redirect=http%3A%2F%2F.com%2FOCC_REPORT_Web%2Findex.html

Content-Length: 0

Date: Thu, 12 Jun 2014 02:52:45 GMT

Connection: close

**** CONTENT ****

HTTP OK - HTTP/1.1 302 Found - 0.003 second response time |time=0.003268s;;;0.000000 size=323B;;;0

 

看到頁面重定向到域名系統,tomcat伺服器是正常執行的,所以302 Found也可以表示tomca伺服器正常運轉無誤,因為架構是用的lvs負載均衡,所以如果動用跳轉後的公用域名來判斷的話,就不能確定是否是這個主機的tomcat,因為公用域名每次只對應其中一個tomcat服務,因為這裡是監控具體的一臺web伺服器的tomcat,所以去監控302埠也是一個不錯的辦法,這裡可以去修改客戶端nrpe.cfg裡面的8200埠的監控命令,改成監控tomcat302狀態值:

Vim /etc/nagios/nrpe.cfg

/usr/lib/nagios/plugins/check_http -I 10.xx.xx.10 -p 8200 -u /OCC_REPORT_Web/index.html  -e 302 -w 3 -c 10

 

報錯記錄(): NRPE: Unable to read output

[1402557345] SERVICE ALERT: webserver;Tomcat_6100_OCC_SSO_Service_Status;UNKNOWN;SOFT;3;NRPE: Unable to read output

 

解決:一般是nrpe路徑不對。

 

報錯記錄()CHECK_NRPE: Error - Could not complete SSL handshake.

[root@cache-2 etc]# /usr/local/nagios/libexec/check_http -I 10.xx.3.xx -p 8100 -u /tradeAdmin/index.html

HTTP OK: HTTP/1.1 302 Found - 319 bytes in 0.064 second response time |time=0.064033s;;;0.000000 size=319B;;;0

[root@cache-2 etc]#

[root@cache-2 etc]# /usr/local/nagios/libexec/check_nrpe -H 10.xx.3.xx -c check_load

CHECK_NRPE: Error - Could not complete SSL handshake.

[root@cache-2 etc]#

解決:/etc/nagios/nrpe.cfg裡面沒有新增nagios伺服器主機ip地址

Vim /etc/nagios/nrpe.cfg

allowed_hosts=127.0.0.1,10.xx.xxx.xx1

之後重啟nrpeservice nrpe restart;再去nagios伺服器上驗證OK:

[root@cache-2 etc]# /usr/local/nagios/libexec/check_nrpe -H 10.xx.3.xx -c check_load

OK - load average: 0.43, 0.17, 0.06|load1=0.430;15.000;30.000;0; load5=0.170;10.000;25.000;0; load15=0.060;5.000;20.000;0;

[root@cache-2 etc]#

 

 

 

來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/29734436/viewspace-2137850/,如需轉載,請註明出處,否則將追究法律責任。

相關文章