点击(此处)折叠或打开
-
#!/usr/bin/env python
-
-
import commands
-
import sys
-
import json
-
-
def help():
-
print "help:"
-
-
def tomcat_jstat(pro_name):
-
process_st=commands.getoutput("ps -ef|grep %s|grep -v get_java_jstat.py|grep -v grep>/dev/null;echo $?" % pro_name)
-
if process_st == '0':
-
PID=commands.getoutput("ps -ef|grep java|grep tomcat|grep -v get_java_jstat.py|grep %s|grep -v grep|awk '{print $2}'" %
-
pro_name)
-
jstat=commands.getoutput("ps -ef|grep java|grep tomcat|grep -v get_java_jstat.py|grep %s|awk '{print $8}'|grep -v grep|
-
grep -v sh|awk -F'java$' '{print $1\"jstat\"}'" % pro_name).replace('\t','').replace('\n','').replace(' ','')
-
tomcat_user1=commands.getoutput("ps -ef|grep java|grep tomcat|grep -v get_java_jstat.py|grep %s|grep -v grep|awk '{prin
-
t $1}'" % pro_name).strip()
-
tomcat_user=commands.getoutput("grep %s /etc/passwd|grep -v grep|awk -F\: '{print $1}'" % tomcat_user1).strip()
-
if PID and jstat and tomcat_user:
-
if tomcat_user == 'app':
-
jstat_keys=commands.getoutput("%s -gcutil %s|head -n 1" % (jstat,PID)).split()
-
jstat_values=commands.getoutput("%s -gcutil %s|tail -n 1" % (jstat,PID)).split()
-
else:
-
jstat_keys=commands.getoutput("su - %s -c '%s -gcutil %s|head -n 1'" % (tomcat_user,jstat,PID)).split()
-
jstat_values=commands.getoutput("su - %s -c '%s -gcutil %s|tail -n 1'" % (tomcat_user,jstat,PID)).split()
-
jstat_infos=dict(zip(jstat_keys,jstat_values))
-
return jstat_infos
-
else:
-
return 0
-
else:
-
return 0
-
-
def get_tomcat_projects():
-
tomcat_users=commands.getoutput("ps -ef|grep java|grep tomcat|grep -v watchdog|grep -v javathreads|grep -v 'kill -9'|grep -
-
v grep|awk '{print $1}'|sort|uniq").split()
-
tomcat_projects=[]
-
for tomcat_user in tomcat_users:
-
if tomcat_user == 'app':
-
tomcat_project=commands.getoutput("ps -ef|grep java|grep -v watchdog|grep -v javathreads|grep -v 'kill -9'|grep -v
-
grep|awk -F'app' '{print $3}'|awk -F\/ '{print $2}'").split('\n')
-
tomcat_projects=tomcat_projects + tomcat_project
-
else:
-
tomcat_project=commands.getoutput("ps -ef|grep java|grep -v watchdog|grep -v javathreads|grep -v 'kill -9'|grep -v
-
grep|awk -F'app' '{print $2}'|awk -F\/ '{print $2}'").split('\n')
-
tomcat_projects=tomcat_projects + tomcat_project
-
tomcat_projects=list(set(tomcat_projects))
-
while '' in tomcat_projects:
-
tomcat_projects.remove('')
-
if len(tomcat_projects) != 0:
-
return tomcat_projects
-
else:
-
return 0
-
-
if __name__ == "__main__":
-
#pro_name=sys.argv[1]
-
pro_names=get_tomcat_projects()
-
if len(pro_names) != 0:
-
#print pro_names
-
for pro_name in pro_names:
-
#print pro_name
-
jstat_file="/opt/zabbix/script/jstat_%s.txt" % pro_name
-
with open(jstat_file,"w") as f:
- json.dump(tomcat_jstat(pro_name),f)
来自 “ ITPUB博客 ” ,链接:http://blog.itpub.net/31442725/viewspace-2142229/,如需转载,请注明出处,否则将追究法律责任。
转载于:http://blog.itpub.net/31442725/viewspace-2142229/