File Attachment via Marid

Marid can subscribe to Opsgenie user actions and execute a script for each user action. A common usage of this feature is by subscribing "Create Action" and attaching a file to the newly created alert via the script. To get more information about action execution, please refer to Marid Integration support document.

This document demonstrates how to download a file from Amazon S3, and then attach it to the alert via Marid with a "Groovy" script. The sample script uses OpsgenieHttpClient object to receive the file from S3 and then uses "opsgenie" script variable to attach the received file to the alert.

Script first constructs OpsfenieHttpClient object by using the "createHttpClient" method. Then OpsfenieHttpClient sends a "GET" request by using the specified URL. If receives file from s3 successfully; attaches the file to the alert.

import com.ifountain.opsgenie.client.http.OpsGenieHttpClient
    import com.ifountain.opsgenie.client.util.ClientConfiguration

    //Creates OpsGenieHttpClient.
    OpsGenieHttpClient HTTP_CLIENT = createHttpClient()

    // S3 url of the file that will be attached to the alert.
    def url = ""

    //Sending request to retrieve file
    def response = HTTP_CLIENT.get(url, [:])
    def code = response.statusCode

    if(code == 200){
        //attaching the file to the alert when retrieving the file successfully.
        logger.warn("File received")

        //filename of the attachment file. Change file extension according to your usage.
        // If your file is a txt, use trial.txt as fileName
        def fileName = "trial.png"

        resp = opsgenie.attach([alertId: alert.alertId, stream: new ByteArrayInputStream(response.getContent()), fileName: fileName])
            logger.warn("Successfully attached details ${fileName}");
            logger.warn("Could not attach details ${fileName}");
        logger.warn("Could not get file from url ${url}. ResponseCode:${code} Reason:" + response.getContentAsString())

    def createHttpClient() {
        //if you want to use different timeout value, add "http.timeout" property to Marid's conf file.
        def timeout = conf["http.timeout"]

        if(timeout == null){
            timeout = 30000;
            timeout = timeout.toInteger();
        ClientConfiguration clientConfiguration = new ClientConfiguration().setSocketTimeout(timeout)
                //To use basic authentication while getting the file that will be attached,
                // comment in the line below and add "user" and "password" properties to Marid's conf file
    //            .setCredentials(new UsernamePasswordCredentials(conf["user"], conf["password"]))
        return new OpsGenieHttpClient(clientConfiguration)

Did this page help you?