Managed to get back to looking at this today. It's an ugly first pass with no debugging or error checking but it appears to validate the approach (I imported more modules that needed :-)). The output lets me set a datapoint for CPU hourly average, I can then present in a dynamic table widget & pull all associated device's avg CPU data in one API call via the /dashboard/widgets/{widgetId}/data endpoint.
/***************************************************************************************/
import org.apache.http.HttpEntity;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;
import javax.crypto.Mac;
import javax.crypto.spec.SecretKeySpec;
import java.security.MessageDigest
import org.apache.commons.codec.binary.Hex;
import com.santaba.agent.groovyapi.http.*;
import groovy.json.JsonSlurper;
import groovy.json.JsonOutput;
// Import relevant properties from the device.
accessId = hostProps.get("lmaccess.id")
accessKey = hostProps.get("lmaccess.key")
account = hostProps.get("lmaccount")
hostname = hostProps.get("system.hostname")
deviceId = '##SYSTEM.DEVICEID##'
dataSourceFilter = 3002393
// Set request target and paramaters to get Device Instances
resourcePath = '/device/devices/' + deviceId + '/instances'
queryParameters = '?filter=dataSourceId:' + dataSourceFilter
url = 'https://' + account + '.logicmonitor.com' + '/santaba/rest' + resourcePath + queryParameters;
// Make HTTP request
HTTP_Request('GET',url,resourcePath)
def instanceResult(responseBody){
json_slurper = new JsonSlurper()
body = json_slurper.parseText(responseBody)
return body
}
def deviceDataSourceId = (instanceResult(responseBody).items.deviceDataSourceId.flatten().first())
resourcePath = '/device/devices/' + deviceId + '/devicedatasources/' + deviceDataSourceId + '/data'
queryParameters = '?aggregate=average&datapoints=CPUBusyPercent'
url = 'https://' + account + '.logicmonitor.com' + '/santaba/rest' + resourcePath + queryParameters
// Make HTTP request
HTTP_Request('GET',url,resourcePath)
def result(responseBody){
json_slurper = new JsonSlurper()
body = json_slurper.parseText(responseBody)
return body
}
def dataPointsRaw = result(responseBody).dataPoints
def valuesRaw = result(responseBody).instances.Microsoft_Windows_CPU.values
def timeRaw = result(responseBody).instances.Microsoft_Windows_CPU.time
//Cleaned payload objects
def dataPoints = (dataPointsRaw.flatten().first())
def values = (valuesRaw.flatten().first())
def time = (timeRaw.flatten().first())
println("dataPoints=${dataPoints}")
println("values=${values}")
println("time=${time}")
return(0)
//Auth and Rest Helpers
// Get current time
def Find_Epoch(){
epoch = System.currentTimeMillis()
return epoch
}
// Calculate signature
def Generate_Signature(verb,resourcePath,data=null){
if(verb == "GET"){
requestVars = verb + Find_Epoch() + resourcePath
}
else{
requestVars = verb + Find_Epoch() + data + 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()
return signature
}
// Build HTTP request.
def HTTP_Request(verb,url,resourcePath,data=''){
CloseableHttpClient httpclient = HttpClients.createDefault()
switch(verb){
case 'GET':
httpReq = new HttpGet(url);
break;
}
httpReq.addHeader("Authorization" , "LMv1 " + accessId + ":" + Generate_Signature(verb,resourcePath) + ":" + Find_Epoch())
httpReq.addHeader("X-Version" , "3")
response = httpclient.execute(httpReq)
responseBody = EntityUtils.toString(response.getEntity())
httpclient.close()
return responseBody
}