Wednesday, August 17, 2022

Analyze Azure Devops agent usage

Azure Devops agents write logs of all the work they do: with one log per pipeline run. These logs can be analyzed to understand the usage levels of agents on the server by using both the file attributes and contents, which include all the variables and script for a process.

Update the powershell script below with the corresponding folders for your agent installation and update the list of agents, or remove the foreach if you only have one agent running. 

The log folders don't appear to be cleaned so you might want to implement a cleaning schedule, or limit the get-childItem to the most recent week or month if you have a lot of traffic on your agents.

$List = New-Object System.Collections.ArrayList foreach ($agent in ("A7","B1","C1")) { $expression = /^.(\d{4}-\d{2}-\d{2}) .*Job message:\n(.*)\[\1 $WorkerFiles=Get-ChildItem C:\azagent\$agent\_diag\Worker*.log foreach ($file in $WorkerFiles) { $contents = ( Get-Content $file -raw )-replace ' \*{3}', ' "BLOCKED"' #$contents = ( Get-Content C:\azagent\B1\_diag\Worker_20210706-104057-utc.log -raw )-replace ' \*{3}', ' "BLOCKED"' $result = $contents -match "(?s)\[(\d{4}-\d{2}-\d{2}).* Job message:(.*?)\[\1" $jobjson = ConvertFrom-Json $Matches[2] if ($result) { $Hash = [ordered]@{ file=$file.Name date = get-date -date $file.CreationTime -Format "yy/MM/dd" start = get-date -date $file.CreationTime -Format "HH:mm" end = get-date -date $file.LastWriteTime -Format "HH:mm" duration = $file.LastWriteTime - $file.CreationTime environment = $jobjson.variables.'release.environmentName'.value agent = $agent project = $jobjson.variables.'release.definitionName'.value link = $jobjson.plan.owner._links.web.href } $List.Add( $([pscustomobject]$Hash) ) } else { write-host "No Match for " + $file.FullName} } } $fileName="$($env:TEMP)\TFSAgentUsage.csv" $List | Export-Csv -NoType -UseCulture -path $fileName

No comments:

Post a Comment