Skip to content

Instantly share code, notes, and snippets.

@Rohitrajak1807
Created July 8, 2022 09:36
Show Gist options
  • Save Rohitrajak1807/bc250995bd14ec3296f4c460dc44ff02 to your computer and use it in GitHub Desktop.
Save Rohitrajak1807/bc250995bd14ec3296f4c460dc44ff02 to your computer and use it in GitHub Desktop.
KQL get max CPU millicores and max memory bytes
let subscriptionId = '{subscription-id}';
let resourceGroup = '{resource-group}';
let clusterName = '{cluster-name}';
let startDateTime = datetime({start-tate-time});
let endDateTime = datetime({end-date-time});
let clusterId = strcat('/subscriptions/', subscriptionId, '/resourceGroups/', resourceGroup, '/providers/Microsoft.ContainerService/managedClusters/', clusterName);
let memoryUsageCounterName = 'memoryRssBytes';
let primaryInventory = KubePodInventory |
where TimeGenerated >= startDateTime |
where TimeGenerated < endDateTime |
where isnotempty(ClusterName) |
where isnotempty(Namespace) |
extend Node = Computer |
where ClusterId == clusterId |
where Node == 'virtual-node-aci-linux' |
project TimeGenerated, ClusterId, ClusterName, Namespace, ServiceName, Node = Computer, ControllerName, Pod = Name, ContainerInstance = ContainerName, ContainerID, InstanceName, PerfJoinKey = strcat(ClusterId, '/', ContainerName), ReadySinceNow = format_timespan(endDateTime - ContainerCreationTimeStamp, 'ddd.hh:mm:ss.fff'), Restarts = ContainerRestartCount, Status = ContainerStatus, ContainerStatusReason = columnifexists('ContainerStatusReason', ''), ControllerKind = ControllerKind, PodStatus, ControllerId = strcat(ClusterId, '/', Namespace, '/', ControllerName);
let latestContainersByController = primaryInventory |
where isnotempty(Node) |
summarize arg_max(TimeGenerated, *) by PerfJoinKey |
project ControllerId, PerfJoinKey;
let filteredMemoryUsage = Perf |
where TimeGenerated >= startDateTime |
where TimeGenerated < endDateTime |
where ObjectName == 'K8SContainer' |
where InstanceName startswith clusterId |
project TimeGenerated, CounterName, CounterValue, InstanceName, Node = Computer |
where Node == 'virtual-node-aci-linux';
let memoryUsageByController = filteredMemoryUsage |
where CounterName =~ memoryUsageCounterName |
extend PerfJoinKey = InstanceName |
summarize Value = max(CounterValue) by PerfJoinKey, CounterName |
join (latestContainersByController) on PerfJoinKey |
summarize Value = sum(Value) by ControllerId, CounterName |
project ControllerId, CounterName, MemoryAggregationValue = Value;
let CPUUsageCounterName = 'cpuUsageNanoCores';
let filteredCPUUsage = Perf |
where TimeGenerated >= startDateTime |
where TimeGenerated < endDateTime |
where ObjectName == 'K8SContainer' |
where InstanceName startswith clusterId |
project TimeGenerated, CounterName, CounterValue, InstanceName, Node = Computer |
where Node == 'virtual-node-aci-linux';
let CPUUsageByController = filteredCPUUsage |
where CounterName =~ CPUUsageCounterName |
extend PerfJoinKey = InstanceName |
summarize Value = max(CounterValue) by PerfJoinKey, CounterName |
join (latestContainersByController) on PerfJoinKey |
summarize Value = sum(Value) by ControllerId, CounterName |
project ControllerId, CounterName, CPUAggregationValue = Value/1000000;
let maxMemoryUsage = primaryInventory |
distinct ControllerId, ControllerName, ControllerKind, Namespace |
join kind=leftouter (memoryUsageByController) on ControllerId |
project MaxMemoryRSS = MemoryAggregationValue, ControllerId;
let maxCPUUsage = primaryInventory |
distinct ControllerId, ControllerName, ControllerKind, Namespace |
join kind=leftouter (CPUUsageByController) on ControllerId |
project MaxCPUUsage = CPUAggregationValue, ControllerId;
maxMemoryUsage |
join(maxCPUUsage) on ControllerId |
project MaxCPUUsage, MaxMemoryRSS |
summarize val1 = sum(MaxCPUUsage), val2 = sum(MaxMemoryRSS)|
project MaxCPUUsage = val1, MaxMemoryRSS = val2;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment