Last active
June 6, 2017 08:40
-
-
Save bullshit/a75f9c57bb03f9863a4cee66375c3f1e to your computer and use it in GitHub Desktop.
Telegraf ISSUE 1827
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include "stdafx.h" | |
// Example copied from https://msdn.microsoft.com/en-us/library/windows/desktop/aa372633(v=vs.85).aspx | |
#include <windows.h> | |
#include <stdio.h> | |
#include <pdh.h> | |
#include <pdhmsg.h> | |
#pragma comment(lib, "pdh.lib") | |
CONST PWSTR COUNTER_PATH = L"\\Process(*)\\% Processor Time"; | |
CONST ULONG SAMPLE_INTERVAL_MS = 1000; | |
void main() | |
{ | |
PDH_HQUERY hQuery = NULL; | |
PDH_STATUS status = ERROR_SUCCESS; | |
PDH_HCOUNTER hCounter = NULL; | |
DWORD dwBufferSize = 0; // Size of the pItems buffer | |
DWORD dwItemCount = 0; // Number of items in the pItems buffer | |
PDH_FMT_COUNTERVALUE_ITEM *pItems = NULL; // Array of PDH_FMT_COUNTERVALUE_ITEM structures | |
if (status = PdhOpenQuery(NULL, 0, &hQuery)) | |
{ | |
wprintf(L"PdhOpenQuery failed with 0x%x.\n", status); | |
goto cleanup; | |
} | |
// Specify a counter object with a wildcard for the instance. | |
if (status = PdhAddCounter(hQuery, COUNTER_PATH, 0, &hCounter)) | |
{ | |
wprintf(L"PdhAddCounter failed with 0x%x.\n", status); | |
goto cleanup; | |
} | |
// Some counters need two sample in order to format a value, so | |
// make this call to get the first value before entering the loop. | |
if (status = PdhCollectQueryData(hQuery)) | |
{ | |
wprintf(L"PdhCollectQueryData failed with 0x%x.\n", status); | |
goto cleanup; | |
} | |
for (int i = 0; i < 10; i++) | |
{ | |
Sleep(SAMPLE_INTERVAL_MS); | |
if (status = PdhCollectQueryData(hQuery)) | |
{ | |
wprintf(L"PdhCollectQueryData failed with 0x%x.\n", status); | |
goto cleanup; | |
} | |
// Get the required size of the pItems buffer. | |
status = PdhGetFormattedCounterArray(hCounter, PDH_FMT_DOUBLE, &dwBufferSize, &dwItemCount, pItems); | |
if (PDH_MORE_DATA == status) | |
{ | |
pItems = (PDH_FMT_COUNTERVALUE_ITEM *)malloc(dwBufferSize); | |
if (pItems) | |
{ | |
status = PdhGetFormattedCounterArray(hCounter, PDH_FMT_DOUBLE, &dwBufferSize, &dwItemCount, pItems); | |
if (ERROR_SUCCESS == status) | |
{ | |
// Loop through the array and print the instance name and counter value. | |
for (DWORD i = 0; i < dwItemCount; i++) | |
{ | |
wprintf(L"counter: %s, value %.20g\n", pItems[i].szName, pItems[i].FmtValue.doubleValue); | |
} | |
} | |
else | |
{ | |
wprintf(L"Second PdhGetFormattedCounterArray call failed with 0x%x.\n", status); | |
goto cleanup; | |
} | |
free(pItems); | |
pItems = NULL; | |
dwBufferSize = dwItemCount = 0; | |
} | |
else | |
{ | |
wprintf(L"malloc for PdhGetFormattedCounterArray failed.\n"); | |
goto cleanup; | |
} | |
} | |
else | |
{ | |
wprintf(L"PdhGetFormattedCounterArray failed with 0x%x.\n", status); | |
goto cleanup; | |
} | |
} | |
cleanup: | |
if (pItems) | |
free(pItems); | |
if (hQuery) | |
PdhCloseQuery(hQuery); // Closes all counter handles and the query handle | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment