Web Hosting Talk







View Full Version : Measure Bandwidth used per site in IIS


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

TheRealDeal
11-09-2002, 11:14 PM
Hmm.. interesting.

Can you provide detail instructions on getting this up? I'm a newbie.

ServerCorps
11-11-2002, 01:45 AM
you just need to know where your w3svc logs are. by default they are in \WINDOWS\system32\Logfiles\W3SVCn

where n is the web site number

change the siteroot constant to where this is, leaving off the site number at the end.

Currently the script requires you to change the name of the logfile to match the dates you are looking for. I rotate logfiles monthly, so the script sample above is looking for ex0211.log, for Novermber 2002.

save and run the script from a command prompt, like:

d:\LogFiles>cscript ShowBandwidth.vbs

It will respond with:

There are 8 sites installed.
Site 1 : 6.46625804901123 mbytes sent.
Site 2 : 98.9921875 kbytes sent.
Site 3 : 4.14982795715332 mbytes sent.
Site 4 : 86.6905479431152 mbytes sent.
Site 5 : 205.2255859375 kbytes sent.
Site 6 : 12.294921875 kbytes sent.
Site 7 : 0 bytes sent.
Site 8 : 342.1923828125 kbytes sent.

D:\LogFiles>

WonderMonkey
11-11-2002, 09:34 AM
Interesting. I was just going to do a version of this in .NET and will use this as a guide if you don't mind. Being new to Hosting I wasn't quite sure how traffic was maintained though I knew the information was also in the logs.

Currently I am writing a small app that will run a series of scripts and email the results to the owners of the sites. Right now I am planning or have done the following:
- How much space is a client using and how much did they buy. Also to include how much SQL Server space they are using and how much they have left.
- How much traffic they have used. Haven't done this one but was planning to. Will use your script as a starting point.
- Other stuff. Not sure what. Maybe how many emails they have set up, etc.

ServerCorps
11-11-2002, 09:57 AM
How much space is a client using and how much did they buy. Also to include how much SQL Server space they are using and how much they have left.


I just posted another script last night in the technical/security that enumerates your user dirs and reports space used by each subdir. It will help you calculate your space usage:

http://www.webhostingtalk.com/showthread.php?s=&threadid=87361

WonderMonkey
11-11-2002, 11:09 AM
Thanks, I'll look at it. I already have that particular piece done but always like to see how others did it.

Mine is in .NET but most times the basic flow is the same.

WonderMonkey
11-11-2002, 11:14 AM
Hey nikko go here:
http://www.webhostingtalk.com/showthread.php?s=&threadid=88095