Chatops: Troubleshooting a server with chatbot
ChatOps is a collaboration model that connects people, tools, process, and automation into a transparent workflow using chat conversation.
Here are few use cases:
- If you are troubleshooting any problem, slack chatbot can be useful if you want everyone to see the specific logs/system metrics around the problem.
- Chatbot can be useful for Alert validation, sometimes alerts trigger due to network fluctuation or routine tasks. Using ChatOps, you can verify whether an alert is, in fact, a threat requiring investigation or is something harmless.
- You are an application developer and need to search for logs in staging/production for your app and are currently within the slack interface, without logging into the servers.
- Troubleshooting a Server from your phone, when you are not on your keyboard.
I have created a chatbot using slack and python library SlackClient to
implement the above mentioned use cases. It uses fabric module to execute the
command on remote hosts in AWS EC2.
How to Build a ChatOps Bot with Slack and Python
Slack is a messaging app that has exploded in popularity among all sorts of teams. The Slack platform allows developers to implement and integrate their own chatbots.
Slack has a variety of APIs that suit various purposes, but the one that I have used for creating bot is the Slack RTM (Real Time Messaging) API.
To write the bot using the Slack RTM API, I have used the official Slack client for python. RTM chatbots rely on WebSockets and long term connections. If you don't prefer to use long lived connection you can use other Slack API.
The following figure provides an overview of these different parts and how they interact together.
Requirements for Slackbot
- Python 3
- pip to handle Python application dependencies.
- Free Slack account with a team on which you have API access
- Official Python slackclient code library built by the Slack team and fabric3 library to execute the command on remote machine.
- Slack API testing token
You can find the code on Github.
Getting started with the slackbot
You can send messages to bot in following order:
@<slackbot> command <action> host <hostname>
- To check Free Memory across AWS EC2 Instances.
- To check Load Average
- To display info about CPU architecture.
- Search for pattern in the log file
- Replying to the thread in slack.
- Get information about the active process.
Sooper job sir
Very nice one!