Forum Discussion

George_Bica's avatar
8 years ago

SDT scheduling using REST API and PowerShell

Trying to schedule a new SDT for January 1, 2018 1:00 AM to 1:30 AM for a device group, but getting a Status:1007 and a blank response.

<# account info #>
$accessId = 'SHj6Hub8e63FUwkc5'
$accessKey = 'xz37=(][{qb6ANLp}5$-S9Hvn6HV292P'
$company = 'api'

# stdTYpe (integer)
# 1 - one time, 2 - Weekly SDT, 3 - Monthly SDT, 4 - Daily SDT
# we have to use "one time" style values because LM has no concept of day of month
$stdTYpe = 1

# type (string)
# ServiceGroupSDT, DeviceGroupSDT, CollectorSDT
$type = "DeviceGroupSDT"

# deviceGroupId  (string)
#
$deviceGroupId = 18

# dataSourceId (integer)
# 0 = ALL
$dataSourceId = 0

<# request details #>
$httpVerb = 'POST'
$resourcePath = '/sdt/sdts'

#serviceGroupSDTs

# data
$data = '{"sdtType":'+$stdTYpe+',"type":"'+ $type +'","deviceGroupId":'+ $deviceGroupId +',"dataSourceId":'+ $dataSourceId +',"startDateTime":1514786400,"endDateTime":1514788200}'


<# Construct URL #>
$url = 'https://' + $company + '.logicmonitor.com/santaba/rest' + $resourcePath

