close

 

先來講一下所謂的MIB,

什麼是MIB ? 

MIB 是 Management Information Base 的縮寫,MIB是網管架構的核心,主要是用來記錄在網路上各個網路設備的屬性與功能的。

部涮網路的硬體或軟體,若以網路管理的角度來看,都是以物件來加以表示,而物件(Object)的規則、定義、與法,是由IETF的SMI管理資訊結構來指定結構與描述資料物件的方法。

 

MIB-II (管理資料庫)

- RFC1213 所定義的管理資訊庫就稱為 MIB-II 。

而 MIB-II的特色 

- 資料庫 (Data-base) 的架構與實行和網路管理協定無關,不同的網路管理協定 ( 如 SNMP , 都可以使用 MIB-II管理資料庫 ) 。

- 每一項網管物件(object) , 又稱網路狀態資訊,均有一個 唯一的物件識別值 (Object Identifier  , 簡稱 ODI )。

- 使用SMI管理資訊架構 來定義每一個網管物件的型態 及 內容值得Range size 。

- 規定代理器上應該保存的網管物件資訊的種類,及每項網管物件資訊與存取權限 。

 

MIB 網管物件的資訊 (總共有五種)

1. 物件名稱與系統設定值。

2. 描述文字 : 用來描述此霧見相關的資訊的文字。

3. 物件資料型態 : 定義其資料型態 , Ex  : integer , string ..etc 。

4. 物件的讀寫方式 : 唯讀、唯寫、可讀寫、不可讀寫。

5. 物件的識別值 (OID) : 在MIB中每個物件的唯一識別值,這個識別值是由一連續的數字所組成。

 

 

 

 

Source Code :

 

 


#include <Windows.h>
#include <IPHlpApi.h>
#include <cstdio>
#include <fstream>
#include <string>
#include <iostream>
#pragma comment(lib, "Iphlpapi.lib")
using namespace std;

void GetMacAddress (string & str)
{
    ifstream infile ;
    bool blRead = true ;
    string strTemp;
    system("ipconfig /all > mac.txt");
    infile.open("mac.txt");
    while (infile && blRead)
    {

        infile>>strTemp ;
        if (strTemp.size()==17)
            blRead = false ;
    }
    infile.close() ;
    if (blRead)
        strTemp = "Load Mac Address Fail" ;
    else
        str = strTemp ;

}

int main()
{
    string strMacName ;
    GetMacAddress(strMacName);

    PMIB_IFTABLE m_pTable = NULL;
    DWORD         m_dwAdapters = 0 ;
    //The GeIfTable function retrives the MIB-II interface table .
    //what is MIB-II interface table ?
    //Ans : 
    /*
     
        管理資訊庫(Management Information
        Base,簡稱MIB) 是整個網管架構的核心。
        z 主要是用來記錄在網路上各個網路設備的
        屬性與功能。
        z 不管是網路上的硬體或軟體,若以網路管
        理的角度來看都是以物件來加以表示,而
        物件的規則、定義與語法是由IETF
        的SMI
        管理資訊結構來指定結構與描述資料物件
        的方法。
        z SNMP所使用的網路管理資訊庫主要是
        MIB-II
        及RMON (Remote Network
        Monitoring)

        RFC 1213所定義的管理資訊庫就稱為MIB-II。
        
    */
    /*
        DWORD GetIfTable (    
                            _Out_ PMIB_IFTABLE pIfTable ,    
                            _Inout_ PULONG       pdwSize,
                            _In_    BOOL         bOrder

                         );

        pIfTable [out]
             A pointer to a buffer that receives the interface table as a MIB_IFTABLE structure.
        pdwSize [in, out]
             On input, specifies the size in bytes of the buffer pointed to by the pIfTable parameter.
             On output, if the buffer is not large enough to hold the returned interface table, 
             the function sets this parameter equal to the required buffer size in bytes
        bOrder [in]
             A Boolean value that specifies whether the returned interface table should be sorted in ascending order by interface index. 
             If this parameter is TRUE, the table is sorted.


    */
    ULONG         uRetCode = GetIfTable(m_pTable,&m_dwAdapters,TRUE);
    if (uRetCode == ERROR_NOT_SUPPORTED)
    {
        printf("> The request is not supported.\n");
        printf("> Exit... \n");
        return (-1);
    }
    if (uRetCode == ERROR_INSUFFICIENT_BUFFER)
    {
        printf("> The data area passed to a system call is too small.\n");
        m_pTable = (PMIB_IFTABLE)new BYTE[65535];
    }

    DWORD dwLastIn  = 0;
    DWORD dwLastOut = 0 ; 
 
    //download speed
    DWORD dwBandIn  = 0 ;
    DWORD dwBandOut = 0 ;

    while (1)
    {
        GetIfTable(m_pTable,&m_dwAdapters,TRUE);
        DWORD dwInOctets    = 0 ;
        DWORD dwOutOctets    = 0 ;

        //Calculation
        MIB_IFROW Row ;
        for (UINT i = 0 ; i<m_pTable->dwNumEntries; i++)
        {
            Row = m_pTable->table[i];
            dwInOctets += Row.dwInOctets;
            dwOutOctets+= Row.dwOutOctets;
        }

        dwBandIn    = dwInOctets  - dwLastIn    ;
        dwBandOut    = dwOutOctets - dwLastOut    ;

        if (dwLastIn <= 0)
        {
            dwBandIn = 0 ;
        }
        else
        {
            dwBandIn = dwBandIn / 1024 ; //b to kb
        }

        if (dwLastOut <= 0 )
        {
            dwBandOut = 0 ;
        }
        else
        {
            dwBandOut = dwBandOut / 1024 ; //b to kb
        }

        dwLastIn  = dwInOctets  ; 
        dwLastOut = dwOutOctets ;
        
        cout << "> Mac Address is : " << strMacName <<endl; 
         
        printf ("> Get  : %u bytes \n",dwLastIn ); 
        printf ("> Send : %u bytes \n",dwLastOut);

        printf ("> Download speed : %u kb \n", dwBandIn );
        printf ("> Upload   speed : %u kb \n", dwBandOut);

        printf ("------------------------------------\n") ;
        Sleep(1000);

    }
    delete [] m_pTable ;
    return 0 ;
}

 

MIB_IFROW 參考 :  https://msdn.microsoft.com/zh-tw/library/windows/desktop/aa366836(v=vs.85).aspx

arrow
arrow
    文章標籤
    C++ net speed MIB MIB-II
    全站熱搜

    Eric 發表在 痞客邦 留言(0) 人氣()