Import DataSources from XML

Last updated on 29 June, 2022

Overview

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

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

Request Information

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 = 'companyName'

#File information
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="test.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
hmac1 = hmac.new(AccessKey, msg=requestVars, digestmod=hashlib.sha256).hexdigest()
signature = base64.b64encode(hmac1.encode())

#Construct headers
auth = 'LMv1 ' + AccessId + ':' + signature.decode() + ':' + 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)
Python 3

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"
PowerShell

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 "[email protected];filename=datasource.xml" 'https://api.logicmonitor.com/santaba/rest/setting/datasources/importxml'
cURL
In This Article