当前位置: > Linux服务器 > 监控工具 >

Zabbix 监控Tomcat

时间:2014-12-28 00:43来源:linux.it.net.cn 作者:IT
使用Zabbix监控Tomcat 我早期使用了Zabbix的内置模板,因为得到的数据没太多用,后改用去调jmap的输出,现在每三分钟执行一次脚本,每30秒取一次值,PSPermGeneration和PSOldGeneration做为报警标准.


监控图如下:

zabbix-jmap

zabbix-jmap

 

 

代码如下,通过执行jmap heap处理堆信息生成一个Dict,实现的比较妖孽 需要依赖crontab 定时执行….(模板文末下载)


  1. #!/usr/bin/python
  2. #-conding:utf-8--#
  3. #-------------------------------------------------------------------------------
  4. # Name: Jmap.py
  5. #
  6. # Author: LiuSha
  7. #
  8. # Created: 9/29/2014
  9. # Copyright: (c) WDZJ-SA 2014
  10. #-------------------------------------------------------------------------------
  11. import subprocess
  12. import re
  13. import os
  14.  
  15. def runCommand(cmd,stdinstr = ''):
  16. p=subprocess.Popen(cmd, shell=True, universal_newlines=True, stdin=subprocess.PIPE,stdout=subprocess.PIPE, stderr=subprocess.STDOUT)
  17. stdoutdata, stderrdata = p.communicate(stdinstr)
  18. return p.returncode, stdoutdata, stderrdata
  19.  
  20. def FormatValue():
  21. Dict = {'PSOldGeneration':{'percent':''},'PSPermGeneration':{'percent':''}}
  22. _Output = runCommand("""jmap -heap `ps aux | grep "/home/tomcat/tomcat/apache-tomcat-7.0.53/conf/logging.properties" | awk '!/grep/{print $2}'`""")[1].replace(' ','').split('\n')
  23. _NewOpt = [x for x in _Output if x]
  24. for i in _NewOpt[-10:-7]:
  25. Dict['PSOldGeneration'][i.split('=')[0]] = re.findall('\d+',i.split('=')[1])[0]
  26.  
  27. Dict['PSOldGeneration']['percent'] = re.findall('\d+\D+\d+',_NewOpt[-7])[0]
  28.  
  29. for i in _NewOpt[-5:-2]:
  30. Dict['PSPermGeneration'][i.split('=')[0]] = re.findall('\d+',i.split('=')[1])[0]
  31.  
  32. Dict['PSPermGeneration']['percent'] = re.findall('\d+\D+\d+',_NewOpt[-2])[0]
  33.  
  34. file = open('%s/Dict.py'%os.path.dirname(__file__),'w')
  35. file.write("""Dict = %s\n\nif __name__ == '__main__':
  36. import sys
  37. print Dict[sys.argv[1]][sys.argv[2]]"""%repr(Dict))
  38. file.close()
  39.  
  40. if __name__ == '__main__':
  41. FormatValue()

 

生成的Dict.py


  1. Dict = {'PSOldGeneration': {'capacity': '602406912', 'used': '101426704', 'percent': '16.8369090691974', 'free': '500980208'}, 'PSPermGeneration': {'capacity': '268435456', 'used': '71036880', 'percent': '26.463299989700317', 'free': '197398576'}}
  2.  
  3. if __name__ == '__main__':
  4. import sys
  5. print Dict[sys.argv[1]][sys.argv[2]]

 

相关Key如下:


  1. UserParameter=Tomcat.PSOldGeneration.capacity[*],/usr/bin/python /software/zabbix/scripts/Dict.py PSOldGeneration capacity
  2. UserParameter=Tomcat.PSOldGeneration.free[*],/usr/bin/python /software/zabbix/scripts/Dict.py PSOldGeneration free
  3. UserParameter=Tomcat.PSOldGeneration.used[*],/usr/bin/python /software/zabbix/scripts/Dict.py PSOldGeneration used
  4. UserParameter=Tomcat.PSOldGeneration.percent[*],/usr/bin/python /software/zabbix/scripts/Dict.py PSOldGeneration percent
  5. UserParameter=Tomcat.PSPermGeneration.capacity[*],/usr/bin/python /software/zabbix/scripts/Dict.py PSPermGeneration capacity
  6. UserParameter=Tomcat.PSPermGeneration.free[*],/usr/bin/python /software/zabbix/scripts/Dict.py PSPermGeneration free
  7. UserParameter=Tomcat.PSPermGeneration.used[*],/usr/bin/python /software/zabbix/scripts/Dict.py PSPermGeneration used
  8. UserParameter=Tomcat.PSPermGeneration.percent[*],/usr/bin/python /software/zabbix/scripts/Dict.py PSPermGeneration percent

 





(责任编辑:IT)
------分隔线----------------------------