Calculating total memory and total disk space in Log Analytics | Quisitive

A colleague of mine recently reached out to me to ask for a query which would display key performance counters for each known server (items like CPU, free disk space, free memory, total memory) in Log Analytics. In the first blog post of this series, we created a pretty simple query handle this question but the query did not include the total amount of memory on a system or the total amount of disk space. To determine these values we need to jump through a few mathematical hoops.

The final query is below:

let CPUAvg = Perf

| where TimeGenerated > now(-60min) and (ObjectName == "Processor"

or ObjectName == "System") and CounterName == "% Processor Time"

| summarize CPUAvg = round(avg(CounterValue)) by bin(TimeGenerated, 1h), Computer

| sort

by TimeGenerated, Computer desc;

//CPUAvg

let CPUCount = Perf

| where TimeGenerated > now(-60min)

| where ObjectName == "Processor" and CounterName == "% Processor Time" and InstanceName!="_Total"

| sort by InstanceName asc nulls first

| summarize CPUCount = dcount(InstanceName) by Computer;

//CPUCount

let FreeMemory = Perf

| where TimeGenerated > now(-60min) and (ObjectName == "Memory" and CounterName contains "Available M")

| summarize FreeMemory = (avg(CounterValue)) by bin(TimeGenerated, 1h), Computer, InstanceName

| sort by TimeGenerated, Computer desc;

//FreeMemory

let UsedMemory = Perf

| where TimeGenerated > now(-60min) and (ObjectName == "Memory" and CounterName == "Committed Bytes")

| summarize UsedMemory = (avg(CounterValue)) by bin(TimeGenerated, 1h), Computer, InstanceName

| sort by TimeGenerated, Computer desc;

//UsedMemory

let TotalMemory = FreeMemory | join UsedMemory on Computer, InstanceName

| project TimeGenerated, Computer, InstanceName, FreeMemory, UsedMemory

| extend TotalMemGB = toint((FreeMemory + (UsedMemory / 1024 / 1024)) / 1024);

//TotalMemory

let FreeMB = Perf

| where TimeGenerated > now(-30min) and CounterName == "Free Megabytes"

| summarize FreeMB = (avg(CounterValue)) by bin(TimeGenerated, 1h), Computer, InstanceName

| sort by TimeGenerated, Computer desc;

//FreeMB

let FreeSpace = Perf

| where TimeGenerated > now(-30min) and CounterName == "% Free Space" and InstanceName !contains "DPM"

| summarize FreeSpace = (avg(CounterValue)) by bin(TimeGenerated, 1h), Computer, InstanceName

| sort by TimeGenerated, Computer desc;

//FreeSpace

let DiskTotalFreeMB = FreeMB | join FreeSpace on Computer, InstanceName

| project TimeGenerated, Computer, InstanceName, FreeMB, FreeSpace

| extend TotalSizeGB = toint((FreeMB / FreeSpace * 100) /1024);

//DiskTotalFreeMB

DiskTotalFreeMB | join kind=leftouter CPUAvg on Computer | join kind=leftouter TotalMemory on Computer | join kind=leftouter CPUCount on Computer | sort by TimeGenerated, Computer, InstanceName

| extend FreeDiskGB = FreeMB/1024

| extend FreeMemoryGB = round(FreeMemory/1024)

| project Computer, InstanceName, CPUCount, CPUAvg, FreeMemoryGB, TotalMemGB, round(FreeDiskGB), TotalSizeGB

| distinct Computer, InstanceName, CPUCount, CPUAvg, FreeMemoryGB, TotalMemGB, round(FreeDiskGB), TotalSizeGB

NOTE: The code above was altered on 7/12/2018 to cover performance counters which listed as either “Available MBytes” or “Available Mbytes” per feedback from Billy York (thanks Billy!). We also added CPUCount to the fields so we can also see how many CPU’s exist on the system (the query was drawn from here).

An output example for this query is shown below showing both the values that had for performance counters and the ones that we have calculated (CPUCount, TotalMemGB and TotalSizeGB):

disk space in log analytics

Here’s an example of the actual numbers for a server (the CM one shown above):

disk space in log analytics

The query’s estimate was 151 GB total size which matched the actual value.

disk space in log analytics

The query’s estimate was 8 GB of memory which matched the actual value.

There are a couple of cool tricks in the query above which are worth mentioning.

Summary: The above query provides way to visualize the KPI’s for a server including an estimated amount of disk space and memory for the system (which I don’t believe anyone else has put together prior to this point in time). While this query does provide all of the KPI’s related to cpu, disk and memory it only provides an estimated value for the total disk space and total memory. To provide completely accurate values we will discuss another method to provide that in the next blog post of this series!