Network ChatBot

When you need the basic information from your network you usually turn to your monitoring system. But wouldn’t be nice to have a different type of interaction? Wouldn’t be nice to ask a question and something/somebody to reply right away? Wouldn’t be nice for other teams in your company to have an easy way to obtain some basic information related to your infrastructure?
This is where a chatbot could help. From this idea, I’ve started to build a chatbot for Slack using Python.
First I’ve searched for an example of a basic chatbot, and found this:
https://www.digitalocean.com/community/tutorials/how-to-build-a-slackbot-in-python-on-ubuntu-20-04
There are other examples but I found this one to be pretty well documented. It explains all the steps you need to do in the Slack app and also on your server where you host the python app and scripts.
And from here I’ve started to write a python script for every function I wanted to give to this chatbot.
I’ve started with a help command so the user will know what this chatbot can do:

Above you see all the commands this chatbot can reply to. It is using API’s to gather the information from Prometheus and LogDNA(the Raspberry Pi ESXi host temperature). It also uses API’s to interact with the Slack app. Everything on the local server is written in Python.

The first command is inventory. It should give you all the devices that are in a YAML file. In my case there are only 2:

Now that we know our devices we can try to get the status of the interfaces from a device:

We can also ask about all the interfaces that are down (in my case it is the same device):

Another useful command in my case is the wifi clients/devices that are connected to my network and their signal strength(using this you can know how far this devices are from the router):

Raspberry Pi temperature is pretty important to me and I really need to have this info whenever I want or wherever I am:

And last , I needed a fast way to get my public IP address as I don’t have a static one:

It is easy to add other commands. All you need to do is to create a python script that does what you need it to do and then use it in the app.py script. This is the main python script where all the commands are “translated” into functions and a reply is provided to the user.

Let’s take for example the “pitemp” command. It has a function to match the command name :

    if "pitemp" in text.lower():
        channel_id = event.get("channel")
        return temp_app(channel_id)

This calls another function that get’s the info from the python script:

def temp_app(channel):
    temp_bot = temperature(channel)
    message = temp_bot.get_message_payload()
    slack_web_client.chat_postMessage(**message)

And the python script using API’s get the info from LogDNA app:

import json
import requests
from datetime import datetime
from datetime import timezone

class temperature:

  def __init__(self, channel):
    self.channel = channel

  def _status(self):
    dt = datetime.now()
    timestamp = dt.replace(tzinfo=timezone.utc).timestamp()
    times = int(timestamp-600)
    api_url = 'https://api.logdna.com/v1/export?from=%i&to=%i&query=PiTemp' % (times,int(timestamp))
    headers = {"servicekey": "549405405940594594540000"}
    response = requests.get(api_url,headers=headers)
    x=int(response.text[123:126])
    text = f"Pi Temperature: {x} C"
    return {"type": "section", "text": {"type": "mrkdwn", "text": text}},

  def get_message_payload(self):
        return {
            "channel": self.channel,
            "blocks": [
                *self._status(),
            ],
        }

All the other commands/functions have a similar process.

You can find them all here:
https://github.com/czirakim/ChatBot

[07.April.2021] I’ve added another command “whois <ip>”.

About the author

Mihai is a Network Aficionado with more than 10 years experience