When last discussing using the XML produced by BIND's statistics server, I showed you how I created a C program to parse the XML, but it didn't occur to me to use XSL and the xsltproc utility to do so.

BIND9 old XML format

A kind reader added a comment to that post a few moments ago, and I took the liberty of streamlining his code a bit to produce this:

<xsl:stylesheet version="1.0" xmlns="http://www.w3.org/1999/xhtml" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<!--
  ** XSLT to parse BIND9 XML from the statistics-server (old format)
  ** inspired by a comment by Andrii Shynkarchuk on 
  ** http://jpmens.net/2010/10/21/using-binds-statistics-server-to-list-zones-and-axfr-the-list/
  **
  ** Set the view name you're interested in where it says "_default"
  ** (This could be accessed with an XSLT param or so.)
  -->
<xsl:output method="text" />
  <xsl:strip-space elements="*"/>

  <xsl:template match="isc/bind/statistics">
    <xsl:apply-templates select="views/view">
    </xsl:apply-templates></xsl:template>
      <xsl:template match="views/view">
      <xsl:choose>
    <xsl:when test="name='_default'">
        <xsl:for-each select="zones/zone">
            <xsl:value-of select="serial" />
            <xsl:text> </xsl:text> 
            <!-- zone name only (w/o class/view) -->
            <xsl:value-of select="substring-before(name, '/')" />
            <xsl:text>&#xa;</xsl:text> <!-- newline -->
        </xsl:for-each>

    </xsl:when>
      </xsl:choose>
    </xsl:template>
</xsl:stylesheet>

If I run the result through xsltproc on a 240MB XML file containing 136,000 zones, I get results in 8 seconds on my laptop, which is pretty good.

curl -qs http://172.16.153.101:8053/ |
    xsltproc bconvert.xsl -
2012011108 example.com
0 0.in-addr.arpa
0 1.0.0.127.in-addr.arpa
0 1.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.ip6.arpa
2011080707 example.org
97 big.aa
...

BIND9 newstats XML format

And this works for me for the "newstats" format, introduced experimentally in 9.9.3:

<xsl:stylesheet version="1.0" xmlns="http://www.w3.org/1999/xhtml" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<!--
  ** XSLT to parse BIND9 (version3) XML from the statistics-server (new format)
  ** inspired by a comment by Andrii Shynkarchuk on 
  ** http://jpmens.net/2010/10/21/using-binds-statistics-server-to-list-zones-and-axfr-the-list/
  **
  ** Set the view name you're interested in where it says "_default"
  ** (This could be accessed with an XSLT param or so.)
  -->
<xsl:output method="text" />
  <xsl:strip-space elements="*"/>

<xsl:template match="statistics"> 
    <xsl:apply-templates select="views/view">
    </xsl:apply-templates></xsl:template>
      <xsl:template match="views/view">
        <xsl:for-each select="zones/zone">
            <xsl:value-of select="serial" />
            <xsl:text> </xsl:text> 
            <xsl:value-of select="@name" />
            <xsl:text>&#xa;</xsl:text> <!-- newline -->
        </xsl:for-each>
</xsl:template> 
</xsl:stylesheet>

Thank you, Andrii. :)

Flattr this
BIND, XML, and XSL :: 01 Mar 2013 :: e-mail

Comments

blog comments powered by Disqus