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