Script 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"112Views1like1Comment