aardio 官方社区

 找回密码
 注册会员

QQ登录

只需一步,快速开始

搜索
查看: 26889|回复: 5

简单的网卡流量监控演示。。。

[复制链接]

8

主题

34

回帖

273

积分

二级会员

积分
273
发表于 2017-11-1 19:09:30 | 显示全部楼层 |阅读模式
适用vista以上系统。。。
  1. namespace netMonitor{
  2.         import win.guid
  3.        
  4.         var dll = ..raw.loadDll("iphlpapi")
  5.         getIfTable2 = dll.api("GetIfTable2", "int(ptr pIfTable)")
  6.         freeMibTable = dll.api("FreeMibTable", "int(ptr pIfTable)")
  7.        
  8.         var guid = win.guid
  9.         class MIB_IF_ROW2{
  10.                 LONG InterfaceLuid;
  11.                 int InterfaceIndex;
  12.                 struct InterfaceGuid = guid();
  13.                 byte Alias[514];
  14.                 byte Description[514];
  15.                 int PhysicalAddressLength;
  16.                 BYTE PhysicalAddress[32];
  17.                 BYTE PermanentPhysicalAddress[32];
  18.                 int Mtu;
  19.                 int Type;
  20.                 int TunnelType;
  21.                 int MediaType;
  22.                 int PhysicalMediumType;
  23.                 int AccessType;
  24.                 int DirectionType;
  25.                 byte InterfaceAndOperStatusFlags;
  26.                 int OperStatus;
  27.                 int AdminStatus;
  28.                 int MediaConnectState;
  29.                 struct NetworkGuid = guid();
  30.                 int ConnectionType;
  31.                 LONG TransmitLinkSpeed;
  32.                 LONG ReceiveLinkSpeed;
  33.                 LONG InOctets;
  34.                 LONG InUcastPkts;
  35.                 LONG InNUcastPkts;
  36.                 LONG InDiscards;
  37.                 LONG InErrors;
  38.                 LONG InUnknownProtos;
  39.                 LONG InUcastOctets;
  40.                 LONG InMulticastOctets;
  41.                 LONG InBroadcastOctets;
  42.                 LONG OutOctets;
  43.                 LONG OutUcastPkts;
  44.                 LONG OutNUcastPkts;
  45.                 LONG OutDiscards;
  46.                 LONG OutErrors;
  47.                 LONG OutUcastOctets;
  48.                 LONG OutMulticastOctets;
  49.                 LONG OutBroadcastOctets;
  50.                 LONG OutQLen;
  51.         }
  52.        
  53.         getInfo = function(){
  54.                 var ptr = ..raw.buffer(4)
  55.                 var ret = getIfTable2(ptr)
  56.                 if(ret) return;
  57.                
  58.                 ptr = ..raw.convert(ptr, {ptr p}).p
  59.                 ret = ..raw.convert(ptr, {int n}).n
  60.                 ret = ..raw.convert(ptr, {
  61.                         int NumEntries;
  62.                         struct table[]={
  63.                                 length = ret;
  64.                                 MIB_IF_ROW2()
  65.                         };
  66.                 })
  67.                 freeMibTable(ptr)
  68.                 return ret;
  69.         }
  70. }

  71. import win.ui
  72. import win.timer
  73. /*DSG{{*/
  74. var winform = win.form(text="网卡流量监控";right=1004;bottom=591)
  75. winform.add(
  76. dtLv={cls="listview";left=2;top=41;right=1001;bottom=563;edge=1;fullRow=1;gridLines=1;z=1}
  77. )
  78. /*}}*/

  79. winform.dtLv.insertColumn("名称", 230)
  80. winform.dtLv.insertColumn("MAC", 130)
  81. winform.dtLv.insertColumn("总发送", 100)
  82. winform.dtLv.insertColumn("总接收", 100)
  83. winform.dtLv.insertColumn("上行", 100)
  84. winform.dtLv.insertColumn("下行", 100)
  85. winform.dtLv.insertColumn("描述", 300)
  86. winform.dtLv.adjust = function(cx,cy){
  87.     winform.dtLv.fillParent(7/*列序号*/);
  88. }
  89. winform.dtLv.setExtended(0x10000/*_LVS_EX_DOUBLEBUFFER*/)

  90. var agoT = netMonitor.getInfo()
  91. for(i=1;agoT.NumEntries){
  92.         var u = agoT.table[ i ]
  93.         //**
  94.         var mac = ..string.format("%.2X-%.2X-%.2X-%.2X-%.2X-%.2X",
  95.                 u.PhysicalAddress[1],
  96.                 u.PhysicalAddress[2],
  97.                 u.PhysicalAddress[3],
  98.                 u.PhysicalAddress[4],
  99.                 u.PhysicalAddress[5],
  100.                 u.PhysicalAddress[6]
  101.         )
  102.         var guid =  ..string.upper(tostring(u.InterfaceGuid))
  103.         //**/
  104.         winform.dtLv.addItem({..string.fromUtf16(u.Alias);mac;u.OutOctets;u.InOctets;"";"";..string.fromUtf16(u.Description)},i)
  105. }

  106. var mR = ..math.round
  107. var atimer = win.timer(,500)
  108. atimer.onTimer = function(hwnd,msg,id,tick){
  109.         var nowT = netMonitor.getInfo()
  110.         if(!agoT){
  111.                 agoT = ..table.clone(nowT)
  112.                 return;
  113.         }
  114.         for(i=1;nowT.NumEntries){
  115.                 var a, n = agoT.table[ i ], nowT.table[ i ]
  116.                
  117.                 var aTx, aRx = a.OutOctets, a.InOctets
  118.                 var nTx, nRx = n.OutOctets, n.InOctets
  119.                
  120.                 aTx ?= mR((nTx-aTx)/512, 2) + 'kb/s'
  121.                 aRx ?= mR((nRx-aRx)/512, 2) + 'kb/s'
  122.                
  123.                 winform.dtLv.setItemText(nTx, i, 3)
  124.                 winform.dtLv.setItemText(nRx, i, 4)
  125.                 winform.dtLv.setItemText(aTx, i, 5)
  126.                 winform.dtLv.setItemText(aRx, i, 6)
  127.                
  128.                 a.InOctets = nRx
  129.                 a.OutOctets = nTx
  130.         }
  131. }
  132. atimer.enable()

  133. winform.show();
  134. win.loopMessage()
