zabbix监控多实例tomcat--不使用JMX

--第一次发帖,轻吐槽啊,


用zabbix监控tomcat,网上搜索大部分都是通过配置JMX端口来进行监控。这个方法对只有一个tomcat的情况支持的还是不错的。

但是在一台主机上有多个tomcat实例的情况下就有问题,问题就不多说了。

 

在ZABBIX中文社区扣扣群62239409,获取部分思路和文档。在此基础上,做了自动发现,而且稍作修改应该是可以是不单支持tomcat的。

方法如下:

首先,需要创建个文档test.txt,将服务和端口写进去。

tomcat|8080|
tomcat|8089|


然后,server_port.py来读取文档并输出json串(服务:端口)。

此脚本经过修改,可以输出多个参数。也加上简单的异常处理。注意修改文件路径。

#!/usr/bin/env python
import os
import json
import sys

f = open("/../test.txt", "r")
ports = []
for port in f.readlines():
        if not port.strip():continue        
        r = port.strip().split('|')
        if len(r)<3:continue
        ports +=[{'{#'+r[0].upper()+'PORT}':r[1],'{#'+r[0].upper()+'A}':r[2]}]
       # ports +=[{'{#'+r[0].upper()+'PORT}':r[1]},{'{#'+r[0]+'A}':r[2]}]
print json.dumps({'data':ports},sort_keys=False,indent=4,separators=(',',':'))

在tomcat中配置管理用户名和密码。此处开放的权限还是比较大的,生产环境建议做下处理。

$TOMCAT_HOME/conf/tomcat-users.xml

默认的<tomcat-users>是注释掉的,需要放开注释并添加。manager-gui权限太大,改为manager-status则只能查看状态页,而不能做manager操作,更加安全。

<del>  <role rolename="manager-gui"/>
  <role rolename="admin-gui"/>
  <user username="zabbix" password="zabbix" roles="manager-gui,admin-gui"/></del>

 <role rolename="manager-status"/>
 <user username="zabbix" password="zabbix" roles="manager-status"/>

 
最后,mon_tomcat.py进行监控,参数有两个:上一步获取的端口和具体的指标信息。注意修改tomcat配置的用户名和密码。
#!/usr/bin/python
import urllib2
import xml.dom.minidom
import sys
 
url = 'http://127.0.0.1:'<span style="color:#FF6666;">+sys.argv[1]+</span>'/manager/status?XML=true'
username = 'zabbix'
password = 'zabbix'
 
passman = urllib2.HTTPPasswordMgrWithDefaultRealm()
passman.add_password(None, url, username, password)
authhandler = urllib2.HTTPBasicAuthHandler(passman)
opener = urllib2.build_opener(authhandler)
urllib2.install_opener(opener)
pagehandle = urllib2.urlopen(url)
xmlData = pagehandle.read()
doc = xml.dom.minidom.parseString(xmlData) 
 
item =<span style="color:#FF6666;"> sys.argv[2]</span>
 
if item == "memory.free":
    print  doc.getElementsByTagName("memory")[0].getAttribute("free")
elif item == "memory.total":
    print  doc.getElementsByTagName("memory")[0].getAttribute("total")
elif item == "memory.max":
    print  doc.getElementsByTagName("memory")[0].getAttribute("max")
elif item == "threadInfo.maxThreads":
    print  doc.getElementsByTagName("threadInfo")[0].getAttribute("maxThreads")
elif item == "threadInfo.currentThreadCount":
    print  doc.getElementsByTagName("threadInfo")[0].getAttribute("currentThreadCount")
elif item == "threadInfo.currentThreadsBusy":
    print  doc.getElementsByTagName("threadInfo")[0].getAttribute("currentThreadsBusy")
elif item == "requestInfo.maxTime":
    print  doc.getElementsByTagName("requestInfo")[0].getAttribute("maxTime")
elif item == "requestInfo.processingTime":
    print  doc.getElementsByTagName("requestInfo")[0].getAttribute("processingTime")
elif item == "requestInfo.requestCount":
    print  doc.getElementsByTagName("requestInfo")[0].getAttribute("requestCount")
elif item == "requestInfo.errorCount":
    print  doc.getElementsByTagName("requestInfo")[0].getAttribute("errorCount")
elif item == "requestInfo.bytesReceived":
    print  doc.getElementsByTagName("requestInfo")[0].getAttribute("bytesReceived")
elif item == "requestInfo.bytesSent":
    print  doc.getElementsByTagName("requestInfo")[0].getAttribute("bytesSent")
else:
    print "unsupport item."


  自定义key是两条,一个是发现端口,一个是tomcat指标。脚本路径根据实际情况修改
UserParameter=server.discovery,/../server_port.py 
UserParameter=tomcat.status[*],/../mon_tomcat.py $1 $2

  
  模板改成自动发现的模板,还有将端口加进去。
 在此列一条吧:
<name>最大处理时间{#TOMCATPORT}</name>
<key>tomcat.status[{#TOMCATPORT},requestInfo.maxTime]</key>

  权限和路径要进行相应的设置和修改
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值