Netzwerktraffic mit Win32_PerfFormattedData_Tcpip_NetworkInterface auslesen?



  • Mojn zusammen!

    Ich bin gerade ein bisschen am basteln!

    Ich möchte meinen Traffic auslesen ...

    Ich hab schon gelesen das dies mit TDI-Filtern gehen soll, hab aber auf die schnelle nix brauchbares gefunden ...

    Dann hab ich in der WMI das gefunden:

    class Win32_PerfFormattedData_Tcpip_NetworkInterface : Win32_PerfFormattedData
    {
      uint32 BytesReceivedPerSec;
      uint32 BytesSentPerSec;
      uint64 BytesTotalPerSec;
      string Caption;
      uint32 CurrentBandwidth;
      string Description;
      uint64 Frequency_Object;
      uint64 Frequency_PerfTime;
      uint64 Frequency_Sys100NS;
      string Name;
      uint32 OutputQueueLength;
      uint32 PacketsOutboundDiscarded;
      uint32 PacketsOutboundErrors;
      uint32 PacketsPerSec;
      uint32 PacketsReceivedDiscarded;
      uint32 PacketsReceivedErrors;
      uint32 PacketsReceivedNonUnicastPerSec;
      uint32 PacketsReceivedPerSec;
      uint32 PacketsReceivedUnicastPerSec;
      uint32 PacketsReceivedUnknown;
      uint32 PacketsSentNonUnicastPerSec;
      uint32 PacketsSentPerSec;
      uint32 PacketsSentUnicastPerSec;
      uint64 Timestamp_Object;
      uint64 Timestamp_PerfTime;
      uint64 Timestamp_Sys100NS;
    };
    

    So schön und gut!

    Ich kann das ganze auch abfragen, nur mit einem kleinen Problem, es sieht wohl so aus, als würden die Daten nur einmal pro Sekunde gefüllt werden, und danach steht überall 0 drin, wenn man jetzt nicht zufällig den richtigen Zeitpunkt erwischt, erhaltet man 0 BytesReceivedBySec, obwohl man gerade mit voller last zieht ....

    Weiß jmd ne lösung dafür??

    Hier mal mein Quellcode der die Daten ausliest:

    int NEOsLcdClass::InitializeWMI()
    {
    	//Just set to true if Initialation was Successfull
    	InitializedWbem = false;
    
    	HRESULT result;
    
    	pLoc = NULL;
    	pSvc = NULL;
    
    	SYSTEM_INFO SysInfo;
    	GetSystemInfo(&SysInfo);
    	NumberOfProzessors = SysInfo.dwNumberOfProcessors;
    
    	if(NumberOfProzessors > 1)
    		NumberOfProzessors ++;
    
    	result = CoInitializeEx(0, COINIT_MULTITHREADED);
    
    	if(result != S_OK)
    	{
    		/* Initializing Failed! */
    		return 1;
    	}
    
    	/* Set COM security levels */
    	result = CoInitializeSecurity(
    			NULL, 
    			-1,                          // COM authentication
    			NULL,                        // Authentication services
    			NULL,                        // Reserved
    			RPC_C_AUTHN_LEVEL_DEFAULT,   // Default authentication 
    			RPC_C_IMP_LEVEL_IMPERSONATE, // Default Impersonation  
    			NULL,                        // Authentication info
    			EOAC_NONE,                   // Additional capabilities 
    			NULL                         // Reserved
    			);
    	if(result != S_OK)
    	{
    		/* Failed! */
    		return 1;
    	}
    
    	/* Obtain the initial locator to WMI */
    	result = CoCreateInstance(CLSID_WbemLocator,0,CLSCTX_INPROC_SERVER,IID_IWbemLocator, (LPVOID *) &pLoc);
    
    	if(result != S_OK)
    	{
    		/* Failed! */
    		return 1;
    	}
    
    	/* Connect to WMI through the IWbemLocator::ConnectServer method */
    	result = pLoc->ConnectServer(
    		_T("ROOT\\CIMV2"), // Object path of WMI namespace
    		NULL,                    // User name. NULL = current user
    		NULL,                    // User password. NULL = current
    		0,                       // Locale. NULL indicates current
    		NULL,                    // Security flags.
    		0,                       // Authority (e.g. Kerberos)
    		0,                       // Context object 
    		&pSvc                    // pointer to IWbemServices proxy
    		);
    	if(result != WBEM_S_NO_ERROR)
    	{
    		/* Failed! */
    		return 1;
    	}
    
    	/* Set security levels on the proxy */
    	result = CoSetProxyBlanket(
    			pSvc,                        // Indicates the proxy to set
    			RPC_C_AUTHN_WINNT,           // RPC_C_AUTHN_xxx
    			RPC_C_AUTHZ_NONE,            // RPC_C_AUTHZ_xxx
    			NULL,                        // Server principal name 
    			RPC_C_AUTHN_LEVEL_CALL,      // RPC_C_AUTHN_LEVEL_xxx 
    			RPC_C_IMP_LEVEL_IMPERSONATE, // RPC_C_IMP_LEVEL_xxx
    			NULL,                        // client identity
    			EOAC_NONE                    // proxy capabilities 
    		);
    	if(result != S_OK)
    	{
    		/* Failed! */
    		return 1;
    	}
    	InitializedWbem = true;
    
    	return 0;
    }
    
    vector<int> NEOsLcdClass::GetNetUsages(vector <CString> &Interfaces)
    {
    	vector<int> Usages;
    
    /* BEGIN */
    
    	HRESULT result;
    	IEnumWbemClassObject* pEnumerator = NULL;
    	IWbemClassObject *pclsObj;
    	DWORD uReturn;
    
    	Interfaces.clear();
    
    	if(InitializedWbem)
    	{
    		/* Use the IWbemServices pointer to make requests of WMI */
    
    		result = pSvc->ExecQuery(
    					_T("WQL"), 
    					_T("SELECT * FROM Win32_PerfFormattedData_Tcpip_NetworkInterface"),
    
    					WBEM_FLAG_BIDIRECTIONAL, 
    					NULL,
    					&pEnumerator);
    
    		if(/*result != WBEM_S_NO_ERROR*/FAILED(result))
    		{
    			/* Failed! */
    			AfxMessageBox(L"Something Happend :(" + IntToStr(WBEM_S_NO_ERROR));
    			vector<int> ClearRetVal;
    			return ClearRetVal;
    		}
    
    		/* Get data */
    
    		int i = 0;
    
    		while(true)
    		{
    			VARIANT vtProp;
    
    			result = pEnumerator->Next(WBEM_INFINITE, 1, &pclsObj, &uReturn);
    
    			if(uReturn != 0 )
    			{
    
    				VariantInit(&vtProp);
    
    				//swscanf(vtProp.bstrVal, L"%I64u", &ulVal[nCtr]);
    
    				Usages.resize(Usages.size()+1);
    
    				result = pclsObj->Get(L"BytesReceivedPerSec", 0, &vtProp, 0, 0);
    				Usages[Usages.size()-1] = vtProp.lVal;
    				VariantClear(&vtProp);
    
    				Usages.resize(Usages.size()+1);
    				result = pclsObj->Get(L"BytesSentPerSec", 0, &vtProp, 0, 0);
    				Usages[Usages.size()-1] = vtProp.lVal;
    				VariantClear(&vtProp);
    
    				Usages.resize(Usages.size()+1);
    				result = pclsObj->Get(L"CurrentBandwidth", 0, &vtProp, 0, 0);
    				Usages[Usages.size()-1] = vtProp.lVal;
    				VariantClear(&vtProp);
    
    				Interfaces.resize(Interfaces.size()+1);
    				result = pclsObj->Get(L"Name", 0, &vtProp, 0, 0);
    				Interfaces[Interfaces.size()-1] = vtProp.bstrVal;
    				VariantClear(&vtProp);
    
    				//AfxMessageBox(IntToStr(vtProp.vt));
    
    				//AfxMessageBox(tmpStr);
    
    //				swscanf(vtProp.bstrVal,L"%d",&Usages[Usages.size()-1]);
    				VariantClear(&vtProp);
    
    				//VERY IMPORTANT!! OTHERWISE MEMORY INCREASES!!
    				pclsObj->Release();
    			}
    			else
    			{
    				// Ups? 
    				break;
    			}
    		}
    
    	}
    	pEnumerator->Release();
    
    	return Usages;
    }
    

    Die Methode GetNetUsages wird übern Timer ca. alle 50 ms aufgerufen!

    Gruß



  • Okay, mit Win32_PerfRawData_Tcpip_NetworkInterface gehts, dort werden die values nicht gelöscht!

    Man muss nur noch eine Diferenz errechnen, und die zeit dazwischen dann hat man die up und down raten!

    vector<__int64> NEOsLcdClass::GetNetUsages(vector <CString> &Interfaces)
    {
    	vector<__int64> Usages;
    
    /* BEGIN */
    
    	HRESULT result;
    	IEnumWbemClassObject* pEnumerator = NULL;
    	IWbemClassObject *pclsObj;
    	DWORD uReturn;
    
    	Interfaces.clear();
    
    	if(InitializedWbem)
    	{
    		/* Use the IWbemServices pointer to make requests of WMI */
    
    		result = pSvc->ExecQuery(
    					_T("WQL"), 
    					_T("SELECT * FROM Win32_PerfRawData_Tcpip_NetworkInterface"),
    
    					WBEM_FLAG_BIDIRECTIONAL, 
    					NULL,
    					&pEnumerator);
    
    		if(/*result != WBEM_S_NO_ERROR*/FAILED(result))
    		{
    			/* Failed! */
    			AfxMessageBox(L"Something Happend :(" + IntToStr(WBEM_S_NO_ERROR));
    			vector<__int64> ClearRetVal;
    			return ClearRetVal;
    		}
    
    		/* Get data */
    
    		__int64 tmpval;
    		int i = 0;
    		while(i++ < NumberOfProzessors)
    		{
    			VARIANT vtProp;
    
    			result = pEnumerator->Next(WBEM_INFINITE, 1, &pclsObj, &uReturn);
    
    			if(uReturn != 0 )
    			{
    
    				VariantInit(&vtProp);
    
    				//swscanf(vtProp.bstrVal, L"%I64u", &ulVal[nCtr]);
    				result = pclsObj->Get(L"Name", 0, &vtProp, 0, 0);
    
    				Interfaces.push_back(vtProp.bstrVal);
    
    				//swscanf(vtProp.bstrVal,L"%d",&Usages[Usages.size()-1]);
    				VariantClear(&vtProp);
    
    				result = pclsObj->Get(L"BytesReceivedPerSec", 0, &vtProp, 0, 0);
    				Usages.push_back(vtProp.ullVal);
    				VariantClear(&vtProp);
    
    				result = pclsObj->Get(L"BytesSentPerSec", 0, &vtProp, 0, 0);
    				Usages.push_back(vtProp.ullVal);
    				VariantClear(&vtProp);
    
    				result = pclsObj->Get(L"Timestamp_Sys100NS", 0, &vtProp, 0, 0);
    				swscanf_s(vtProp.bstrVal, L"%I64u", &tmpval);
    				Usages.push_back(tmpval);
    
    				//Usages.push_back(vtProp.ullVal);
    				VariantClear(&vtProp);
    
    				//VERY IMPORTANT!! OTHERWISE MEMORY INCREASES!!
    				pclsObj->Release();
    			}
    			else
    			{
    				/* Ups? */
    			}
    		}		
    	}
    	pEnumerator->Release();
    
    	return Usages;
    }
    
    void NEOsLcdClass::UpdateUsagesTimerFunction()
    {	
    	vector<__int64> NetUsage;
    
    	/* GET NET STATS */
    	NetUsage = GetNetUsages(NetInterfaces);
    	if(NetInterfaces.size() != TotalBytesReceived.size())
    	{
    		//RESET!!;
    
    		TotalBytesReceived.resize(NetInterfaces.size());
    		TotalBytesSent.resize(NetInterfaces.size());
    		TimeStamp_Sys100NS.resize(NetInterfaces.size());
    
    		l_TotalBytesReceived.resize(NetInterfaces.size());
    		l_TotalBytesSent.resize(NetInterfaces.size());
    		l_TimeStamp_Sys100NS.resize(NetInterfaces.size());
    
    		for(int i=0;i<(int)NetInterfaces.size();i++)
    		{
    			TotalBytesReceived[i] = NetUsage[i*3 + 0];
    			TotalBytesSent[i] = NetUsage[i*3 + 1];
    			TimeStamp_Sys100NS[i] = NetUsage[i*3 + 2];
    
    			l_TotalBytesReceived[i] = TotalBytesReceived[i];
    			l_TotalBytesSent[i] = TotalBytesSent[i];
    			l_TimeStamp_Sys100NS[i] = TimeStamp_Sys100NS[i];
    		}
    	}
    
    	for(i=0;i < NetInterfaces.size(); i++)
    	{
    		TotalBytesReceived[i] = NetUsage[i*3 + 0];
    		TotalBytesSent[i] = NetUsage[i*3 + 1];
    		TimeStamp_Sys100NS[i] = NetUsage[i*3 + 2];
    	}
    
    	tmpVal = "";
    
    	__int64 BytesReceived,BytesSent,TimeDif;
    
    	for(i=0;i < TotalBytesReceived.size(); i++)
    	{
    		BytesReceived = TotalBytesReceived[i] - l_TotalBytesReceived[i];
    		BytesSent = TotalBytesSent[i] - l_TotalBytesSent[i];
    
    		TimeDif = TimeStamp_Sys100NS[i] - l_TimeStamp_Sys100NS[i];
    
    		if(TimeDif > 0)
    		{
    			BytesReceived = (int)floor((double)10000000/TimeDif * BytesReceived);
    			BytesSent = (int)floor((double)10000000/TimeDif * BytesSent);
    		}
    		else
    		{
    			BytesReceived = 0;
    			BytesSent = 0;
    		}
    //Reset Vals
    		l_TotalBytesReceived[i] = TotalBytesReceived[i];
    		l_TotalBytesSent[i] = TotalBytesSent[i];
    		l_TimeStamp_Sys100NS[i] = TimeStamp_Sys100NS[i];
    
    	}
    }
    

    Gruß


Anmelden zum Antworten