Custom Threshold export
Im trying to automate the export of all custom ("INSTANCE") alert thresholds from a specific LogicMonitor device group (org.org.devices) using PowerShell and the LogicMonitor REST API. I want to get a total count and optionally a breakdown per device.
I have the following script which:
Authenticates using the LMv1 HMAC scheme
Finds the device group by its full path
Recursively retrieves all subgroups and devices
Fetches datasources and instances for each device
Counts thresholds where thresholdSource == 'INSTANCE'
Runs in parallel across devices for performance
param (
[string]$AccessId,
[string]$AccessKey,
[string]$Company,
[string]$GroupPath,
[int] $MaxThreads = 10
)
function Invoke-LmApi {
param(
[string]$Method,
[string]$ResourcePath,
[string]$QueryParams = '',
[string]$Body = ''
)
$epoch = [math]::Round((Get-Date).ToUniversalTime() - [datetime]'1970-01-01').TotalMilliseconds
$toSign = "$Method$epoch$Body$ResourcePath"
$hmac = New-Object System.Security.Cryptography.HMACSHA256
$hmac.Key = [Text.Encoding]::UTF8.GetBytes($AccessKey)
$hash = $hmac.ComputeHash([Text.Encoding]::UTF8.GetBytes($toSign))
$signature = [Convert]::ToBase64String($hash)
$auth = "LMv1 ${AccessId}:${signature}:${epoch}"
$url = "https://$Company.logicmonitor.com/santaba/rest$ResourcePath$QueryParams"
return Invoke-RestMethod -Uri $url -Method $Method -Headers @{ Authorization = $auth }
}
function Get-GroupIdByPath {
param([string]$Path)
$resp = Invoke-LmApi -Method GET -ResourcePath '/device/groups' -QueryParams "?filter=fullPath:$Path&size=1"
if (-not $resp.data.items) { throw "Group '$Path' not found" }
return $resp.data.items[0].id
}
function Count-CustomThresholds {
param([string]$RootPath)
$rootId = Get-GroupIdByPath -Path $RootPath
$groupIds = @($rootId)
# TODO: collect subgroups, devices and count thresholds in parallel
}
Count-CustomThresholds -RootPath "$Company/$GroupPath"
However, I'm having trouble getting the script to reliably find my target group and export the counts. It either reports "Group not found" or hangs/slowly processes. Here is the current PowerShell script:
What I've tried so far:
Switching between filtering on name: vs fullPath:
Adjusting group path prefixes (with/without company)
Serial vs parallel loops
Verifying the group exists via manual API calls
Questions:
What is the recommended way to filter by device group path (fullPath) using LogicMonitor's API?
Are there any pitfalls in the LMv1 authentication or parallel Invoke-RestMethod calls I should avoid?
How can I streamline this script to reliably export a summary of custom thresholds with minimal API calls?
Any guidance would be greatly appreciated. Thanks in advance!