Page 1 sur 1

Statistiques réseau

Publié : ven. 25/juin/2004 11:25
par Heis Spiter
Bonjour à tous ! Je suis actuellement coincé avec une API, quelqu'un peut-il m'éclairer ? Voici le bout de code que j'ai pondu

Code : Tout sélectionner

Structure STAT_WORKSTATION_0
  StatisticsStartTime.l
  BytesReceived.l 
  SmbsReceived.l
  PagingReadBytesRequested.l  
  NonPagingReadBytesRequested.l  
  CacheReadBytesRequested.l  
  NetworkReadBytesRequested.l 
  BytesTransmitted.l 
  SmbsTransmitted.l
  PagingWriteBytesRequested.l 
  NonPagingWriteBytesRequested.l 
  CacheWriteBytesRequested.l 
  NetworkWriteBytesRequested.l
  InitiallyFailedOperations.l
  FailedCompletionOperations.l
  ReadOperations.l
  RandomReadOperations.l
  ReadSmbs.l
  LargeReadSmbs.l
  SmallReadSmbs.l
  WriteOperations.l
  RandomWriteOperations.l
  WriteSmbs.l
  LargeWriteSmbs.l
  SmallWriteSmbs.l
  RawReadsDenied.l
  RawWritesDenied.l 
  NetworkErrors.l
  Sessions.l
  FailedSessions.l
  Reconnects.l 
  CoreConnects.l
  Lanman20Connects.l
  Lanman21Connects.l 
  LanmanNtConnects.l 
  ServerDisconnects.l
  HungSessions.l
  UseCount.l
  FailedUseCount.l 
  CurrentCommands.l
EndStructure
DefType .STAT_WORKSTATION_0 stats
NetStatisticsGet_("", "", 0, 0, @stats)
Ce code "marche", aucune erreur rien, seulement que NetStatisticsGet renvoi 50 (demande impossible) et tout les données de la structure valent 10
Where is the problem ?
Merci

Publié : ven. 25/juin/2004 11:32
par Ombres
Peut être parce que:
The NetStatisicsGet function is obsolete. Win32-based applications should use the NetStatisticsGet2 function
:)

Publié : ven. 25/juin/2004 11:34
par Heis Spiter
Alors la c'est encore pire ! Aucune documentation dessus chez Microsft et PureBasic ne la reconnais même pas :?

Publié : ven. 25/juin/2004 11:52
par fweil
...,

J'ai trois listings que je te propose pour faire des stats réseau :

Code : Tout sélectionner

Global ListIconGadget.l
Global CurrentDirectory.s
Global EOL.s
Global NewReceivedBytes.l
Global NewSentBytes.l
Global OldReceivedBytes.l
Global OldSentBytes.l
Global CurrentReceivedBytes.l
Global CurrentSentBytes.l
Global CurrentReceivedBits.l
Global CurrentSentBits.l

