Jump to content

BandWidth Monitor


SoulA
 Share

Recommended Posts

I tried to improve upon Lod3n's bandwidth monitor and have it support more than one interface.

EDIT: put up a nicer version combining aceguy's code and some other stuff.

#include <ButtonConstants.au3>
#include <GUIConstantsEx.au3>
#include <WindowsConstants.au3>

$aLabel = _GetNames(_GetObj())
Dim $aCheckBox[$aLabel[0] + 1][2]
Dim $aAdapter[2]

$hSelection = GUICreate("Selection", 300, $aLabel[0] * 30 + 30, -1, -1, -1, BitOR($WS_EX_TOOLWINDOW, $WS_EX_WINDOWEDGE))
If $aLabel[0] = 0 Then
    MsgBox(0, "Error", "No Adapters Found", 0, $hSelection)
    Exit
EndIf

For $i = 1 To $aLabel[0]
    $aCheckBox[$i][0] = GUICtrlCreateCheckbox($aLabel[$i], 8, $i * 20, 290)
    $aCheckBox[$i][1] = $aLabel[$i]
Next
$aCheckBox[0][0] = $aLabel[0]

$Button1 = GUICtrlCreateButton("okay", 100, $aLabel[0] * 30, 75)
GUISetState(@SW_SHOW)

While 1
    Sleep(50)
    $nMsg = GUIGetMsg()
    Switch $nMsg
        Case $GUI_EVENT_CLOSE
            Exit
        Case $Button1
            $i1 = 1
            $bExit = 0
            For $i = 1 To $aCheckBox[0][0]
                If GUICtrlRead($aCheckBox[$i][0]) = 1 Then
                    $aAdapter[0] = $i1
                    ReDim $aAdapter[$i1 + 1]
                    $aAdapter[$i1] = $aCheckBox[$i][1]
                    $i1 += 1
                    $bExit = 1
                EndIf
            Next
            If $bExit = 1 Then
                GUIDelete($hSelection)
                ExitLoop
            Else
                MsgBox(0, "Error", "No Adapter Selected", 0, $hSelection)
            EndIf
    EndSwitch
WEnd

Opt("GUIOnEventMode", 1)
$hMonitor = GUICreate("Monitor", 150, 100, -1, -1, -1, BitOR($WS_EX_TOOLWINDOW, $WS_EX_WINDOWEDGE))
GUISetFont(12)
$Label1 = GUICtrlCreateLabel("", 8, 8, 100, 24)
$Label2 = GUICtrlCreateLabel("", 8, 23, 100, 24)
$Label3 = GUICtrlCreateLabel("", 8, 38, 100, 24)
$Label4 = GUICtrlCreateLabel("", 8, 53, 100, 24)
$label5 = GUICtrlCreateLabel("", 8, 68, 100, 24)
GUISetOnEvent($GUI_EVENT_CLOSE, "_Exit")
GUISetState(@SW_SHOW)
$Data_now = _GetData(_GetObj(), $aAdapter)
GUICtrlSetData($Label5, Round($Data_now[0] / 073741824, 2) & "Total GB")
While 1
    $btmp= _GetData(_GetObj(), $aAdapter)
    Sleep(1000)

    $aData = _GetData(_GetObj(), $aAdapter)

    $bData = $aData[0] - $Data_now[0]
   
    $difftmp= ($aData[0]-$btmp[0])
   
    GUICtrlSetData($Label1, Round($difftmp, 2) & " Diff")
   
    GUICtrlSetData($Label2, Round($bData / 1024, 2) & " KB")

    GUICtrlSetData($Label3, Round($bData / 1048576, 2) & " MB")

    GUICtrlSetData($Label4, Round($bData / 073741824, 2) & " GB")
WEnd

Func _GetNames($colItems)
    Local $aName[2], $i = 1
    If IsObj($colItems) Then
        For $objItem In $colItems
            $aName[0] = $i
            ReDim $aName[$i + 1]
            $aName[$i] = $objItem.Name
            $i += 1
        Next
    EndIf
    Return $aName
EndFunc   ;==>_GetNames

Func _GetData($colItems, $aAdapter)
    Local $aData[2]
    If IsObj($colItems) Then
        For $objItem In $colItems
            For $i = 1 To $aAdapter[0]
                If $objItem.Name = $aAdapter[$i] Then
                    $aData[0] += $objItem.BytesReceivedPersec
                    $aData[1] += $objItem.BytesSentPersec
                EndIf
            Next
        Next
    EndIf
    Return $aData
