Please find below a groovy script enabling the 'Send to Teams' action type. This is required to enable forwarding of incidents from Entuity to Microsoft Teams.
// forward a payload to Teams
def getContainingDeviceNameAndIp = { sourceDescriptor ->
if (!sourceDescriptor) {
logger.warn("getContainingDeviceNameAndIp: Could no obtain source descriptor")
return ["", ""]
}
def classicType = sourceDescriptor.classicType
def OBJ_DEVICE = 4, OBJ_IPV4ADDRESS = 2048, OBJ_IPV6ADDRESS = 16384
/*
* If the currently processed object is an event or and incident comes from a device then
* return the device's name and polling IP address
*/
if (classicType == OBJ_DEVICE) {
def proxy = createInventoryProxy(sourceDescriptor)
if (logger.isTraceEnabled()) {
logger.trace("getContainingDeviceNameAndIp: Getting data for a device: ${sourceDescriptor.name} cont")
logger.trace("getContainingDeviceNameAndIp: Getting data for a device ip address: ${proxy?.devPolledIpAddr}")
}
return [sourceDescriptor.name, proxy.devPolledIpAddr]
}
/*
* If the currently processed object is an event or and incident comes from an IPv4/6 address
* than return that address as a name as well
*/
else if (classicType == OBJ_IPV4ADDRESS || classicType == OBJ_IPV6ADDRESS) {
return [sourceDescriptor.externalId, sourceDescriptor.externalId]
}
/*
* If the currently processed object is something else e.g. a port on a device then
* obtain a container device id from the source descriptor and look up a containing
* device object using specially prepared comp id.
*/
else {
def deviceId = sourceDescriptor.containerDeviceId
if (logger.isTraceEnabled()) {
logger.trace("getContainingDeviceNameAndIp: Getting container device id: ${deviceId}")
}
if (!deviceId) {
logger.warn("getContainingDeviceNameAndIp: Could not obtain containerDeviceId")
return ["", ""]
}
int[] compId = new int[4];
compId[0] = OBJ_DEVICE
compId[1] = deviceId
compId[2] = 0
compId[3] = 0
def device = lookupObject(compId)
if (logger.isTraceEnabled()) {
logger.trace("getContainingDeviceNameAndIp: Getting container device: ${device?.name} for compId ${compId} continue...")
logger.trace("getContainingDeviceNameAndIp: Getting container device ip addr: ${device?.devPolledIpAddr}")
}
if (!device) {
logger.warn("getContainingDeviceNameAndIp: Could no obtain containing device")
return ["", ""]
}
return [device.name, device.devPolledIpAddr]
}
}
/***
* Returns serverName:port
*/
def constructLocalHostName = {
def serverWebPort = getConfigProperty("webportnum")
def serverName = shortHostname
return "${serverName}:${serverWebPort}"
}
/**
* Returns the name of the ena host or central server
* that is forwarding this event
*/
def constructHostName = { ms ->
if(ms) {
def consolidationServerName = getConfigProperty("consolidation_server_name")
def consolidationServerWebPort = getConfigProperty("consolidation_server_web_port")
if (consolidationServerName) {
if (consolidationServerWebPort) {
return "${consolidationServerName}:${consolidationServerWebPort}"
} else {
return "${consolidationServerName}"
}
} else {
return constructLocalHostName()
}
} else {
return constructLocalHostName()
}
}
/**
* Returns a webUI or component viewer URL to
* the object that the event or incident is comming from.
*/
def constructUrl = { sd ->
def url = "";
def classicType = sd?.classicType
def OBJ_TYPE_SYSTEM = com.entuity.events.event.ObjType.OBJ_TYPE_SYSTEM.toInt()
def OBJ_TYPE_VIEW = com.entuity.events.event.ObjType.OBJ_TYPE_VIEW.toInt()
if(classicType != OBJ_TYPE_VIEW) {
def ssl = getConfigProperty("consolidation_server_web_ssl")
if(ssl == null)
ssl = getConfigProperty("server.ssl_enabled")
def proto = ssl == "true" ? "https" : "http";
def swId = sd.swId;
if (swId != null) {
url = "${proto}://"
//def serverName = constructHostName(true)
def serverName = getConfigProperty("reverseconn.server_hostname")
url += "${serverName}"
if (classicType != OBJ_TYPE_SYSTEM) {
url += "/webUI/main.do?url=/webUI/objectSummary.do%3Fserver%3D${sourceDescriptor?.eyeServerId}%26id%3D${swId}"
} else {
url += "/webUI/main.do?url=/webUI/viewEvents.do%3Fserver%3D${sourceDescriptor?.eyeServerId}%26view%3DMy%20Network"
}
}
}
return url.toString()
}
/**
* Get string version of incident state
*/
def getStateString = { state ->
switch(state) {
case 1 : return "Open"
case 2 : return "Closed"
case 4 : return "Expired"
case 8 : return "PreOpen"
}
}
// map Entuity to Agent severity levels
def getSeverity = { level ->
if(level == 10)
"Critical"
else if (level == 8)
"Severe"
else if (level == 6)
"Major"
else if (level == 4)
"Minor"
else if (level ==2)
"Warning"
else if (level == 0)
"Info"
else
"Unknown"
}
if(incident) {
def sd = sourceDescriptor;
/**
* Enrich the sourceDesriptor with more event data
*/
sd.enrich(facilities);
def iUrl = constructUrl(sd);
def node = sd.name
def severity = getSeverity(severity)
def eClass = "Entuity from Incident"
def fullTitle = "${severity} ${eClass}"
def iStatus = getStateString(state)
def titleDevice = "${name} on ${node} is ${iStatus}"
Date latestdate = new Date()
def time = latestdate.toString()
def jsonBody = new groovy.json.JsonBuilder()
def devNameAndIp = getContainingDeviceNameAndIp(sd)
jsonBody {
type "message"
attachments( [ {
contentType "application/vnd.microsoft.card.adaptive"
contentUrl null
content {
type "AdaptiveCard"
$schema "http://adaptivecards.io/schemas/adaptive-card.json"
version "1.4"
body {
type "TextBlock"
text titleDevice
wrap true
size "Large"
fontType "Default"
weight "Bolder"
color "Warning"
horizontalAlignment "center"
}
{
type "TextBlock"
text "Created Date ${time}"
isSubtle true
wrap true
}
{
type "Container"
items {
type "TextBlock"
text "Device Information"
wrap true
size "Large"
weight "Bolder"
}
{
type "FactSet"
facts {
title "Device Name"
value "${node}"
}
{
title "IP"
value "${devNameAndIp[1]}"
}
}
}
{
type "Container"
items {
type "TextBlock"
text "Incident Information"
wrap true
size "Large"
weight "Bolder"
}
{
type "FactSet"
facts ([ {
title "Incident Type"
value reason
}])
spacing "Small"
}
}
{
type "ActionSet"
actions([ {
type "Action.OpenUrl"
title "View Incident"
url "${iUrl}"
}
])
}
}
}
])
}
// Create JSON Payload
/*def jsonBody = groovy.json.JsonOutput.toJson(
[
"type":"message",
"text":title,
]
)*/
def jsonLog = jsonBody.toPrettyString()
if(state != PREOPEN) {
nbsp; try {
def teamsUrl = param("path")
logger.info("Forwarding incident to Teams ${jsonLog}");
def post = new URL("${teamsUrl}").openConnection() as HttpURLConnection
post.requestMethod = "POST"
post.doOutput = true
post.setRequestProperty("Content-Type", "application/json")
post.setRequestProperty("Accept", "application/json")
logger.info(post)
def out = new OutputStreamWriter(post.outputStream)
out.write(jsonBody.toPrettyString())
out.close()
logger.info(post.getResponseMessage())
} catch (Exception e) {
logger.info(e.getMessage())
}
} else {
logger.info("Not Forwarding incident to Teams ${jsonBody}");
}
}
Comments
0 comments
Please sign in to leave a comment.