LogicMonitor seeks to disrupt AI landscape with $800M strategic investment at $2.4B valuation to revolutionize data centers.

Learn More

Creating Services Using LogicMonitor REST API v3

Last updated on 07 October, 2024

LogicMonitor Service Insight enables you to create logical services to monitor at service level. A service can be created on one or more monitored devices available under Resources. Resources include devices, cloud resources, kubernetes resources (for example, pods, nodes, services,  deployments, and so on). For more information, see About LM Service Insights

You can manage services using LogicMonitor REST API devices endpoints. In LogicMonitor, a service is considered as a type of device and the devices API endpoints can be considered as the resources API endpoint, you can programmatically manage (add, create, update, and delete) services with the devices API endpoints.

Creating Services using LogicMonitor REST API v3

  1. Add a device of type 6 (POST /device/devices).
  2. Add a Datasource that was created using the LogicMonitor user interface or imported using the API.
    The datasource automatically applies to the service to aggregate the data.

Required Fields

The following table displays all mandatory fields for creating services using the /device/devices endpoint.

PropertyTypeDescription
nameStringFor devices, it is the host name or IP address. For services, unless you are representing a cluster with a cluster IP, it is the display name without spaces.
Example – “name”:”Prod-Service”
displayNameStringIt is the display name of the service. It determines how the service is displayed on the resource tree.
Example – “displayName”:”Reporting Service”
preferredCollectorIdIntegerFor services, this must always be -4 which indicates that data aggregation happens at LogicMonitor’s backend.
Example – “preferredCollectorId”:-4
deviceTypeIntegerFor services, this must always be 6.
Example – “deviceType”:6
predef.bizservice.membersJSON objectIt is a custom property, and defines the devices and instances that must be grouped in the service. Top level objects are device and instance which enable you to add devices and instances respectively. Note: As the value is a string, double quotes inside the value may need to be escaped. For example, in Python you place “\\” before every double quote inside the value string.
Example – “customProperties”:[{“name”:”predef.bizservice.members”,”value”:”{“device”:[{“deviceGroupFullPath”:”*”,”deviceDisplayName”:”*”,”deviceProperties”:[{“name”:”kubernetes.label.app”,”value”:”argus”}]}],”instance”:[]}”}]
predef.bizservice.evalMembersIntervalJSON objectIt is a custom property and controls the frequency at which membership is re-evaluated for the service.
Example – “customProperties”:[{“name”:”predef.bizservice.members”,”value”:”30″}]

Optional Fields

In addition to the mandatory fields, you can include the following optional fields to create services such as, the membership re-evaluation rate.

PropertyTypeDescription
hostGroupIdsStringThe IDs of the groups the service is in. You can provide a list of group IDs separated by a comma.
Example – “hostGroupIds”:"2,34,5"
descriptionStringThe description of the service.Example – “description”:”The production reporting service”
disableAlertingBooleanIndicates whether alerting is disabled (true) or enabled (false) for this service.
Example – “disableAlerting”:false
customPropertiesJSON objectThe custom properties defined for this service. Each property must have a name and value.
Example – “customProperties”:[{“name”:”team”,”value”:”prod”},{“name”:”location”,”value”:”Santa Barbara, CA”}]

Example

The following Python example adds a service Reporting Service that groups all devices with property kubernetes.label.app=argus. The member re-evaluation interval is set to 30 minutes.

#!/bin/env python
 
import requests
import json
import hashlib
import base64
import time
import hmac
import getpass
 
#Account Info: LogicMonitor recommends to NEVER hardcode the credentials. Instead, retrieve the values from a secure storage.
#Note: The below is provided for illustration purposes only.
AccessId = getpass.getpass("Enter your AccessId: ")
AccessKey = getpass.getpass("Enter your AccessKey: ")
Company = 'apiAccount'
 
#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 digest = hmac.new(
        AccessKey.encode('utf-8'),
        msg=requestVars.encode('utf-8'),
        digestmod=hashlib.sha256).hexdigest()
signature = base64.b64encode(digest.encode('utf-8')).decode('utf-8')  
 
#Construct headers auth = 'LMv1 ' + AccessId + ':' + str(signature) + ':' + epoch
headers = {'Content-Type':'application/json','Authorization':auth,'X-Version':'3'} 
 
#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
In This Article