// *** Establish API Creds ***
def accessId = hostProps.get("lmaccess.id");
def accessKey = hostProps.get("lmaccess.key");
def account = 'micoresolutions';
// *** Get Datasource ID for device as string. ***
def resourcePath = '/device/devices/'+hostProps.get("system.deviceID")+'/devicedatasources'
def url = "https://" + account + ".logicmonitor.com" + "/santaba/rest" + resourcePath;
//get current time
epoch = System.currentTimeMillis();
//calculate signature
requestVars = "GET" + epoch + resourcePath;
hmac = Mac.getInstance("HmacSHA256");
secret = new SecretKeySpec(accessKey.getBytes(), "HmacSHA256");
hmac.init(secret);
hmac_signed = Hex.encodeHexString(hmac.doFinal(requestVars.getBytes()));
signature = hmac_signed.bytes.encodeBase64();
// HTTP GET
CloseableHttpClient httpdsid = HttpClients.createDefault();
httpGet = new HttpGet(url);
URI uri = new URIBuilder(httpGet.getURI())
.addParameter("fields", "id")
.addParameter("filter", "dataSourceName:Microsoft_SQLServer_GlobalPerformance")
.build();
httpGet.setURI(uri);
httpGet.addHeader("Authorization" , "LMv1 " + accessId + ":" + signature + ":" + epoch);
response = httpdsid.execute(httpGet);
responseBody = EntityUtils.toString(response.getEntity());
code = response.getStatusLine().getStatusCode();
datapull = new JsonSlurper().parseText(responseBody.toString());
datapull = datapull.data.items.id.value.toString()
datapull = datapull.replace("[","")
datasouceid = datapull.replace("]","")
httpdsid.close()
// *** get instance ID for each instance on device ***
resourcePath = "/device/devices/"+hostProps.get("system.deviceID")+"/devicedatasources/${datasouceid}/instances"
url = "https://" + account + ".logicmonitor.com" + "/santaba/rest" + resourcePath;
//get current time
epoch = System.currentTimeMillis();
//calculate signature
requestVars = "GET" + epoch + resourcePath;
hmac = Mac.getInstance("HmacSHA256");
secret = new SecretKeySpec(accessKey.getBytes(), "HmacSHA256");
hmac.init(secret);
hmac_signed = Hex.encodeHexString(hmac.doFinal(requestVars.getBytes()));
signature = hmac_signed.bytes.encodeBase64();
// HTTP GET
CloseableHttpClient httpinstanceid = HttpClients.createDefault();
httpGet = new HttpGet(url);
httpGet.addHeader("Authorization" , "LMv1 " + accessId + ":" + signature + ":" + epoch);
response = httpinstanceid.execute(httpGet);
responseBody = EntityUtils.toString(response.getEntity());
code = response.getStatusLine().getStatusCode();
datapull = new JsonSlurper().parseText(responseBody.toString());
device = datapull.data.items.id
httpinstanceid.close()