How to get Azure VM Guest Metrics (Performance Counters) through an API?

%3CLINGO-SUB%20id%3D%22lingo-sub-254226%22%20slang%3D%22en-US%22%3EHow%20to%20get%20Azure%20VM%20Guest%20Metrics%20(Performance%20Counters)%20through%20an%20API%3F%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-254226%22%20slang%3D%22en-US%22%3E%3CP%3EAble%20to%20retrieve%20Azure%20VM%20Host%20Metrics%20using%20Azure%20Management%20API%20call.%3C%2FP%3E%3CP%3E%3CA%20href%3D%22https%3A%2F%2Fdocs.microsoft.com%2Fen-us%2Fazure%2Fvirtual-machines%2Flinux%2Fmetrics-vm-usage-rest%22%20target%3D%22_blank%22%20rel%3D%22noopener%20noreferrer%20noopener%20noreferrer%22%3Ehttps%3A%2F%2Fdocs.microsoft.com%2Fen-us%2Fazure%2Fvirtual-machines%2Flinux%2Fmetrics-vm-usage-rest%3C%2FA%3E%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EWhen%20trying%20to%20fetch%20performance%20counters%20using%20Azure%20Log%20Analytics%20API%20as%20per%20below%20reference%20link%2C%20it%20is%26nbsp%3B%20generating%20below%20error.%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3E%3CA%20href%3D%22https%3A%2F%2Fdev.loganalytics.io%2Fapiexplorer%2Fquery%3FapiKey%3DDEMO_KEY%26amp%3BappId%3DDEMO_WORKSPACE%26amp%3Btimespan%3DPT12H%26amp%3Bquery%3DAzureActivity%2520%257C%2520summarize%2520count%22%20target%3D%22_blank%22%20rel%3D%22nofollow%20noopener%20noreferrer%20noopener%20noreferrer%22%3Ehttps%3A%2F%2Fdev.loganalytics.io%2Fapiexplorer%2Fquery%3FapiKey%3DDEMO_KEY%26amp%3BappId%3DDEMO_WORKSPACE%26amp%3Btimespan%3DPT12H%26amp%3Bquery%3DAzureActivity%2520%257C%2520summarize%2520count%3C%2FA%3E%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3E%7B%3CBR%20%2F%3E%22error%22%3A%20%7B%3CBR%20%2F%3E%22message%22%3A%20%22Valid%20authentication%20was%20not%20provided%22%2C%3CBR%20%2F%3E%22code%22%3A%20%22AuthorizationRequiredError%22%2C%3CBR%20%2F%3E%22innererror%22%3A%20%7B%3CBR%20%2F%3E%22code%22%3A%20%22UnsupportedKeyError%22%2C%3CBR%20%2F%3E%22message%22%3A%20%22The%20given%20API%20Key%20is%20not%20valid%20for%20the%20request%22%3CBR%20%2F%3E%7D%3CBR%20%2F%3E%7D%3CBR%20%2F%3E%7D%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3ECan%20you%20please%20provide%20anyone%20how%20to%20obtain%20these%20by%20correcting%20above%20error%3F%3C%2FP%3E%3CP%3EDEMO_KEY%20%3A%20key%20generated%20after%20registering%20app%20under%20Azure%20AD%20App%20registrations%20and%20providing%20owner%20permissions%20for%20registered%20app%20to%20access%20Log%20Analytics%20services.%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-LABS%20id%3D%22lingo-labs-254226%22%20slang%3D%22en-US%22%3E%3CLINGO-LABEL%3EAPI%3C%2FLINGO-LABEL%3E%3CLINGO-LABEL%3EAzure%20Log%20Analytics%3C%2FLINGO-LABEL%3E%3CLINGO-LABEL%3Eazure%20vm%3C%2FLINGO-LABEL%3E%3CLINGO-LABEL%3EGuest%3C%2FLINGO-LABEL%3E%3CLINGO-LABEL%3EJson%3C%2FLINGO-LABEL%3E%3CLINGO-LABEL%3Emetrics%3C%2FLINGO-LABEL%3E%3C%2FLINGO-LABS%3E%3CLINGO-SUB%20id%3D%22lingo-sub-259750%22%20slang%3D%22en-US%22%3ERe%3A%20How%20to%20get%20Azure%20VM%20Guest%20Metrics%20(Performance%20Counters)%20through%20an%20API%3F%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-259750%22%20slang%3D%22en-US%22%3E%3CP%3EHi%2C%3C%2FP%3E%0A%3CP%3EI%20think%20you%20might%20have%20mixed%20the%20logs%20and%20metrics%20APIs%2C%20which%20are%20separate.%20Logs%20contain%20a%20lot%20of%20performance%20data%2C%20some%20of%20which%20also%20covered%20by%20Metrics%2C%20which%20might%20be%20confusing.%3C%2FP%3E%0A%3CP%3ETo%20query%2C%20for%20example%2C%20the%20top%203%20highest-memory%20using%20computers%20you%20want%20to%20run%20this%20query%20over%20Log%20Analytics%3A%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CPRE%3EPerf%20%0A%7C%20where%20TimeGenerated%20%26gt%3B%20ago(1h)%20%0A%7C%20where%20ObjectName%20%3D%3D%20%5C%22Memory%5C%22%20and%20CounterName%20%3D%3D%20%5C%22Used%20Memory%20MBytes%5C%22%20%0A%7C%20summarize%20average_used_memory%3Davg(CounterValue)%20by%20Computer%20%0A%7C%20top%203%20by%20average_used_memory%3C%2FPRE%3E%0A%3CP%3E(note%20this%20specific%20counter%20name%20is%20reported%20for%20Linux%20machines%20only).%20To%20run%20this%20through%20the%20API%2C%20post%20the%20query%20in%20a%20JSON%20format%20(shown%20below)%20to%20%22%3CA%20href%3D%22https%3A%2F%2Fapi.loganalytics.io%2Fv1%2Fworkspaces%2FDEMO_WORKSPACE%2Fquery%22%20target%3D%22_blank%22%20rel%3D%22nofollow%20noopener%20noreferrer%20noopener%20noreferrer%22%3Ehttps%3A%2F%2Fapi.loganalytics.io%2Fv1%2Fworkspaces%2FDEMO_WORKSPACE%2Fquery%3C%2FA%3E%22%20(url%20for%20the%20demo%20workspace%20API)%20and%20add%20the%202%20required%20headers%2C%20as%20shown%20below%3A%3C%2FP%3E%0A%3CP%3E%3CSPAN%20class%3D%22lia-inline-image-display-wrapper%20lia-image-align-inline%22%20style%3D%22width%3A%20999px%3B%22%3E%3CIMG%20src%3D%22https%3A%2F%2Fgxcuf89792.i.lithium.com%2Ft5%2Fimage%2Fserverpage%2Fimage-id%2F53767iCCCA3C7AB58CDF99%2Fimage-size%2Flarge%3Fv%3D1.0%26amp%3Bpx%3D999%22%20alt%3D%22API%20query.png%22%20title%3D%22API%20query.png%22%20%2F%3E%3C%2FSPAN%3E%3C%2FP%3E%0A%3CP%3EYou%20can%20find%20the%20details%20%3CA%20href%3D%22https%3A%2F%2Fdev.loganalytics.io%2F%22%20target%3D%22_blank%22%20rel%3D%22nofollow%20noopener%20noreferrer%20noopener%20noreferrer%22%3Ehere%3C%2FA%3E.%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3EHTH%2C%3C%2FP%3E%0A%3CP%3ENoa%3C%2FP%3E%3C%2FLINGO-BODY%3E
Highlighted
Occasional Visitor

