#!/usr/bin/python
#-conding:utf-8--#
#-------------------------------------------------------------------------------
# Name: Jmap.py
#
# Author: LiuSha
#
# Created: 9/29/2014
# Copyright: (c) WDZJ-SA 2014
#-------------------------------------------------------------------------------
import subprocess
import re
import os
def runCommand(cmd,stdinstr = ''):
p=subprocess.Popen(cmd, shell=True, universal_newlines=True, stdin=subprocess.PIPE,stdout=subprocess.PIPE, stderr=subprocess.STDOUT)
stdoutdata, stderrdata = p.communicate(stdinstr)
return p.returncode, stdoutdata, stderrdata
def FormatValue():
Dict = {'PSOldGeneration':{'percent':''},'PSPermGeneration':{'percent':''}}
_Output = runCommand("""jmap -heap `ps aux | grep "/home/tomcat/tomcat/apache-tomcat-7.0.53/conf/logging.properties" | awk '!/grep/{print $2}'`""")[1].replace(' ','').split('\n')
_NewOpt = [x for x in _Output if x]
for i in _NewOpt[-10:-7]:
Dict['PSOldGeneration'][i.split('=')[0]] = re.findall('\d+',i.split('=')[1])[0]
Dict['PSOldGeneration']['percent'] = re.findall('\d+\D+\d+',_NewOpt[-7])[0]
for i in _NewOpt[-5:-2]:
Dict['PSPermGeneration'][i.split('=')[0]] = re.findall('\d+',i.split('=')[1])[0]
Dict['PSPermGeneration']['percent'] = re.findall('\d+\D+\d+',_NewOpt[-2])[0]
file = open('%s/Dict.py'%os.path.dirname(__file__),'w')
file.write("""Dict = %s\n\nif __name__ == '__main__':
import sys
print Dict[sys.argv[1]][sys.argv[2]]"""%repr(Dict))
file.close()
if __name__ == '__main__':
FormatValue()