复制代码

38

主题

129

回帖

1045

积分

荣誉会员

积分
1045
发表于 2017-11-1 20:07:27 | 显示全部楼层
帮你补个图片O(∩_∩)O~ QQ截图20171101201054.jpg

166

主题

2154

回帖

1万

积分

管理员

积分
13056
发表于 2017-11-1 20:28:26 | 显示全部楼层
感谢分享代码,还是写的不错的,指针那一块可以不用raw.buffer也可以:
getInfo = function(){  
   
var ret,pMibIfTable2 = dll.GetIfTable2({ptr p})
   
var mibIfTable2 = ..raw.convert( pMibIfTable2.p,{int NumEntries})
     
    mibIfTable2 = ..raw.convert(pMibIfTable2.p, {
        int NumEntries;
        struct table[]={  
            length = mibIfTable2.NumEntries;
            MIB_IF_ROW2()
        };
    })
   
    freeMibTable(pMibIfTable2.p)
   
return mibIfTable2;
}



8

主题

34

回帖

273

积分

二级会员

积分
273
 楼主| 发表于 2017-11-1 21:58:57 | 显示全部楼层
Jacen.He 发表于 2017-11-1 20:28
感谢分享代码,还是写的不错的,指针那一块可以不用raw.buffer也可以:

原来还可以这样写,多谢版主指点,又学到了一点。。。

8

主题

34

回帖

273

积分

二级会员

积分
273
 楼主| 发表于 2017-11-1 22:00:39 | 显示全部楼层
popdes 发表于 2017-11-1 20:07
帮你补个图片O(∩_∩)O~

互相学习了

166

主题

2154

回帖

1万

积分

管理员

积分
13056
发表于 2020-1-1 15:03:59 | 显示全部楼层
小改了一下,提交到扩展库里了,示例:
import win.ui
import win.timer
/*DSG{{*/
var winform = win.form(text="网卡流量监控";right=1004;bottom=591)
winform.add(
dtLv={cls=
"listview";left=2;top=41;right=1001;bottom=563;edge=1;fullRow=1;gridLines=1;z=1}
)
/*}}*/

winform.dtLv.insertColumn(
"名称", 230)
winform.dtLv.insertColumn(
"MAC", 130)
winform.dtLv.insertColumn(
"总发送", 100)
winform.dtLv.insertColumn(
"总接收", 100)
winform.dtLv.insertColumn(
"上行", 100)
winform.dtLv.insertColumn(
"下行", 100)
winform.dtLv.insertColumn(
"描述", 300)
winform.dtLv.adjust =
function(cx,cy){
    winform.dtLv.fillParent(7
/*列序号*/);
}
winform.dtLv.setExtended(0x10000
/*_LVS_EX_DOUBLEBUFFER*/)

import inet.monitor;
var prevNetInfo = inet.monitor.getInfo()
for(i=1;prevNetInfo.NumEntries){
   
var u = prevNetInfo.table[ i ]
   
var mac = ..string.format("%.2X-%.2X-%.2X-%.2X-%.2X-%.2X"
        ,string.unpack(u.PhysicalAddress)
    )
   
    winform.dtLv.addItem({u.Alias;mac;u.OutOctets;u.InOctets;
"";"";u.Description},i)
}

var round = ..math.round
var netTimer = win.timer(,500)
netTimer.onTimer =
function(hwnd,msg,id,tick){
   
var netInfo = inet.monitor.getInfo()
   
if(!prevNetInfo){
        prevNetInfo = ..table.clone(netInfo)
        
return;
    }
   
   
for(i=1;netInfo.NumEntries){
        
var a, n = prevNetInfo.table[ i ], netInfo.table[ i ]
        
        
var aTx, aRx = a.OutOctets, a.InOctets
        
var nTx, nRx = n.OutOctets, n.InOctets
        
        aTx ?= round((nTx-aTx)/512, 2) +
'kb/s'
        aRx ?= round((nRx-aRx)/512, 2) +
'kb/s'
        
        winform.dtLv.setItemText(nTx, i, 3)
        winform.dtLv.setItemText(nRx, i, 4)
        winform.dtLv.setItemText(aTx, i, 5)
        winform.dtLv.setItemText(aRx, i, 6)
        
        a.InOctets = nRx
        a.OutOctets = nTx
    }
}
netTimer.enable()

winform.show();
win.loopMessage()


您需要登录后才可以回帖 登录 | 注册会员

本版积分规则

手机版|未经许可严禁引用或转载本站文章|aardio.com|aardio 官方社区 ( 皖ICP备09012014号 )

GMT+8, 2025-6-13 10:03 , Processed in 0.066669 second(s), 25 queries .

Powered by Discuz! X3.5

Copyright © 2001-2024 Tencent Cloud.

快速回复 返回顶部 返回列表