Creating automation script to fetch assets or devices from Claroty
About this task
An automation script is created for fetching device details from Claroty and store them in the Assets table of Maximo IT. The asset table of Maximo IT. The script is designed to limit the data to 100 records per request and increment the offset (0, 100, 200, and so on) until all records are fetched.
Follow the steps below to create the automation script:
Procedure
- Open the Automation Scripts application.
-
On the navigation panel, click Create and select
Script with Action Launch Point : Step 1 of 3, and
enter data in the fields as follows:
- Launch Point: Specify the launch point name, for example, CLAROTY.
- Object: Search for the ASSET object and select it.
- Action: Specify the Action name same as the name given for Launch Point, for example, CLAROTY.
- Check the checkbox for Active? and click Next.
-
On the Create Script with Action Launch Point : Step 2 of
3 pop-up, enter the details in the fields as follows:
- Script: Specify as Jython
- Script Language: Search and select jython
- Log level: Search and select ERROR
- Click Next.
-
On the Create Script with Action Launch Point : Step 3 of
3. In the Script box, enter the following script and click
Create.
Note: The below script is a sample and for reference only, update the script as per your custom field ID's and other changes as necessary.
from java.lang import System, Class, String, StringBuffer from java.net import HttpURLConnection, URL from java.util import HashMap from com.ibm.json.java import JSON, JSONObject,JSONArray from com.ibm.json.java import JSONObject, JSONArray from java.lang import System, Class, String, StringBuffer from java.util import Date, Properties, List, ArrayList ,HashMap , Calendar from org.apache.http import HttpEntity, HttpHeaders, HttpResponse, HttpVersion from org.apache.http.client.entity import UrlEncodedFormEntity from org.apache.http.client.methods import HttpPost from org.apache.http.entity import StringEntity from org.apache.http.impl.client import DefaultHttpClient from org.apache.http.params import BasicHttpParams, HttpParams, HttpProtocolParamBean from psdi.mbo import Mbo, MboRemote, MboSet, MboSetRemote,MboConstants,SqlFormat from psdi.security import UserInfo from psdi.server import MXServer from com.google.gson import JsonParser, JsonElement,JsonObject,Gson if 'mbo' in globals(): # Called from an action (user interaction) token = mbo.getString("TOKEN") c_url = mbo.getString("URL") else: # Called from a CRON (background process) token = str(arg.split(',')[1]) c_url = str(arg.split(',')[0]) # Calling the location script & classification script service.invokeScript("CLAROTYLOC") service.invokeScript("CLAROTYBASECLASS") # passing the filters for claroty device api, we are here passing device category with value IT/OT/IoT & site name as Washington. (these filters & their respective values can be change as per the need) def generateJSON(offset): filterObj = { "filter_by": { "operation": "and", "operands": [ { "field": "device_category", "operation": "in", "value": ["IT","OT","IoT"] }, { "field": "site_name", "operation": "equals", "value": "Washington" } ] }, "limit": 100, "offset": offset, "fields": [ "site_name", "device_name", "device_category", "device_subcategory", "device_type", "uid", "asset_id", "mac_list" ], "include_count": True } jsonData = Gson().toJson(filterObj) return jsonData # method for HTTP POST using url, JSON body and token with bearer authorization def httpPost(uri, jsonstring): # get http parameters params = BasicHttpParams() paramsBean = HttpProtocolParamBean(params) # get http body entities entity = StringEntity(jsonstring) # get client, http headers and request client = DefaultHttpClient() request = HttpPost(uri) request.setParams(params) request.addHeader(HttpHeaders.CONTENT_TYPE, "application/json") request.addHeader('Authorization',str(token)) request.setEntity(entity) # get client response response = client.execute(request) # return JSON response and reference number status = response.getStatusLine().getStatusCode() obj = JSONObject.parse(response.getEntity().getContent()) return obj # Generate initial JSON string jsonStr = generateJSON(0) jsdata = str(jsonStr).replace("\'", "\"") url = str(c_url) logArr=[] log = httpPost(url, jsdata) offset=0 limit=100 # Parse the response jsonElement = JsonParser().parse(log.toString()) jsonObject = jsonElement.getAsJsonObject() # append 0 offset data into logarr logArr.append(jsonObject.get('devices')) # fetch total count of data from claroty result=jsonObject.get('count') totalCount = int(result.toString()) # Check if there are more pages to fetch if(totalCount//limit != 0): maxval = -(totalCount//-limit) for i in range(1,maxval): offset = offset+100 jsonStr = generateJSON(offset) jsdata = str(jsonStr).replace("\'", "\"") logs = httpPost(url, jsdata) jsonElement = JsonParser().parse(logs.toString()) jsonObject = jsonElement.getAsJsonObject() # append next offset data into logarr logArr.append(jsonObject.get('devices')) # Create a new object to accumulate the arrays under one key accumulated_array = {"devices":[]} # Loop through each key-value pair in the original object and accumulate the values for val in logArr: accumulated_array["devices"].extend(val) # Now `accumulated_array` contains the accumulation of all arrays accumulated_data = str(accumulated_array).replace("\'", "\"") # pass claroty api response to CLAROTY_CREATEASSET script varx = HashMap() varx.put("respData",accumulated_data) #call when needs to import assets service.invokeScript("CLAROTY_CREATEASSET ",varx)
In the provided sample script, we are mapping device data from Claroty to Maximo IT’s asset attributes. Some examples of these mappings include:
- assetnum (Maximo) is mapped to asset_id (Claroty)
- description (Maximo) is mapped to device_name (Claroty)
- location (Maximo) is mapped to site_name (Claroty)
These mappings are stored in a local JSON file (C:/Discovery/data1.json), which allows for easy updates to the attribute mappings as needed. The script fetches device details from Claroty, applies the mappings from the file, and then inserts the mapped data into Maximo IT. These can be updated as needed.Data1.json file structure { "result": [{ "ASSETNUM":"asset_id", "SERIALNUM":"serial_number", "DESCRIPTION":"device_name", "ASSETTAG":"uid", "ASSETTYPE":"machine_type", "DEVICETYPE":"device_type", "LOCATION":"site_name", "MANUFACTURER":"manufacturer", "MODELID":"model" }] }
- Save your changes.