LogicMonitor SDKs
Last updated on 28 September, 2022LogicMonitor has Python and Go SDKs available for v2 of our REST API. These SDKs allow more efficient and convenient interaction with v2 of LogicMonitor’s REST API, and you may find them useful for building out API-based integrations and workflow processes.
Note: For Python SDKs, the REQUEST parameters can contain camelCase or an underscore. However, the RESPONSE parameters will always contain an underscore. For example, the REQUEST parameter can be testLocation or test_location. The RESPONSE parameter will be test_location.
Downloading the SDKs
You can download the compressed tar file directly for the Python SDK here and the Go SDK here. These SDKs are also available via LogicMonitor’s GitHub.
Using the SDKs
The Python and Go SDKs are auto-generated using Swagger Codegen. Detailed documentation with additional examples for these SDKs is available here. The following are examples to help you get started with the Go and Python SDKs.
Example 1: Get all alerts via Python SDK
from __future__ import print_function
import time
import logicmonitor_sdk
from logicmonitor_sdk.rest import ApiException
from pprint import pprint
# Configure API key authorization: LMv1
configuration = logicmonitor_sdk.Configuration()
configuration.company = 'COMPANY_NAME'
configuration.access_id = 'API_ACCESS_ID'
configuration.access_key = 'API_ACCESS_KEY'
# create an instance of the API class
api_instance = logicmonitor_sdk.LMApi(logicmonitor_sdk.ApiClient(configuration))
try:
# get alert list
api_response = api_instance.get_alert_list()
pprint(api_response)
except ApiException as e:
print("Exception when calling LMApi->getAlertList: %s\n" % e)
Example 2: Add device via Python SDK
from __future__ import print_function
import time
import logicmonitor_sdk
from logicmonitor_sdk.rest import ApiException
from pprint import pprint
# Configure API key authorization: LMv1
configuration = logicmonitor_sdk.Configuration()
configuration.company = 'COMPANY_NAME'
configuration.access_id = 'API_ACCESS_ID'
configuration.access_key = 'API_ACCESS_KEY'
# create an instance of the API class
api_instance = logicmonitor_sdk.LMApi(logicmonitor_sdk.ApiClient(configuration))
body = {"name": "8.8.8.8","displayName": "myDevice","preferredCollectorId": 10,"disableAlerting": "true"}
try:
# add device
api_response = api_instance.add_device(body)
pprint(api_response)
except ApiException as e:
print("Exception when calling LMApi->addDevice: %s\n" % e)
Example 3: Get all alerts via Go SDK
package main
import (
"fmt"
"github.com/logicmonitor/lm-sdk-go/client"
"github.com/logicmonitor/lm-sdk-go/client/lm"
)
func main() {
// define the client
config := client.NewConfig()
accessID := "API_ACCESS_ID"
config.SetAccessID(&accessID)
accessKey := "API_ACCESS_KEY"
config.SetAccessKey(&accessKey)
domain := "COMPANY.logicmonitor.com"
config.SetAccountDomain(&domain)
lmSdk := client.New(config)
// prepare params and request
params := lm.NewGetAlertListParams()
filter := "acked:false"
params.SetFilter(&filter)
resp, err := lmSdk.LM.GetAlertList(params)
if err != nil {
fmt.Println(err.Error())
}
// parse the alert list
idList := make([]string, 0)
for _, alert := range resp.Payload.Items {
idList = append(idList, alert.ID)
}
fmt.Println(idList)
}
Filter Syntax
Filter values are passed as request parameter filters to APIs and they are used to filter values from API response.
Following are the supported operators: –
- $filter ::= $fieldName $operator $valueList
- $fieldName ::= string
- $operator ::= >: | > | <: | < | : | !: | ~ | !~
- $valueList ::= $value [“|” $value]*
- $value ::= numeric or string
Where,
- : is an exact match
- !: not equal to
- ~ contain/partial match
- !~ not contains
- > greater than
- >: greater than or equal to
- < less than
- <: less than or equal to
Examples
1. {{url}}/device/devices?filter=id!~1,deviceType~0
This syntax returns all the device entities not having 1 in the id field and deviceType contain 0
Response -
{ [
{
"id": 2,
"displayName": "198.168.0.1",
"deviceType": 0
},
{
"id": 28,
"displayName": "198.2.2.12",
"deviceType": 0
},
{
"id": 33,
"displayName": "198.2.2.17",
"deviceType": 0
}
]}
Where (,) acts like an AND operator.
2. {{url}}/device/devices?filter=id:1
This syntax returns all the device entities with id equals to 1
Response -
{ [
{
"id": 1,
"displayName": "198.168.0.1",
"deviceType": 0
}
]}
Multiple filters
- For boolean type checks in the filter –
You can filter the responses by filtering strings, which are either true or false.
The strings must be in lower case.
filter =sdted:”true”
Or filter =sdted:true
- For integer type checks in the filter –
The integer value must not be in double-quotes.
filter =duration:153