CMS 3D CMS Logo

getRunAppsInfo.py
Go to the documentation of this file.
1 #!/usr/bin/env python
2 """This module provides the PID and the log file name of the running DQM
3 applications (consumers), thus completing the information generated by
4 ExtractAppInfoFromXML.
5 
6 When used as a script the following options are accepted:
7  -f Show all columns
8  -h show headers
9 """
10 import sys
11 import os.path
12 import getopt as gop
13 import ExtractAppInfoFromXML as appinf
14 
15 # ssh srv-c2d05-18.cms ps -eo pid,cmd | grep 22101
16 ################################################################################
17 def getAppPID(srv,port):
18  try:
19  print "Connecting to server: "+srv+" and fetching PID for application running on port: "+port
20  cf=os.popen('ssh '+srv+' ps -eo pid,cmd | grep '+port)
21  l=cf.readline();
22  if l=="":
23  cf.close()
24  return "App Not Running"
25  else:
26  pidv=l.split()
27  cf.close();
28  return pidv[0]
29  except:
30  sys.stderr.write( "Something went really bad\n" )
31  return -1
32 ################################################################################
33 def getAppLogFileName(srv,pid):
34 # try:
35  #pid=getAppPID(srv,port)
36  if pid=="App Not Running":
37  return "No active log file"
38  else:
39  print "Connecting to server: "+srv+" and fetching LogFile for application with PID: "+pid
40  cf=os.popen('ssh '+srv+' ls -l /tmp | grep '+pid)
41  l=cf.readline();
42  if l=="":
43  cf.close()
44  return "App Not Running???"
45  else:
46  logfilev=l.split()
47  cf.close();
48  return logfilev[-1]
49 # except Exception,e:
50 # sys.stderr.write( "Something went really bad\n" + e[0])
51 # return -1
52 ################################################################################
53 def getRunningAppsInfo(filename):
54  (table,grid)=appinf.getAppInfo(filename,2,1,3,4)
55  for apps in grid:
56  apps.insert(3,getAppPID(apps[1],apps[2]))
57  apps.insert(4,getAppLogFileName(apps[1],apps[3]))
58  return grid
59 ################################################################################
60 #Script operation #
61 ################################################################################
62 if __name__ == "__main__":
63  fullinfo=False
64  headers=False
65  try:
66  (args,filename)=gop.getopt(sys.argv[1:],"hf",["help"])
67  except getopt.GetoptError:
68  sys.stderr.write( "Sintax Error unrecognised option" )
69  sys.stderr.write( __doc__ )
70  sys.exit(2)
71 
72  for item in args:
73  if item[0]=="-h":
74  headers=True
75  elif item[0]=="-f":
76  fullinfo=True
77  elif item[0]=="--help":
78  sys.stdout.write(__doc__)
79  sys.exit(2)
80  if len(filename)==0:
81  sys.stderr.write( "\nERROR: xdaq XML config file name not present, please specify\n\n" )
82  sys.stdout.write(__doc__)
83  elif len(filename) > 1:
84  sys.stderr.write( "\nERROR: Too many file names or other arguments, please specify only 1\n\n" )
85  sys.stdout.write(__doc__)
86  sys.exit(2)
87  elif not os.path.exists(filename[0]):
88  sys.stderr.write( "\nERROR: xdaq XML config file does not exist please verify\n\n" )
89  sys.stdout.write(__doc__)
90  sys.exit(2)
91  grid=getRunningAppsInfo(filename[0])
92  if fullinfo:
93  if headers:
94  grid.insert(0,["Application","Server","Port","PID","LogfileName","App Config File"])
95  else:
96  if headers:
97  i=0;
98  for record in grid:
99  newrecord=[record[0],record[3],record[4]]
100  grid[i]=newrecord
101  del record
102  i+=1
103  grid.insert(0,["Application","PID","LogfileName"])
104 
105  else:
106  i=0;
107  for record in grid:
108  newrecord=[record[0],record[3],record[4]]
109  grid[i]=newrecord
110  del record
111  i+=1
112 
113  appinf.printGrid(grid)
114 
115 
def getAppLogFileName(srv, pid)
def getAppPID(srv, port)
def getRunningAppsInfo(filename)