LM Wrapper (for the Python SDK)
LogicMonitor's Python SDK is pretty good at simplifying some of the more complicated aspects of interacting with LogicMonitor's API. To learn more about the SDK and how to use it, look here and here.
To make things even simpler for myself, I created a wrapper function that creates the api_instance object using credentials stored on the local file system. Here's the documentation:
About
lmwrapper is a Python library for simplifying interaction with the LogicMonitor API. It uses the LogicMonitor Python SDK, but takes it a step further by facilitating credentials management. All credentials are stored in a JSON file called creds.json that looks like this:
{
"lmstuartweenig": {
"API_ACCESS_ID": "adelarthrosomatous",
"API_ACCESS_KEY": "zygomaticoauricularis",
"COMPANY_NAME": "lmstuartweenig"
},
"traininglab": {
"API_ACCESS_ID": "adelarthrosomatous",
"API_ACCESS_KEY": "zygomaticoauricularis",
"COMPANY_NAME": "traininglab"
}
}
If only one set of credentials is needed, the JSON file would look like this:
{
"lmstuartweenig": {
"API_ACCESS_ID": "adelarthrosomatous",
"API_ACCESS_KEY": "zygomaticoauricularis",
"COMPANY_NAME": "lmstuartweenig"
}
}
Installation
Just create a file called lm.py with the contents down below in the same directory as the creds.json file. You'll either need to have the logicmonitor_sdk installed or downloaded into the current directory.
Setup
You can use this tool either in a script or in the interpreter. Either way, once you've got the credentials file setup, getting started only takes one line:
from lm import lm
Usage
If multiple sets of credentials are found in the credentials file, you will be prompted for which set you want to use. Simply return the number (index) of the set you want to use, then you can call any of the published SDK methods against the `lm` object. The example below uses the REPL style of getting the output. You could just as easily pprint it or store the results in a variable.
>>> from lm import lmThe following credentials were found in creds.json.0. lmstuartweenig1. traininglabWhich one would you like to use? 0>>> lm.get_device_list(){'items': [{'auto_properties': [{'name': 'predef.externalResourceID', 'value': '06:57:9f:e2:63:b8'}, {'name': 'auto.eri.override', 'value': '1'}, {'name': 'auto.product.name', 'value': 'null'}, {'name': 'auto.idledays', 'value': '98'}, {'name': 'auto.ip.v4.routing_enabled', 'value': 'false'}, {'name': 'auto.enterprise_number', 'value': '8072'}, {'name': 'auto.endpoint.uptime', 'value': '482 days, 22:17:23.40'}, {'name': 'auto.memory.total', 'value': '8373010432'},---OUTPUT TRUNCATED---
If your credentials file only has one set of credentials, the selection will default to using that one set of credentials.
Attributions
Special thanks go to the following for spawning the idea, method, and benefits:
- Michael Leo at Harvard University
- @Stefan W at Harvard University
lm.py
# (C) 2021-2022 LogicMonitor Inc.# Author: Stuart Weenigimport logicmonitor_sdk, jsonfrom os import pathimport __main__ as maindef response_to_json(response): return(json.dumps(response.to_dict()))credsfile = "creds.json"if path.exists(credsfile): with open(credsfile) as f: creds = json.load(f) if len(creds) == 0: print(f"No credentials found in file {credsfile}.") elif len(creds) == 1 or hasattr(main,'__file__'): # print(f"Using creds {list(creds.values())[0]['API_ACCESS_ID']}@{list(creds.values())[0]['COMPANY_NAME']}") creds = list(creds.values())[0] elif len(creds) > 1: print(f"The following credentials were found in {credsfile}.") for i, key in enumerate(creds): print(f"{i}. {key}") try: credidx = int(input("Which one would you like to use? ")) except KeyboardInterrupt: print("\n") quit() creds = list(creds.values())[credidx] configuration = logicmonitor_sdk.Configuration() configuration.company = creds['COMPANY_NAME'] configuration.access_id = creds['API_ACCESS_ID'] configuration.access_key = creds['API_ACCESS_KEY'] lm = logicmonitor_sdk.LMApi( logicmonitor_sdk.ApiClient( configuration ) )else: print(f"File not found: {credsfile}") print("""Create a config file called creds.json in this directory that looks like this:{ "API_ACCESS_ID": "adelarthrosomatous", "API_ACCESS_KEY": "zygomaticoauricularis", "COMPANY_NAME": "yourportalname"}""") lm = ""