source: trunk/src/PBSQuery.py @ 169

Last change on this file since 169 was 169, checked in by sil, 16 years ago

trunk/src/PBSQuery.py:

  • Added get_server_name() function. This will return the PBS-server's name.
  • Property svn:keywords set to Id
File size: 7.6 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 169 2008-10-09 07:21:34Z sil $
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
62import UserDict
63import string
64import sys
65import re
66
67class PBSError(Exception):
68        def __init__(self, msg=''):
69                self.msg = msg
70                Exception.__init__(self, msg)
71               
72        def __repr__(self):
73                return self.msg
74
75        __str__ = __repr__
76
77
78class PBSQuery:
79        def __init__(self, server=None):
80                if not server:
81                        self.server = pbs.pbs_default()
82                else:
83                        self.server = server
84
85
86        def _connect(self):
87                """Connect to the PBS/Torque server"""
88                self.con = pbs.pbs_connect(self.server)
89                if self.con < 0:
90                        str = "Could not make a connection with %s\n" %(self.server)
91                        raise PBSError(str)
92
93        def _disconnect(self):
94                """Close the PBS/Torque connection"""
95                pbs.pbs_disconnect(self.con)
96                self.attribs = 'NULL'
97
98        def _list_2_attrib(self, list):
99                """Convert a python list to an attrib list suitable for pbs"""
100                self.attribs = pbs.new_attrl( len(list) )
101                i = 0 
102                for attrib in list:
103                        self.attribs[i].name = attrib
104                        i = i + 1
105
106        def _pbsstr_2_list(self, str, delimiter):
107                """Convert a string to a python list and use delimiter as spit char"""
108                l = sting.splitfields(str, delimiter)
109                if len(l) > 1:
110                        return l
111
112        def _list_2_dict(self, l, class_func):
113                """Convert a pbsstat function list to a class dictionary"""
114                self.d = {}
115                for item in l:
116                        new = class_func()
117                        self.d[item.name] = new
118                       
119                        for a in item.attribs:
120                                new.name = item.name
121                                if a.resource:
122                                        new[a.name + '.' + a.resource] = a.value
123                                else:
124                                        new[a.name] = a.value
125                self._free(l)
126               
127        def _free(self, memory):
128                # Not needed any more
129                #pbs.pbs_statfree(memory)
130                a = 1 
131
132        def _statserver(self, attrib_list=None):
133                """Get the server config from the pbs server"""
134                if attrib_list:
135                        self._list_2_attrib(attrib_list)
136                else:
137                        self.attribs = 'NULL' 
138                       
139                self._connect()
140                serverinfo = pbs.pbs_statserver(self.con, self.attribs, 'NULL')
141                self._disconnect() 
142               
143                self.serverinfo = {}
144                self._list_2_dict(serverinfo, server)
145
146        def get_serverinfo(self, attrib_list=None):
147                self._statserver(attrib_list)
148                return self.d
149
150        def _statqueue(self, queue_name='', attrib_list=None):
151                """Get the queue config from the pbs server"""
152                if attrib_list:
153                        self._list_2_attrib(attrib_list)
154                else:
155                        self.attribs = 'NULL' 
156                       
157                self._connect()
158                queues = pbs.pbs_statque(self.con, queue_name, self.attribs, 'NULL')
159                self._disconnect() 
160               
161                self._list_2_dict(queues, queue)
162
163        def getqueue(self, name, attrib_list=None):
164                self._statqueue(name, attrib_list)
165                q_attrs = self.d['q_express']
166                return self.d
167       
168        def getqueues(self, attrib_list=None):
169                self._statqueue('', attrib_list)
170                return self.d
171
172        def _statnode(self, select='', attrib_list=None, property=None):
173                """Get the node config from the pbs server"""
174                if attrib_list:
175                        self._list_2_attrib(attrib_list)
176                else:
177                        self.attribs = 'NULL' 
178                       
179                if property:
180                        select = ':%s' %(property)
181
182                self._connect()
183                nodes = pbs.pbs_statnode(self.con, select, self.attribs, 'NULL')
184                self._disconnect() 
185               
186                self.nodes = {}
187                self._list_2_dict(nodes, node)
188
189        def getnode(self, name, attrib_list=None):
190                self._statnode(name, attrib_list)
191                return self.d
192       
193        def getnodes(self, attrib_list=None):
194                self._statnode('', attrib_list)
195                return self.d
196
197        def getnodes_with_property(self, property, attrib_list=None):
198                self._statnode('', attrib_list, property)
199                return self.d
200
201        def _statjob(self, job_name='', attrib_list=None):
202                """Get the job config from the pbs server"""
203                if attrib_list:
204                        self._list_2_attrib(attrib_list)
205                else:
206                        self.attribs = 'NULL' 
207                       
208                self._connect()
209                jobs = pbs.pbs_statjob(self.con, job_name, self.attribs, 'NULL')
210                self._disconnect() 
211               
212                self.jobs = {}
213                self._list_2_dict(jobs, job)
214
215        def getjob(self, name, attrib_list=None):
216                self._statjob(name, attrib_list)
217                return self.d
218       
219        def getjobs(self, attrib_list=None):
220                self._statjob('', attrib_list)
221                return self.d
222
223        def get_server_name(self):
224                return self.server
225
226class _PBSobject(UserDict.UserDict):
227        TRUE  = 1
228        FALSE = 0
229
230        def __init__(self):
231                UserDict.UserDict.__init__(self)
232                self.name = None
233
234        def get_value(self, key):
235                if self.has_key(key):
236                        return self[key]
237                else:
238                        return None
239
240class job(_PBSobject):
241        """PBS job class""" 
242        def is_running(self):
243                if self.get_value('job_state') == 'Q':
244                        return self.FALSE
245                else:
246                        return self.TRUE
247
248        def get_nodes(self):
249                nodes = self.get_value('exec_host')
250                if nodes:
251                        l = string.split(nodes,'+')
252                        return l
253                return list()
254
255
256class node(_PBSobject):
257        """PBS node class"""
258       
259        def is_free(self):
260                """Check if node is free"""
261                if self.get_value('state') == 'free':
262                        return self.TRUE
263                else: 
264                        return self.FALSE
265
266        def has_job(self):
267                """Does the node run a job"""
268                if self.get_value('jobs'):
269                        return self.TRUE
270                else:
271                        return self.FALSE
272       
273        def get_jobs(self, unique=None):
274                """Returns a list of the currently running job-id('s) on the node"""
275                jobstring = self.get_value('jobs')
276                if jobstring:
277                        joblist = re.compile('[^\\ /]\\d+[^/.]').findall( jobstring )
278                        if not unique:
279                                return joblist
280                        else:
281                                uniq = {}
282                                for job in joblist:
283                                        uniq[job] = 1
284                                return uniq.keys()
285                return list()
286
287
288
289
290
291class queue(_PBSobject):
292        """PBS queue class"""
293        def is_enabled(self):
294                if self.get_value('enabled') == 'True':
295                        return self.TRUE
296                else:
297                        return self.FALSE
298
299        def is_execution(self):
300                if self.get_value('queue_type') == 'Execution':
301                        return self.TRUE
302                else:
303                        return self.FALSE
304
305class server(_PBSobject):
306        """PBS server class"""
307
308        def get_version(self):
309                return self.get_value('pbs_version')
310
311def main():
312        p = PBSQuery() 
313        serverinfo = p.get_serverinfo()
314        for server in serverinfo.keys():
315                print server, ' version: ', serverinfo[server].get_version()
316        for resource in serverinfo[server].keys():
317                print '\t ', resource, ' = ', serverinfo[server][resource]
318
319        queues = p.getqueues()
320        for queue in queues.keys():
321                print queue
322                if queues[queue].is_execution():
323                        print '\t ', queues[queue]
324                if queues[queue].has_key('acl_groups'):
325                        print '\t acl_groups: yes'
326                else:
327                        print '\t acl_groups: no'
328
329        jobs = p.getjobs()
330        for name,job in jobs.items():
331                if job.is_running():
332                        print job
333
334        l = ['state']
335        nodes = p.getnodes(l)
336        for name,node in nodes.items():
337                if node.is_free(): 
338                        print node
339
340if __name__ == "__main__":
341        main()
Note: See TracBrowser for help on using the repository browser.