Sending MongoDB Alerts to Microsoft Teams

· 3min · Dan F.
Table of Contents

Recently, I discovered that MongoDB Atlas and Mongo Ops Manager do not have the ability to send alerts directly to Microsoft Teams. Since I've been using rust more frequently, I decided to create a small web service that receives MongoDB alerts, and transforms them into compatible Microsoft Teams Cards, before posting them to a specified Teams incoming webhook.

You can browse the git repo here. I am definitely no rust expert, so I welcome pull requests or issues.

Overview

The executable listens on the following paths when running, for proper json posts:

/echo:
    This will return the json back to the user, good for debugging
/stdout:
    This will save the posted body to stdout within the container
/alert?channel=$CHANNEL:
    This will receive posted alerts and transform them before sending them to the Microsoft Teams webhook
/testalert:
    This will receive posted alerts, and return the transformed card back to the client
/health:
    Prints ok if server is healthy
/config:
    Shows the current configuration of endpoints

Usage

The two primary ways to utilize mongo_alerts_2teams are either through a docker container, or through a compiled binary.

First, you will need to create a config.yml file, which will contain your endpoints, and Teams urls. Create a file named config_example.yml following this format:

endpointone: 
  url: "https://outlook.office.com/webhook/"

endpointtwo: 
  url: "https://outlook.office.com/webhook/"

To start the docker container:

docker run -d -p 8000:8000  -v $(pwd)/config_example.yml:/app/config_example.yml findelabs/mongo_alerts_2teams:latest --config /app/config_example.yml

When, compiling from source. First make sure you have rust installed:

cargo install --git https://github.com/findelabs/mongo_alerts_2teams.git

Once you have either a running container or binary, you can add the link to either Atlas or Mongo Ops Manager as a webhook for alerts. This link should look something like http://myserver:8000/alert.

Examples

You can use this test alert to see if you container or binary is working properly. First go ahead and start up mongo_alerts_2teams on port 8000. Then post this body to http://localhost:8000/testalert:

curl -s http://localhost:8000/testalert -d "{
  \"alertConfigId\":\"5d2f6c93aa9b4c5735a37475\",
  \"clusterId\":\"5e1cc8d07a924c28f0847da8\",
  \"clusterName\":\"replicaset_name\",
  \"created\":\"2020-07-23T18:24:06Z\",
  \"eventTypeName\":\"PRIMARY_ELECTED\",
  \"groupId\":\"5e11d830a59b4c5bd7dd011a\",
  \"hostId\":\"8561bbbdc6d06fed0f983a344aa8366a\",
  \"hostnameAndPort\":\"localhost:27017\",
  \"id\":\"5f19a5c654964862806e9715\",
  \"links\":[
    {
      \"href\":\"http://localhost:8080/api/public/v1.0/groups/5e1ad8b08a9b4c5bd71d011a/alerts/5f1fd5c6549d486d80719715\",
  \"rel\":\"self\"
    }
  ],
  \"replicaSetName\":\"replicaset_name\",
  \"status\":\"CLOSED\",
  \"typeName\":\"REPLICA_SET\",
  \"updated\":\"2020-07-23T18:24:06Z\"
}" 

This should return a json doc like the one below, which is the MongoDB alert transformed into a card that Teams recognizes:

{
  "@context": "https://schema.org/extensions",
  "@type": "MessageCard",
  "sections": [
    {
      "activityImage": "https://company-30077.frontify.com/api/screen/download/eyJpZCI6MzQwMzI2NCwidmVyc2lvbiI6IjIwMTktMDgtMDIgMTk6Mjg6MDYifQ:frontify:DkJTntON9g0YByA8Q_M4vJX_XxO7je1rn7PJN6RJ_TI/?download&title_as_filename&track",
      "activitySubtitle": null,
      "activityTitle": "Replica set elected a new primary",
      "facts": [
        {
          "name": "Replicaset",
          "value": "\"replicaset_name\""
        },
        {
          "name": "Server",
          "value": "\"localhost:27017\""
        },
        {
          "name": "Type",
          "value": "\"REPLICA_SET\""
        }
      ]
    }
  ],
  "summary": "MongoDB Alert Card",
  "themeColor": "12924F",
  "title": "Alert Closed"
}

Has been tested on Ubuntu, MongoDB Atlas, and Ops Manager 4.4.1