<?xml version="1.0"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">

  <!-- Create a status page from the mod_status_xml XML data almost the same as 
       mod_status gives you; 
       Mark Cox, mjc@awe.com, Aug 2001 

       Known to work on MSIE 6, Mozilla 0.9.3 (Windows, Linux).  
       Known not to work on MSIE 5.5 (Mac)

   -->

<xsl:output encoding="ISO-8859-1" method="html"/>

<xsl:template match="server">
  <html>
    <head><title>Server Status</title></head>
    <body>
      <h1>Apache Server Status for <xsl:value-of select="@name"/></h1>
      Server version: <xsl:value-of select="version"/>
      <hr/>
      Current time:
         <xsl:call-template name="displaydate">
            <xsl:with-param name="date" select="@time"/>
         </xsl:call-template><br/>
      Restart time: 
         <xsl:call-template name="displaydate">
            <xsl:with-param name="date" select="restarttime"/>
         </xsl:call-template><br/>
      Server uptime: <xsl:apply-templates select="uptime"/><br/>
      Parent server generation: <xsl:value-of select="parentgen"/><br/>
      Total accesses: <xsl:value-of select="hits"/> -
      Total traffic: <xsl:value-of select="totalkb"/> kB<br/>
      CPU Load: <xsl:value-of select="cpu/@load"/>%<br/>
      <xsl:apply-templates select="stats"/>
      <xsl:value-of select="childcount/@busy"/> requests currently being
      processed, <xsl:value-of select="childcount/@idle"/> idle servers.<br/>
      <xsl:apply-templates select="shortscore"/>
      <xsl:apply-templates select="scoreboard"/>
    </body>
  </html>
  </xsl:template>

  <xsl:template match="stats">
    <xsl:value-of select="reqpersec"/> requests/sec -
    <xsl:value-of select="bytespersec"/> B/sec -
    <xsl:value-of select="bytesperreq"/> B/request<br/>
  </xsl:template>

  <xsl:template match="shortscore">
    <pre>
    <xsl:call-template name="shortscoreline">
      <xsl:with-param name="txt" select="."/>
    </xsl:call-template>
    </pre>
    Scoreboard Key: <br/>
    "<B><code>_</code></B>" Waiting for Connection, 
    "<B><code>S</code></B>" Starting up, 
    "<B><code>R</code></B>" Reading Request, <BR/>
    "<B><code>W</code></B>" Sending Reply, 
    "<B><code>K</code></B>" Keepalive (read), 
    "<B><code>D</code></B>" DNS Lookup,<BR/>
    "<B><code>L</code></B>" Logging, 
    "<B><code>G</code></B>" Gracefully finishing, 
    "<B><code>.</code></B>" Open slot with no current process<p/>
  </xsl:template>

  <!-- put a br every 64 characters -->
  <xsl:template name="shortscoreline">
    <xsl:param name="txt" select="."/>
    <xsl:if test="$txt != ''">
       <xsl:value-of select="substring($txt,0,64)"/><br/>      
       <xsl:call-template name="shortscoreline">
         <xsl:with-param name="txt" select="substring($txt,65)"/>
       </xsl:call-template>
    </xsl:if>
  </xsl:template>

  <xsl:template match="scoreboard">

    <table bgcolor="#ffffff" border="0">
      <tr bgcolor="#000000">
        <td><font face="Arial,Helvetica" color="#ffffff"><b>Srv</b></font></td>
        <td><font face="Arial,Helvetica" color="#ffffff"><b>PID</b></font></td>
        <td><font face="Arial,Helvetica" color="#ffffff"><b>Acc</b></font></td>
        <td><font face="Arial,Helvetica" color="#ffffff"><b>M</b></font></td>
        <td><font face="Arial,Helvetica" color="#ffffff"><b>SSL</b></font></td>
        <td><font face="Arial,Helvetica" color="#ffffff"><b>CPU</b></font></td>
        <td><font face="Arial,Helvetica" color="#ffffff"><b>SS</b></font></td>
        <td><font face="Arial,Helvetica" color="#ffffff"><b>Req</b></font></td>
        <td><font face="Arial,Helvetica" color="#ffffff"><b>Conn</b></font></td>
        <td><font face="Arial,Helvetica" color="#ffffff"><b>Child</b></font></td>
        <td><font face="Arial,Helvetica" color="#ffffff"><b>Slot</b></font></td>
        <td><font face="Arial,Helvetica" color="#ffffff"><b>Host</b></font></td>
        <td><font face="Arial,Helvetica" color="#ffffff"><b>VHost</b></font></td>
        <td><font face="Arial,Helvetica" color="#ffffff"><b>Request</b></font></td>
      </tr>

      <xsl:apply-templates select="child"/>

    </table>
    <hr/>       
          <table> 
          <tr><th>Srv</th><td>Child Server number - generation </td></tr>
          <tr><th>PID</th><td>OS process ID </td></tr>
          <tr><th>Acc</th><td>Number of accesses this connection / this child / this slot</td></tr>
          <tr><th>M</th><td>Mode of operation</td></tr>
          <tr><th>CPU</th><td>CPU usage, number of seconds</td></tr>
          <tr><th>SS</th><td>Seconds since beginning of most recent request</td></tr>
          <tr><th>Req</th><td>Milliseconds required to process most recent request</td></tr>
          <tr><th>Conn</th><td>Kilobytes transferred this connection</td></tr>
          <tr><th>Child</th><td>Megabytes transferred this child</td></tr>
          <tr><th>Slot</th><td>Total megabytes transferred this slot</td></tr>
        </table>
  </xsl:template>

  <xsl:template match="child">

    <tr bgcolor="#ffffff">
      <td><b><xsl:value-of select="@num"/>-<xsl:value-of select="generation"/></b></td>
      <td><xsl:value-of select="@pid"/></td>
      <td><xsl:value-of select="accesses/@connection"/>/<xsl:value-of select="accesses/@child"/>/<xsl:value-of select="accesses/@slot"/></td>
      <td><xsl:value-of select="@status"/></td>
      <td nowrap="true"><font face="Arial,Helvetica" size="-1"><xsl:value-of select="ssl"/></font></td>
      <xsl:variable name="cpu" select="round(100 * ( cpu/@u + cpu/@s + cpu/@cu + cpu/@cs ) ) div 100"/>
      <td><xsl:value-of select="$cpu"/></td>       
      <!-- mozilla doesn't do format-number 
      <xsl:variable name="cpu" select="cpu/@u + cpu/@s + cpu/@cu + cpu/@cs "/>
      <td><xsl:value-of select="format-number($cpu,'0.00')"/></td>       -->
      <td><xsl:value-of select="secondssince"/></td>
      <td><xsl:value-of select="reqtime"/></td>
      <td>
        <xsl:call-template name="displaybytes">
          <xsl:with-param name="bytes" select="connbytes"/>
        </xsl:call-template>
      </td>
      <td>
        <xsl:call-template name="displaybytes">
          <xsl:with-param name="bytes" select="mybytes"/>
        </xsl:call-template>
      </td>
      <td>
        <xsl:call-template name="displaybytes">
          <xsl:with-param name="bytes" select="bytes"/>
        </xsl:call-template>
      </td>
      <td nowrap="true"><font face="Arial,Helvetica" size="-1"><xsl:value-of select="client"/></font></td>
      <td nowrap="true"><font face="Arial,Helvetica" size="-1"><xsl:value-of select="vhost"/></font></td>
      <td nowrap="true"><font face="Arial,Helvetica" size="-1"><xsl:value-of select="request"/></font>
    </td>
  </tr>

  </xsl:template>

  <!-- Convert bytes into GB, MB, kB, or B -->

  <xsl:template name="displaybytes">
    <xsl:param name="bytes" select="."/>
    <xsl:choose>
      <xsl:when test="$bytes &lt; 5120">
        <xsl:value-of select="$bytes"/>B
      </xsl:when>
      <xsl:when test="$bytes &lt; 524288">
        <xsl:variable name="kb" select="round($bytes div 102.4) div 10"/>
        <xsl:value-of select="$kb"/>kB
        <!--        <xsl:variable name="kb" select="$bytes div 1024"/>
        <xsl:value-of select="format-number($kb,'0.#')"/>kB-->
      </xsl:when>
      <xsl:when test="$bytes &lt; 536870912">
        <xsl:variable name="mb" select="round($bytes div 104857.6) div 10"/>
        <xsl:value-of select="$mb"/>MB
        <!--        <xsl:variable name="mb" select="$bytes div 1048576"/>
        <xsl:value-of select="format-number($mb,'0.#')"/>MB-->
      </xsl:when>
      <xsl:otherwise>
        <xsl:variable name="gb" select="round($bytes div 107374182.4) div 10"/>
        <xsl:value-of select="$gb"/>GB
        <!-- <xsl:variable name="gb" select="$bytes div 1073741824"/>
        <xsl:value-of select="format-number($gb,'0.#')"/>GB-->
      </xsl:otherwise>
    </xsl:choose>
  </xsl:template>

  <!-- Convert uptime in seconds into X days, X hours, X mins, X seconds -->

  <xsl:template match="uptime">
    <xsl:variable name="uptime" select="."/>
    <xsl:variable name="secs" select="floor($uptime mod 60)"/>
    <xsl:variable name="mins" select="floor($uptime div 60) mod 60"/>
    <xsl:variable name="hrs" select="floor($uptime div 3600) mod 24"/>
    <xsl:variable name="days" select="floor($uptime div 86400)"/>
    <xsl:if test="$days = 1">1 day </xsl:if>
    <xsl:if test="$days > 1">
      <xsl:value-of select="$days"/> days
    </xsl:if>
    <xsl:if test="$hrs = 1">1 hour </xsl:if>
    <xsl:if test="$hrs > 1">
      <xsl:value-of select="$hrs"/> hours
    </xsl:if>
    <xsl:if test="$mins = 1">1 minute </xsl:if>
    <xsl:if test="$mins > 1">
      <xsl:value-of select="$mins"/> minutes
    </xsl:if>
    <xsl:if test="$secs = 1">1 second</xsl:if>
    <xsl:if test="$secs > 1">
      <xsl:value-of select="$secs"/> seconds
    </xsl:if>
  </xsl:template>

  <!-- Convert encoded date into readable date -->

  <xsl:template name="displaydate">
    <xsl:param name="date" select="."/>
    <xsl:variable name="mstr" select="'JanFebMarAprMayJunJulAugSepOctNovDec'"/>  
    <!-- Day Month Year -->
    <xsl:value-of select="number(substring($date, 7, 2))" />
    <xsl:text> </xsl:text>
    <xsl:value-of select="substring($mstr, number(substring($date,5,2))*3-2, 3)"/>
    <xsl:text> </xsl:text>
    <xsl:value-of select="substring($date, 1, 4)" />
    <xsl:text> </xsl:text>
    <!-- H:M:S TZ -->
    <xsl:value-of select="substring($date, 9, 2)" />
    <xsl:text>:</xsl:text>
    <xsl:value-of select="substring($date,11, 2)" />
    <xsl:text>:</xsl:text>
    <xsl:value-of select="substring($date,13, 2)" />
    <xsl:text> </xsl:text>
    <xsl:value-of select="substring($date,15, 3)" />
  </xsl:template>

</xsl:stylesheet>

