Forum Discussion
import groovy.json.JsonSlurper
import java.time.LocalDateTime
import com.logicmonitor.mod.Snippets
// switch on to enable debug output
def debug = false
// get the info we need to call the API
def client_key = hostProps.get("ciscoSN2INFOapi.key")
def client_secret = hostProps.get("ciscoSN2INFOapi.pass")
def serial_number = hostProps.get("auto.endpoint.serial_number")
// Get an API token. This calls a function that will get it from collector cache if available, otherwise will call the API to generate a new token
api_token = getToken(client_key, client_secret, false, debug)
// Create GET request
def coverageApiURL = new URL("https://apix.cisco.com/sn2info/v2/coverage/summary/serial_numbers/${serial_number}?page_index=1");
connection = coverageApiURL.openConnection();
connection.setRequestMethod("GET");
connection.setRequestProperty("Accept", "application/json");
connection.setRequestProperty("Authorization", "Bearer " + api_token);
if (connection.responseCode == 200) {
apiResponse = connection.inputStream.withReader { Reader reader -> reader.text }
} else {
println("Failed: ${connection.responseCode}")
}
apiResponseJson = new JsonSlurper().parseText(apiResponse);
// println "auto.support.service_contract_cover=${apiResponseJson.serial_numbers[0].is_covered}"
// println "auto.support.warranty_end_date=${apiResponseJson.serial_numbers[0].warranty_end_date}"
// STANDARD OUTPUTS AVAILABLE FOR ALL DEVICES
// ==========================================
// Cisco: Indicates whether the specified serial number is covered by a service contract; one of the following values: YES or NO. If the serial number is covered by a service contract, the value is Yes.
def is_covered = apiResponseJson.serial_numbers[0].is_covered
if (is_covered) { println "auto.ciscosupport.has_service_contract=${is_covered}" }
// Cisco: End date of the warranty for the specified serial number in the following format: YYYY-MM-DD; for example, 2010-01-01.
def warranty_end_date = apiResponseJson.serial_numbers[0].warranty_end_date
if (warranty_end_date) { println "auto.ciscosupport.warranty_end_date=${warranty_end_date}" }
// Function for retrieving token from the collector cache or getting a new one from the Cisco API if we don't
// have one cached (either it's expired or we've never run this script before on this collector)
def getToken (client_key, client_secret, force = false, debug = false) {
// Do we already have a token in the cache on this collector?
def scriptCache = this.class.classLoader.loadClass("com.santaba.agent.util.script.ScriptCache").getCache();
access_token = scriptCache.get("cisco-support-api-token") ?: null
if (access_token == null || force == true) {
// we must create a new token as there wasn't a valid one in the cache, or the
// calling script has forced us to get a new one
LMDebugPrint("Getting a new token from Cisco API", debug)
// Create POST URL.
def loginUrl = new URL("https://id.cisco.com/oauth2/default/v1/token");
connection = loginUrl.openConnection();
connection.setRequestMethod("POST");
connection.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
connection.setDoOutput(true);
// Build credential check
def authRequest = 'grant_type=client_credentials&client_id=' + client_key + '&client_secret=' + client_secret
// Write credential request to output stream.
def writer = new OutputStreamWriter(connection.getOutputStream());
writer.write(authRequest);
writer.flush();
response = connection.inputStream.withReader { Reader reader -> reader.text }
jsonResponse = new JsonSlurper().parseText(response);
// calculate the datetime that the token will expire and add it to the json response
LocalDateTime now = LocalDateTime.now();
LocalDateTime expiryDateTime = now.plusSeconds(jsonResponse.expires_in)
jsonResponse.expiryDateTime = expiryDateTime
// store the token in the cache with a lifetime 2 mins less than what it really has to make sure
// we don't try and use it when it is about to expire
cacheLifetime = (jsonResponse.expires_in - 120) * 1000
scriptCache.set("cisco-support-api-token", jsonResponse.access_token, cacheLifetime)
access_token = jsonResponse.access_token
} else {
LMDebugPrint("Reusing token from collector cache", debug)
}
return access_token
}
/**
* Helper function to print out debug messages for troubleshooting purposes.
*/
def LMDebugPrint(message, boolean debug) {
if (debug) {
println(message.toString())
}
}