Able to retrieve Azure VM Host Metrics using Azure Management API call.

https://docs.microsoft.com/en-us/azure/virtual-machines/linux/metrics-vm-usage-rest

 

When trying to fetch performance counters using Azure Log Analytics API as per below reference link, it is  generating below error.

 

https://dev.loganalytics.io/apiexplorer/query?apiKey=DEMO_KEY&appId=DEMO_WORKSPACE&timespan=PT12H&qu...

 

{
"error": {
"message": "Valid authentication was not provided",
"code": "AuthorizationRequiredError",
"innererror": {
"code": "UnsupportedKeyError",
"message": "The given API Key is not valid for the request"
}
}
}

 

Can you please provide anyone how to obtain these by correcting above error?

DEMO_KEY : key generated after registering app under Azure AD App registrations and providing owner permissions for registered app to access Log Analytics services.

1 Reply
Highlighted

Hi,

I think you might have mixed the logs and metrics APIs, which are separate. Logs contain a lot of performance data, some of which also covered by Metrics, which might be confusing.

To query, for example, the top 3 highest-memory using computers you want to run this query over Log Analytics:

 

Perf 
| where TimeGenerated > ago(1h) 
| where ObjectName == \"Memory\" and CounterName == \"Used Memory MBytes\" 
| summarize average_used_memory=avg(CounterValue) by Computer 
| top 3 by average_used_memory

(note this specific counter name is reported for Linux machines only). To run this through the API, post the query in a JSON format (shown below) to "https://api.loganalytics.io/v1/workspaces/DEMO_WORKSPACE/query" (url for the demo workspace API) and add the 2 required headers, as shown below:

API query.png

You can find the details here.

 

HTH,

Noa