REST API Developer's Guide

Creating Services via the API

Overview

LM Service Insight enables you to create and monitor at the service level by creating logical "services" in LogicMonitor. The release of LM Service Insight coincided with the new umbrella term "resources". Resources include devices, services, cloud resources, kubernetes resources (e.g. pods, nodes, services, deployments), and new types will be added as we move forwards. To ensure backwards compatibility, we did not rename our API resources. As such, API management of services is actually done via the devices resource, where a service is a type of device (and where the devices API endpoint really can be thought of as the resources API endpoint). This means that you can programmatically manage (add, create, update, delete) services with the devices API resource. This document provides examples and additional information for doing so. 

Creating a service in the UI comprises of two parts:
  1. grouping together devices and instances to create the service
  2. creating a DataSource that is configured to aggregate data across service members
With the API, these two steps would be:
  1. add a device of type 6 (POST /device/devices)
  2. add a DataSource
While the ability to add DataSources isn't published quite yet (coming soon...), you can easily add the service itself via API (and have DataSources that were created via UI or imported via API and that automatically apply to the service to aggregate data).

Details & Required Fields

Aside from specifying a device type of 6 to create a service, there are a number of other requirements. Most importantly, the service members are specified via property. Below is a table that shows all required fields for creating a service with the /device/devices endpoint:

Property

Description

Type

Example

name For devices this is the host name or IP address. For services, unless you are trying to represent a cluster with a cluster IP, this field can be a display name without spaces. String "name":"Prod-Service"
displayName The display name of the service - this determines how the service will display in the resource tree. String "displayName":"Reporting Service"
preferredCollectorId For services, this should always be -4, which indicates that data aggregation will happen on LogicMonitor's backend. Integer "preferredCollectorId":-4
deviceType For services, this should always be 6. Integer "deviceType":6
predef.bizservice.members This field is a custom property, and defines the devices and instances that should be grouped into the service. Top level objects are 'device' and 'instance' (which enable you to add devices and instances respectively). Note that because the value is a string, double quotes inside the value may need to be escaped. E.g. in Python you would place "\\" before every double quote inside the value string. JSON object "customProperties":[{"name":"predef.bizservice.members","value":"{"device":[{"deviceGroupFullPath":"*","deviceDisplayName":"*","deviceProperties":[{"name":"kubernetes.label.app","value":"argus"}]}],"instance":[]}"}]
predef.bizservice.evalMembersInterval This is a custom property, and controls how frequently membership is re-evaluated for the service. JSON object "customProperties":[{"name":"predef.bizservice.members","value":"30"}]


In addition to the required fields above, there are a number of optional fields you can include when creating your service (such as membership re-evaluation rate):

Property

Description

Type

Example

hostGroupIds The Id(s) of the groups the service is in, where multiple group ids should be comma separated String "hostGroupIds":"2,34"
description The service description String "description":"The production reporting service"
disableAlerting Indicates whether alerting is disabled (true) or enabled (false) for this service Boolean "disableAlerting":false
customProperties Define custom properties for this service. Each property needs to have a name and a value. JSON object "customProperties":[{"name":"team","value":"prod"},{"name":"location","value":"Santa Barbara, CA"}]


The following example script adds a Service named 'Reporting Service' that groups all devices with the property 'kubernetes.label.app=argus'. The member re-evaluation interval is set to 30 minutes:
#!/usr/bin/env python

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

#Account Info
AccessId ='xxxxx'
AccessKey ='yyyy'
Company = 'ACCOUNT'

#Request Info
httpVerb ='POST'
resourcePath = '/device/devices'
queryParams ='?v=2'
data = '{"name":"testAPIService","displayName":"Reporting Service","deviceType":6,"preferredCollectorId":-4,"customProperties":[{"name":"predef.bizService.evalMembersInterval","value":"30"},{"name":"predef.bizservice.members","value":"{\\"device\\":[{\\"deviceGroupFullPath\\":\\"*\\",\\"deviceDisplayName\\":\\"*\\",\\"deviceProperties\\":[{\\"name\\":\\"kubernetes.label.app\\",\\"value\\":\\"argus\\"}]}],\\"instance\\":[]}"}]}'

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

#Construct headers
auth = 'LMv1 ' + AccessId + ':' + signature.decode() + ':' + epoch
headers = {'Content-Type':'application/json','Authorization':auth}

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

#Print status and body of response
print ('Response Status:',response.status_code)
parsed = json.loads(response.content)
print(json.dumps(parsed, indent=4))