Skip to content

Commit

Permalink
Fixed jobs parsing. pbsstatus now prints status
Browse files Browse the repository at this point in the history
  • Loading branch information
necrolyte2 committed May 1, 2015
1 parent 6311364 commit 3e29953
Show file tree
Hide file tree
Showing 5 changed files with 84 additions and 9 deletions.
10 changes: 10 additions & 0 deletions README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -9,3 +9,13 @@ pypbs

.. image:: https://coveralls.io/repos/VDBWRAIR/pypbs/badge.svg
:target: https://coveralls.io/r/VDBWRAIR/pypbs

Small api for pbs stuff

Some useful commands too

.. code-block:: bash
pbsstatus
NP Utilization Cluster Load Avail CPU Used CPU Total CPU Running Jobs
90.62% 43.03% 3 29 32 17
8 changes: 6 additions & 2 deletions pypbs/nodesxml.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,10 @@ def parse_xml(nodes_xml):
tdict = {}
for attrib in node:
tdict[attrib.tag] = parse_list_string(attrib.text)
if 'jobs' not in tdict:
tdict['jobs'] = []
elif isinstance(tdict['jobs'],str):
tdict['jobs'] = [tdict['jobs']]
nodes[tdict['name']] = tdict
return nodes

Expand Down Expand Up @@ -42,11 +46,11 @@ def parse_list_string(string):

# Now do another search for subdict patterns
subdict = {}
p = '(\w+)=((?:\w\(\S+\)\s{0,1})+)'
p = '(\w+)=([\w\d\.]+\(.*?\)(?: \S+\(.*?\)){0,})'
stuff = re.findall(p, string)
for key, values in stuff:
subdict[key] = {}
defs = re.findall('(?:(\w)\((.*?)\))', values)
defs = re.findall('(?:(\S+)\((.*?)\))', values)
for _key, d in defs:
subdict[key][_key] = parse_list_string(d)
_dict.update(subdict)
Expand Down
20 changes: 18 additions & 2 deletions pypbs/pbsstatus.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
#!/usr/bin/env python

from __future__ import print_function

import xml.etree.ElementTree as ET
import re

Expand Down Expand Up @@ -103,8 +105,22 @@ def parse_job_string(job_str):

return job

def cluster_status(cluster_info):
'''
Return a formatted string that should display cluster status nicely
:param dict cluster_info: dict of cluster information
'''
template = 'NP Utilization\tCluster Load\tAvail CPU\tUsed CPU\tTotal CPU\tRunning Jobs\n'
template += '{np_utilization:14.2%}\t{load_utilization:12.2%}\t{avail_np:9}\t{used_np:8}\t{total_np:9}\t{running_jobs:12}'
return template.format(
**cluster_info
)

def main():
xml = get_pbsnodes_xml()
nodes = nodesxml.parse_xml(xml)
import pprint
pprint.pprint(nodes)
#import pprint
#pprint.pprint(nodes)
cinfo = cluster_info(nodes)
print(cluster_status(cinfo))
25 changes: 25 additions & 0 deletions tests/output.xml
Original file line number Diff line number Diff line change
Expand Up @@ -49,4 +49,29 @@
<mom_manager_port>15003</mom_manager_port>
<gpus>1</gpus>
</Node>
<Node>
<name>host5.example.com</name>
<state>free</state>
<power_state>Running</power_state>
<np>12</np>
<properties>bigmem,tesla</properties>
<ntype>cluster</ntype>
<status>rectime=1430334888,macaddr=00:00:00:00:00:03,cpuclock=OnDemand:2301MHz,varattr=,jobs=,state=free,netload=622455994750,gres=,loadave=17.77,ncpus=24,physmem=264455068kb,availmem=260210680kb,totmem=272647060kb,idletime=102260,nusers=1,nsessions=11,sessions=25749 5514 2372 1830 1494 1421 1350 18106 17897 31317 2600,uname=Linux host5.example.com 2.6.32-431.20.3.el6.x86_64 #1 SMP Fri Jun 6 18:30:54 EDT 2014 x86_64,opsys=linux</status>
<mom_service_port>15002</mom_service_port>
<mom_manager_port>15003</mom_manager_port>
<gpus>2</gpus>
</Node>
<Node>
<name>host6.example.com</name>
<state>free</state>
<power_state>Running</power_state>
<np>12</np>
<properties>bigmem,tesla</properties>
<ntype>cluster</ntype>
<jobs>0/1.host1.example.com</jobs>
<status>rectime=1430334888,macaddr=00:00:00:00:00:03,cpuclock=OnDemand:2301MHz,varattr=,jobs=,state=free,netload=622455994750,gres=,loadave=17.77,ncpus=24,physmem=264455068kb,availmem=260210680kb,totmem=272647060kb,idletime=102260,nusers=1,nsessions=11,sessions=25749 5514 2372 1830 1494 1421 1350 18106 17897 31317 2600,uname=Linux host6.example.com 2.6.32-431.20.3.el6.x86_64 #1 SMP Fri Jun 6 18:30:54 EDT 2014 x86_64,opsys=linux</status>
<mom_service_port>15002</mom_service_port>
<mom_manager_port>15003</mom_manager_port>
<gpus>2</gpus>
</Node>
</Data>
30 changes: 25 additions & 5 deletions tests/test_nodesxml.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,23 @@ def test_parses_comma_lists(self):
self.nodes['host4.example.com']['jobs'], list
)

def test_parses_status_jobs(self):
hostinfo = self.nodes['host3.example.com']
jobs = hostinfo['status']['jobs']
self.assertEqual(
'2600', jobs['656.host1.example.com']['session_id']
)

def test_when_only_one_job_still_returns_list(self):
self.assertEqual(
['0/1.host1.example.com'], self.nodes['host6.example.com']['jobs']
)

def test_when_no_jobs_sets_empty_list(self):
self.assertEqual(
[], self.nodes['host5.example.com']['jobs']
)

def test_builds_sub_dict_with_equal_signs(self):
self.assertEqual(
'1430334904', self.nodes['host1.example.com']['status']['rectime']
Expand All @@ -44,8 +61,11 @@ def test_returns_dictionary(self):
nodesxml.parse_list_string('foo=bar,baz=,bar=foo')
)

def test_parses_sub_dictionary_in_paren(self):
self.assertEqual(
{'foo':'bar','baz':'foo','jobs':{'1':{'foo':'1','bar':'2'},'2':{'foo':'1','bar':'2'}}},
nodesxml.parse_list_string('foo=bar,jobs=1(foo=1,bar=2) 2(foo=1,bar=2),baz=foo')
)
def test_parses_actual(self):
status = 'rectime=1430334888,macaddr=00:00:00:00:00:03,cpuclock=OnDemand:2301MHz,varattr=,jobs=656.host1.example.com(cput=704205,energy_used=0,mem=663876kb,vmem=290383028kb,walltime=1143760,session_id=2600) 812.host1.example.com(cput=466890,energy_used=0,mem=851568kb,vmem=286089568kb,walltime=513972,session_id=31317) 851.host1.example.com(cput=171210,energy_used=0,mem=856996kb,vmem=290125588kb,walltime=171501,session_id=17897) 855.host1.example.com(cput=169197,energy_used=0,mem=530380kb,vmem=290383028kb,walltime=169460,session_id=18106) 864.host1.example.com(cput=107891,energy_used=0,mem=941696kb,vmem=290326052kb,walltime=108039,session_id=1350) 865.host1.example.com(cput=107606,energy_used=0,mem=832564kb,vmem=290193180kb,walltime=107777,session_id=1421) 866.host1.example.com(cput=107372,energy_used=0,mem=916400kb,vmem=290324252kb,walltime=107567,session_id=1494) 868.host1.example.com(cput=106830,energy_used=0,mem=830340kb,vmem=290060052kb,walltime=107005,session_id=1830) 870.host1.example.com(cput=106174,energy_used=0,mem=843748kb,vmem=290125588kb,walltime=106340,session_id=2372) 892.host1.example.com(cput=95790,energy_used=0,mem=836868kb,vmem=286180260kb,walltime=95935,session_id=5514) 978.host1.example.com(cput=7449,energy_used=0,mem=846060kb,vmem=290060052kb,walltime=7463,session_id=25749),state=free,netload=622455994750,gres=,loadave=17.77,ncpus=24,physmem=264455068kb,availmem=260210680kb,totmem=272647060kb,idletime=102260,nusers=1,nsessions=11,sessions=25749 5514 2372 1830 1494 1421 1350 18106 17897 31317 2600,uname=Linux host3.example.com 2.6.32-431.20.3.el6.x86_64 #1 SMP Fri Jun 6 18:30:54 EDT 2014 x86_64,opsys=linux'
r = nodesxml.parse_list_string(status)
self.assertEqual('1430334888', r['rectime'])
self.assertIn('jobs', r)
self.assertIn('656.host1.example.com', r['jobs'])
self.assertEqual('704205', r['jobs']['656.host1.example.com']['cput'])
self.assertEqual(11, len(r['jobs']))

0 comments on commit 3e29953

Please sign in to comment.