Procedure UpdateStats(n.l)
  Repeat
    RunProgram("netstat.bat", " ", CurrentDirectory, 1 | 2)
    If ReadFile(0, "netstat.txt")
        SendMessage_(ListIconGadget, #LVM_DELETEALLITEMS, 0, 0)
        For i = 1 To 5
          b$ = ReadString()
        Next
        b$ = Trim(b$)
        While FindString(b$, "  ", 1) <> 0
          b$ = Mid(b$, 1, FindString(b$, "  ", 1) - 1) + Mid(b$, FindString(b$, "  ", 1) + 1, Len(b$) - FindString(b$, "  ", 1) - 1 + 1)
        Wend
        b$ = ReplaceString(b$, " ", Chr(10))
;        AddGadgetItem(0, -1, b$)
        OldReceivedBytes = NewReceivedBytes
        OldSentBytes = NewSentBytes
        NewReceivedBytes = Val(StringField(b$, 2, Chr(10)))
        NewSentBytes = Val(StringField(b$, 3, Chr(10)))
        CurrentReceivedBytes = NewReceivedBytes - OldReceivedBytes
        CurrentSentBytes = NewSentBytes - OldSentBytes
        CurrentReceivedBits = CurrentReceivedBytes * 8
        CurrentSentBits = CurrentSentBytes * 8
        Debug "CurrentSentBits=" + Str(CurrentSentBits) + " CurrentReceivedBits=" + Str(CurrentReceivedBits)
        CloseFile(0)
    EndIf
    Delay(1000)
  ForEver
EndProcedure

  tz = GetTickCount_()
  CurrentDirectory = Space(255)
  GetCurrentDirectory_(255, CurrentDirectory)
  EOL = Chr(13) + Chr(10)
  Quit = #FALSE
  ThreadID = 0
  If OpenWindow(0, 0, 0, 640, 480, #PB_Window_SystemMenu | #PB_Window_MinimizeGadget | #PB_Window_MaximizeGadget | #PB_Window_SizeGadget | #PB_Window_TitleBar | #PB_Window_ScreenCentered, "Netstat")
      AddKeyboardShortcut(0, #PB_Shortcut_Escape, 99)
;      If CreateGadgetList(WindowID())
;          ListIconGadget = ListIconGadget(0, 0, 0, 640, 480, "x", 120)
;          AddGadgetColumn(0, 1, "Reçu", 120)
;          AddGadgetColumn(0, 2, "Emis", 120)
;      EndIf
      If CreateFile(0, CurrentDirectory + "\netstat.bat")
          WriteStringN("netstat.exe -e > netstat.txt")
          CloseFile(0)
      EndIf
      Repeat
        Select WaitWindowEvent()
          Case #PB_Event_CloseWindow
            Quit = #TRUE
          Case #PB_Event_Menu
            Select EventMenuID()
              Case 99
                Quit = #TRUE
            EndSelect
        EndSelect
        If GetTickCount_() - tz > 1000 And ThreadID = 0
            ThreadID = CreateThread(@UpdateStats(), 0)
        EndIf
      Until Quit
      DeleteFile("netstat.txt")
      CloseWindow(0)
  EndIf
End

Code : Tout sélectionner

Enumeration
  #Window_Main
  #Gadget_Editor
EndEnumeration

#MIB_TCP_STATE_CLOSED = 0
#MIB_TCP_STATE_LISTEN = 1
#MIB_TCP_STATE_SYN_SENT = 2
#MIB_TCP_STATE_SYN_RCVD = 3
#MIB_TCP_STATE_ESTAB = 4
#MIB_TCP_STATE_FIN_WAIT1 = 5
#MIB_TCP_STATE_FIN_WAIT2 = 6
#MIB_TCP_STATE_CLOSE_WAIT = 7
#MIB_TCP_STATE_CLOSING = 8
#MIB_TCP_STATE_LAST_ACK = 9
#MIB_TCP_STATE_TIME_WAIT = 10
#MIB_TCP_STATE_DELETE_TCB = 11

Structure MIB_IPSTATS
  dwForwarding.l ; IP forwarding enabled Or disabled
  dwDefaultTTL.l ; Default time-to-live
  dwInReceives.l ; datagrams received
  dwInHdrErrors.l ; received header errors
  dwInAddrErrors.l ; received address errors
  dwForwDatagrams.l ; datagrams forwarded
  dwInUnknownProtos.l ; datagrams with unknown protocol
  dwInDiscards.l ; received datagrams discarded
  dwInDelivers.l ; received datagrams delivered
  dwOutRequests.l
  dwRoutingDiscards.l
  dwOutDiscards.l ; sent datagrams discarded
  dwOutNoRoutes.l ; datagrams For which no route
  dwReasmTimeout.l ; datagrams For which all frags didn't arrive
  dwReasmReqds.l ; datagrams requiring reassembly
  dwReasmOks.l ; successful reassemblies
  dwReasmFails.l ; failed reassemblies
  dwFragOks.l ; successful fragmentations
  dwFragFails.l ; failed fragmentations
  dwFragCreates.l ; datagrams fragmented
  dwNumIf.l ; number of interfaces on computer
  dwNumAddr.l ; number of IP address on computer
  dwNumRoutes.l ; number of routes in routing table
EndStructure

Structure MIB_TCPSTATS
  dwRtoAlgorithm.l ; timeout algorithm
  dwRtoMin.l ; minimum timeout
  dwRtoMax.l ; maximum timeout
  dwMaxConn.l ; maximum connections
  dwActiveOpens.l ; active opens
  dwPassiveOpens.l ; passive opens
  dwAttemptFails.l ; failed attempts
  dwEstabResets.l ; establised connections reset
  dwCurrEstab.l ; established connections
  dwInSegs.l ; segments received
  dwOutSegs.l ; segment sent
  dwRetransSegs.l ; segments retransmitted
  dwInErrs.l ; incoming errors
  dwOutRsts.l ; outgoing resets
  dwNumConns.l ; cumulative connections
EndStructure

Structure MIB_UDPSTATS
  dwInDatagrams.l ; received datagrams
  dwNoPorts.l ; datagrams For which no port
  dwInErrors.l ; errors on received datagrams
  dwOutDatagrams.l ; sent datagrams
  dwNumAddrs.l ; number of entries in UDP listener table
EndStructure

Structure MIBICMPSTATS
  dwMsgs.l ; number of messages
  dwErrors.l ; number of errors
  dwDestUnreachs.l ; destination unreachable messages
  dwTimeExcds.l ; time-to-live exceeded messages
  dwParmProbs.l ; parameter problem messages
  dwSrcQuenchs.l ; source quench messages
  dwRedirects.l ; redirection messages
  dwEchos.l ; echo requests
  dwEchoReps.l ; echo replies
  dwTimestamps.l ; timestamp requests
  dwTimestampReps.l ; timestamp replies
  dwAddrMasks.l ; address mask requests
  dwAddrMaskReps.l ; address mask replies
EndStructure

Structure MIBICMPINFO
  icmpInStats.MIBICMPSTATS ; stats For incoming messages
  icmpOutStats.MIBICMPSTATS ; stats For outgoing messages
EndStructure

Global ip.MIB_IPSTATS
Global tcp.MIB_TCPSTATS
Global udp.MIB_UDPSTATS
Global icmp.MIBICMPINFO

Global EOL.s, TAB.s
EOL = Chr(13) + Chr(10)
TAB = Chr(9)

Procedure.s Stats()
    txtOutput.s = ""
    If GetIpStatistics_(ip)
        txtOutput = txtOutput + "Unable to retrieve IP Statistics"
    Else
        txtOutput = txtOutput + "IP Statistics" + EOL + "==============================" + EOL
        txtOutput = txtOutput + "IP forwarding enabled or disabled:" + TAB + Str(ip\dwForwarding) + EOL
        txtOutput = txtOutput + "default time-to-live:" + TAB + Str(ip\dwDefaultTTL) + EOL
        txtOutput = txtOutput + "datagrams received:" + TAB + Str(ip\dwInReceives) + EOL
        txtOutput = txtOutput + "received header errors:" + TAB + Str(ip\dwInHdrErrors) + EOL
        txtOutput = txtOutput + "received address errors:" + TAB + Str(ip\dwInAddrErrors) + EOL
        txtOutput = txtOutput + "datagrams forwarded:" + TAB + Str(ip\dwForwDatagrams) + EOL
        txtOutput = txtOutput + "datagrams with unknown protocol:" + TAB + Str(ip\dwInUnknownProtos) + EOL
        txtOutput = txtOutput + "received datagrams discarded:" + TAB + Str(ip\dwInDiscards) + EOL
        txtOutput = txtOutput + "received datagrams delivered:" + TAB + Str(ip\dwInDelivers) + EOL
        txtOutput = txtOutput + "outgoing datagrams requested:" + TAB + Str(ip\dwOutRequests) + EOL
        txtOutput = txtOutput + "outgoing datagrams discarded:" + TAB + Str(ip\dwRoutingDiscards) + EOL
        txtOutput = txtOutput + "sent datagrams discarded:" + TAB + Str(ip\dwOutDiscards) + EOL
        txtOutput = txtOutput + "datagrams for which no route:" + TAB + Str(ip\dwOutNoRoutes) + EOL
        txtOutput = txtOutput + "datagrams for which all frags didn't arrive:" + TAB + Str(ip\dwReasmTimeout) + EOL
        txtOutput = txtOutput + "datagrams requiring reassembly:" + TAB + Str(ip\dwReasmReqds) + EOL
        txtOutput = txtOutput + "successful reassemblies:" + TAB + Str(ip\dwReasmOks) + EOL
        txtOutput = txtOutput + "failed reassemblies:" + TAB + Str(ip\dwReasmFails) + EOL
        txtOutput = txtOutput + "successful fragmentations:" + TAB + Str(ip\dwFragOks) + EOL
        txtOutput = txtOutput + "failed fragmentations:" + TAB + Str(ip\dwFragFails) + EOL
        txtOutput = txtOutput + "datagrams fragmented:" + TAB + Str(ip\dwFragCreates) + EOL
        txtOutput = txtOutput + "number of interfaces on computer:" + TAB + Str(ip\dwNumIf) + EOL
        txtOutput = txtOutput + "number of IP address on computer:" + TAB + Str(ip\dwNumAddr) + EOL
        txtOutput = txtOutput + "number of routes in routing table:" + TAB + Str(ip\dwNumRoutes) + EOL
        txtOutput = txtOutput + EOL
    EndIf
    
    If GetTcpStatistics_(tcp)
        txtOutput = txtOutput + "Unable to retrieve TCP Statistics"
    Else
        txtOutput = txtOutput + "TCP Statistics" + EOL + "==============================" + EOL
        txtOutput = txtOutput + "timeout algorithm:" + TAB + Str(tcp\dwRtoAlgorithm) + EOL
        txtOutput = txtOutput + "minimum timeout:" + TAB + Str(tcp\dwRtoMin) + EOL
        txtOutput = txtOutput + "maximum timeout:" + TAB + Str(tcp\dwRtoMax) + EOL
        txtOutput = txtOutput + "maximum connections:" + TAB + Str(tcp\dwMaxConn) + EOL
        txtOutput = txtOutput + "active opens:" + TAB + Str(tcp\dwActiveOpens) + EOL
        txtOutput = txtOutput + "passive opens:" + TAB + Str(tcp\dwPassiveOpens) + EOL
        txtOutput = txtOutput + "failed attempts:" + TAB + Str(tcp\dwAttemptFails) + EOL
        txtOutput = txtOutput + "establised connections reset:" + TAB + Str(tcp\dwEstabResets) + EOL
        txtOutput = txtOutput + "established connections:" + TAB + Str(tcp\dwCurrEstab) + EOL
        txtOutput = txtOutput + "segments received:" + TAB + Str(tcp\dwInSegs) + EOL
        txtOutput = txtOutput + "segment sent:" + TAB + Str(tcp\dwOutSegs) + EOL
        txtOutput = txtOutput + "segments retransmitted:" + TAB + Str(tcp\dwRetransSegs) + EOL
        txtOutput = txtOutput + "incoming errors:" + TAB + Str(tcp\dwInErrs) + EOL
        txtOutput = txtOutput + "outgoing resets:" + TAB + Str(tcp\dwOutRsts) + EOL
        txtOutput = txtOutput + "cumulative connections:" + TAB + Str(tcp\dwNumConns) + EOL
        txtOutput = txtOutput + EOL
    EndIf

    If GetUdpStatistics_(udp)
        txtOutput = txtOutput + "Unable to retrieve UDP Statistics"
    Else
        txtOutput = txtOutput + "UDP Statistics" + EOL + "==============================" + EOL
        txtOutput = txtOutput + "received datagrams:" + TAB + Str(udp\dwInDatagrams) + EOL
        txtOutput = txtOutput + "datagrams for which no port:" + TAB + Str(udp\dwNoPorts) + EOL
        txtOutput = txtOutput + "errors on received datagrams:" + TAB + Str(udp\dwInErrors) + EOL
        txtOutput = txtOutput + "sent datagrams:" + TAB + Str(udp\dwOutDatagrams) + EOL
        txtOutput = txtOutput + "number of entries in UDP listener table:" + TAB + Str(udp\dwNumAddrs) + EOL
        txtOutput = txtOutput + EOL
    EndIf

    If GetIcmpStatistics_(icmp)
        txtOutput = txtOutput + "Unable to retrieve ICMP Statistics"
    Else
        txtOutput = txtOutput + "ICMP Statistics" + EOL + "==============================" + EOL
        txtOutput = txtOutput + "*****  In  *****" + EOL
        txtOutput = txtOutput + "number of messages:" + TAB + Str(icmp\icmpInStats\dwMsgs) + EOL
        txtOutput = txtOutput + "number of errors:" + TAB + Str(icmp\icmpInStats\dwErrors) + EOL
        txtOutput = txtOutput + "destination unreachable messages:" + TAB + Str(icmp\icmpInStats\dwDestUnreachs) + EOL
        txtOutput = txtOutput + "time-to-live exceeded messages:" + TAB + Str(icmp\icmpInStats\dwTimeExcds) + EOL
        txtOutput = txtOutput + "parameter problem messages:" + TAB + Str(icmp\icmpInStats\dwParmProbs) + EOL
        txtOutput = txtOutput + "source quench messages:" + TAB + Str(icmp\icmpInStats\dwSrcQuenchs) + EOL
        txtOutput = txtOutput + "redirection messages:" + TAB + Str(icmp\icmpInStats\dwRedirects) + EOL
        txtOutput = txtOutput + "echo requests:" + TAB + Str(icmp\icmpInStats\dwEchos) + EOL
        txtOutput = txtOutput + "echo replies:" + TAB + Str(icmp\icmpInStats\dwEchoReps) + EOL
        txtOutput = txtOutput + "timestamp requests:" + TAB + Str(icmp\icmpInStats\dwTimestamps) + EOL
        txtOutput = txtOutput + "timestamp replies:" + TAB + Str(icmp\icmpInStats\dwTimestampReps) + EOL
        txtOutput = txtOutput + "address mask requests:" + TAB + Str(icmp\icmpInStats\dwAddrMasks) + EOL
        txtOutput = txtOutput + "address mask replies:" + TAB + Str(icmp\icmpInStats\dwAddrMaskReps) + EOL
        txtOutput = txtOutput + EOL
        txtOutput = txtOutput + "*****  Out  *****" + EOL
        txtOutput = txtOutput + "number of messages:" + TAB + Str(icmp\icmpOutStats\dwMsgs) + EOL
        txtOutput = txtOutput + "number of errors:" + TAB + Str(icmp\icmpOutStats\dwErrors) + EOL
        txtOutput = txtOutput + "destination unreachable messages:" + TAB + Str(icmp\icmpOutStats\dwDestUnreachs) + EOL
        txtOutput = txtOutput + "time-to-live exceeded messages:" + TAB + Str(icmp\icmpOutStats\dwTimeExcds) + EOL
        txtOutput = txtOutput + "parameter problem messages:" + TAB + Str(icmp\icmpOutStats\dwParmProbs) + EOL
        txtOutput = txtOutput + "source quench messages:" + TAB + Str(icmp\icmpOutStats\dwSrcQuenchs) + EOL
        txtOutput = txtOutput + "redirection messages:" + TAB + Str(icmp\icmpOutStats\dwRedirects) + EOL
        txtOutput = txtOutput + "echo requests:" + TAB + Str(icmp\icmpOutStats\dwEchos) + EOL
        txtOutput = txtOutput + "echo replies:" + TAB + Str(icmp\icmpOutStats\dwEchoReps) + EOL
        txtOutput = txtOutput + "timestamp requests:" + TAB + Str(icmp\icmpOutStats\dwTimestamps) + EOL
        txtOutput = txtOutput + "timestamp replies:" + TAB + Str(icmp\icmpOutStats\dwTimestampReps) + EOL
        txtOutput = txtOutput + "address mask requests:" + TAB + Str(icmp\icmpOutStats\dwAddrMasks) + EOL
        txtOutput = txtOutput + "address mask replies:" + TAB + Str(icmp\icmpOutStats\dwAddrMaskReps) + EOL
        txtOutput = txtOutput + EOL
    EndIf
    ProcedureReturn txtOutput
EndProcedure

  WindowXSize = 640
  WindowYSize = 480
  Quit = #FALSE
  If OpenWindow(#Window_Main, 0, 0, WindowXSize, WindowYSize, #PB_Window_SystemMenu | #PB_Window_MinimizeGadget | #PB_Window_MaximizeGadget | #PB_Window_SizeGadget | #PB_Window_TitleBar | #PB_Window_ScreenCentered, "NetStats")
      AddKeyboardShortcut(#Window_Main, #PB_Shortcut_Escape, #PB_Shortcut_Escape)
      If CreateGadgetList(WindowID())
          EditorGadget (#Gadget_Editor, 10, 10, WindowXSize - 20, WindowYSize - 20, #PB_Container_Raised)
      EndIf
      SetGadgetText(#Gadget_Editor, Stats())
      Repeat
        Select WaitWindowEvent()
          Case #PB_Event_CloseWindow
            Quit = #TRUE
          Case #PB_Event_Menu
            Select EventMenuID()
              Case #PB_Shortcut_Escape
                Quit = #TRUE
            EndSelect
        EndSelect
      Until Quit
      CloseWindow(#Window_Main)
  EndIf
  TerminateProcess_(GetCurrentProcess_(), 0)
End      

Code : Tout sélectionner

Enumeration
  #Gadget_String_IPConfig
  #Gadget_String_Netstat_a
  #Gadget_String_Netstat_e
  #Gadget_String_Netstat_n
  #Gadget_String_Netstat_r
  #Gadget_String_Netstat_s
  #Gadget_String_Stats
  #Gadget_Image_Send_Receive_Bytes
  #Window_Main
  #Gadget_Panel
EndEnumeration

Global CurrentDirectory.s, BatchFileName.s, StatisticsFileName.s, EOL.s
Global StartTime.l, DiffTime.l, OldTime.l, DiffReceivedBytes.l, ReceivedBytes.l, OldReceivedBytes.l, DiffSentBytes.l, SentBytes.l , OldSentBytes.l
Global StatsMaxItems.l
Global ImageID_Background.l, ImageID_Send_receive_Bytes.l
Global WindowXSize.l, WindowYSize.l

Structure Stats
  ReceivedBytes.l
  SentBytes.l
  TimeFrame.l
EndStructure

Structure CommandToPanel
  Command.s
  GadgetNumber.l
  FileName.s
EndStructure

NewList StatsList.Stats()
NewList Commands.CommandToPanel()
  
#MIB_TCP_STATE_CLOSED = 0
#MIB_TCP_STATE_LISTEN = 1
#MIB_TCP_STATE_SYN_SENT = 2
#MIB_TCP_STATE_SYN_RCVD = 3
#MIB_TCP_STATE_ESTAB = 4
#MIB_TCP_STATE_FIN_WAIT1 = 5
#MIB_TCP_STATE_FIN_WAIT2 = 6
#MIB_TCP_STATE_CLOSE_WAIT = 7
#MIB_TCP_STATE_CLOSING = 8
#MIB_TCP_STATE_LAST_ACK = 9
#MIB_TCP_STATE_TIME_WAIT = 10
#MIB_TCP_STATE_DELETE_TCB = 11

Structure MIB_IPSTATS
  dwForwarding.l ; IP forwarding enabled Or disabled
  dwDefaultTTL.l ; Default time-to-live
  dwInReceives.l ; datagrams received
  dwInHdrErrors.l ; received header errors
  dwInAddrErrors.l ; received address errors
  dwForwDatagrams.l ; datagrams forwarded
  dwInUnknownProtos.l ; datagrams with unknown protocol
  dwInDiscards.l ; received datagrams discarded
  dwInDelivers.l ; received datagrams delivered
  dwOutRequests.l
  dwRoutingDiscards.l
  dwOutDiscards.l ; sent datagrams discarded
  dwOutNoRoutes.l ; datagrams For which no route
  dwReasmTimeout.l ; datagrams For which all frags didn't arrive
  dwReasmReqds.l ; datagrams requiring reassembly
  dwReasmOks.l ; successful reassemblies
  dwReasmFails.l ; failed reassemblies
  dwFragOks.l ; successful fragmentations
  dwFragFails.l ; failed fragmentations
  dwFragCreates.l ; datagrams fragmented
  dwNumIf.l ; number of interfaces on computer
  dwNumAddr.l ; number of IP address on computer
  dwNumRoutes.l ; number of routes in routing table
EndStructure

Structure MIB_TCPSTATS
  dwRtoAlgorithm.l ; timeout algorithm
  dwRtoMin.l ; minimum timeout
  dwRtoMax.l ; maximum timeout
  dwMaxConn.l ; maximum connections
  dwActiveOpens.l ; active opens
  dwPassiveOpens.l ; passive opens
  dwAttemptFails.l ; failed attempts
  dwEstabResets.l ; establised connections reset
  dwCurrEstab.l ; established connections
  dwInSegs.l ; segments received
  dwOutSegs.l ; segment sent
  dwRetransSegs.l ; segments retransmitted
  dwInErrs.l ; incoming errors
  dwOutRsts.l ; outgoing resets
  dwNumConns.l ; cumulative connections
EndStructure

Structure MIB_UDPSTATS
  dwInDatagrams.l ; received datagrams
  dwNoPorts.l ; datagrams For which no port
  dwInErrors.l ; errors on received datagrams
  dwOutDatagrams.l ; sent datagrams
  dwNumAddrs.l ; number of entries in UDP listener table
EndStructure

Structure MIBICMPSTATS
  dwMsgs.l ; number of messages
  dwErrors.l ; number of errors
  dwDestUnreachs.l ; destination unreachable messages
  dwTimeExcds.l ; time-to-live exceeded messages
  dwParmProbs.l ; parameter problem messages
  dwSrcQuenchs.l ; source quench messages
  dwRedirects.l ; redirection messages
  dwEchos.l ; echo requests
  dwEchoReps.l ; echo replies
  dwTimestamps.l ; timestamp requests
  dwTimestampReps.l ; timestamp replies
  dwAddrMasks.l ; address mask requests
  dwAddrMaskReps.l ; address mask replies
EndStructure

Structure MIBICMPINFO
  icmpInStats.MIBICMPSTATS ; stats For incoming messages
  icmpOutStats.MIBICMPSTATS ; stats For outgoing messages
EndStructure

Global ip.MIB_IPSTATS
Global tcp.MIB_TCPSTATS
Global udp.MIB_UDPSTATS
Global icmp.MIBICMPINFO

Global EOL.s, TAB.s
EOL = Chr(13) + Chr(10)
TAB = Chr(9)

Procedure.s Stats()
  txtOutput.s = ""
  If GetIpStatistics_(ip)
      txtOutput = txtOutput + "Unable to retrieve IP Statistics"
  Else
      txtOutput = txtOutput + "IP Statistics" + EOL + "==============================" + EOL
      txtOutput = txtOutput + "IP forwarding enabled or disabled:" + TAB + Str(ip\dwForwarding) + EOL
      txtOutput = txtOutput + "default time-to-live:" + TAB + Str(ip\dwDefaultTTL) + EOL
      txtOutput = txtOutput + "datagrams received:" + TAB + Str(ip\dwInReceives) + EOL
      txtOutput = txtOutput + "received header errors:" + TAB + Str(ip\dwInHdrErrors) + EOL
      txtOutput = txtOutput + "received address errors:" + TAB + Str(ip\dwInAddrErrors) + EOL
      txtOutput = txtOutput + "datagrams forwarded:" + TAB + Str(ip\dwForwDatagrams) + EOL
      txtOutput = txtOutput + "datagrams with unknown protocol:" + TAB + Str(ip\dwInUnknownProtos) + EOL
      txtOutput = txtOutput + "received datagrams discarded:" + TAB + Str(ip\dwInDiscards) + EOL
      txtOutput = txtOutput + "received datagrams delivered:" + TAB + Str(ip\dwInDelivers) + EOL
      txtOutput = txtOutput + "outgoing datagrams requested:" + TAB + Str(ip\dwOutRequests) + EOL
      txtOutput = txtOutput + "outgoing datagrams discarded:" + TAB + Str(ip\dwRoutingDiscards) + EOL
      txtOutput = txtOutput + "sent datagrams discarded:" + TAB + Str(ip\dwOutDiscards) + EOL
      txtOutput = txtOutput + "datagrams for which no route:" + TAB + Str(ip\dwOutNoRoutes) + EOL
      txtOutput = txtOutput + "datagrams for which all frags didn't arrive:" + TAB + Str(ip\dwReasmTimeout) + EOL
      txtOutput = txtOutput + "datagrams requiring reassembly:" + TAB + Str(ip\dwReasmReqds) + EOL
      txtOutput = txtOutput + "successful reassemblies:" + TAB + Str(ip\dwReasmOks) + EOL
      txtOutput = txtOutput + "failed reassemblies:" + TAB + Str(ip\dwReasmFails) + EOL
      txtOutput = txtOutput + "successful fragmentations:" + TAB + Str(ip\dwFragOks) + EOL
      txtOutput = txtOutput + "failed fragmentations:" + TAB + Str(ip\dwFragFails) + EOL
      txtOutput = txtOutput + "datagrams fragmented:" + TAB + Str(ip\dwFragCreates) + EOL
      txtOutput = txtOutput + "number of interfaces on computer:" + TAB + Str(ip\dwNumIf) + EOL
      txtOutput = txtOutput + "number of IP address on computer:" + TAB + Str(ip\dwNumAddr) + EOL
      txtOutput = txtOutput + "number of routes in routing table:" + TAB + Str(ip\dwNumRoutes) + EOL
      txtOutput = txtOutput + EOL
  EndIf
  
  If GetTcpStatistics_(tcp)
      txtOutput = txtOutput + "Unable to retrieve TCP Statistics"
  Else
      txtOutput = txtOutput + "TCP Statistics" + EOL + "==============================" + EOL
      txtOutput = txtOutput + "timeout algorithm:" + TAB + Str(tcp\dwRtoAlgorithm) + EOL
      txtOutput = txtOutput + "minimum timeout:" + TAB + Str(tcp\dwRtoMin) + EOL
      txtOutput = txtOutput + "maximum timeout:" + TAB + Str(tcp\dwRtoMax) + EOL
      txtOutput = txtOutput + "maximum connections:" + TAB + Str(tcp\dwMaxConn) + EOL
      txtOutput = txtOutput + "active opens:" + TAB + Str(tcp\dwActiveOpens) + EOL
      txtOutput = txtOutput + "passive opens:" + TAB + Str(tcp\dwPassiveOpens) + EOL
      txtOutput = txtOutput + "failed attempts:" + TAB + Str(tcp\dwAttemptFails) + EOL
      txtOutput = txtOutput + "establised connections reset:" + TAB + Str(tcp\dwEstabResets) + EOL
      txtOutput = txtOutput + "established connections:" + TAB + Str(tcp\dwCurrEstab) + EOL
      txtOutput = txtOutput + "segments received:" + TAB + Str(tcp\dwInSegs) + EOL
      txtOutput = txtOutput + "segment sent:" + TAB + Str(tcp\dwOutSegs) + EOL
      txtOutput = txtOutput + "segments retransmitted:" + TAB + Str(tcp\dwRetransSegs) + EOL
      txtOutput = txtOutput + "incoming errors:" + TAB + Str(tcp\dwInErrs) + EOL
      txtOutput = txtOutput + "outgoing resets:" + TAB + Str(tcp\dwOutRsts) + EOL
      txtOutput = txtOutput + "cumulative connections:" + TAB + Str(tcp\dwNumConns) + EOL
      txtOutput = txtOutput + EOL
  EndIf

  If GetUdpStatistics_(udp)
      txtOutput = txtOutput + "Unable to retrieve UDP Statistics"
  Else
      txtOutput = txtOutput + "UDP Statistics" + EOL + "==============================" + EOL
      txtOutput = txtOutput + "received datagrams:" + TAB + Str(udp\dwInDatagrams) + EOL
      txtOutput = txtOutput + "datagrams for which no port:" + TAB + Str(udp\dwNoPorts) + EOL
      txtOutput = txtOutput + "errors on received datagrams:" + TAB + Str(udp\dwInErrors) + EOL
      txtOutput = txtOutput + "sent datagrams:" + TAB + Str(udp\dwOutDatagrams) + EOL
      txtOutput = txtOutput + "number of entries in UDP listener table:" + TAB + Str(udp\dwNumAddrs) + EOL
      txtOutput = txtOutput + EOL
  EndIf

  If GetIcmpStatistics_(icmp)
      txtOutput = txtOutput + "Unable to retrieve ICMP Statistics"
  Else
      txtOutput = txtOutput + "ICMP Statistics" + EOL + "==============================" + EOL
      txtOutput = txtOutput + "*****  In  *****" + EOL
      txtOutput = txtOutput + "number of messages:" + TAB + Str(icmp\icmpInStats\dwMsgs) + EOL
      txtOutput = txtOutput + "number of errors:" + TAB + Str(icmp\icmpInStats\dwErrors) + EOL
      txtOutput = txtOutput + "destination unreachable messages:" + TAB + Str(icmp\icmpInStats\dwDestUnreachs) + EOL
      txtOutput = txtOutput + "time-to-live exceeded messages:" + TAB + Str(icmp\icmpInStats\dwTimeExcds) + EOL
      txtOutput = txtOutput + "parameter problem messages:" + TAB + Str(icmp\icmpInStats\dwParmProbs) + EOL
      txtOutput = txtOutput + "source quench messages:" + TAB + Str(icmp\icmpInStats\dwSrcQuenchs) + EOL
      txtOutput = txtOutput + "redirection messages:" + TAB + Str(icmp\icmpInStats\dwRedirects) + EOL
      txtOutput = txtOutput + "echo requests:" + TAB + Str(icmp\icmpInStats\dwEchos) + EOL
      txtOutput = txtOutput + "echo replies:" + TAB + Str(icmp\icmpInStats\dwEchoReps) + EOL
      txtOutput = txtOutput + "timestamp requests:" + TAB + Str(icmp\icmpInStats\dwTimestamps) + EOL
      txtOutput = txtOutput + "timestamp replies:" + TAB + Str(icmp\icmpInStats\dwTimestampReps) + EOL
      txtOutput = txtOutput + "address mask requests:" + TAB + Str(icmp\icmpInStats\dwAddrMasks) + EOL
      txtOutput = txtOutput + "address mask replies:" + TAB + Str(icmp\icmpInStats\dwAddrMaskReps) + EOL
      txtOutput = txtOutput + EOL
      txtOutput = txtOutput + "*****  Out  *****" + EOL
      txtOutput = txtOutput + "number of messages:" + TAB + Str(icmp\icmpOutStats\dwMsgs) + EOL
      txtOutput = txtOutput + "number of errors:" + TAB + Str(icmp\icmpOutStats\dwErrors) + EOL
      txtOutput = txtOutput + "destination unreachable messages:" + TAB + Str(icmp\icmpOutStats\dwDestUnreachs) + EOL
      txtOutput = txtOutput + "time-to-live exceeded messages:" + TAB + Str(icmp\icmpOutStats\dwTimeExcds) + EOL
      txtOutput = txtOutput + "parameter problem messages:" + TAB + Str(icmp\icmpOutStats\dwParmProbs) + EOL
      txtOutput = txtOutput + "source quench messages:" + TAB + Str(icmp\icmpOutStats\dwSrcQuenchs) + EOL
      txtOutput = txtOutput + "redirection messages:" + TAB + Str(icmp\icmpOutStats\dwRedirects) + EOL
      txtOutput = txtOutput + "echo requests:" + TAB + Str(icmp\icmpOutStats\dwEchos) + EOL
      txtOutput = txtOutput + "echo replies:" + TAB + Str(icmp\icmpOutStats\dwEchoReps) + EOL
      txtOutput = txtOutput + "timestamp requests:" + TAB + Str(icmp\icmpOutStats\dwTimestamps) + EOL
      txtOutput = txtOutput + "timestamp replies:" + TAB + Str(icmp\icmpOutStats\dwTimestampReps) + EOL
      txtOutput = txtOutput + "address mask requests:" + TAB + Str(icmp\icmpOutStats\dwAddrMasks) + EOL
      txtOutput = txtOutput + "address mask replies:" + TAB + Str(icmp\icmpOutStats\dwAddrMaskReps) + EOL
      txtOutput = txtOutput + EOL
  EndIf
  ProcedureReturn txtOutput
EndProcedure

Procedure GetStatistics()
  Repeat
    If CreateFile(1, BatchFileName)
        ResetList(Commands())
        While NextElement(Commands())
          WriteStringN(Commands()\Command + " " + Commands()\FileName)
        Wend
        CloseFile(1) 
    EndIf
    If RunProgram(BatchFileName,"","c:\", 1 | 2)
        ResetList(Commands())
        While NextElement(Commands())
          If ReadFile(0, Commands()\FileName)
              BufferLength = Lof()
              *Buffer = AllocateMemory(BufferLength)
              ReadData(*Buffer, BufferLength)
              CloseFile(0)
              FullData.s = PeekS(*Buffer, BufferLength)
              SetGadgetText(Commands()\GadgetNumber, FullData)
              FreeMemory(*Buffer)
              iu = FindString(FullData, "Octets", 1)
              If iu
                  a$ = Mid(FullData, iu, 100)
                  Debug a$
                  sReceivedBytes.s = Trim(Mid(a$, FindString(a$, " ", 1), Len(a$) - FindString(a$, " ", 1) + 1))
                  sReceivedBytes = Mid(sReceivedBytes, 1, FindString(sReceivedBytes, EOL, 1) - 1)
                  SentBytes.l = Val(Trim(Mid(sReceivedBytes, FindString(sReceivedBytes, " ", 1), Len(sReceivedBytes) - FindString(sReceivedBytes, " ", 1) + 1)))
                  ReceivedBytes.l = Val(Mid(sReceivedBytes, 1, FindString(sReceivedBytes, " " , 1) - 1))
                  Debug ReceivedBytes
                  Debug SentBytes
                  DiffTime = ElapsedMilliseconds() - OldTime
                  DiffReceivedBytes = ReceivedBytes - OldReceivedBytes
                  DiffSentBytes = SentBytes - OldSentBytes
                  OldTime = ElapsedMilliseconds()
                  OldReceivedBytes = ReceivedBytes
                  OldSentBytes = SentBytes
                  AddElement(StatsList())
                  StatsList()\ReceivedBytes = DiffReceivedBytes
                  StatsList()\SentBytes = DiffSentBytes
                  StatsList()\TimeFrame = ElapsedMilliseconds()
                  Debug CountList(StatsList())
                  If CountList(StatsList()) > StatsMaxItems
                      FirstElement(StatsList())
                      DeleteElement(StatsList())
                  EndIf


                  UseImage(1)
                  StartDrawing(ImageOutput())
                    DrawImage(ImageID_Background, 0, 0)
                    If CountList(StatsList()) > 1
                        ResetList(StatsList())
                        NextElement(StatsList())
                        StartTime = StatsList()\TimeFrame
                        While NextElement(StatsList()) And ListIndex(StatsList()) < CountList(StatsList()) - 1
                          X2 =(StatsList()\TimeFrame - StartTime) / 1000
                          Y2 = WindowYSize / 2 - StatsList()\ReceivedBytes / 1200
                          LineXY(X1, Y1, X2, Y2, #Red)
                          Debug Str(X1) + " " + Str(Y1) + " " + Str(X2) + " " + Str(Y2)
                          X1 = X2
                          Y1 = Y2
                        Wend
                    EndIf
                  StopDrawing()
                  SetGadgetState(#Gadget_Image_Send_Receive_Bytes, ImageID_Send_receive_Bytes)


              EndIf
          EndIf
        Wend
    EndIf
    Sleep_(1000)
  ForEver
EndProcedure

  StatsMaxItems = 100
  CurrentDirectory = Space(#MAX_PATH)
  GetCurrentDirectory_(#MAX_PATH, CurrentDirectory)
  CurrentDirectory + "\"
  BatchFileName.s = CurrentDirectory + "IPStats.bat"
  StatisticsFileName.s = CurrentDirectory + "IPStats"
  AddElement(Commands()) : Commands()\Command = "ipconfig /all > " : Commands()\GadgetNumber = #Gadget_String_IPConfig : Commands()\FileName = CurrentDirectory + "ipconfig.txt"
  AddElement(Commands()) : Commands()\Command = "netstat -a > " : Commands()\GadgetNumber = #Gadget_String_Netstat_a : Commands()\FileName = CurrentDirectory + "netstata.txt"
  AddElement(Commands()) : Commands()\Command = "netstat -e > " : Commands()\GadgetNumber = #Gadget_String_Netstat_e : Commands()\FileName = CurrentDirectory + "netstate.txt"
  AddElement(Commands()) : Commands()\Command = "netstat -n > " : Commands()\GadgetNumber = #Gadget_String_Netstat_n : Commands()\FileName = CurrentDirectory + "netstatn.txt"
  AddElement(Commands()) : Commands()\Command = "netstat -r > " : Commands()\GadgetNumber = #Gadget_String_Netstat_r : Commands()\FileName = CurrentDirectory + "netstatr.txt"
  AddElement(Commands()) : Commands()\Command = "netstat -s > " : Commands()\GadgetNumber = #Gadget_String_Netstat_s : Commands()\FileName = CurrentDirectory + "netstats.txt"
  EOL = Chr(13) + Chr(10)
  WindowXSize = 640
  WindowYSize = 480
  Quit = #FALSE
  If OpenWindow(#Window_Main, 0, 0, WindowXSize, WindowYSize, #PB_Window_SystemMenu | #PB_Window_MinimizeGadget | #PB_Window_MaximizeGadget | #PB_Window_SizeGadget | #PB_Window_TitleBar | #PB_Window_ScreenCentered, "NetStat")
      ImageID_Background = CreateImage(0, WindowXSize / 2 - 20, WindowYSize / 2 - 20)
      StartDrawing(ImageOutput())
        Box(0, 0, ImageWidth(), ImageHeight(), #Black)
        For x = 0 To ImageWidth() Step 20
          LineXY(x, 0, x, ImageHeight(), #Green)
        Next
        For y = 0 To ImageWidth() Step 20
          LineXY(0, y, ImageWidth(), y, #Green)
        Next
      StopDrawing()
      ImageID_Send_receive_Bytes = CreateImage(1, WindowXSize / 2 - 20, WindowYSize / 2 - 20)
      AddKeyboardShortcut(#Window_Main, #PB_Shortcut_Escape, #PB_Shortcut_Escape)
      If CreateGadgetList(WindowID())
          PanelGadget(#Gadget_Panel, 10, 10, WindowXSize - 20, WindowYSize - 20)
            ResetList(Commands())
            While NextElement(Commands())
              AddGadgetItem(#Gadget_Panel, -1, Mid(Commands()\Command, 1, FindString(Commands()\Command, ">", 1) - 1))
                EditorGadget(Commands()\GadgetNumber, 10, 10, WindowXSize - 45, WindowYSize - 65, "")
            Wend
              AddGadgetItem(#Gadget_Panel, -1, "Stats")
                EditorGadget(#Gadget_String_Stats, 10, 10, WindowXSize - 45, WindowYSize - 65, "")
            AddGadgetItem(#Gadget_Panel, -1, "Graphical monitor")
              ImageGadget(#Gadget_Image_Send_Receive_Bytes, 10, 10, WindowXSize / 2 - 20, WindowYSize / 2 - 20, ImageID_Send_receive_Bytes, #PB_Image_Border)
          CloseGadgetList()
      EndIf
      SetGadgetState(#Gadget_Panel, #Gadget_String_Netstat_e)
      OldTime = ElapsedMilliseconds()
      StartTime = OldTime
      ThreadID1 = CreateThread(@GetStatistics(), 0)
;      ThreadID2 = CreateThread(@Stats(), 0)
      Repeat
        Select WaitWindowEvent()
          Case #PB_Event_CloseWindow
            Quit = #TRUE
          Case #PB_Event_Menu
            Select EventMenuID()
              Case #PB_Shortcut_Escape
                Quit = #TRUE
            EndSelect
        EndSelect
        SetGadgetText(#Gadget_String_Stats, Stats())
;        Delay(10)
      Until Quit
      KillThread(ThreadID1)
;      KillThread(ThreadID2)
      CloseWindow(0)
  EndIf
  DeleteFile(BatchFileName)
  DeleteFile(StatisticsFileName)
  TerminateProcess_(GetCurrentProcess_(), 0)
End