Fixing misconfigured Auto-Balanced Collector assignments
I’ve seen this issue pop up a lot in support so I figured this post may help some folks out. I just came across a ticket the other day so it’s fresh on my mind! In order for Auto-Balanced Collector Groups (ABCG) to work properly, i.e.balance and failover, you have to make sure that the Collector Group is set to the ABCG and (and this is the important part) the Preferred Collector is set to “Auto Balance”. If it is set to an actual Collector ID, then it won’t get the benefits of the ABCG. You want this, not that: Ok, so that’s cool but now the real question is how do you fix this? There’s not really a good way to surface in the portal all devices where this is misconfigured. It’s not a system property so a report or AppliesTo query won’t help here… Fortunately, not all hope is lost! You can use the✨API✨ When you GET a Resource/device, you will get back some JSON and what you want is for the autoBalancedCollectorGroupId field to equal the preferredCollectorGroupId field. If “Preferred Collector” is not “Auto Balance” and set to a ID, then autoBalancedCollectorGroupId will be 0 . Breaking it down step by step: First, get a list of all ABCG IDs https://www.logicmonitor.com/swagger-ui-master/api-v3/dist/#/Collector%20Groups/getCollectorGroupList /setting/collector/groups?filter=autoBalance:true Then, with any given ABCG ID, you can filter a device list for all devices where there’s this mismatch https://www.logicmonitor.com/swagger-ui-master/api-v3/dist/#/Devices/getDeviceList /device/devices?filter=autoBalancedCollectorGroupId:0,preferredCollectorGroupId:11 (where 11 is the ID of a ABCG) And now for each device returned, make a PATCH so that autoBalancedCollectorGroupId is now set to preferredCollectorGroupId https://www.logicmonitor.com/swagger-ui-master/api-v3/dist/#/Devices/patchDevice Here’s a link to the full script, written in Python for you to check out. I’ll also add it below in a comment since this is already getting long. Do you have a better, easier, or more efficient way of doing this? I’d love to hear about it!290Views12likes9CommentsHow to use a bearer token for authentication using Python SDK
I am trying to use a bearer token for authentication using Python SDK. On the documentation I found several examples using the LMv1 access_id and access_key values, like this: # Configure API key authorization: LMv1 configuration = logicmonitor_sdk.Configuration() configuration.company = 'YOUR_COMPANY' configuration.access_id = 'YOUR_ACCESS_ID' configuration.access_key = 'YOUR_ACCESS_KEY' # create an instance of the API class api_instance = logicmonitor_sdk.LMApi(logicmonitor_sdk.ApiClient(configuration)) but couldn’t find any example on using bearer token for authentication. After some search I found something about defining “api_token” and “api_token_prefix” but couldn’t get it to work. Anybody have any example I could follow to use this?417Views17likes21CommentsSDK install deprecated
Got this on a recent reinstall of logicmonitor_sdk using pip. DEPRECATION: logicmonitor_sdk is being installed using the legacy 'setup.py install' method, because it does not have a 'pyproject.toml' and the 'wheel' package is not installed. pip 23.1 will enforce this behaviour change. A possible replacement is to enable the '--use-pep517' option. Discussion can be found at https://github.com/pypa/pip/issues/8559 The part that worries me is that pip 23.1 will enforce the pyproject.toml and given the rate that LM publishes updates the SDK, pip installs will breakat some point. For me that point is pretty soon because 23.1 is the current version of pip. Luckily, I haven’t upgraded my pip from 22.3.1 to 23.1 like it’s prompting me to. Anybody else tried pip installing it while already on 23.1? Guess I’ll mock it up in docker.Solved88Views3likes4Commentsv3 Python SDK outdated?
Hello, I’m trying to use the Python SDK to add websites for monitoring. For my current use case, the primary reason for this is to gain some alerting on our SSL expiry times. Using the SDK has been a generally frustrating experience all around so far, however I’ve noticed that the v3 API is returning fields which aren’t supported or documented by the SDK or API docs. Specifically, for the Website endpoints (GET/POST/PUT), the top-level fields `schema`, `trigger_ssl_expiration_alert` and `trigger_ssl_status_alert` are all unsupported in the 3.0.172 SDK. Unfortunately these also all sound like the fields I need to be setting in order to get SSL monitoring set up. It also appears that the top-level `schema` field is required to enable HTTPS monitoring, as the `schema` field in the `steps` is ignored. I can see that this top-levle field is set to https when toggling the schema in the web UI, when using dev tools to inspect the request. I’ve already done a fair bit of work using the SDK, so I’d hate to have to rip it out at this point and start building all my requests manually in order to support this. I’m using logicmonitor_sdk 3.0.172 at present, which is the latest available release via Pip and tarball. I can see that these fields have been added on the master branch in Github. Is there a timeline for the release of these changes? While I’m here, can I also ask that this functionality is added to your Terraform provider, because that would save me a lot of hours and stress going forward. 😉76Views1like4CommentsAPI not working properly?
Hello guys, I've raised a case with LM support already but I want to share this with the community (to make sure you guys experience this as well or no). We've created a simple Python script (about 4/5 months ago) that checks on a specific device (using its ID) & look for the presence of a certain DataSource (in this specific scenario 'snmp64_If-').Solved36Views0likes2CommentsScript Walkthrough - Automating Collector Installs
Let’s walk through the attached Python script that utilizes the LogicMonitor API. This script will automate a collector install for Linux. It will Create the Collector Object, Download the collector installer, give the collector executable permissions, and finally install and register the collector. Full documentation including examples for the LogicMonitor REST API can be fond in the below link https://www.logicmonitor.com/support/rest-api-developers-guide/ We have examples for most of the functions in the API on our support site. Using these examples, it’s possible to copy\paste only making minor changes to create a script for your needs. This example will explain how to do this as well as combine multiple example scripts together. Declaring the environment and classes. This can be mostly copied\pasted from the example templates. #!/bin/env python import requests import json import hashlib import base64 import time import hmac import commands This is the authentication setup. It’s easier than it looks. To get the AccessID and AccessKey, open the console and create a new or use an existing user. Under the API Tokens, press the + button to generate the tokens. The company name is the portal ie, mine is ‘lmjeffwoeber.logicmonitor.com’ so my company is 'lmjeffwoeber' #Account Info AccessId ='bB29823sN4MdwC9B3k3i' AccessKey ='P-6D)xH_76Q4+AS2-T67hZ%N3|Nc2]6LC4U647%5' Company = 'lmjeffwoeber' Now that we have our authentication setup we need to gather the data to put into our API call URL and create the collector object on the portal. This is from the example on the Add a Collector page https://www.logicmonitor.com/support/rest-api-developers-guide/collectors/add-a-collector/ Feel free to add or remove properties to the “data=’{" string to fit your environment, a complete list of properties can be found in the above link. #Create Collector Object #Request Info httpVerb ='POST' resourcePath = '/setting/collectors' queryParams = '' data = '{"description":"TestCollector1","backupAgentId":39}' #Construct URL url = 'https://'+ Company +'.logicmonitor.com/santaba/rest' + resourcePath +queryParams Next construct the URL, this can be mostly copied\pasted as it does not really change. #Get current time in milliseconds epoch = str(int(time.time() * 1000)) #Concatenate Request details requestVars = httpVerb + epoch + data + resourcePath #Construct signature signature = base64.b64encode(hmac.new(AccessKey,msg=requestVars,digestmod=hashlib.sha256).hexdigest()) #Construct headers auth = 'LMv1 ' + AccessId + ':' + signature + ':' + epoch headers = {'Content-Type':'application/json','Authorization':auth} This part executes the URL and the API will create the Collector Object #Make request response = requests.post(url, data=data, headers=headers) Next is the tricky part. We need the collector ID for the rest of the script so we have to extract that from the returned JSON file and add it to a local variable for later use. LogicMonitor uses the JSON format as payloads. You can read about parsing JSON files from http://docs.python-guide.org/en/latest/scenarios/json/ Lets use the script to walk through the parsing process. This is JSON snippet is what the Add Collector function will return. Response Body: { "status" : 200, "errmsg" : "OK", "data" : { "id" : 84, "createdOn" : 1486761412, "updatedOn" : 1486761412, "upTime" : 0, "watchdogUpdatedOn" : 1486761412, "status" : 0, The collectorID:# or '"id" : 84' is needed for the rest of the script. The jsonResponse = json.loads(response.content) is used to grab the JSON response from the above response function and define it to the local variable jsonResponse. This will give the script access to the returned JSON data. The JSON dictionaries needs to be specified to pull specific data, to accomplish this take a look at the "{", entries in the JSON snippet. These are the dictionaries. To simplify this, we need to place them in [ ] to pull the data out of the JSON file. In this case their is only the main body dictionary which does not need to be references and a "data" dictionary. To pull the value for the ID dictionary use the syntax ['data']['id'] as seen in the example below. #Parse response jsonResponse = json.loads(response.content) #The CollectorID is needed to download the installer deviceID=(jsonResponse['data']['id']) Now the ID number is assigned to the variable 'deviceID'. We are using the Collector ID for URLs so we need to convert this from a int to a string. Easy enough using the str() function #Converting the int to a str deviceIDstr=str(deviceID) #print(deviceIDstr) Now the string version of the collector ID is in the variable deviceIDstr and we can use that in the API URLs. The print function provides nice feedback for the end user so they can track what is happening. print"Create Collector Object ID:"+(deviceIDstr)+" success!" The first part is done and we have created a Collector Object on the portal and stored the Collector ID locally in a variable. Time to download the collector installer. The template for this section can be found on our support site https://www.logicmonitor.com/support/rest-api-developers-guide/collectors/downloading-a-collector-installer/ Declaring the environment and classes and setting up authentication can be skipped as it’s already done. Lets build the URL according to the example script. #Download Collector Installer #Request Info httpVerb ='GET' Next add the collector ID to the URL this can do this by adding in the deviceIDstr var directly in the resource path. #Adding the CollectorID to the URL resourcePath = '/setting/collectors/'+deviceIDstr+'/installers/Linux64' Any property found on the above link to the REST API Collector download can be set on the queryParams variable. The example is using collectorsize= and setting it to nano, as this is just a lab. #Specify size [nano|small|medium|large] queryParams = '?collectorSize=nano' data = '' Just in the case as the previous section, it's possible to copy\paste the construction of the URL as it’s mostly the same as the example. #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 signature = base64.b64encode(hmac.new(AccessKey,msg=requestVars,digestmod=hashlib.sha256).hexdigest()) #Construct headers auth = 'LMv1 ' + AccessId + ':' + signature + ':' + epoch headers = {'Content-Type':'application/json','Authorization':auth} #Make request response = requests.get(url, data=data, headers=headers) This part downloads the collector as “LogicMonitorSetup.bin” to the current working directory. #Print status and write body of response to a file print 'Response Status:',response.status_code file_ = open('LogicMonitorSetup.bin', 'w') file_.write(response.content) file_.close() The download is now complete, now we need to give the LogicMonitorSetup.bin file execute permissions and run it. I used the commands function feel free to use any preferred alternative, we are just running shell commands at this point. #Give execute perm to collector install and install with the silent option commands.getstatusoutput('chmod +x LogicMonitorSetup.bin') End user feedback print"adding execute permissions to the collector download" print"Starting collector install" Run the Setup.bin file with the silent option -y A full list of install options can be seen by running ./LogicMonitorSetup.bin -h commands.getstatusoutput('./LogicMonitorSetup.bin -y') print"Script Complete" And that's it! A new collector should be registered on the portal. -Jeff Below is a copy of the example script used for this post. #!/bin/env python import requests import json import hashlib import base64 import time import hmac import commands #Account Info AccessId ='j53939s54CP3Z9SUp6S5' AccessKey ='k[t2nP6-Srie[=M9Ju%-H8riy8Sww3Mp9j%kse5X' Company = 'lmjeffwoeber' #Create Collector Object #Request Info httpVerb ='POST' resourcePath = '/setting/collectors' queryParams = '' data = '{"description":"TestCollector1"}' #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 signature = base64.b64encode(hmac.new(AccessKey,msg=requestVars,digestmod=hashlib.sha256).hexdigest()) #Construct headers auth = 'LMv1 ' + AccessId + ':' + signature + ':' + epoch headers = {'Content-Type':'application/json','Authorization':auth} #Make request response = requests.post(url, data=data, headers=headers) #Parse response jsonResponse = json.loads(response.content) #The CollectorID is needed to download the installer deviceID=(jsonResponse['data']['id']) #Converting the int to a str deviceIDstr=str(deviceID) #print(deviceIDstr) print"Create Collector Object ID:"+(deviceIDstr)+" success!" #Download Collector Installer #Request Info httpVerb ='GET' #Adding the CollectorID to the URL resourcePath = '/setting/collectors/'+deviceIDstr+'/installers/Linux64' #Specify size [nano|small|medium|large] queryParams = '?collectorSize=nano' data = '' #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 signature = base64.b64encode(hmac.new(AccessKey,msg=requestVars,digestmod=hashlib.sha256).hexdigest()) #Construct headers auth = 'LMv1 ' + AccessId + ':' + signature + ':' + epoch headers = {'Content-Type':'application/json','Authorization':auth} #Make request response = requests.get(url, data=data, headers=headers) #Print status and write body of response to a file Print"Downloading Collector Installer" print 'Response Status:',response.status_code file_ = open('LogicMonitorSetup.bin', 'w') file_.write(response.content) file_.close() #Give execute perm to collector install and install with the silent option commands.getstatusoutput('chmod +x LogicMonitorSetup.bin') print"adding execute permissions to the collector download" print"Starting collector install" #-m bypasses mem check as my lab only has 1 gig of ram. Remove in production commands.getstatusoutput('./LogicMonitorSetup.bin -y -m') print"Script Complete"113Views1like1Comment