EndFunc   ;==>_GetData

Func _GetObj()
    Local $wbemFlagReturnImmediately = 0x10, $wbemFlagForwardOnly = 0x20, $colItems = "", $strComputer = "localhost"
    $objWMIService = ObjGet("winmgmts:\\" & $strComputer & "\root\CIMV2")

    $colItems = $objWMIService.ExecQuery("SELECT BytesReceivedPersec,BytesSentPersec,Name FROM Win32_PerfRawData_Tcpip_NetworkInterface", "WQL", $wbemFlagReturnImmediately + $wbemFlagForwardOnly)
    Return $colItems
EndFunc   ;==>_GetObj

Func _Exit()
    Exit
EndFunc
Edited by SoulA
Link to comment
Share on other sites

The numbers seem to work great.. But could display it in kilobytes? I would actually do something like this:

Gigabytes: 0

Megabytes:12

Kilobytess:500

Bytes:40

And as soon as there are 1024 kilobytes, it goes up megabyte and resets the kilobyte label to 0, same for megabytes to gigabytes and bytes to kilobytes..

My Programs[list][*]Knight Media Player[*]Multiple Desktops[*]Daily Comics[*]Journal[/list]
Link to comment
Share on other sites

SWEET Prog.

#include <ButtonConstants.au3>
#include <GUIConstantsEx.au3>
#include <WindowsConstants.au3>

$aLabel = _GetNames(_GetObj())
Dim $aCheckBox[$aLabel[0] + 1][2]
Dim $aAdapter[2]

$hSelection = GUICreate("Selection", 300, $aLabel[0] * 45, -1, -1, -1, BitOR($WS_EX_TOOLWINDOW, $WS_EX_WINDOWEDGE))
If $aLabel[0] = 0 Then
    MsgBox(0, "Error", "No Adapters Found", 0, $hSelection)
    Exit
EndIf

For $i = 1 To $aLabel[0]
    $aCheckBox[$i][0] = GUICtrlCreateCheckbox($aLabel[$i], 8, $i * 20, 290)
    $aCheckBox[$i][1] = $aLabel[$i]
Next
$aCheckBox[0][0] = $aLabel[0]

$Button1 = GUICtrlCreateButton("okay", 100, $aLabel[0] * 30, 75)
GUISetState(@SW_SHOW)

While 1
    Sleep(50)
    $nMsg = GUIGetMsg()
    Switch $nMsg
        Case $GUI_EVENT_CLOSE
            Exit
        Case $Button1
            $i1 = 1
            $bExit = 0
            For $i = 1 To $aCheckBox[0][0]
                If GUICtrlRead($aCheckBox[$i][0]) = 1 Then
                    $aAdapter[0] = $i1
                    ReDim $aAdapter[$i1 + 1]
                    $aAdapter[$i1] = $aCheckBox[$i][1]
                    $i1 += 1
                    $bExit = 1
                EndIf
            Next
            If $bExit = 1 Then
                GUIDelete($hSelection)
                ExitLoop
            Else
                MsgBox(0, "Error", "No Adapter Selected", 0, $hSelection)
            EndIf
    EndSwitch
WEnd

$hMonitor = GUICreate("Monitor", 212, 118, -1, -1, -1, BitOR($WS_EX_TOOLWINDOW, $WS_EX_WINDOWEDGE))
GUISetFont(12)
$Label1 = GUICtrlCreateLabel("", 8, 8, 100, 24)
$Label2 = GUICtrlCreateLabel("", 8, 23, 100, 24)
$Label3 = GUICtrlCreateLabel("", 8, 38, 100, 24)
$Label4 = GUICtrlCreateLabel("", 8, 53, 100, 24)
GUISetState(@SW_SHOW)
$Data_now = _GetData(_GetObj(), $aAdapter)
ConsoleWrite($Data_now[0])
While 1
    $btmp= _GetData(_GetObj(), $aAdapter)
    Sleep(1000)

    If GUIGetMsg() = $GUI_EVENT_CLOSE Then Exit
    $aData = _GetData(_GetObj(), $aAdapter)

    $bData = $aData[0] - $Data_now[0]
    
    $difftmp= ($aData[0]-$btmp[0])
    
    GUICtrlSetData($Label1, Round($difftmp, 2) & " Diff")
    
    GUICtrlSetData($Label2, Round($bData / 1024, 2) & " KB")

    GUICtrlSetData($Label3, Round($bData / 1048576, 2) & " MB")

    GUICtrlSetData($Label4, Round($bData / 073741824, 2) & " GB")
