使用了JMX监控tomcat,出现各种问题,主要是和监控环境有关,一台服务器上多个tomcat项目,网上搜到一篇不错的监控
如果上两步结果没有通过,请检查脚本和或者zabbix_agent服务。
http://blog.youkuaiyun.com/nisan892541/article/details/47727967
环境:Centos6.9 ,192.168.199.111
监控的Tomcat项目:tomcat-web,tomcat-app
安装zabbix-agent之部分就不多说了
首先配置tomcat项目的status监控页面,关于这个页面的配置,百度搜索
到/etc/zabbix创建一个文档,将项目和端口写进去,并授权为zabbix权限
# cd /etc/zabbix/
# vim test.txt
tomcat|8080|tomcat-web
tomcat|8081|tomcat-app
# chown -R zabbix.zabbix /etc/zabbix/test/txt
继续在
/etc/zabbix下创建python文件server_port.py活的项目端口和项目名称(仅支持python2)# vim server_port.py
#!/usr/bin/env python
import os
import json
import sys
f = open("/etc/zabbix/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]}]
print json.dumps({'data':ports},sort_keys=False,indent=4,separators=(',',':'))
执行项目结果:
# python server_port.py
{
"data":[
{
"{#TOMCATPORT}":"8080",
"{#TOMCATA}":"tomcat-web"
},
{
"{#TOMCATPORT}":"8081",
"{#TOMCATA}":"tomcat-app"
}
]
}
继续在
/etc/zabbix下创建两个python文件# vim mon_tomcat.py
#!/usr/bin/python
import urllib2
import xml.dom.minidom
import sys
url = 'http://192.168.199.111:'+sys.argv[1]+'/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 =sys.argv[2]
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."
url的IP根据实际情况写,?XML=true是将页面格式化为xml格式,username和password是登陆状态页面的账号和密码同时授权以上两个python文件执行权限
# chmod +x mon_tomcat.py server_port.py
# ls -l mon_tomcat.py server_port.py
-rwxr-xr-x. 1 root root 2069 Jan 8 11:18 mon_tomcat.py
-rwxr-xr-x. 1 root root 502 Jan 8 11:23 server_port.py
在/etc/zabbix/zabbix_agentd.d目录下创建agentd服务监控tomcat配置文件
# cat userparameter_tomcat.conf
UserParameter=server.discovery,/etc/zabbix/server_port.py
UserParameter=tomcat.status[*],/etc/zabbix/mon_tomcat.py $1 $2 $3
之后重启tomcat上的zabbix_agent服务
# service zabbix-agent restart
接下来执行在被监控段和监控的测试一下脚本和服务
测试1:在TOMCAT服务器执行mon_tomcat.py文件,查看是否能得到结果
# python mon_tomcat.py 8080 memory.free tomcat-web
1367364464
测试2:在zabbix_server端使用zabbix_get 查看是否得到结果
# zabbix_get -s 192.168.199.111 -k "server.discovery"
{
"data":[
{
"{#TOMCATPORT}":"8080",
"{#TOMCATA}":"tomcat-web"
},
{
"{#TOMCATPORT}":"8081",
"{#TOMCATA}":"tomcat-app"
}
]
}
# zabbix_get -s 192.168.199.111 -k "tomcat.status[8080,memory.free,tomcat-web]"
1351970800
server.discovery这个监控结果获得的json格式的数据,将端口和项目名称对应,分别传递不同的参数,这写参数将在下面创建模板的时候会用到
如果上两步结果没有通过,请检查脚本和或者zabbix_agent服务。
接下来可以开始配置监控:使用自发现模板
- 在配置->模板->创建模板
- 配置->模板 中找到这个模板,进入自发现规则,创建发现规则
- 在自发现规则中->监控项原型,创建监控项原型
键值就是前面使用zabbix_get命令-k中的值
这个值中有三个参数,分别为自发现清单键值获得的结果:zabbix_get -s 192.168.199.111 -k "server.discovery"
名称:$3代表键值中的第三个参数,也就是项目名称
单位:,memory.free获取到的结果是以字节大小的,所以需要加上单位B
创建后的结果
- 继续在自发现清单下创建图像原型
图像名称可以使用第三个参数{#TOMCATA}区分,
监控项选择对应的监控项目原型
之后在监控主机中添加这个自发现模板
感谢zabbix监控多实例tomcat--不使用JMX提供的资料和脚本