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("ciscoEOXapi.key")
def client_secret = hostProps.get("ciscoEOXapi.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)
// Call the EOX API to get end-of-life information about the device (if it is available)
def eoxApiURL = new URL("https://apix.cisco.com/supporttools/eox/rest/5/EOXBySerialNumber/1/${serial_number}");
connection = eoxApiURL.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);
// Cisco: Last date to receive service and support for the product. After this date, all support services for the product are unavailable, and the product becomes obsolete.
if (apiResponseJson.EOXRecord[0].LastDateOfSupport != "null") {
println "auto.ciscosupport.endofsupport=${apiResponseJson.EOXRecord[0].LastDateOfSupport.value}"
}
// Cisco: The date on which the end of sale and the end-of-life milestones for a Product is communicated to the public.
if (apiResponseJson.EOXRecord[0].EOXExternalAnnouncementDate ) {
println "auto.ciscosupport.endoflifenotification=${apiResponseJson.EOXRecord[0].EOXExternalAnnouncementDate.value}"
}
// Cisco: The Product is no longer offered for sale after this date. This is also the last date to order the Product through Cisco point-of-sale mechanisms. The EOS date is documented in the EOL notification.
if (apiResponseJson.EOXRecord[0].EndOfSaleDate) {
println "auto.ciscosupport.endofsale=${apiResponseJson.EOXRecord[0].EndOfSaleDate.value}"
}
// 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())
}
}