Skip to content

Instantly share code, notes, and snippets.

@sufw
Last active November 19, 2015 12:49
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save sufw/bd4bb9b414a4c89b0623 to your computer and use it in GitHub Desktop.
Save sufw/bd4bb9b414a4c89b0623 to your computer and use it in GitHub Desktop.
SAP CBMA Alerts to OpsGenie
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:ns1="http://mmg.com/Alerting"
version="1.0">
<!-- This XSLT mapping converts an XML representation of an alert message from the
SAP Component-Based Alert Framework into the JSON format expected by the OpsGenie
Alert API (https://www.opsgenie.com/docs/web-api/alert-api#createAlertRequest).
Created by Sascha Wenninger (@sufw) and released under Creative Commons BY-SA.
Available at https://gist.github.com/sufw/bd4bb9b414a4c89b0623 -->
<xsl:strip-space elements="*"/>
<xsl:output method="text"/>
<xsl:variable name="APIKey"><!-- INSERT your OpsGenie API key here. --></xsl:variable>
<xsl:variable name="Flow">
<xsl:if test="/ns1:AlertMessage/FromService">
<xsl:value-of select="/ns1:AlertMessage/FromService"/>
</xsl:if>
<xsl:if test="/ns1:AlertMessage/Interface">
<xsl:text> (</xsl:text>
<xsl:value-of select="/ns1:AlertMessage/Interface"/>
<xsl:text>)</xsl:text>
</xsl:if>
<xsl:if test="/ns1:AlertMessage/ToService">
<xsl:text> --&gt; </xsl:text>
<xsl:value-of select="/ns1:AlertMessage/ToService"/>
</xsl:if>
</xsl:variable>
<xsl:template match="/ns1:AlertMessage">
<xsl:text>{</xsl:text>
<xsl:apply-templates/>
<xsl:text>"apiKey":"</xsl:text>
<xsl:value-of select="$APIKey"/>
<xsl:text>",</xsl:text>
<xsl:text>"source":"PO Alert Framework"</xsl:text>
<xsl:text>}</xsl:text>
</xsl:template>
<xsl:template match="ErrText">
<xsl:variable name="text" select="translate(string(),'&quot;','')"/>
<!-- The 'title' of an Alert in OpsGenie is length-limited, so we need to truncate it. -->
<xsl:variable name="maxLength">130</xsl:variable>
<xsl:variable name="excessLength" select="string-length($text) &gt; ($maxLength - string-length($Flow) - 5)"/>
<xsl:text>"message":"</xsl:text>
<!-- the two xsl:text elements below are where the "-5" comes from -->
<xsl:value-of select="substring($text,1,$maxLength - string-length($Flow) - 5)"/>
<xsl:if test="$excessLength">
<xsl:text>...</xsl:text>
</xsl:if>
<xsl:text>: </xsl:text>
<xsl:value-of select="$Flow"/>
<xsl:text>",</xsl:text>
<xsl:text>"description":"Message ID: </xsl:text>
<xsl:value-of select="../MsgId"/>
<xsl:text>\n</xsl:text>
<xsl:value-of select="$Flow"/>
<xsl:text>\n</xsl:text>
<xsl:if test="string-length($excessLength) &gt; 0">
<xsl:text>Full Error:\n</xsl:text>
<xsl:value-of select="$text"/>
</xsl:if>
<xsl:text>",</xsl:text>
</xsl:template>
<xsl:template match="Component">
<xsl:text>"entity":"</xsl:text>
<xsl:value-of select="translate(string(),'&quot;','')"/>
<xsl:text>",</xsl:text>
</xsl:template>
<xsl:template match="MsgId">
<xsl:if test="string-length() &gt; 0">
<xsl:text>"alias":"</xsl:text>
<xsl:value-of select="string()"/>
<xsl:text>",</xsl:text>
</xsl:if>
</xsl:template>
<xsl:template match="Timestamp">
<xsl:text>"details":{"timestamp":"</xsl:text>
<xsl:value-of select="string()"/>
<xsl:text>"},</xsl:text>
</xsl:template>
<xsl:template match="*" priority="-10">
<!-- mop up any elements not processed by a template, and do nothing with them. -->
</xsl:template>
</xsl:stylesheet>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment