<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet type="text/css" href="/stylesheets/rss.css"?>
<rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:trackback="http://madskills.com/public/xml/rss/module/trackback/">
  <channel>
    <title>gridengine.info : Tag xml::smart, everything about xml::smart</title>
    <link>http://gridengine.info/tag/xml::smart.rss</link>
    <language>en-us</language>
    <ttl>40</ttl>
    <description>tracking Grid Engine news, bugs, howtos and best practices</description>
    <item>
      <title>Easy gridengine XML handling via Perl XML::Smart</title>
      <description>&lt;p&gt;Joe Landman from &lt;a href="http://scalableinformatics.com/"&gt;Scalable Informatics&lt;/a&gt; posted about &lt;a href="http://gridengine.sunsource.net/servlets/ReadMsg?list=users&amp;msgNo=13647"&gt;his success&lt;/a&gt; with the Perl &lt;span class="code"&gt;XML::Smart&lt;/span&gt; ( &lt;a href="http://search.cpan.org/dist/XML-Smart/"&gt;CPAN&lt;/a&gt;, &lt;a href="http://search.cpan.org/src/GMPASSOS/XML-Smart-1.6.9/README"&gt;readme&lt;/a&gt;, &lt;a href="http://search.cpan.org/dist/XML-Smart/lib/XML/Smart/FAQ.pod"&gt;FAQ&lt;/a&gt;, &lt;a href="http://search.cpan.org/dist/XML-Smart/lib/XML/Smart/Tutorial.pod"&gt;tutorial&lt;/a&gt;) module.  &lt;/p&gt;&lt;p&gt;Unlike many of the XML handling methods within the Perl universe, this module stands on its own without a huge and complicated chain of external dependencies. &lt;/p&gt;&lt;p&gt;&lt;span class="code"&gt;XML::Smart&lt;/span&gt; can quickly and cleanly parse XML documents into perl datastructures that can efficiently traversed and sorted. This makes it a great method for simple perl scripts designed to grab bits of data or information that does not get displayed in the human-readble &lt;span class="code"&gt;qstat&lt;/span&gt; output. &lt;/p&gt;

Joe's comments:
&lt;blockquote&gt;&lt;i&gt; Our 6.0u6 perl based parser fits into a single line, after we grab 
the data.&lt;/i&gt;&lt;pre&gt;
	$qstat=`/opt/gridengine/bin/lx24-amd64/qstat -xml`;
	$xml    = XML::Smart-&gt;new($qstat);

&lt;/pre&gt;&lt;i&gt;(no schema/DTD needed)&lt;br/&gt;
then for example, iterating over all the jobs  ...
&lt;/i&gt;&lt;pre&gt;
	foreach ($xml-&gt;{job_info}-&gt;{queue_info}-&gt;{job_list}('@') )
	  {
	   ...
	  }&lt;/pre&gt;&lt;/blockquote&gt;&lt;p&gt;Using some example code (included at the end of this article by permission) kindly provided by Joe, I was able to whip up a little "just playing" script that checks all pending jobs for hard resource requests. When a hard request is found, the script simply prints out a line that lists the Job ID, Job Name and the value of the hard resource request. The script looks like this:&lt;/p&gt;&lt;div class="codePost"&gt;&lt;pre&gt;
#!/usr/bin/perl -w
use XML::Smart;
my ($xml,$qstat);

$qstat=`/opt/sge6s2u1/bin/lx24-amd64/qstat -xml -r -f`;
$xml    = XML::Smart-&gt;new($qstat);

foreach ($xml-&gt;{job_info}-&gt;{job_info}-&gt;{job_list}('@') )
{
    if($_-&gt;{hard_request}) {
      print "Job ID $_-&gt;{JB_job_number} ($_-&gt;{JB_name}) has a hard_request: ";
      print "$_-&gt;{hard_request}{name}=$_-&gt;{hard_request} \n";
    }
}
&lt;/pre&gt;&lt;/div&gt;&lt;br/&gt;&lt;p&gt;Output looks like this:&lt;/p&gt;&lt;div class="codePost"&gt;&lt;pre&gt;
[dag@dcore-amd ~]$ ./test.pl
Job ID 47 (impossibleJob) has a hard_request: arch=darwin 
[dag@dcore-amd ~]$ 
&lt;/pre&gt;&lt;/div&gt;&lt;br/&gt;&lt;p&gt;
Additional pointers and examples from Scalable Informatics are included below ...
&lt;/p&gt;&lt;br/&gt;

