Blog

Blog

/ by Marek / ,  + .

Getting Oxidized to Talk to Mattermost

It is no secret that we use Mattermost at Faelix — after all, it is a product we already support to be able to offer it to our customers. And like many network operators we use Oxidized to track and log changes to our routers and switches, even when those changes are made by automation tools.

As part of our move to using more ChatOps within the business I wanted to get visibility of network changes within our network operations channel in Mattermost. A quick and dirty script achieved this:

#!/usr/bin/python3

import os
import subprocess
import mattermostdriver

MM_TOKEN = "XXXXXXXXXXXXXXXXXXXXXXXXXX"
MM_CHANNEL_ID = "XXXXXXXXXXXXXXXXXXXXXXXXXX"

mm = mattermostdriver.Driver( { 'url': 'mattermost.example.com',
                                'port': 443,
                                'scheme': 'https',
                                'token': MM_TOKEN,
                                } )

git = subprocess.Popen( [ "git",
                          "--bare",
                          '--git-dir=%s' % os.environ[ "OX_REPO_NAME" ],
                          "show",
                          "--no-color",
                          os.environ[ "OX_REPO_COMMITREF" ],
                          ], stdout = subprocess.PIPE )
diff = git.stdout.read().decode( 'utf-8', 'ignore' )

msg = '''\
Changes to `%s`:

``` diff
%s
```
''' % ( os.environ[ "OX_NODE_NAME" ],
        diff,
        )
mm.login()
mm.posts.create_post( options = { 'channel_id': MM_CHANNEL_ID,
                                  'message': msg,
                                  } )

Configuring Oxidized was relatively straightforward too:

hooks:
  git2mm:
    type: exec
    events: [post_store]
    cmd: /home/oxidized/git2mm.py

That’s all there was to it!