[go: up one dir, main page]

Menu

[r167]: / scripts / log2html.py  Maximize  Restore  History

Download this file

172 lines (145 with data), 4.1 kB

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
import sys, os
from Ft.Xml.Xslt import Transform
"""
log2html.py is a script that takes a SQL prevent log as input, and produces an html representation as output.
"""
USAGE = """
usage:
log2html.py <sqlprevent_application.log>
"""
__author__ = 'Levi Stoddard'
__date__ = 'May 20th, 2009'
# get log file to parse
if len(sys.argv) != 2:
print(USAGE)
exit(1)
input_file = sys.argv[1]
# read xml to transform
xml_file = open(input_file, 'r')
xml_str = "<log>%s</log>" % xml_file.read()
xml_file.close()
# xslt that will be applied to the log file
xslt_str = """
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:date="http://exslt.org/dates-and-times"
extension-element-prefixes="date">
<xsl:output method="html"
indent="yes"
doctype-public="-//W3C//DTD XHTML 1.0 Strict//EN"
doctype-system="http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"/>
<xsl:template match="/">
<html>
<head>
<style type="text/css">
/*
* query styles
*/
tr.query { background: blue; }
tr.specifics { background: yellow; }
tr.even { background: #99DDFF; }
tr.odd { background: #99BBFF; }
.tainted { color: red; }
/*
* error styles
*/
tr.error { background: red; }
</style>
</head>
<body>
<h1><xsl:value-of select="concat('SQL Prevent Log (', date:date-time(), ')')" /></h1>
<xsl:apply-templates />
</body>
</html>
</xsl:template>
<xsl:template match="log">
<xsl:apply-templates />
</xsl:template>
<xsl:template match="error|query">
<xsl:variable name="name" select="name()"/>
<p>
<table border="1">
<xsl:call-template name="header">
<xsl:with-param name="title"><xsl:value-of select="$name" /></xsl:with-param>
<xsl:with-param name="class"><xsl:value-of select="$name" /></xsl:with-param>
</xsl:call-template>
<xsl:apply-templates />
</table>
</p>
</xsl:template>
<xsl:template match="performance">
<xsl:apply-templates />
</xsl:template>
<xsl:template match="statement">
<xsl:apply-templates />
</xsl:template>
<xsl:template match="time-start|time-stop|time-delta|benign|time|msg|trace">
<tr>
<td><xsl:value-of select="concat(name(), ': ')" /></td>
<td><xsl:value-of select="." /></td>
</tr>
</xsl:template>
<xsl:template match="sql">
<tr>
<td>sql:</td>
<td>
<xsl:apply-templates />
</td>
</tr>
</xsl:template>
<xsl:template match="tainted|clean">
<span>
<xsl:attribute name="class">
<xsl:value-of select="name()" />
</xsl:attribute>
<xsl:value-of select="." />
</span>
</xsl:template>
<xsl:template match="specifics">
<xsl:if test="count(./*) > 0">
<xsl:call-template name="header">
<xsl:with-param name="title">specifics</xsl:with-param>
<xsl:with-param name="class">specifics</xsl:with-param>
</xsl:call-template>
<xsl:for-each select="./*">
<tr>
<!-- give rows alternating colors-->
<xsl:attribute name="class">
<xsl:choose>
<xsl:when test="position() mod 2 = 1">odd</xsl:when>
<xsl:otherwise>even</xsl:otherwise>
</xsl:choose>
</xsl:attribute>
<!-- node name -->
<td>
<xsl:value-of select="name()" />
</td>
<!-- node value/attributes -->
<td>
<xsl:value-of select="." />
<xsl:for-each select="./@*">
<xsl:value-of select="concat(' (', name(), ': ', ., ')')"/>
</xsl:for-each>
</td>
</tr>
</xsl:for-each>
</xsl:if>
</xsl:template>
<xsl:template name="header">
<xsl:param name="title" />
<xsl:param name="class" />
<tr>
<xsl:attribute name="class"><xsl:value-of select="$class"/></xsl:attribute>
<th><xsl:value-of select="$title" /></th>
<th></th>
</tr>
</xsl:template>
</xsl:stylesheet>
"""
# transform against xslt, and write to output file
result = Transform(xml_str, xslt_str)
base_name = os.path.splitext(input_file)[0]
html_file = open("%s.html" % base_name, 'w+')
html_file.write(result)
html_file.close()
print("html output written to %s" % html_file.name)