source: trunk/examples/pbsmon.py @ 329

Last change on this file since 329 was 242, checked in by bas, 14 years ago

applied a patch for pbsmon.py

  • Property svn:keywords set to Id
File size: 3.2 KB
Line 
1#!/usr/bin/env python
2#
3# Author: Bas van der Vlies <basv@sara.nl>
4# Date  : 17 Aug 2001
5# Desc. : This script displays the status of the PBS batch.
6#         It display the status of a node. The ideas are
7#         based on the awk-script of Willem Vermin
8#
9# CVS info:
10# $Id: pbsmon.py 242 2010-07-08 12:57:08Z bas $
11#
12import sys
13import string
14import re
15
16import pbs
17
18pbs_ND_free_and_job = 'not_all_procs_used'
19
20translate_state = {
21    pbs.ND_free             : '_',
22    pbs.ND_down             : 'X',
23    pbs.ND_offline          : '.',
24    pbs.ND_reserve          : 'R',
25    pbs.ND_job_exclusive    : 'J',
26    pbs.ND_job_sharing      : 'S',
27    pbs.ND_busy             : '*',
28    pbs.ND_state_unknown    : '?',   
29    pbs.ND_timeshared       : 'T',
30    pbs.ND_cluster          : 'C',
31    pbs_ND_free_and_job     : 'j'
32}
33
34
35
36def display_cluster_status(nl, sl):
37
38  # Thanks to Daniel Olson, we have now code that can handle
39  # 2 and 3 digit hostname numbers
40  #
41  if len(nl) == 1: 
42    width = len( nl[0] )
43  else:
44    width = len( nl[-1] )
45
46  # Determine what format we have to use
47  #
48  if width == 3:
49    step = end = 19
50    format = '%3s'
51  else:
52    step  = end = 25
53    format = '%2s'
54
55  start = 0 
56
57  items = len(nl)
58
59  # Sanity check
60  if end > items:
61    end = items
62
63  while start < items:
64
65    print ' ',
66    for j in range(start,end):
67      print format %(nl[j]) ,
68
69    print '\n ',
70    for j in range(start,end):
71      print format %(sl[j]) ,
72   
73    print '\n'
74
75    start = end
76    end = end + step
77
78    if end > items:
79      end = items
80   
81  # Now some statistics
82  #
83  n = 0
84  for key in translate_state.keys():
85    value = translate_state[key]
86    print "%3s %-21s : %d\t |" %( value, key, sl.count(value) ),
87    if n%2:
88      print ''
89    n = n + 1
90
91def main():
92  state_list = []
93  node_list  = []
94  node_nr    = 0
95
96  if len(sys.argv) > 1:
97    pbs_server = sys.argv[1]
98  else:
99    pbs_server = pbs.pbs_default()
100    if not pbs_server:
101      print "No default pbs server, usage: pbsmon [server] "
102      sys.exit(1)
103
104  con = pbs.pbs_connect(pbs_server)
105  if con < 0:
106     errno, text = pbs.error()
107     print errno, text
108     sys.exit(1)
109
110  # We are only interested in the state and jobs of a node
111  #
112  attrl = pbs.new_attrl(2);
113  attrl[0].name='state'
114  attrl[1].name = 'jobs'
115
116
117  nodes = pbs.pbs_statnode(con, "", attrl, "NULL")
118
119  # Some is het None dan weer NULL, beats me
120  #
121  for node in nodes:
122
123    # display_node_status(batch_info)
124    node_attr = node.attribs
125
126    # A node can have serveral states, huh. We are only
127    # interested in first entry.
128    #
129    temp = string.splitfields(node_attr[0].value, ',')
130    state = temp[0]
131
132    # look if on a free node a job is scheduled, then mark it
133    # as other state
134    #
135    if state == pbs.ND_free:
136       if len([x for x in node_attr if x.name == 'jobs']):
137          state_list.append(translate_state[pbs_ND_free_and_job])
138       else:
139          state_list.append(translate_state[state])
140    else:
141          state_list.append(translate_state[state])
142               
143               
144    re_host = re.compile(r"""
145
146      (?P<name>\d+)
147
148      """, re.VERBOSE)
149
150    result = re_host.search(node.name)
151    if result:
152      node_list.append( result.group('name') )
153    else:
154      node_nr = node_nr + 1
155      node_list.append( str(node_nr) )
156
157  display_cluster_status(node_list, state_list)
158
159main()
Note: See TracBrowser for help on using the repository browser.