不知道是幹什麼的指令碼

ringoo_ming發表於2017-07-18

點選(此處)摺疊或開啟

  1. #!/usr/bin/env python

  2. import commands
  3. import sys
  4. import json

  5. def help():
  6.     print "help:"

  7. def tomcat_jstat(pro_name):
  8.     process_st=commands.getoutput("ps -ef|grep %s|grep -v get_java_jstat.py|grep -v grep>/dev/null;echo $?" % pro_name)
  9.     if process_st == '0':
  10.         PID=commands.getoutput("ps -ef|grep java|grep tomcat|grep -v get_java_jstat.py|grep %s|grep -v grep|awk '{print $2}'" %
  11.  pro_name)
  12.         jstat=commands.getoutput("ps -ef|grep java|grep tomcat|grep -v get_java_jstat.py|grep %s|awk '{print $8}'|grep -v grep|
  13. grep -v sh|awk -F'java$' '{print $1\"jstat\"}'" % pro_name).replace('\t','').replace('\n','').replace(' ','')
  14.         tomcat_user1=commands.getoutput("ps -ef|grep java|grep tomcat|grep -v get_java_jstat.py|grep %s|grep -v grep|awk '{prin
  15. t $1}'" % pro_name).strip()
  16.         tomcat_user=commands.getoutput("grep %s /etc/passwd|grep -v grep|awk -F\: '{print $1}'" % tomcat_user1).strip()
  17.         if PID and jstat and tomcat_user:
  18.             if tomcat_user == 'app':
  19.                 jstat_keys=commands.getoutput("%s -gcutil %s|head -n 1" % (jstat,PID)).split()
  20.                 jstat_values=commands.getoutput("%s -gcutil %s|tail -n 1" % (jstat,PID)).split()
  21.             else:
  22.                 jstat_keys=commands.getoutput("su - %s -c '%s -gcutil %s|head -n 1'" % (tomcat_user,jstat,PID)).split()
  23.                 jstat_values=commands.getoutput("su - %s -c '%s -gcutil %s|tail -n 1'" % (tomcat_user,jstat,PID)).split()
  24.             jstat_infos=dict(zip(jstat_keys,jstat_values))
  25.             return jstat_infos
  26.         else:
  27.             return 0
  28.     else:
  29.         return 0

  30. def get_tomcat_projects():
  31.     tomcat_users=commands.getoutput("ps -ef|grep java|grep tomcat|grep -v watchdog|grep -v javathreads|grep -v 'kill -9'|grep -
  32. v grep|awk '{print $1}'|sort|uniq").split()
  33.     tomcat_projects=[]
  34.     for tomcat_user in tomcat_users:
  35.         if tomcat_user == 'app':
  36.             tomcat_project=commands.getoutput("ps -ef|grep java|grep -v watchdog|grep -v javathreads|grep -v 'kill -9'|grep -v
  37. grep|awk -F'app' '{print $3}'|awk -F\/ '{print $2}'").split('\n')
  38.             tomcat_projects=tomcat_projects + tomcat_project
  39.         else:
  40.             tomcat_project=commands.getoutput("ps -ef|grep java|grep -v watchdog|grep -v javathreads|grep -v 'kill -9'|grep -v
  41. grep|awk -F'app' '{print $2}'|awk -F\/ '{print $2}'").split('\n')
  42.             tomcat_projects=tomcat_projects + tomcat_project
  43.     tomcat_projects=list(set(tomcat_projects))
  44.     while '' in tomcat_projects:
  45.         tomcat_projects.remove('')
  46.     if len(tomcat_projects) != 0:
  47.         return tomcat_projects
  48.     else:
  49.         return 0

  50. if __name__ == "__main__":
  51.     #pro_name=sys.argv[1]
  52.     pro_names=get_tomcat_projects()
  53.     if len(pro_names) != 0:
  54.         #print pro_names
  55.         for pro_name in pro_names:
  56.             #print pro_name
  57.             jstat_file="/opt/zabbix/script/jstat_%s.txt" % pro_name
  58.             with open(jstat_file,"w") as f:
  59.                 json.dump(tomcat_jstat(pro_name),f)

來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/31442725/viewspace-2142229/,如需轉載,請註明出處,否則將追究法律責任。

相關文章