slack slackbot

Creating a slackbot to manage VPS’s in openstack – Part 1

This series of posts will tell you how I made a slackbot that communicate (safely) with multiple VPS’es and run various commands on them. Also returning output back to slack!

Intro

So at my work we use slack, we also have a shitload of websites hosted in the cloud. To be more precise they run via a tower in openstack. We also manage these websites ourself, and I’m actually the dude that gets called outside office hours to fix any urging problems regarding those websites.

To be honest, that rarely happens but when it does; I have to grab my laptop, login and ssh to the server. About 90% of these errors are because of some wacko spambot, a trigger happy DDOS’er or things related to that. The solution mostly consists of restarting apache2, php5-fpm or mysql.

Threads are getting jammed or are just stuck from getting 1000 hits. Sure, we can automate it to restart that when it happens but you are more safe to just do it by yourself.

Slack

So back to slack. We all use slack, I have it on my cellphone and we have channels with reports from pingdom. Pingdom is a service that sends us a message when a site is down (500 error for instance). Now my goal was to create a slackbot that could perform a command on that particular VPS running the website that went down.

PHP based slack bot

So I went to google and I found this lib: https://github.com/jclg/php-slack-bot To be honest it is a fairly good base to make your bot. It works perfectly fine for a basic part, but you are semi-limited because of his setup. You kind of want to use his part as base and re-use that. Yet you are semi-limited because of the abstract classes. A bit tl;dr: He uses an Bot.php as “start”, but if you want to build a bot on steroids you want to use your own functions and just use some simple parts to send/retrieve data from/to Slack.

Installing SSH2 for PHP 7

So, there is a nice PHP ext: ssh2 yet this is not included in PHP 7 by default

// Installs dependency

apt-get install libssh2–1-dev libssh2–1

// Get the PHP 7 php-ssh2

wget https://github.com/Sean-Der/pecl-networking-ssh2/archive/php7.zip
unzip pecl-networking-ssh2-php7.zip
cd pecl-networking-ssh2-php7

// Making the build

phpize
./configure
sudo make
sudo make install

// Add the ext to your php.ini

extension=ssh2.so

Now our base is set to work with PHP ssh2. In my next post ill describe how to combine a Slackbot and this PHP ext together.

This page
Next part!
part 3

Leave a Reply

Your email address will not be published. Required fields are marked *