1.proc.py
#/usr/bin/python
import subprocess
import os
import socket
import json
import glob
import time
import datetime
javas=[]
java_dir_proc=‘xx‘
now = datetime.datetime.now()
strdatetime = now.strftime("%Y/%m/%d")
java_dir_home=[‘ip1‘,‘ip2‘]
def get_exception(java_dir):
a = os.listdir(java_dir)
for x in a:
#javapath = os.path.join(java_dir, x)
#if os.path.isdir(javapath):
if len(x) != 0:
javas.append({‘{#EXCEPTION}‘:x.strip(‘\n‘)})
for i in java_dir_home:
java_dir=java_dir_proc + i + ‘/‘ + strdatetime
get_exception(java_dir)
news_javas = []
for id in javas:
if id not in news_javas:
news_javas.append(id)
print json.dumps({‘data‘:news_javas},indent=4,separators=(‘,‘,‘:‘))
2.proc_num.py
#!/usr/bin/python
import sys
import subprocess
import os
import socket
import json
import glob
import time
import datetime
zdict={}
zd=[]
javas=[]
java_dir_proc=‘xx‘
now = datetime.datetime.now()
strdatetime = now.strftime("%Y/%m/%d")
java_dir_home=[‘ip1‘,‘ip2‘]
def get_exception(javapath):
if os.path.isdir(javapath):
#CMD="cd javapath && ls|wc -l"
os.chdir(javapath)
CMD=‘ls|wc -l‘
t=subprocess.Popen(CMD,shell=True,stdout=subprocess.PIPE,env=None)
for java in t.stdout.readlines():
if len(java) != 0:
S=java.strip(‘\n‘).strip(‘:‘)
return S
if len(sys.argv) == 2:
argv1 = sys.argv[1]
for i in java_dir_home:
javapath=java_dir_proc + i + ‘/‘ + strdatetime + ‘/‘ + argv1
zdict[i]=get_exception(javapath)
#print zdict[i]
zd.append(int(zdict[i]))
print reduce(lambda x,y:x+y,zd)
else:
print -1
3.cat check_proc_exception.conf
UserParameter=proc.discovery,/usr/bin/python /usr/local/zabbix/bin/proc.py
UserParameter=proc.num[*],/usr/bin/python /usr/local/zabbix/bin/procnum.py $1
4.建立模板
5.其实还可以写的更好
比如
UserParameter=proc.num[*],/usr/bin/python /usr/local/zabbix/bin/procnum.py $1 $2
对应于proc.num[{#EXCEPTION},$2]
写的差,有几个地方要优化下。比如+,怎么连接字符串。
6.图像