source: trunk/examples/topjob @ 280

Last change on this file since 280 was 280, checked in by willem, 12 years ago

wv add topjob

  • Property svn:executable set to *
File size: 2.4 KB
Line 
1#!/usr/bin/env python
2
3# topjob, gojob jobnr [nodenr]
4#               jobnr: the number of the job
5#               nodenr: the rank of the node in the job
6# depending on the name with this script is called it performs the
7# following:
8
9# called as topjob:
10#       shows the output of top -u user on the node
11#       - one cycle of top
12#       - user: the user the job belongs to
13#
14# called as gojob:
15#       logs in to the node as the user who invokes this script
16#            (os.getenv('USER'))
17#
18#  Willem Vermin, SARA, April 2012
19#
20from PBSQuery import PBSQuery
21import sys,os
22def uniq(seq, idfun=None): 
23  # http://www.peterbe.com/plog/uniqifiers-benchmark
24   # order preserving
25   if idfun is None:
26       def idfun(x): return x
27   seen = {}
28   result = []
29   for item in seq:
30       marker = idfun(item)
31       if marker in seen: continue
32       seen[marker] = 1
33       result.append(item)
34   return result
35
36def usage(a):
37  if a == 'gojob':
38    print a,'logs you in to a node where a job is running'
39  if a == 'topjob':
40    print a,'shows the system usage of a node where a job is running'
41   
42  print 'Usage:'
43  print a,'jobnumber [nodenumber]'
44  print 'where jobnumber is the number of the job'
45  print '      nodenumber is the rank number of the node allocated to the job'
46  print '      (default 0)'
47 
48me = sys.argv[0].split('/')[-1]
49print '['+me+']'
50p = PBSQuery()
51seq = list(p.get_serverinfo())[0]
52
53try:
54  j=sys.argv[1]
55except:
56  usage(me)
57  sys.exit(1)
58
59# check if numerical jobnumber. (Be aware: dependecy jobs have a '-')
60try:
61  nj = int(j.split('.')[0].split('-')[0]) 
62except:
63  usage(me)
64  sys.exit(1)
65
66if len(sys.argv) > 2:
67  try:
68    num = int(sys.argv[2])
69  except:
70    usage(me)
71    sys.exit(1)
72else:
73  num = 0
74
75job = p.getjob(j)
76
77try:
78  h = job['exec_host'][0]
79except:
80  print 'No such job:',j
81  sys.exit(1)
82
83hh = h.split('+')
84nodes=[]
85for h in hh:
86  nodes = nodes + [ h.split('/')[0]]
87
88nodes = uniq(nodes)
89print 'Job',j,'is running on',len(nodes),'nodes:'
90i=0
91for h in nodes:
92  print h,
93  i = i+1
94  if i > 7:
95    i=0
96    print
97if i != 0:
98  print
99
100if num >= len(nodes):
101  print 'No node number',num
102  sys.exit(1)
103
104if me == 'topjob':
105  user=job['Job_Owner'][0].split('@')[0]
106  print 'top for node #',num,':',nodes[num],'user:',user
107  sys.stdout.flush()
108  os.system('ssh '+nodes[num]+' top -n1 -b -u ' + user)
109
110if me == 'gojob':
111  user = os.getenv('USER')
112  print 'logging in to node #',num,':',nodes[num],'user:',user
113  sys.stdout.flush()
114  os.system('ssh -X '+nodes[num])
115
Note: See TracBrowser for help on using the repository browser.