8 years ago
Rest API and Powershell
So I have had pretty good luck converting the Python examples to Powershell.
The one thing I can not figure out how to do is return volume usage for a specific host.
Anyone got any example ...
Hi Gary,
To get volume usage, you'll want to use the data resource: https://www.logicmonitor.com/support/rest-api-developers-guide/data/get-data/
You'll need the id of the device you want to get volume usage for, and the datasource and instance ids that correspond to volume usage for that device. Alternatively, you can leave out the instance id if you have multiple volumes you want usage for on the same device. In PowerShell, the request should look something like this (with the correct ids, of course):
<# account info #>
$accessId = 'YQQ75w6Mxx9zWIeAMq5H'
$accessKey = 'f)!Z}%spR=6en+4^s2$t32r-3=NpdQ]2T{-deI)8'
$company = 'apiAccount'
<# request details #>
$httpVerb = 'GET'
$resourcePath = '/device/devices/533/devicedatasources/10256/instances/23/data'
$queryParams = ‘?period=2’
<# 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 + $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 Get -Header $headers
<# Print status and body of response #>
$status = $response.status
$body = $response.data| ConvertTo-Json -Depth 5
Write-Host "Status:$status"
Write-Host "Response:$body"
Thanks,
Sarah