<# 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 -Body $data -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"

 

  • The working script

    <# account info #>
    $accessId = 'IDHERE'
    $accessKey = 'KEYHERE'
    $company = 'api'
    
    # stdTYpe (integer)
    # 1 - one time, 2 - Weekly SDT, 3 - Monthly SDT, 4 - Daily SDT
    # we have to use "one time" style values because LM has no concept of day of month
    $stdTYpe = 1
    
    # type (string)
    # ServiceGroupSDT, DeviceGroupSDT, CollectorSDT
    $type = "DeviceGroupSDT"
    
    # deviceGroupId  (string)
    #
    $deviceGroupId = 18
    
    # dataSourceId (integer)
    # 0 = ALL
    $dataSourceId = 0
    
    <# request details #>
    $httpVerb = 'POST'
    $resourcePath = '/sdt/sdts'
    
    # maintenance start
    $maintenancestart = "01/01/2018 01:00:00"
    
    #maintenance length (in minutes)
    $maintenancelength = 30
    
    $startDate = (Get-Date -Date $maintenancestart).ToUniversalTime()
    $startDateepoch = [Math]::Round((New-TimeSpan -start (Get-Date -Date "1/1/1970") -end $startDate).TotalMilliseconds)
    
    $endDate = $startDate.AddMinutes($maintenancelength)
    $endDateepoch = [Math]::Round((New-TimeSpan -start (Get-Date -Date "1/1/1970") -end $endDate).TotalMilliseconds)
    
    # device group data
    $data = '{"sdtType":'+$stdTYpe+',"type":"'+ $type +'","deviceGroupId":'+ $deviceGroupId +',"dataSourceId":'+ $dataSourceId +',"startDateTime":'+ $startDateepoch +',"endDateTime":'+ $endDateepoch +'}'
    
    <# Construct URL #>
    $url = 'https://' + $company + '.logicmonitor.com/santaba/rest' + $resourcePath
    
    <# 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 -Body $data -Header $headers
    
    <# Print status and body of response #>
    $status = $response.status
    $body = $response.data| ConvertTo-Json -Depth 5
    
    # Write-Host "Query:$response"
    Write-Host "Status:$status"
    Write-Host "Response:$body"

     

  • Updated version of the script. I think I will stop here :D

    # Usage via command line
    #
    # LogicMonitor-Add-SDTs.ps1 -maintenance_date "12/03/2018" -maintenance_time "01:00:00" -maintenance_length 30 -type "serviceGroupSDT" -id 14
    #
    # Usage via CSV import:
    
    # $csv = Import-Csv file.csv
    # foreach ($line in $csv) {
    #       LogicMonitor-Add-SDTs.ps1 -maintenance_date $line.date -maintenance_time $line.time -maintenance_length $line.length -type $line.type -id $line.id
    # }
    #
    # the CSV file needs to have the following header and content type
    # Date,Time,Length,Type,ID
    # 
     
      Param(
                    [Parameter(Mandatory=$True)]
                    [string]$maintenance_date,
                    [Parameter(Mandatory=$True)]
                    [string]$maintenance_time,
                    [Parameter(Mandatory=$True)]
                    [int]$maintenance_length,
                    [Parameter(Mandatory=$True)]
                    [string]$type,
                    [Parameter(Mandatory=$True)]
                    [int]$id,
                    [switch]$force = $false
                    )
    
    <# account info #>
    $accessId = 'IDHERE'
    $accessKey = 'KEYHERE'
    $company = 'api'
    
    # stdTYpe (integer)
    # 1 - one time, 2 - Weekly SDT, 3 - Monthly SDT, 4 - Daily SDT
    # we have to use "one time" style values because LM has no concept of day of month
    $stdTYpe = 1
    
    
    # dataSourceId (integer)
    # 0 = ALL
    $dataSourceId = 0
    
    <# request details #>
    $httpVerb = 'POST'
    $resourcePath = '/sdt/sdts'
    
    $startDate = (Get-Date -Date "$maintenance_date $maintenance_time").ToUniversalTime()
    $startDateepoch = [Math]::Round((New-TimeSpan -start (Get-Date -Date "1/1/1970") -end $startDate).TotalMilliseconds)
    
    $endDate = $startDate.AddMinutes($maintenance_length)
    $endDateepoch = [Math]::Round((New-TimeSpan -start (Get-Date -Date "1/1/1970") -end $endDate).TotalMilliseconds)
    
    # data
    switch ($type)
    
        {
         "DeviceGroupSDT" {$data = '{"sdtType":'+$stdTYpe+',"type":"'+ $type +'","deviceGroupId":'+ $Id +',"dataSourceId":'+ $dataSourceId +',"startDateTime":'+ $startDateepoch +',"endDateTime":'+ $endDateepoch +'}'}
         "ServiceGroupSDT" {$data = '{"sdtType":1,"type":"ServiceGroupSDT","serviceGroupId":14,"startDateTime":'+ $startDateepoch +',"endDateTime":'+ $endDateepoch +'}'}
         "CollectorSDT" {$data = '{"sdtType":'+$stdTYpe+',"type":"'+ $type +'","collectorId":'+ $Id +',"startDateTime":'+ $startDateepoch +',"endDateTime":'+ $endDateepoch +'}'}
         default {$data = ""}
        }
    
    <# Construct URL #>
    $url = 'https://' + $company + '.logicmonitor.com/santaba/rest' + $resourcePath
    
    <# 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 -Body $data -Header $headers
    
    <# Print status and body of response #>
    $status = $response.status
    $body = $response.data| ConvertTo-Json -Depth 5
    
    # Write-Host "Query:$data"
    Write-Host "Status:$status"
    Write-Host "Response:$body"

     

  • Hi George,

    I am looking for device SDT which it can read devices from txt file. I tried to modify the above script and getting below error. Could you help me on it.

    I am looking the solution to read the device names from txt file and making it in to sdt. Getting below output

    Status:1007
    Response:

    <# account info #>
    $accessId = 'XXXXX'
    $accessKey = 'XXXXX'
    $company = 'XXX'
     
    # stdTYpe (integer)
    # 1 - one time, 2 - Weekly SDT, 3 - Monthly SDT, 4 - Daily SDT
    # we have to use "one time" style values because LM has no concept of day of month
    $stdTYpe = 1
     
    # type (string)
    # ServiceGroupSDT, DeviceGroupSDT, CollectorSDT
    $type = "DeviceSDT"
     
    # deviceGroupId (string)
    #
    $deviceDisplayName = 'hostname'
     
    # dataSourceId (integer)
    # 0 = ALL
    #$dataSourceId = 0
     
    <# request details #>
    $httpVerb = 'POST'
    $resourcePath = '/sdt/sdts'
     
    # maintenance start
    $maintenancestart = "07/12/2018 11:30:00"
     
    #maintenance length (in minutes)
    $maintenancelength = 30
     
    $startDate = (Get-Date -Date $maintenancestart).ToUniversalTime()
    $startDateepoch = [Math]::Round((New-TimeSpan -start (Get-Date -Date "1/1/1970") -end $startDate).TotalMilliseconds)
     
    $endDate = $startDate.AddMinutes($maintenancelength)
    $endDateepoch = [Math]::Round((New-TimeSpan -start (Get-Date -Date "1/1/1970") -end $endDate).TotalMilliseconds)
     
    # device group data
    $data = '{"sdtType":'+$stdTYpe+',"type":"'+ $type +'","devicename":'+ $deviceDisplayName +',"startDateTime":'+ $startDateepoch +',"endDateTime":'+ $endDateepoch +'}'
     
    <# Construct URL #>
    $url = 'https://' + $company + '.logicmonitor.com/santaba/rest' + $resourcePath
     
    <# 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 -Body $data -Header $headers
     
    <# Print status and body of response #>
    $status = $response.status
    $body = $response.data| ConvertTo-Json -Depth 5
     
    # Write-Host "Query:$response"
    Write-Host "Status:$status"
    Write-Host "Response:$body"

     

  • On 12/7/2017 at 2:22 PM, bellarmin.c said:

    Hi George,

    I am looking for device SDT which it can read devices from txt file. I tried to modify the above script and getting below error. Could you help me on it.

    I am looking the solution to read the device names from txt file and making it in to sdt. Getting below output

    Status:1007
    Response:

    <# account info #>
    $accessId = 'XXXXX'
    $accessKey = 'XXXXX'
    $company = 'XXX'
     
    # stdTYpe (integer)
    # 1 - one time, 2 - Weekly SDT, 3 - Monthly SDT, 4 - Daily SDT
    # we have to use "one time" style values because LM has no concept of day of month
    $stdTYpe = 1
     
    # type (string)
    # ServiceGroupSDT, DeviceGroupSDT, CollectorSDT
    $type = "DeviceSDT"
     
    # deviceGroupId (string)
    #
    $deviceDisplayName = 'hostname'
     
    # dataSourceId (integer)
    # 0 = ALL
    #$dataSourceId = 0
     
    <# request details #>
    $httpVerb = 'POST'
    $resourcePath = '/sdt/sdts'
     
    # maintenance start
    $maintenancestart = "07/12/2018 11:30:00"
     
    #maintenance length (in minutes)
    $maintenancelength = 30
     
    $startDate = (Get-Date -Date $maintenancestart).ToUniversalTime()
    $startDateepoch = [Math]::Round((New-TimeSpan -start (Get-Date -Date "1/1/1970") -end $startDate).TotalMilliseconds)
     
    $endDate = $startDate.AddMinutes($maintenancelength)
    $endDateepoch = [Math]::Round((New-TimeSpan -start (Get-Date -Date "1/1/1970") -end $endDate).TotalMilliseconds)
     
    # device group data
    $data = '{"sdtType":'+$stdTYpe+',"type":"'+ $type +'","devicename":'+ $deviceDisplayName +',"startDateTime":'+ $startDateepoch +',"endDateTime":'+ $endDateepoch +'}'
     
    <# Construct URL #>
    $url = 'https://' + $company + '.logicmonitor.com/santaba/rest' + $resourcePath
     
    <# 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 -Body $data -Header $headers
     
    <# Print status and body of response #>
    $status = $response.status
    $body = $response.data| ConvertTo-Json -Depth 5
     
    # Write-Host "Query:$response"
    Write-Host "Status:$status"
    Write-Host "Response:$body"

     

     

    I just realized I am not following my own post. What is the error message?

  • Since it has been a while and no response was given, if you are attempting to use device names from a list you need to specify the write property. The above code would return a bad JSON response.

    "deviceDisplayName":