File: genhtml.py

package info (click to toggle)
radiuscontext 1.76-3
  • links: PTS
  • area: main
  • in suites: woody
  • size: 176 kB
  • ctags: 135
  • sloc: python: 1,120; perl: 57; sh: 50; makefile: 40
file content (224 lines) | stat: -rw-r--r-- 7,924 bytes parent folder | download | duplicates (2)
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
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
#!/usr/bin/python
#
#  Copyright (c) 1998-2001 Sean Reifschneider, tummy.com, ltd.  
#	All Rights Reserved.
#
#  Module which generates HTML-format reports.  Is called by the generator.py
#  module *ONLY*.  Requires only being imported by the "stdreport" program,
#  it automatically registers with the generator to generate reports.
#
#  http://www.tummy.com/radiusContext/
#  ftp://ftp.tummy.com/pub/tummy/radiusContext/

import time
import generator
import os
from radiussupp import *

class genhtml:
	genname = "HTML"

	def __init__(self):
		self.out = None
		self.outIndex = None
		self.outMainIndex = None
		self.reportDirName = "."
		self.reportIndexName = "index"
		self.reportSuffix = ".html"
		self.splitNum = None
		self.lastIndexDir = None

	def setSplit(self, split = None):
		self.splitNum = split

	def reportDir(self, name):
		self.reportDirName = name

	def reportIndex(self, name):
		self.reportIndexName = name

	def Start(self, sesData):
		dir = self.reportDirName + '/'
		if self.splitNum > 0:
			dir = '%s%s/' % ( dir, sesData['userName'][:self.splitNum] )

		try: os.mkdir(dir)
		except os.error: pass
		outputName = dir + sesData["userName"] + self.reportSuffix
		try:
			self.out = open(outputName, "w")
		except IOError:
			sys.stdout.write('Error writing file "%s"' % outputName)
			sys.stdout.write('Make sure it\'s directory exists and is writable.\n')
			sys.exit(1)

		title = "Session report for: %s\n" % ( sesData["userName"] )
		self.out.write("<HTML>\n<HEAD>\n<TITLE>" + title + "</TITLE>\n</HEAD>\n")
		self.out.write("<BODY><H1>" + title + "</H1>\n")
		self.out.write("<TABLE BORDER=1>\n")
		self.out.write("<TR><TH>Login Date</TH><TH>Time<BR>This</TH>")
		self.out.write("<TH>Time<BR>Total</TH>")
		self.out.write("<TH>BW<BR>In/Out</TH><TH>Caller ID</TH>")
		self.out.write("<TH>IP Address</TH><TH>Reason</TH></TR>\n")

	def Stop(self, sesData):
		if self.out != None:
			self.out.write("</TABLE>\n")
			self.out.write("<TABLE BORDER=1>\n")
			self.out.write("<TR><TD>Total Online Time:</TD><TD>%s</TD></TR>" %
					( secsToStr(sesData["timeOn"]) ))
			self.out.write("<TR><TD>Days on:</TD><TD>%d</TD></TR>\n"
					% ( sesData["numDays"] ))
			self.out.write("<TR><TD>Average Online Times:</TD><TD>%s per day"
					% ( secsToStr(sesData["timeOn"] / sesData["numDays"]) ))
			self.out.write("</TD><TD>%s per session</TD></TR>\n" %
					(secsToStr(sesData["timeOn"] / sesData["sessionCount"]) ))

			self.out.write("<TR><TD>Total Data Transferred In/Out:</TD>")
			self.out.write("<TD>%s/%s</TD></TR>\n" %
					( bytesToStr(sesData["bytesIn"]),
					bytesToStr(sesData["bytesOut"]) ))
			self.out.write("<TR><TD>Rate In/Out:</TD><TD>%s/%s</TD>\n" %
					( sesData["bwIn"], sesData["bwOut"] ))
			self.out.write("</TABLE>\n")
			self.out.write("\n<P>Generated by <A HREF=\"http://www.tummy.com/" +
					"radiusContext/\">radiusContext</A>\n")
			self.out.write("</BODY></HTML>\n")
			self.out.close()
			self.out = None

	def Item(self, userData, sesData):
		termReason = sesData.get("Acct-Terminate-Cause", "")
		if termReason == "User-Request":
			termReason = ""
		if not termReason:
			termReason = sesData.get("Ascend-Disconnect-Cause", "")

		#  get caller-id information
		callerId = ''
		if sesData.has_key('Calling-Station-Id'):
			callerId = sesData['Calling-Station-Id']
		if sesData.has_key("Caller-Id"):
			sessCallerID = sesData['Caller-Id']

		#  get IP address information
		ipAddr = ''
		if sesData.has_key('Framed-IP-Address'):
			ipAddr = sesData['Framed-IP-Address']

		#  write out record
		self.out.write("<TR><TD>%-23s</TD><TD>%8s</TD><TD>%8s</TD>" %
				( sesData["Session-Start-Date"],
				secsToStr(sesData["currentTime"], 2),
				secsToStr(userData["timeOn"], 2) ))
		self.out.write("<TD>%6s/%-6s</TD><TD>%s</TD><TD>%s</TD>"
				"<TD>%s</TD></TR>\n" % ( sesData["bwIn"], sesData["bwOut"],
				callerId, ipAddr, termReason ))

	def IndexStart(self):
		self.outMainIndex = open(self.reportDirName + "/" + self.reportIndexName
				+ self.reportSuffix, "w")
		title = "Overall Dial-In Report generated %s" % (
				time.strftime("%A, %B %d, %Y", time.localtime(time.time())) )
		self.IndexWriteUserHeader(self.outMainIndex, title)

	def IndexStop(self, indexData):
		if self.outIndex != None:
			self.outIndex.write("</TABLE>\n")
			self.outIndex.close()
			self.outIndex = None

		if self.outMainIndex != None:
			self.outMainIndex.write("</TABLE>\n")
			self.outMainIndex.write("<TABLE BORDER=1>\n")

			self.outMainIndex.write(
					"<TR><TD>Average Online Time:</TD><TD></TD><TD>%s per user" %
					( secsToStr(indexData["timePerUser"]) ))
			self.outMainIndex.write("</TD><TD>%s per session</TD></TR>\n" %
					( secsToStr(indexData["timePerSes"]) ))
			self.outMainIndex.write("<TR><TD>Average Sessions:</TD><TD></TD>")
			self.outMainIndex.write("<TD>%d per user</TD></TR>\n" %
					( indexData["sesPerUser"] ))
			self.outMainIndex.write("<TR><TD>Data In/Out:</TD><TD>%s/%s total</TD>"
					% ( bytesToStr(indexData["bytesIn"]),
					bytesToStr(indexData["bytesOut"]) ))
			self.outMainIndex.write("<TD>%s/%s per user</TD>" %
					( bytesToStr(indexData["dataInPerUser"]),
					bytesToStr(indexData["dataOutPerUser"]) ))
			self.outMainIndex.write("<TD>%s/%s per session</TD></TR>\n" %
					( bytesToStr(indexData["dataInPerSes"]),
					bytesToStr(indexData["dataOutPerSes"]) ))

			self.outMainIndex.write("<TR><TD>Throughput In/Out:</TD>")
			self.outMainIndex.write("<TD>%s/%s total</TD>" %
					( bytesToStr(indexData["bwIn"]),
					bytesToStr(indexData["bwOut"]) ))
			self.outMainIndex.write("<TD>%s/%s per user</TD></TR>\n" %
					( bytesToStr(indexData["bwInUser"]),
					bytesToStr(indexData["bwOutUser"]) ))
			self.outMainIndex.write("</TABLE>\n")
			self.outMainIndex.write("\n<P>Generated by " +
					"<A HREF=\"http://www.tummy.com/radiusContext/\">" +
					"radiusContext</A>\n")
			self.outMainIndex.write("</BODY></HTML>\n")
			self.outMainIndex.close()
			self.outMainIndex = None


	####################################
	#  write the user header information

	def IndexWriteUserHeader(self, fp, title):
		fp.write("<HTML>\n<HEAD>\n<TITLE>" + title
				+ "</TITLE>\n</HEAD>\n")
		fp.write("<BODY><H1>" + title + "</H1>\n")
		fp.write("<TABLE BORDER=1>\n")
		fp.write("<TR><TH>User Name</TH><TH>Session<BR>Count</TH><TH>")
		fp.write("Time<BR>Total</TH><TH>Time<BR>Per Day</TH><TH>")
		fp.write("Time<BR>Per Ses</TH><TH>BW<BR>In/Out</TH></TR>\n")


	def IndexItem(self, indexData, userData):
		dir = ''
		if self.splitNum > 0:
			dir = userData['userName'][:self.splitNum]
		else:
			dir = '.'

		#  re-open split index
		out = self.outMainIndex
		if self.splitNum > 0:
			if dir != self.lastIndexDir and self.outIndex != None:
				self.outIndex.write("</TABLE>\n</BODY>\n</HTML>\n")
				self.outIndex.close()
				self.outIndex = None

			if self.outIndex == None:
				self.outIndex = open(self.reportDirName + '/' + dir
						+ '/' + self.reportIndexName + self.reportSuffix, 'w')
				title = "Dial-In Report ``%s'' generated %s" % ( dir,
						time.strftime("%A, %B %d, %Y", time.localtime(time.time())) )
				self.IndexWriteUserHeader(self.outIndex, title)

				self.outMainIndex.write('<TR><TD><A HREF="%s/%s%s">%s</A><BR>'
						'</TD></TR>\n' %
						( dir, self.reportIndexName, self.reportSuffix, dir ))
			out = self.outIndex

		#  remember the last index dir name
		self.lastIndexDir = dir

		#  write index
		out.write("<TR><TD><A HREF=\"" + userData["userName"]
				+ self.reportSuffix + "\">%s</TD><TD>%d</TD><TD>%s</TD><TD>" %
				( userData["userName"], userData["sessionCount"],
				secsToStr(userData["timeOn"], 2) ))
		out.write("%s</TD><TD>%s</TD><TD>%s/%s</TD><TD></TR>\n" %
				( secsToStr(userData["timeOn"] / userData["numDays"], 2),
				secsToStr(userData["timeOn"] / userData["sessionCount"], 2),
				bytesToStr(userData["bytesIn"]),
				bytesToStr(userData["bytesOut"]) ))
		out.flush()

generator.append(genhtml())