REST API Developer's Guide

Import DataSources from XML

You can use LogicMonitor's REST API to programmatically import DataSources from XML.

Note: As with all of our API calls, authentication is required.  

 

HTTP Method: POST

Resource URI: /setting/datasources/importxml

Note the following:

  • the request payload needs to include the content for a valid xml file enclosed in boundary parameters
  • the Content-Type header needs to be set to multipart/form-data and needs to call out the boundary parameter

 

Examples

Python

The following Python script imports a datasource into account api.logicmonitor.com from xml file 'datasource.xml':

#!/bin/env python

import requests
import json
import hashlib
import base64
import time
import hmac

#Account Info
AccessId ='48v2wRzfK94y53sq5EuF'
AccessKey ='H_D9i(f5~B^U36^K6i42=^nS~e75gy382Bf6{)P+'
Company = 'api'

#File information
fileName= 'test.xml'
file = open ('test.xml','r')
xml = file.read()

#Request Info
httpVerb ='POST'
resourcePath = '/setting/datasources/importxml'
queryParams =''
data = '''------WebKitFormBoundary12345BUlKhOAAA1X
Content-Disposition: form-data; name="file"; filename="datasource.xml"
Content-Type: text/xml

''' + xml + '''

------WebKitFormBoundary12345BUlKhOAAA1X--'''

#Construct URL 
url = 'https://'+ Company +'.logicmonitor.com/santaba/rest' + resourcePath +queryParams

#Get current time in milliseconds
epoch = str(int(time.time() * 1000))

#Concatenate Request details
requestVars = httpVerb + epoch + data + resourcePath

#Construct signature
signature = base64.b64encode(hmac.new(AccessKey,msg=requestVars,digestmod=hashlib.sha256).hexdigest())

#Construct headers
auth = 'LMv1 ' + AccessId + ':' + signature + ':' + epoch
headers = {'Content-Type':'multipart/form-data; boundary=----WebKitFormBoundary12345BUlKhOAAA1X','Authorization':auth}

#Make request
response = requests.post(url, data=data, headers=headers)

#Print status and body of response
print 'Response Status:',response.status_code
print 'Response Body:',response.content

 

PowerShell

The following PowerShell script imports a datasource into account api.logicmonitor.com from xml file 'datasource.xml':

<# Use TLS 1.2 #>
[Net.ServicePointManager]::SecurityProtocol =
[Net.SecurityProtocolType]::Tls12 $accessId = '48v2wRzfK94y53sq5EuF' $accessKey = 'aB=k9opY87U~^_cvHiq~98G-j6Q_ja4sr6J8-4+)' $company = 'api' $uploadFile = 'datasource.xml' $FilePath = "C:\Users\Sarah\Desktop\datasource.xml" $httpVerb = 'POST' $resourcePath = '/setting/datasources/importxml' $queryParams = '' $file = Get-Content $FilePath $boundary = [System.Guid]::NewGuid().ToString() $LF = "\r\n" $data = '------' + $boundary +' Content-Disposition: form-data; name="file"; filename="datasource.xml" Content-Type: text/xml '+$file+' ------' + $boundary + '--' $url = 'https://' + $company + '.logicmonitor.com/santaba/rest' + $resourcePath + $queryParams $epoch = [Math]::Round((New-TimeSpan -start (Get-Date -Date "1/1/1970") -end (Get-Date).ToUniversalTime()).TotalMilliseconds) $requestVars = $httpVerb + $epoch + $data + $resourcePath $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())) $auth = 'LMv1 ' + $accessId + ':' + $signature + ':' + $epoch $headers = New-Object "System.Collections.Generic.Dictionary[[String],[String]]" $headers.Add("Authorization",$auth) $headers.Add("Content-Type",'multipart/form-data; boundary=----$boundary') $response = Invoke-RestMethod -Uri $url -Method $httpVerb -Body $data -Header $headers $status = $response.status $body = $response.data Write-Host "Status:$status" Write-Host "Response:$body"

 

cURL

The following curl request imports a datasource into account api.logicmonitor.com from xml file 'datasource.xml':

curl --user 'apiUser:1234567' -X POST -H "multipart/form-data" -F "file=@datasource.xml;filename=datasource.xml" 'https://api.logicmonitor.com/santaba/rest/setting/datasources/importxml'