Forum Discussion

Paul_Carroll's avatar
7 years ago

Add device to Collector with least devices in rest api

I hope someone here can help,

Using powershell and the rest API I would like to do a lookup of selected collectors find which has the least amount of devices on it then add the a new device .

I have the script for adding the device and that works great just need the collector lookup, has anyone done something similar.

Thank you in advanced for all help provided

Paul

  • Here is what I use to "report" on collectors and their versions.

     

    $status = $null
    $body = $null
    $response = $null
    
    <# Use TLS 1.2 #>
    [Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Tls12
    
    <# account info #>
    $accessId = ''
    $accessKey = ''
    $company = 'SITE'
    
    <# request details #>
    $httpVerb = 'GET'
    $resourcePath = '/setting/collectors'
    $queryParams = '?size=1000&fields=id,description,hostname,platform,collectorGroupName,collectorSize,numberOfHosts,build'
    #$queryParams = ''
    $status = $null
    $body = $null
    $response = $null
    $data = ''
    
    <# Construct URL #>
    $url = 'https://' + $company + '.logicmonitor.com/santaba/rest' + $resourcePath + $queryParams
    
    <# Get current time in milliseconds #>
    $epoch = [Math]::Round((New-TimeSpan -start (Get-Date -Date "1/1/1970") -end (Get-Date).ToUniversalTime()).TotalMilliseconds)
    
    <# Concatenate Request Details #>
    $requestVars = $httpVerb + $epoch + $data + $resourcePath
    
    <# Construct Signature #>
    $hmac = New-Object System.Security.Cryptography.HMACSHA256
    $hmac.Key = [Text.Encoding]::UTF8.GetBytes($accessKey)
    $signatureBytes = $hmac.ComputeHash([Text.Encoding]::UTF8.GetBytes($requestVars))
    $signatureHex = [System.BitConverter]::ToString($signatureBytes) -replace '-'
    $signature = [System.Convert]::ToBase64String([System.Text.Encoding]::UTF8.GetBytes($signatureHex.ToLower()))
    
    <# Construct Headers #>
    $auth = 'LMv1 ' + $accessId + ':' + $signature + ':' + $epoch
    $headers = New-Object "System.Collections.Generic.Dictionary[[String],[String]]"
    $headers.Add("Authorization",$auth)
    $headers.Add("Content-Type",'application/json')
    
    <# Make Request #>
    $response = Invoke-RestMethod -Uri $url -Method $httpVerb -Header $headers 
    
    <# Print status and body of response #>
    $status = $response.status
    #$body = $response.data| ConvertTo-Json -Depth 5
    
    $body = $response.data | ConvertTo-Json -Depth 2
    $Collectors = $body | ConvertFrom-Json
    
    #$Collectors.items | Sort collectorGroupName | format-table -AutoSize
    $Collectors.items | Sort collectorGroupName | Out-Gridview
    #$body

     

    The numberOfHosts would help you out in this case.