Forum Discussion
llama
21 days agoNeophyte
ok so i've got this now
// import the logicmonitor http and jsonslurper classes
import com.santaba.agent.groovyapi.http.*
import groovy.json.JsonSlurper
import java.text.SimpleDateFormat
import java.util.TimeZone
// setup
ip = hostProps.get('system.hostname')
user = hostProps.get('ise.monitoring.user')
pass = hostProps.get('ise.monitoring.pass')
host = hostProps.get('auto.system_name')
// base64 encoding un+pw for basic auth header
auth_string = user + ':' + pass
encodedString = auth_string.bytes.encodeBase64()
auth = 'Basic ' + encodedString
// open a connection
httpClient = HTTP.open(ip, 443)
// define api endpoint
url = "https://${ip}/api/v1/certs/system-certificate/${host}"
// create headers
headers = [:]
headers['Authorization'] = auth
headers['Content-Type'] = 'application/json'
// get request to api endpoint
getResponse = httpClient.get(url, headers)
// get status code
statusCode = httpClient.getStatusCode()
// close connection
httpClient.close()
// Check for HTTP response status
if (statusCode != 200) {
println "Error: Received HTTP status code ${statusCode}"
return 1
}
// parse response and error handling
response = httpClient.getResponseBody()
// current time in epoch
now_epoch = new Date().getTime()
data = new JsonSlurper().parseText(response)
// Collect friendlyNames and expirationDates into a list of maps
data_list = data.response.collect { [friendlyName: it.friendlyName, expirationDate: it.expirationDate] }
// define simple date format
dateFormat = new SimpleDateFormat('EEE MMM dd HH:mm:ss z yyyy')
dateFormat.setTimeZone(TimeZone.getTimeZone('UTC')) // set Tz to UTC
// Loop over the data_list to process each item
data_list.each { item ->
// Print friendly name and expiration date
println "friendlyName: ${item.friendlyName}, expirationDate: ${item.expirationDate}"
// Parse the expiration date
try {
date = dateFormat.parse(item.expirationDate)
epoch_millis = date.getTime()
println "expiry_epoch: ${epoch_millis}"
// Calculate days to expiry
millis_to_expiry = epoch_millis - now_epoch
days_to_expiry = millis_to_expiry / (1000 * 60 * 60 * 24)
println "days_to_expiry: ${days_to_expiry}"
} catch (Exception e) {
println "Error parsing expiration date for ${item.friendlyName}: ${e.message}"
return 1
}
}
// If everything was successful, exit with code 0
return 0
and it returns as shown in the screenshot, however my data points don't work because it says it's NaN:
Attribute not valid or not found in output - (method=regex, param=days_to_expiry:\s\d+)
in your example Mike_Moniz you have TextDescriptor=Number
how would you set the datapoint up? would that be multiline key/value pair?
Apologies for the potentially dumb question, i'm new to logic monitor.
Related Content
- 7 months ago
- 2 years ago
- 3 months ago