User Tools

Site Tools


windows:autoit:bandwidth_reporter

I had some problems with bandwidth in a low-bandwidth office. Everybody denied using up the bandwidth, so I wrote this script and launched it on their PCs so I could see who was eating up the bandwidth.

BWRep.au3
#Include <Array.au3>
; Check WMI network data thingy script
Opt("TrayIconHide", 1)
 
; Base vars
$FirstLoop = 1
$LoopMS = 10000
$LoopS = $LoopMS/1000
$DivideBy = 1024
 
; Log
Global $MainLog = "\\192.168.222.222\log\bw\" & @ComputerName & ".txt"
Func logPrint($text)
; No log? Create one + write.
	$StampFormat = @YEAR & "-" & @MON & "-" & @MDAY & " " & @HOUR & ":" & @MIN & ":" & @SEC
 
	If Not FileExists($MainLog) Then
		FileWriteLine($MainLog, "Log created: " & $StampFormat)
	EndIf
 
	FileWriteLine($MainLog, $StampFormat & " [" & @AutoItPID & "] >> " & $text)
; Return.
	Return 0
EndFunc   ;==>logPrint
 
$wbemFlagReturnImmediately = 0x10
$wbemFlagForwardOnly = 0x20
 
$objWMIService = ObjGet("winmgmts:\\.\root\cimv2")
 
; Fetching interfaces list to build an array
$iflist = $objWMIService.ExecQuery("SELECT Name FROM Win32_PerfRawData_Tcpip_NetworkInterface")
$iflp = 0
For $interface In $iflist
	$iflp = $iflp+1
Next
; Declare the interfaces array size of the $iflp + 3 subarray-entry-thingies
; Subarray explanation: 0 = name, 1 = LastUp, 2 = LastDown
Local $InterfacesArr[$iflp][3]
 
While 1
; Fetch total bandwidth, used to calculate bandwidth usage since last X seconds
	$smart = $objWMIService.ExecQuery("SELECT Name, BytesReceivedPersec, BytesSentPersec, PacketsPersec FROM Win32_PerfRawData_Tcpip_NetworkInterface")
 
	If IsObj($smart) then
; Declare interfaces loop counter
		$IFLOOPCNT = 0
		For $item In $smart
;			If $item.BytesReceivedPersec > 0 AND $item.BytesSentPersec > 0 Then
; Getting data? Must be a valid interface.
;			If $item.PacketsPersec > 0 AND !StringInStr($item.Name, "MS TCP Loopback interface") Then
;			If $item.PacketsPersec > 0 AND $item.Name = "Intel[R] 82566DM Gigabit Network Connection" Then
;				MsgBox(64, "pausefisk", "Interface: " & $item.Name & @CRLF & "Incomming: " & $item.BytesReceivedPersec & @CRLF & "Outgoing: " & $item.BytesSentPersec & @CRLF)
;				If ($LastUp > 0 AND $LastDown > 0) OR $FirstLoop = 1 Then
					$InterfacesArr[$IFLOOPCNT][0] = $item.Name
					$TmpUp = $item.BytesSentPersec
					$TmpDown = $item.BytesReceivedPersec
; Set value
					$TUP = $TmpUp-$InterfacesArr[$IFLOOPCNT][1]
; Calculate total KB
					$TUP = $TUP/1024
; Calculate KB/s
					$TUP = $TUP/$LoopS
; Repeating, just with down instead of up.
					$TDOWN = $TmpDown-$InterfacesArr[$IFLOOPCNT][2]
					$TDOWN = $TDOWN/1024
					$TDOWN = $TDOWN/$LoopS
					If $FirstLoop <> 1 AND ($TUP > 0 OR $TDOWN > 0) Then
						logPrint("Down:" & $TDOWN & " - Up:" & $TUP & " - Iface:" & $InterfacesArr[$IFLOOPCNT][0])
					EndIf
					$FirstLoop = 0
; Place values in vars.
;					$LastUp = $TmpUp
;					$LastDown = $TmpDown
					$InterfacesArr[$IFLOOPCNT][1] = $TmpUp
					$InterfacesArr[$IFLOOPCNT][2] = $TmpDown
;				EndIf
;			EndIf
; Increment interfaces loop counter
			$IFLOOPCNT = $IFLOOPCNT+1
		Next
	Else
; Do we need any output here? Perhaps some serverfeedback?
	EndIf
	Sleep($LoopMS)
WEnd
</code>
 
The bash script I wrote to make a better overview for myself.
<code - MonitorBWUsage.sh>
#!/bin/bash
while true; do
# Reset counters
	DLSTATS=0
	ULSTATS=0
# Loop-de-loooooooop! :D
	for x in `ls *.txt | grep -v "DESOVD"`; do
		XNEW=`echo $x | cut -d'.' -f1`
		echo -n "CHECKING $XNEW --- "
		LOGDATA=`tail -n 3 $x | grep -v "MS TCP Loopback interface" | tail -n 1`
#echo `expr length $LOGDATA`
#exit
		if [ -n "$LOGDATA" ] && [ `echo $LOGDATA | wc -c` -gt 30 ]; then
			echo $LOGDATA
			TMPDL=`echo $LOGDATA | awk {'print $5'} | cut -d':' -f2`
			DLSTATS=`echo $DLSTATS + $TMPDL | bc`
			TMPUL=`echo $LOGDATA | awk {'print $7'} | cut -d':' -f2`
			ULSTATS=`echo $ULSTATS + $TMPUL | bc`
		else
			echo "ERROR, missing data - Sleeping 1 second to allow the PC to write data for next loop."
			sleep 1
		fi
 
#echo "TMPDL=$TMPDL --- DLSTATS=$DLSTATS"
#echo "TMPUL=$TMPUL --- ULSTATS=$ULSTATS"
#exit
	done
	echo "STATS - DL:$DLSTATS - UL:$ULSTATS"
 
	echo "-----------------------------------------------------------------"
	sleep 10
done
windows/autoit/bandwidth_reporter.txt · Last modified: 2015/08/15 22:56 by 127.0.0.1

Donate Powered by PHP Valid HTML5 Valid CSS Driven by DokuWiki