WEnd

Func _GetNames($colItems)
    Local $aName[2], $i = 1
    If IsObj($colItems) Then
        For $objItem In $colItems
            $aName[0] = $i
            ReDim $aName[$i + 1]
            $aName[$i] = $objItem.Name
            $i += 1

        Next
    EndIf
    Return $aName
EndFunc   ;==>_GetNames

Func _GetData($colItems, $aAdapter)
    Local $aData[2]
    If IsObj($colItems) Then
        For $objItem In $colItems
            For $i = 1 To $aAdapter[0]
                If $objItem.Name = $aAdapter[$i] Then
                    $aData[0] += $objItem.BytesReceivedPersec
                    $aData[1] += $objItem.BytesSentPersec
                EndIf
            Next
        Next
    EndIf
    Return $aData
EndFunc   ;==>_GetData

Func _GetObj()
    Local $wbemFlagReturnImmediately = 0x10, $wbemFlagForwardOnly = 0x20, $colItems = "", $strComputer = "localhost"
    $objWMIService = ObjGet("winmgmts:\\" & $strComputer & "\root\CIMV2")

    $colItems = $objWMIService.ExecQuery("SELECT BytesReceivedPersec,BytesSentPersec,Name FROM Win32_PerfRawData_Tcpip_NetworkInterface", "WQL", $wbemFlagReturnImmediately + $wbemFlagForwardOnly)
    Return $colItems
EndFunc   ;==>_GetObj
Edited by Aceguy
Link to comment
Share on other sites

COOL! One more change I would make is turning on event mode for the second part of GUI since the sleep in the while loop is so long.

#include <ButtonConstants.au3>
#include <GUIConstantsEx.au3>
#include <WindowsConstants.au3>

$aLabel = _GetNames(_GetObj())
Dim $aCheckBox[$aLabel[0] + 1][2]
Dim $aAdapter[2]

$hSelection = GUICreate("Selection", 300, $aLabel[0] * 45, -1, -1, -1, BitOR($WS_EX_TOOLWINDOW, $WS_EX_WINDOWEDGE))
If $aLabel[0] = 0 Then
    MsgBox(0, "Error", "No Adapters Found", 0, $hSelection)
    Exit
EndIf

For $i = 1 To $aLabel[0]
    $aCheckBox[$i][0] = GUICtrlCreateCheckbox($aLabel[$i], 8, $i * 20, 290)
    $aCheckBox[$i][1] = $aLabel[$i]
Next
$aCheckBox[0][0] = $aLabel[0]

$Button1 = GUICtrlCreateButton("okay", 100, $aLabel[0] * 30, 75)
GUISetState(@SW_SHOW)

While 1
    Sleep(50)
    $nMsg = GUIGetMsg()
    Switch $nMsg
        Case $GUI_EVENT_CLOSE
            Exit
        Case $Button1
            $i1 = 1
            $bExit = 0
            For $i = 1 To $aCheckBox[0][0]
                If GUICtrlRead($aCheckBox[$i][0]) = 1 Then
                    $aAdapter[0] = $i1
                    ReDim $aAdapter[$i1 + 1]
                    $aAdapter[$i1] = $aCheckBox[$i][1]
                    $i1 += 1
                    $bExit = 1
                EndIf
            Next
            If $bExit = 1 Then
                GUIDelete($hSelection)
                ExitLoop
            Else
                MsgBox(0, "Error", "No Adapter Selected", 0, $hSelection)
            EndIf
    EndSwitch
WEnd

Opt("GUIOnEventMode", 1)
$hMonitor = GUICreate("Monitor", 212, 118, -1, -1, -1, BitOR($WS_EX_TOOLWINDOW, $WS_EX_WINDOWEDGE))
GUISetFont(12)
$Label1 = GUICtrlCreateLabel("", 8, 8, 100, 24)
$Label2 = GUICtrlCreateLabel("", 8, 23, 100, 24)
$Label3 = GUICtrlCreateLabel("", 8, 38, 100, 24)
$Label4 = GUICtrlCreateLabel("", 8, 53, 100, 24)
GUISetOnEvent($GUI_EVENT_CLOSE, "_Exit")
GUISetState(@SW_SHOW)
$Data_now = _GetData(_GetObj(), $aAdapter)
While 1
    $btmp= _GetData(_GetObj(), $aAdapter)
    Sleep(1000)

    $aData = _GetData(_GetObj(), $aAdapter)

    $bData = $aData[0] - $Data_now[0]
   
    $difftmp= ($aData[0]-$btmp[0])
   
    GUICtrlSetData($Label1, Round($difftmp, 2) & " Diff")
   
    GUICtrlSetData($Label2, Round($bData / 1024, 2) & " KB")

    GUICtrlSetData($Label3, Round($bData / 1048576, 2) & " MB")

    GUICtrlSetData($Label4, Round($bData / 073741824, 2) & " GB")
