一 应用场景描述
最近线上业务的Tomcat总是崩溃停止服务。使用jstat命令查看Java进程的JVM内存信息。但是使用命令jstat只能查看一段时间的数据,不能总是盯着屏幕手动查看,于是想到了将jstat的数据通过zabbix进行绘图展现并报警。同时,一台服务器上可能会有多个Java程序在运行,包括不同的Tomcat,或者是其他Java应用,例如Logstash,Elasticsearch等。这就需要用到Zabbix的Low Level Discovery功能。
二 编写监控脚本
在编写脚本之前需要了解jstat命令的使用,详细使用方法可以参考官方文档或者man手册。还有需要了解JVM虚拟机的内存管理和垃圾回收机制。知道什么是Eden Space,S0 Space,S1 Space和Old Space以及Perm Space等
![23319dc4d26778e90d2457976fdfa2cf.png](https://i-blog.csdnimg.cn/blog_migrate/d05ff6ed8cd24fac98042ca0db63b8c4.jpeg)
关于Java内存管理和垃圾回收的理论知识可以参考文章
http://www.journaldev.com/2856/java-jvm-memory-model-and-garbage-collection-monitoring-tuning
$ sudo /opt/app/jdk/bin/jstat -gc 30166 S0C S1C S0U S1U EC EU OC OU PC PU YGC YGCT FGC FGCT GCT 195904.0 195904.0 19609.5 0.0 1567680.0 561577.4 13769152.0 2668191.5 42944.0 42827.3 8714 345.808 0 0.000 345.808
$ sudo /opt/app/jdk/bin/jstat -gcutil 30166 S0 S1 E O P YGC YGCT FGC FGCT GCT 10.01 0.00 90.27 19.38 99.73 8714 345.808 0 0.000 345.808$ sudo /opt/app/jdk/bin/jstat -gccapacity 30166 NGCMN NGCMX NGC S0C S1C EC OGCMN OGCMX OGC OC PGCMN PGCMX PGC PC YGC FGC 1959488.0 1959488.0 1959488.0 195904.0 195904.0 1567680.0 13769152.0 13769152.0 13769152.0 13769152.0 21248.0 169984.0 42944.0 42944.0 8717 0
-gc 和 -gccapacity显示的大小是KB
显示两行信息
如果通过zabbix agent的方式获取不同字段的值
可以这样
sudo /opt/app/jdk/bin/jstat -gcutil 30166|awk '{print $4}'|grep -E '^[0-9]'
如果使用zabbix sender的方式发送数据,可以将显示的结果生成一个python字典
{'S0':7.43,'S1':0.00,'E':86.62',.................}
将这个字典中的数据全部通过zabbix_sender发送到zabbix_proxy或者zabbix_server,需要注意的是zabbix_sender指定的主机名需要和zabbix页面配置的主机名相同
Java服务发现脚本
java_discovery.py
脚本中的发现规则根据情况自己定义,这里定义三个规则,/opt/app/下面所有tomcat-开头的tomcat服务,/opt/logs/logstash/和/opt/app/elasticsearch目录下的规则
#/usr/bin/python#This script is used to discovery disk on the serverimport subprocessimport osimport socketimport jsonimport globjava_names_file='java_names.txt'javas=[]if os.path.isfile(java_names_file):# print 'java_names_file exists!'########## here should use % (java_names_file) instead of using the python variable java_names_file directly inside the ''' ''' quotes##### args='''awk -F':' '{print $1':'$2}' %s''' % (java_names_file) t=subprocess.Popen(args,shell=True,stdout=subprocess.PIPE).communicate()[0]elif glob.glob('/opt/app/tomcat-*') and not os.path.isdir('/opt/logs/logstash') and not os.path.isdir('/opt/app/elasticsearch/config'): t=subprocess.Popen('ls /opt/app/tomcat-*|grep tomcat_',shell=True,stdout=subprocess.PIPE)elif not glob.glob('/opt/app/tomcat-*') and os.path.isdir('/opt/logs/logstash') and not os.path.isdir('/opt/app/elasticsearch/config'): t=subprocess.Popen(''' ls /opt/logs/logstash/|grep -E 'logstash_'|sed 's/.log//g' ''',shell=True,stdout=subprocess.PIPE)elif not glob.glob('/opt/app/tomcat-*') and not os.path.isdir('/opt/logs/logstash') and os.path.isdir('/opt/app/elasticsearch/config'): t=subprocess.Popen(''' ls /opt/app/elasticsearch/config|grep -E 'elasticsearch.yml'| sed 's/.yml//g' ''',shell=True,stdout=subprocess.PIPE)elif not glob.glob('/opt/app/tomcat-*') and os.path.isdir('/opt/logs/logstash') and os.path.isdir('/opt/app/elasticsearch/config'): t=subprocess.Popen(''' ls /opt/logs/logstash /opt/app/elasticsearch/config|grep -E 'logstash_|elasticsearch.yml'| sed 's/.log|.yml//g' ''',shell=True,stdout=subprocess.PIPE)elif glob.glob('/opt/app/tomcat-*') and os.path.isdir('/opt/logs/logstash') and not os.path.isdir('/opt/app/elasticsearch/config'): t=subprocess.Popen('''ls /opt/app/tomcat-* /opt/logs/logstash/|grep -E 'tomcat_|logstash_'|sed 's/.log//g' ''',shell=True,stdout=subprocess.PIPE)elif glob.glob('/opt/app/tomcat-*') and os.path.isdir('/opt/logs/logstash') and os.path.isdir('/opt/app/elasticsearch/config'): t=subprocess.Popen('''ls /opt/app/tomcat-* /opt/logs/logstash/ /opt/app/elasticsearch/config|grep -E 'tomcat_|logstash_|elasticsearch.yml'| sed 's/.log|.yml//g' ''',shell=True,stdout=subprocess.PIPE)for java in t.stdout.readlines(): if len(java) != 0: javas.append({'{#JAVA_NAME}':java.strip('')})print json.dumps({'data':javas},indent=4,separators=(',',':'))
执行结果:
$ python java_discovery.py { "data":[ { "{#JAVA_NAME}":"elasticsearch" }, { "{#JAVA_NAME}":"logstash_central" }, { "{#JAVA_NAME}":"logstash_shipper" } ]}
在模板中的items,graphs,triggers中使用{#JAVA_NAME}这个宏变量
jstat状态信息获取脚本
jstat_status.py
脚本根据https://github.com/gcharot/Zabbix/tree/master/zjstat 这里的代码进行改编
脚本中主要做以下事情:
找到指定Java服务的pid,然后执行jstat命令,然后执行zabbix_sender命令
#!/usr/bin/pythonimport subprocessimport sysimport os__maintainer__ = "John Wang"jps = '/opt/app/jdk/bin/jps'jstat = '/opt/app/jdk/bin/jstat'zabbix_sender = "/opt/app/zabbix/sbin/zabbix_sender"zabbix_conf = "/opt/app/zabbix/conf/zabbix_agentd.conf"send_to_zabbix = 1#"{#JAVA_NAME}":"tomcat_web_1"def usage():"""Display program usage"""print "Usage :