&lt;h4&gt;Scalable Informatics provided the following example code and explanations. &lt;/h4&gt;&lt;br/&gt;
The included code is copyright (c) 2004-2005 Scalable Informatics and licensed under &lt;a href="http://www.gnu.org/licenses/gpl.txt"&gt;GPL 2&lt;/a&gt;&lt;br/&gt;&lt;br/&gt;&lt;p&gt;&lt;i&gt;What we use today looks just like this:&lt;/i&gt;&lt;/p&gt;&lt;div class="codePost"&gt;&lt;pre&gt;
use XML::Smart;
my ($xml,$qstat);

$qstat=`/opt/gridengine/bin/lx24-amd64/qstat -xml`;
$xml	= XML::Smart-&gt;new($qstat);

foreach ($xml-&gt;{job_info}-&gt;{queue_info}-&gt;{job_list}('@') )
  {
     # stuff with each job.  All the per job attributes are now available as
     # $_-&gt;{attribute_name}.
     #
  }&lt;/pre&gt;&lt;/div&gt;&lt;br/&gt;&lt;p&gt;&lt;i&gt;Now if you want to get fancy, and sort by *any* attribute (up or down, using JB_Owner in this case, refer to the XML for what you want to sort&lt;/i&lt;/p&gt;&lt;div class="codePost"&gt;&lt;pre&gt;
use XML::Smart;
my ($xml,$qstat,@jobs);

$qstat=`/opt/gridengine/bin/lx24-amd64/qstat -xml`;
$xml	= XML::Smart-&gt;new($qstat);
@jobs   = $xml-&gt;{job_info}-&gt;{queue_info}-&gt;{job_list}('@');

foreach ( sort { $a-&gt;{JB_Owner} cmp  $b-&gt;{JB_Owner} } @jobs )
  {
     # stuff with each job.  All the per job attributes are now available as
     # $_-&gt;{attribute_name}.
     #
  }&lt;/pre&gt;&lt;/div&gt;&lt;br/&gt;&lt;p&gt;&lt;i&gt;To extract execution times requires a bit more work (need to parse 2 dates, subtract one from another, then return the value in a sensible format).  Code to do that looks like this:&lt;/i&gt;&lt;/p&gt;&lt;div class="codePost"&gt;&lt;pre&gt;
use Date::Manip;
my ($d,$t,$olddate,$delta,$dt,$date);

# ... some place later in the code ...
($d,$t)=split(/\s+/, $_-&gt;{JAT_start_time}  );
if ($d =~ /(\d+)\/(\d+)\/(\d+)/)  { $date = sprintf "%.4i%.2i%.2i",$3,$1,$2; }
       if ($t =~ /(\d+):(\d+):(\d+)/)  { $date .= sprintf "%i%i%i",$1,$2,$3; }
       $olddate = ParseDate($date );
$delta = DateCalc($olddate,$today);
      $dt = Delta_Format($delta,0,qw(%st));
       printf  "%.1f second(s)\n",$dt;&lt;/pre&gt;&lt;/div&gt;&lt;br/&gt;&lt;p&gt;&lt;i&gt;The issue in part is that SGE does not define an elapsed job runtime field somewhere, you need to calculate it.  Hopefully this will change.&lt;/i&gt;&lt;/p&gt;&lt;p&gt;&lt;i&gt;You can easily combine this into a program that grabs all the relevant data and outputs what you need.  If you are using XSLT or similar, you could use this as a parser call-back.&lt;/i&gt;&lt;/p&gt;&lt;p&gt;&lt;i&gt;The XML::Smart module is the recommended way to go with Perl.  It is extremely fast and very flexible while also being very easy to use. Just don't peek too much at its internal data structures, they can be ... interesting.  Note also that they can get huge.  So if your xml is more than a few gigabytes in size, you might need to do a little extra work.&lt;/i&gt;&lt;/p&gt;</description>
      <pubDate>Fri, 11 Nov 2005 14:45:23 -0500</pubDate>
      <guid isPermaLink="false">urn:uuid:a2e3808a-2449-4e04-bb22-3172c40b75de</guid>
      <author>dag@sonsorol.org (chris)</author>
      <comments>http://gridengine.info/2005/11/11/easy-gridengine-xml-handling-via-perl-xml-smart#comments</comments>
      <category>Grid Engine XML</category>
      <category>perl</category>
      <category>xml::smart</category>
      <category>xml</category>
      <link>http://gridengine.info/2005/11/11/easy-gridengine-xml-handling-via-perl-xml-smart</link>
    </item>
  </channel>
</rss>