WEnd

Func _GetNames($colItems)
    Local $aName[2], $i = 1
    If IsObj($colItems) Then
        For $objItem In $colItems
            $aName[0] = $i
            ReDim $aName[$i + 1]
            $aName[$i] = $objItem.Name
            $i += 1

        Next
    EndIf
    Return $aName
EndFunc   ;==>_GetNames

Func _GetData($colItems, $aAdapter)
    Local $aData[2]
    If IsObj($colItems) Then
        For $objItem In $colItems
            For $i = 1 To $aAdapter[0]
                If $objItem.Name = $aAdapter[$i] Then
                    $aData[0] += $objItem.BytesReceivedPersec
                    $aData[1] += $objItem.BytesSentPersec
                EndIf
            Next
        Next
    EndIf
    Return $aData
EndFunc   ;==>_GetData

Func _GetObj()
    Local $wbemFlagReturnImmediately = 0x10, $wbemFlagForwardOnly = 0x20, $colItems = "", $strComputer = "localhost"
    $objWMIService = ObjGet("winmgmts:\\" & $strComputer & "\root\CIMV2")

    $colItems = $objWMIService.ExecQuery("SELECT BytesReceivedPersec,BytesSentPersec,Name FROM Win32_PerfRawData_Tcpip_NetworkInterface", "WQL", $wbemFlagReturnImmediately + $wbemFlagForwardOnly)
    Return $colItems
EndFunc   ;==>_GetObj

Func _Exit()
    Exit
EndFunc
Link to comment
Share on other sites

hey, cool. im using this for my school. im only allowed 4 gb of bandwidth a week, this will help greatly. ive changed it a little.

#include <ButtonConstants.au3>
#include <GUIConstantsEx.au3>
#include <GUIConstants.au3>
#include <WindowsConstants.au3>

$aLabel = _GetNames(_GetObj())
Dim $aCheckBox[$aLabel[0] + 1][2]
Dim $aAdapter[2]

$hSelection = GUICreate("Selection", 300, $aLabel[0] * 45, -1, -1, -1, BitOR($WS_EX_TOOLWINDOW,$WS_EX_WINDOWEDGE))
If $aLabel[0] = 0 Then 
    MsgBox(0, "Error", "No Adapters Found", 0)
    Exit
EndIf

For $i = 1 to $aLabel[0]
    $aCheckBox[$i][0] = GUICtrlCreateCheckbox($aLabel[$i], 8, $i * 20, 290)
    GUICtrlSetState(-1, $GUI_CHECKED)
    $aCheckBox[$i][1] = $aLabel[$i]
Next
$aCheckBox[0][0] = $aLabel[0]

$Button1 = GUICtrlCreateButton("Okay", 100, $aLabel[0] * 30, 75)
GUISetState(@SW_SHOW)

While 1
    Sleep(50)
    $nMsg = GUIGetMsg()
    Switch $nMsg
        Case $GUI_EVENT_CLOSE
            Exit
        Case $Button1
            $i1 = 1
            $bExit = 0
            For $i = 1 to $aCheckBox[0][0]
                If GUICtrlRead($aCheckBox[$i][0]) = 1 Then
                    $aAdapter[0] = $i1
                    ReDim $aAdapter[$i1 + 1]
                    $aAdapter[$i1] = $aCheckBox[$i][1]
                    $i1 += 1
                    $bExit = 1
                EndIf
            Next
            If $bExit = 1 Then
                GUIDelete($hSelection)
                ExitLoop
            Else
                MsgBox(0, "Error", "No Adapter Selected", 0)
            EndIf
    EndSwitch
WEnd

$Tenth = 20

$hMonitor   = GUICreate("Monitor", 212, 100, -1, -1)
              GUISetFont(9)
