#!/usr/bin/env python
#encoding:utf-8
import os
import sys
import time
import subprocess


class myclass:
      print ''' \033[32;1m
###########################################################################
JAVA module:                                                              #
  feinnoopen_01 media_ims_01 mop_ims_download_01 osp_ims_01 ippbx_ims_01  #
  mop_ims_01 mop_ims_download_01 msms_gw_01 portal_ims_01 sso_ims_01      #
                                                                          #
C module:                                                                 #
  ap cagw dbproxy egm ft gd gm im logserver msrp msrpgw ps robot sbc      #
  soap soap_proxy ss stg                                                  #
########################################################################### \033[0m
         '''
      def __init__(self):
          self.name=sys.argv[1]
          self.file=sys.argv[0]
          self.modstat = "ps -ef|grep -v grep|grep -v %s|grep %s|awk '{print $2}'" %(self.file,self.name)
          self.modname="kill -9 `ps -ef|grep -v grep|grep -v %s|grep %s|awk '{print $2}'`" %(self.file,self.name)
          self.start="/EUT/internal/webapp/%s/bin/startup.sh;tail -f /EUT/internal/webapp/%s/logs/catalina.out" %(self.name,self.name)    
          self.cache="rm -rf /EUT/internal/webapp/%s/work/Catalina/*" %(self.name)    
          self.cmodstat = "ps -ef|grep -Ev 'grep|supervise'|grep -v %s|grep -w %s_01|awk '{print $2}'" %(self.file,self.name)
          self.cmodname="kill -9 `ps -ef|grep -Ev 'grep|supervise'|grep -v %s|grep -w %s_01|awk '{print $2}'`" %(self.file,self.name)
          self.cstart="cd /EUT/internal/pgm/%s/%s_01;nohup ./bin/%s_01 &" %(self.name,self.name,self.name)
          self.jlist=["feinnoopen_01", "media_ims_01", "mop_ims_download_01", "osp_ims_01", "ippbx_ims_01", "mop_ims_01", "mop_ims_download_01", "msms_gw_01", "portal_ims_01", "sso_ims_01"]
          self.clist=["ap", "cagw", "dbproxy", "egm", "ft", "gd", "gm", "im", "logserver", "msrp", "msrpgw", "ps", "robot", "sbc", "soap", "soap_proxy", "ss", "stg"]
          self.pid="ps -ef|grep -Ev 'grep|supervise'|grep -v %s|grep -w %s_01" %(self.file,self.name)  
          self.jpid="ps -ef|grep -v grep|grep -v %s|grep %s" %(self.file,self.name)


      def stat(self):
          return os.popen(self.modstat).readlines()

      def run(self):
          subprocess.call(self.start, shell=True)
   
      def clear(self):
          subprocess.call(self.cache, shell=True)
      
      def kill(self):
          subprocess.call(self.modname, shell=True)           
      
      def cstat(self):
          return os.popen(self.cmodstat).readlines()
   
      def crun(self):
          subprocess.call(self.cstart, shell=True)

      def ckill(self):
          subprocess.call(self.cmodname, shell=True) 
    


if __name__=="__main__" :
  
    
  try:
     p=myclass()
     
     #print p.clist
     if p.name in p.jlist:
        print "============="
        print p.name 
        print "============="
        result=p.stat()

        if len(result) == 0:
            print "\033[32;1m%s模块没有启动 \033[0m" %p.name
            print "\033[32;1m开始清缓存 \033[0m"
            p.clear()
            time.sleep(2)
            print "\033[32;1m启动模块%s \033[0m" %p.name
            p.run()     
        else:
            print "\033[32;1m停止%s模块  \033[0m" %p.name
            p.kill()
            print "\033[32;1m开始清缓存  \033[0m"
            p.clear()
            time.sleep(2) 
            print "\033[32;1m启动%s模块 \033[0m" %p.name
            p.run()         

     elif p.name in p.clist:
        print "============"
        print p.name
        print "============"
        cresult=p.cstat()
       
        if len(cresult) == 0:
           print "\033[32;1m%s模块没有启动 \033[0m" %p.name
           p.crun()     
        else:
           print "\033[32;1m%s模块进程存在 \033[0m" %p.name
           #time.sleep(1)
           print "\033[32;1m杀死进程%s \033[0m" %p.name
           p.ckill()
          # time.sleep(2)
           print "\033[32;1m开始启动进程%s \033[0m" %p.name
           p.crun() 
           time.sleep(1)
           print "\033[32;1m进程%s已启动,列出进程ID \033[0m" %p.name
           os.system(p.pid) 
     else:
        print "\033[31;1m%s 不存在,兄弟请参照列表输入模块名字 \033[0m" %p.name
          

  
  except KeyboardInterrupt:
     print "\033[31;1m退出日志监听!!!\033[0m"

     print "\033[32;1m列出%s进程:\033[0m" %p.name
     os.system(p.jpid)  
   
  except:
     print "\033[31;1m请输入你的模块: \033[0m"