source: trunk/pbs_swig/distro/src/PBSQuery.py @ 99

Last change on this file since 99 was 99, checked in by bas, 18 years ago

distro/src/PBSQuery.py:

  • Can now handle exceptions, eg if we can not make an connection with the server

distro/pbs_python.spec:

  • New spec file from Martin
  • Property svn:keywords set to Id
File size: 7.8 KB
Line 
1#
2# Authors: Roy Dragseth (roy.dragseth@cc.uit.no)
3#          Bas van der Vlies (basv@sara.nl)
4#
5# SVN INFO:
6#       $Id: PBSQuery.py 99 2005-11-23 14:52:53Z bas $
7#
8"""
9Usage: from PBSQuery import PBSQuery
10
11This class gets the info from the pbs_server via the pbs.py module
12for the several batch objects. All get..() functions return an dictionary
13with id as key and batch object as value
14
15There are four batch objects:
16 - server
17 - queue
18 - job
19 - node
20
21Each object can be handled as an dictionary and has several member
22functions. The second parameter is an python list and can be used if you
23are only interested in certain resources, see example
24
25There are the following functions for PBSQuery:
26  job -
27        getjob(job_id, attributes=<default is all>)
28        getjobs(attributes=<default is all>)
29 
30  node -
31        getnode(node_id, attributes=<default is all>)
32        getnodes(attributes=<default is all>)
33
34  queue -
35        getqueue(queue_id, attributes=<default is all>)
36        getqueues(attributes=<default is all>)
37
38  server -
39        get_serverinfo(attributes=<default is all>)
40
41Here is an example how to use the module:
42        from PBSQuery import PBSQuery
43        p = PBSQuery()
44        nodes = p.getnodes()
45        for name,node in nodes.items():
46            print name
47            if node.is_free():
48               print node, node['state']
49
50        l = [ 'state', 'np' ]
51        nodes = p.getnodes(l)
52        for name,node in nodes.items():
53               print node, node['state']
54
55The parameter 'attributes' is an python list of resources that
56you are interested in, eg: only show state of nodes
57        l = list()
58        l.append('state')
59        nodes = p.getnodes(l)
60"""
61import pbs
62
63class PBSError(Exception):
64        def __init__(self, msg=''):
65                self.msg = msg
66                Exception.__init__(self, msg)
67        def __repr__(self):
68                return self.msg
69        __str__ = __repr__
70
71
72class PBSQuery:
73    def __init__(self, server=None):
74        if not server:
75            self.server = pbs.pbs_default()
76        else:
77            self.server = server
78
79
80    def _connect(self):
81       """Connect to the PBS/Torque server"""
82       self.con = pbs.pbs_connect(self.server)
83       if self.con < 0:
84          str = "Could not make an connection with %s\n" %(self.server)
85          raise PBSError(str)
86
87    def _disconnect(self):
88       """Close the PBS/Torque connection"""
89       pbs.pbs_disconnect(self.con)
90       self.attribs = 'NULL'
91
92    def _list_2_attrib(self, list):
93        """Convert an python list to an attrib list suitable for pbs"""
94        self.attribs = pbs.new_attrl( len(list) )
95        i = 0 
96        for attrib in list:
97            self.attribs[i].name = attrib
98            i = i + 1
99
100    def _pbsstr_2_list(self, str, delimiter):
101        """Convert an string to an python list and use delimiter as spit char"""
102        l = sting.splitfields(str, delimiter)
103        if len(l) > 1:
104           return l
105
106    def _list_2_dict(self, l, class_func):
107        """Convert an pbsstat function list to an class dictionary"""
108        self.d = {}
109        for item in l:
110            new = class_func()
111            for a in item.attribs:
112                new.name = item.name
113                if a.resource:
114                   new.attribs[a.name + '.' + a.resource] = a.value
115                else:
116                   new.attribs[a.name] = a.value
117                self.d[item.name] = new
118        self._free(l)
119               
120    def _free(self, memory):
121        pbs.pbs_statfree(memory)
122
123    def _statserver(self, attrib_list=None):
124        """Get the server config from the pbs server"""
125        if attrib_list:
126           self._list_2_attrib(attrib_list)
127        else:
128           self.attribs = 'NULL'
129
130        self._connect()
131        serverinfo = pbs.pbs_statserver(self.con, self.attribs, 'NULL')
132        self._disconnect()
133
134        self.serverinfo = {}
135        self._list_2_dict(serverinfo, server)
136
137    def get_serverinfo(self, attrib_list=None):
138        self._statserver(attrib_list)
139        return self.d
140
141    def _statqueue(self, queue_name='', attrib_list=None):
142        """Get the queue config from the pbs server"""
143        if attrib_list:
144           self._list_2_attrib(attrib_list)
145        else:
146           self.attribs = 'NULL'
147
148        self._connect()
149        queues = pbs.pbs_statque(self.con, queue_name, self.attribs, 'NULL')
150        self._disconnect()
151
152        self.queues = {}
153        self._list_2_dict(queues, queue)
154
155    def getqueue(self, name, attrib_list=None):
156        self._statqueue(name, attrib_list)
157        return self.d
158       
159    def getqueues(self, attrib_list=None):
160        self._statqueue('', attrib_list)
161        return self.d
162
163    def _statnode(self, node_name='', attrib_list=None):
164        """Get the node config from the pbs server"""
165        if attrib_list:
166           self._list_2_attrib(attrib_list)
167        else:
168           self.attribs = 'NULL'
169
170        self._connect()
171        nodes = pbs.pbs_statnode(self.con, node_name, self.attribs, 'NULL')
172        self._disconnect()
173
174        self.nodes = {}
175        self._list_2_dict(nodes, node)
176
177    def getnode(self, name, attrib_list=None):
178        self._statnode(name, attrib_list)
179        return self.d
180       
181    def getnodes(self, attrib_list=None):
182        self._statnode('', attrib_list)
183        return self.d
184
185    def _statjob(self, job_name='', attrib_list=None):
186        """Get the job config from the pbs server"""
187        if attrib_list:
188           self._list_2_attrib(attrib_list)
189        else:
190           self.attribs = 'NULL'
191           
192        self._connect()
193        jobs = pbs.pbs_statjob(self.con, job_name, self.attribs, 'NULL')
194        self._disconnect()
195
196        self.jobs = {}
197        self._list_2_dict(jobs, job)
198
199    def getjob(self, name, attrib_list=None):
200        self._statjob(name, attrib_list)
201        return self.d
202       
203    def getjobs(self, attrib_list=None):
204        self._statjob('', attrib_list)
205        return self.d
206
207class _PBSobject:
208    TRUE  = 1
209    FALSE = 0
210
211    def __init__(self):
212        self.attribs = {}
213        self.name = None
214
215    def __len__(self):
216        return(len(self.attribs.keys()))
217
218    def __str__(self):
219        data = self.name + '\n'
220        for name,value in self.attribs.items():
221                data = data + '\t%s = %s\n' %(name, value)
222        return(data)
223
224    __repr__ = __str__
225
226    def __getitem__(self,key):
227        if key == 'name':
228            return self.name
229       
230        return(self.attribs[key])
231
232    def get_value(self, key):
233        if self.attribs.has_key(key):
234           return self.attribs[key]
235        else:
236           return None
237
238    def has_key(self, key):
239        if self.attribs.has_key(key):
240           return self.TRUE
241        else:
242           return None
243
244    def keys(self):
245        return self.attribs.keys()
246
247    def values(self):
248        return self.attribs.values()
249
250    def items(self):
251        return self.attribs.items()
252   
253class job(_PBSobject):
254    """PBS job class"""
255
256    def is_running(self):
257        if self.get_value('job_state') == 'Q':
258           return self.FALSE
259        else:
260           return self.TRUE
261
262class node(_PBSobject):
263    """PBS node class"""
264
265    def is_free(self):
266        """Check if node is free"""
267        if self.get_value('state') == 'free':
268              return self.TRUE
269        else:
270              return self.FALSE
271
272    def has_job(self):
273        """Does the node run a job"""
274        if self.get_value('jobs'):
275              return self.TRUE
276        else:
277              return self.FALSE
278
279class queue(_PBSobject):
280    """PBS queue class"""
281
282    def is_enabled(self):
283        if self.get_value('enabled') == 'True':
284           return self.TRUE
285        else:
286           return self.FALSE
287
288    def is_execution(self):
289        if self.get_value('queue_type') == 'Execution':
290           return self.TRUE
291        else:
292           return self.FALSE
293
294
295class server(_PBSobject):
296    """PBS server class"""
297
298def main():
299    p = PBSQuery()
300
301    serverinfo = p.get_serverinfo()
302    for name, server in serverinfo.items():
303        print server
304        for key in server.keys():
305                print key, ' = ', server[key]
306
307    queues = p.getqueues()
308    for queue in queues.values():
309        if queue.is_execution():
310           print queue
311
312    jobs = p.getjobs()
313    for name,job in jobs.items():
314        if job.is_running():
315           print job
316
317    l = ['state']
318    nodes = p.getnodes(l)
319    for name,node in nodes.items():
320        if node.is_free(): 
321           print node
322
323if __name__ == "__main__":
324    main()
Note: See TracBrowser for help on using the repository browser.