ServerCorps
11-09-2002, 01:46 AM
Heres a VBscript I whipped up to measure the amout of bandwidth a site uses per month. It loops through all sites installed on a server and reports bytes, kbytes or mbytes based on how much was used. Place the guts below in a VBScript file (*.vbs). You need to set your log files up to log monthly, so you only have to loop through 1 file per site per month. I didn't automate this script to look for last months logs, but that's what I'm going to do on my servers. She will currently crap out if a logfile doesn't exist in the specified dir. You should add some error handlers to avoid this.
You also need to specify the correct path to where your logs are. In this example they are in d:\logfiles\w3svc<site#>\:
Enjoy.
Const WebLogRoot = "d:\logfiles\w3svc"
dim log
Set log = CreateObject ("MSWC.IISLog")
wscript.echo "There are " & CountSites("w3svc", "IIsWebServer", "WEB") & " sites installed."
for I = 1 to CountSites("w3svc", "IIsWebServer", "WEB")
log.OpenLogFile WebLogRoot & I & "\ex0211.log", 1, "W3SVC", I , "W3C Extended Log File Format"
dim SentBytes, RecBytes
SentBytes = 0
RecBytes = 0
log.ReadLogRecord 'get the first record of the file
do while not log.AtEndOfLog
SentBytes = SentBytes + log.BytesSent ' add it up
RecBytes = RecBytes + log.BytesReceived ' add it up
log.ReadLogRecord 'read another line,
loop ' and do it again
if sentbytes > 1024 then 'its at least a kb
sentbytes = sentbytes/1024 ' get kbytes
if sentbytes > 1024 then 'its over a meg
sentbytes = sentbytes/1024 ' sentbytes now = mb
wscript.echo "Site " & I & ":" & SentBytes & " mbytes sent."
else
wscript.echo "Site " & I & ":" & SentBytes & " kbytes sent."
end if
else ' it wasn't over a k
wscript.echo "Site " & I & ":" & SentBytes & " bytes sent."
end if
' if you bill on total bytes xferred and not just sent bytes, do it for the
'received bytes as well
' wscript.echo "Site " & I & ":" & RecBytes & " kbytes received."
log.CloseLogFiles 1 ' clean up
next 'move on the the next site
'***********************************
'* CountSites enumerates the websites installed on a server
'* returns the count
'***********************************
Function CountSites(SiteService, SiteClass, Sitedescription)
Dim IISOBJ, Site, Extra
Sites = 0
on error resume next
Set IISOBJ = GetObject("IIS://localhost/" & SiteService)
for each site in IISOBJ
if (Site.Class = SiteClass) then
Sites = sites + 1
end if
next
Set IISOBJ = nothing
CountSites = Sites
end Function
You also need to specify the correct path to where your logs are. In this example they are in d:\logfiles\w3svc<site#>\:
Enjoy.
Const WebLogRoot = "d:\logfiles\w3svc"
dim log
Set log = CreateObject ("MSWC.IISLog")
wscript.echo "There are " & CountSites("w3svc", "IIsWebServer", "WEB") & " sites installed."
for I = 1 to CountSites("w3svc", "IIsWebServer", "WEB")
log.OpenLogFile WebLogRoot & I & "\ex0211.log", 1, "W3SVC", I , "W3C Extended Log File Format"
dim SentBytes, RecBytes
SentBytes = 0
RecBytes = 0
log.ReadLogRecord 'get the first record of the file
do while not log.AtEndOfLog
SentBytes = SentBytes + log.BytesSent ' add it up
RecBytes = RecBytes + log.BytesReceived ' add it up
log.ReadLogRecord 'read another line,
loop ' and do it again
if sentbytes > 1024 then 'its at least a kb
sentbytes = sentbytes/1024 ' get kbytes
if sentbytes > 1024 then 'its over a meg
sentbytes = sentbytes/1024 ' sentbytes now = mb
wscript.echo "Site " & I & ":" & SentBytes & " mbytes sent."
else
wscript.echo "Site " & I & ":" & SentBytes & " kbytes sent."
end if
else ' it wasn't over a k
wscript.echo "Site " & I & ":" & SentBytes & " bytes sent."
end if
' if you bill on total bytes xferred and not just sent bytes, do it for the
'received bytes as well
' wscript.echo "Site " & I & ":" & RecBytes & " kbytes received."
log.CloseLogFiles 1 ' clean up
next 'move on the the next site
'***********************************
'* CountSites enumerates the websites installed on a server
'* returns the count
'***********************************
Function CountSites(SiteService, SiteClass, Sitedescription)
Dim IISOBJ, Site, Extra
Sites = 0
on error resume next
Set IISOBJ = GetObject("IIS://localhost/" & SiteService)
for each site in IISOBJ
if (Site.Class = SiteClass) then
Sites = sites + 1
end if
next
Set IISOBJ = nothing
CountSites = Sites
end Function