$Menu       = GUICtrlCreateMenu("&Options")
$bytesize   = GUICtrlCreateMenu("Byte Size", $Menu)
$kbyte      = GUICtrlCreateMenuItem("KB", $bytesize)
$mbyte      = GUICtrlCreateMenuItem("MB", $bytesize)
              GUICtrlSetState(-1, $GUI_CHECKED)
$gbyte      = GUICtrlCreateMenuItem("GB", $bytesize)

$Label1     = GUICtrlCreateLabel("Bandwidth: Megabytes", 8, 8, 145, 24)
$Label3     = GUICtrlCreateLabel("", 8, 32, 210, 24)
$Label5     = GUICtrlCreateLabel("", 100, 32, 110, 20, $SS_RIGHT)
$progress   = GUICtrlCreateProgress(8, 56, 160, 15)
$Label7     = GUICtrlCreateLabel("4 GB", 180, 55, 100, 24)

GUISetState(@SW_SHOW)

While 1
    $Msg = GUIGetMsg()
    If $Msg = $GUI_EVENT_CLOSE Then Exit
    
    If $Msg = $kbyte Then 
        GUICtrlSetState($kbyte, $GUI_CHECKED)
        GUICtrlSetState($mbyte, $GUI_UNCHECKED)
        GUICtrlSetState($gbyte, $GUI_UNCHECKED)
        
        $Tenth = 10
        
        GUICtrlSetData($Label1, "Bandwidth: Kilobytes")
    ElseIf $Msg = $mbyte Then 
        GUICtrlSetState($kbyte, $GUI_UNCHECKED)
        GUICtrlSetState($mbyte, $GUI_CHECKED)
        GUICtrlSetState($gbyte, $GUI_UNCHECKED)
        
        $Tenth = 20
        
        GUICtrlSetData($Label1, "Bandwidth: Megabytes")
    ElseIf $Msg = $gbyte Then 
        GUICtrlSetState($kbyte, $GUI_UNCHECKED)
        GUICtrlSetState($mbyte, $GUI_UNCHECKED)
        GUICtrlSetState($gbyte, $GUI_CHECKED)
        
        $Tenth = 30
        
        GUICtrlSetData($Label1, "Bandwidth: Gigabytes")
    EndIf
    $aData = _GetData(_GetObj(), $aAdapter)
    
    $BYTE = Round( ($aData[0]) / (2 ^ $Tenth),4)
    
    GUICtrlSetData($Label3, $BYTE)
    GUICtrlSetData($progress, ($aData[0] / (4 * (2^30))) * 100)
    GUICtrlSetData($Label5, Round($aData[0] / (4 * (2^30)) * 100, 2) & "%")
    
    
WEnd

Func _GetNames($colItems)
    Local $aName[2], $i = 1
    If IsObj($colItems) then
        For $objItem In $colItems
            $aName[0] = $i
            ReDim $aName[$i + 1]
            $aName[$i] = $objItem.Name
            $i += 1
            
        Next
    EndIf
    Return $aName
EndFunc

Func _GetData($colItems, $aAdapter)
    Local $aData[2]
    If IsObj($colItems) then
        For $objItem In $colItems
            For $i = 1 to $aAdapter[0]
                If $objItem.Name = $aAdapter[$i] Then
                    $aData[0] += $objItem.BytesReceivedPersec
                    $aData[1] += $objItem.BytesSentPersec
                EndIf
            Next
        Next
    EndIf
    Return $aData
EndFunc
    
Func _GetObj()
    Local $wbemFlagReturnImmediately = 0x10, $wbemFlagForwardOnly = 0x20, $colItems = "", $strComputer = "localhost"
    $objWMIService = ObjGet("winmgmts:\\" & $strComputer & "\root\CIMV2")
    
    $colItems = $objWMIService.ExecQuery("SELECT BytesReceivedPersec,BytesSentPersec,Name FROM Win32_PerfRawData_Tcpip_NetworkInterface", "WQL", $wbemFlagReturnImmediately + $wbemFlagForwardOnly)
    Return $colItems
EndFunc

do you think you can add like K/bs or something, i probably could figure it out. i just dont got the time right now.

Edited by billthecreator

[font=Microsoft Sans Serif]My Scripts: From Most recent to least.[/font]Countdown GUI | QLOCK TWO | FlipClock | Slot Machine My UDF:_GenerateRandomNoRepeat | _GuiSnap

Link to comment
Share on other sites

  • 2 weeks later...
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...