[91] | 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 392 2005-03-02 16:47:25Z 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 pbs objects. All get..() functions returns an dictionary |
---|
| 13 | of openpbs/torque objects. There are four objects: |
---|
| 14 | - server |
---|
| 15 | - queue |
---|
| 16 | - job |
---|
| 17 | - node |
---|
| 18 | |
---|
| 19 | Each object can be handled as an dictionary and has several member |
---|
| 20 | functions. |
---|
| 21 | |
---|
| 22 | There are the following functions for PBSQuery: |
---|
| 23 | job - |
---|
| 24 | getjob(name, attributes=<default is all>) |
---|
| 25 | getjobs(attributes=<default is all>) |
---|
| 26 | |
---|
| 27 | node - |
---|
| 28 | getnode(name, attributes=<default is all>) |
---|
| 29 | getnodes(attributes=<default is all>) |
---|
| 30 | |
---|
| 31 | queue - |
---|
| 32 | getqueue(name, attributes=<default is all>) |
---|
| 33 | getqueues(attributes=<default is all>) |
---|
| 34 | |
---|
| 35 | server - |
---|
| 36 | get_serverinfo(attributes=<default is all>) |
---|
| 37 | |
---|
| 38 | Here is an example how to use the module: |
---|
| 39 | from PBSQuery import PBSQuery |
---|
| 40 | p = PBSQuery() |
---|
| 41 | nodes = p.getnodes() |
---|
| 42 | for name,node in nodes.items(): |
---|
| 43 | print name |
---|
| 44 | if node.is_free(): |
---|
| 45 | print node |
---|
| 46 | |
---|
| 47 | """ |
---|
| 48 | import pbs |
---|
| 49 | |
---|
| 50 | |
---|
| 51 | class PBSQuery: |
---|
| 52 | def __init__(self, server=None): |
---|
| 53 | if not server: |
---|
| 54 | self.server = pbs.pbs_default() |
---|
| 55 | else: |
---|
| 56 | self.server = server |
---|
| 57 | |
---|
| 58 | self.con = pbs.pbs_connect(self.server) |
---|
| 59 | self.attribs = 'NULL' |
---|
| 60 | |
---|
| 61 | def __del__(self): |
---|
| 62 | pbs.pbs_disconnect(self.con) |
---|
| 63 | self.con = None |
---|
| 64 | self.attribs = None |
---|
| 65 | |
---|
| 66 | def _list_2_attrib(self, list): |
---|
| 67 | """Convert an python list to an attrib list suitable for pbs""" |
---|
| 68 | self.attribs = pbs.new_attrl( len(list) ) |
---|
| 69 | i = 0 |
---|
| 70 | for attrib in list: |
---|
| 71 | self.attribs[i].name = attrib |
---|
| 72 | i = i + 1 |
---|
| 73 | |
---|
| 74 | def _list_2_dict(self, l, class_func): |
---|
| 75 | """Convert an pbsstat function list to an class dictionary""" |
---|
| 76 | self.d = dict() |
---|
| 77 | for item in l: |
---|
| 78 | new = class_func() |
---|
| 79 | for a in item.attribs: |
---|
| 80 | new.name = item.name |
---|
| 81 | if a.resource: |
---|
| 82 | new.attribs[a.name + '.' + a.resource] = a.value |
---|
| 83 | else: |
---|
| 84 | new.attribs[a.name] = a.value |
---|
| 85 | self.d[item.name] = new |
---|
| 86 | self._free(l) |
---|
| 87 | |
---|
| 88 | def _free(self, memory): |
---|
| 89 | pbs.pbs_statfree(memory) |
---|
| 90 | |
---|
| 91 | def _statserver(self, attrib_list=None): |
---|
| 92 | """Get the server config from the pbs server""" |
---|
| 93 | if attrib_list: |
---|
| 94 | self._list_2_attrib(attrib_list) |
---|
| 95 | else: |
---|
| 96 | self.attribs = 'NULL' |
---|
| 97 | |
---|
| 98 | serverinfo = pbs.pbs_statserver(self.con, self.attribs, 'NULL') |
---|
| 99 | self.serverinfo = dict() |
---|
| 100 | self._list_2_dict(serverinfo, server) |
---|
| 101 | |
---|
| 102 | def get_serverinfo(self, attrib_list=None): |
---|
| 103 | self._statserver(attrib_list) |
---|
| 104 | return self.d |
---|
| 105 | |
---|
| 106 | def _statqueue(self, queue_name='', attrib_list=None): |
---|
| 107 | """Get the queue config from the pbs server""" |
---|
| 108 | if attrib_list: |
---|
| 109 | self._list_2_attrib(attrib_list) |
---|
| 110 | else: |
---|
| 111 | self.attribs = 'NULL' |
---|
| 112 | |
---|
| 113 | queues = pbs.pbs_statque(self.con, queue_name, self.attribs, 'NULL') |
---|
| 114 | self.queues = dict() |
---|
| 115 | self._list_2_dict(queues, queue) |
---|
| 116 | |
---|
| 117 | def getqueue(self, name, attrib_list=None): |
---|
| 118 | self._statqueue(name, attrib_list) |
---|
| 119 | return self.d |
---|
| 120 | |
---|
| 121 | def getqueues(self, attrib_list=None): |
---|
| 122 | self._statqueue('', attrib_list) |
---|
| 123 | return self.d |
---|
| 124 | |
---|
| 125 | def _statnode(self, node_name='', attrib_list=None): |
---|
| 126 | """Get the node config from the pbs server""" |
---|
| 127 | if attrib_list: |
---|
| 128 | self._list_2_attrib(attrib_list) |
---|
| 129 | else: |
---|
| 130 | self.attribs = 'NULL' |
---|
| 131 | |
---|
| 132 | nodes = pbs.pbs_statnode(self.con, node_name, self.attribs, 'NULL') |
---|
| 133 | self.nodes = dict() |
---|
| 134 | self._list_2_dict(nodes, node) |
---|
| 135 | |
---|
| 136 | def getnode(self, name, attrib_list=None): |
---|
| 137 | self._statnode(name, attrib_list) |
---|
| 138 | return self.d |
---|
| 139 | |
---|
| 140 | def getnodes(self, attrib_list=None): |
---|
| 141 | self._statnode('', attrib_list) |
---|
| 142 | return self.d |
---|
| 143 | |
---|
| 144 | def _statjob(self, job_name='', attrib_list=None): |
---|
| 145 | """Get the job config from the pbs server""" |
---|
| 146 | if attrib_list: |
---|
| 147 | self._list_2_attrib(attrib_list) |
---|
| 148 | else: |
---|
| 149 | self.attribs = 'NULL' |
---|
| 150 | |
---|
| 151 | jobs = pbs.pbs_statjob(self.con, job_name, self.attribs, 'NULL') |
---|
| 152 | self.jobs = dict() |
---|
| 153 | self._list_2_dict(jobs, job) |
---|
| 154 | |
---|
| 155 | def getjob(self, name, attrib_list=None): |
---|
| 156 | self._statjob(name, attrib_list) |
---|
| 157 | return self.d |
---|
| 158 | |
---|
| 159 | def getjobs(self, attrib_list=None): |
---|
| 160 | self._statjob('', attrib_list) |
---|
| 161 | return self.d |
---|
| 162 | |
---|
| 163 | class _PBSobject: |
---|
| 164 | TRUE = 1 |
---|
| 165 | FALSE = 0 |
---|
| 166 | |
---|
| 167 | def __init__(self): |
---|
| 168 | self.attribs = dict() |
---|
| 169 | self.name = None |
---|
| 170 | |
---|
| 171 | def __len__(self): |
---|
| 172 | return(len(self.attribs.keys())) |
---|
| 173 | |
---|
| 174 | def __str__(self): |
---|
| 175 | data = self.name + '\n' |
---|
| 176 | for name,value in self.attribs.items(): |
---|
| 177 | data = data + '\t%s = %s\n' %(name, value) |
---|
| 178 | return(data) |
---|
| 179 | |
---|
| 180 | __repr__ = __str__ |
---|
| 181 | |
---|
| 182 | def __getitem__(self,key): |
---|
| 183 | if key == 'name': |
---|
| 184 | return self.name |
---|
| 185 | |
---|
| 186 | return(self.attribs[key]) |
---|
| 187 | |
---|
| 188 | def get_value(self, key): |
---|
| 189 | if self.attribs.has_key(key): |
---|
| 190 | return self.attribs[key] |
---|
| 191 | else: |
---|
| 192 | return None |
---|
| 193 | |
---|
| 194 | def keys(self): |
---|
| 195 | return self.attribs.keys() |
---|
| 196 | |
---|
| 197 | def values(self): |
---|
| 198 | return self.attribs.values() |
---|
| 199 | |
---|
| 200 | def items(self): |
---|
| 201 | return self.attribs.items() |
---|
| 202 | |
---|
| 203 | class job(_PBSobject): |
---|
| 204 | """PBS job class""" |
---|
| 205 | |
---|
| 206 | def is_running(self): |
---|
| 207 | if self.get_value('job_state') == 'Q': |
---|
| 208 | return self.FALSE |
---|
| 209 | else: |
---|
| 210 | return self.TRUE |
---|
| 211 | |
---|
| 212 | class node(_PBSobject): |
---|
| 213 | """PBS node class""" |
---|
| 214 | |
---|
| 215 | def is_free(self): |
---|
| 216 | """Check if node is free""" |
---|
| 217 | if self.get_value('state') == 'free': |
---|
| 218 | return self.TRUE |
---|
| 219 | else: |
---|
| 220 | return self.FALSE |
---|
| 221 | |
---|
| 222 | def has_job(self): |
---|
| 223 | """Does the node run a job""" |
---|
| 224 | if self.get_value('jobs'): |
---|
| 225 | return self.TRUE |
---|
| 226 | else: |
---|
| 227 | return self.FALSE |
---|
| 228 | |
---|
| 229 | class queue(_PBSobject): |
---|
| 230 | """PBS queue class""" |
---|
| 231 | |
---|
| 232 | def is_enabled(self): |
---|
| 233 | if self.get_value('enabled') == 'True': |
---|
| 234 | return self.TRUE |
---|
| 235 | else: |
---|
| 236 | return self.FALSE |
---|
| 237 | |
---|
| 238 | def is_execution(self): |
---|
| 239 | if self.get_value('queue_type') == 'Execution': |
---|
| 240 | return self.TRUE |
---|
| 241 | else: |
---|
| 242 | return self.FALSE |
---|
| 243 | |
---|
| 244 | |
---|
| 245 | class server(_PBSobject): |
---|
| 246 | """PBS server class""" |
---|
| 247 | |
---|
| 248 | def main(): |
---|
| 249 | p = PBSQuery() |
---|
| 250 | |
---|
| 251 | serverinfo = p.get_serverinfo() |
---|
| 252 | for name, server in serverinfo.items(): |
---|
| 253 | print server |
---|
| 254 | for key in server.keys(): |
---|
| 255 | print key, ' = ', server[key] |
---|
| 256 | |
---|
| 257 | queues = p.getqueues() |
---|
| 258 | for queue in queues.values(): |
---|
| 259 | if queue.is_execution(): |
---|
| 260 | print queue |
---|
| 261 | |
---|
| 262 | jobs = p.getjobs() |
---|
| 263 | for name,job in jobs.items(): |
---|
| 264 | if job.is_running(): |
---|
| 265 | print job |
---|
| 266 | |
---|
| 267 | l = ['state'] |
---|
| 268 | nodes = p.getnodes(l) |
---|
| 269 | for name,node in nodes.items(): |
---|
| 270 | if node.is_free(): |
---|
| 271 | print node |
---|
| 272 | |
---|
| 273 | if __name__ == "__main__": |
---|
| 274 | main() |
---|