• 0
Paul Carroll

Add device to Collector with least devices in rest api

Question

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

Share this post


Link to post
Share on other sites

3 answers to this question

Recommended Posts

  • 1

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.

Share this post


Link to post
Share on other sites
  • 0

Hi Paul,

We are actively working on an enhancement to LogicMonitor to support Load Balanced Collectors.  One of the key features is that when adding a device you can pick a Collector Group instead of an individual collector.  More to come in the next month or two!

~Forrest

Share this post


Link to post
Share on other sites
  • 0
On 13/11/2018 at 5:31 AM, Joe Williams said:

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.

 Thanks Joe.

Really appreciated

I have added the below which filters by selected collectors and selects the one with the least number of machines

Where-Object {($_.id -eq "CollectorId") -or ($_.id -eq "CollectorId")} | Sort-Object numberOfHosts -Descending:$false | Select-Object -First 1| Out-Gridview

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now