Get Server Notification on Telegram App

Bash Script to send report to telegram if high resource usage is found in the server.

Here is a bash script that can monitor server load and memory usage at regular intervals. It has been tested on Centos 7. The telegram API is used to send attachment instead of just text message. This is because sending text has character limit. Resource usage statistics can be sent in attachment instead of plain message.
#!/bin/bash
#This script monitors server load and memory usage for every 10 seconds and sends notification to telegram in case of high usage.
#Include telegram chat id and bot token ID here
chat_id="123456789"
token="112233445:AABbcd1FfggHHi22jkLmmn0PPQQRstu"
#Temporary files to store data
resource_usage_info=/tmp/resource_usage_info.txt
msg_caption=/tmp/telegram_msg_caption.txt
#Set threshold levels for memory usage and load here. If the usage exceeds these values, the notification will be sent.
mem_threshold=90 #Should be interger. This is in percentage
load_threshold=$(nproc) #Should be integer. Usually total number of cores.
#Telegram API to send notificaiton.
function telegram_send
{
curl -s -F chat_id=$chat_id -F document=@$resource_usage_info -F caption="$caption" https://api.telegram.org/bot$token/sendDocument > /dev/null 2&>1
}
#Monitoring Load on the server
while :
do
    min_load=$(cat /proc/loadavg | cut -d . -f1)
    if [ $min_load -ge $load_threshold ]
        then
        echo -e "High CPU usage detected on $(hostname)\n$(uptime)" > $msg_caption
        echo -e "CPU usage report from $(hostname)\nServer Time : $(date +"%d%b%Y %T")\n\n\$uptime\n$(uptime)\n\n%CPU %MEM USER\tCMD" >         $resource_usage_info
        ps -eo pcpu,pmem,user,cmd | sed '1d' | sort -nr >> $resource_usage_info 
        caption=$(<$msg_caption)
        telegram_send
        rm -f $resource_usage_info
        rm -f $msg_caption
        sleep 900 #stop executing script for 15 minutes
    fi
    sleep 10
#Monitoring Memory usage on the server
    mem=$(free -m)
    mem_usage=$(echo "$mem" | awk 'NR==2{printf "%i\n", ($3*100/$2)}')
    if [ $mem_usage -gt $mem_threshold ]
    then
        echo -e "High Memory usage detected on $(hostname)\n$(echo $mem_usage% memory usage)" > $msg_caption
        echo -e "Memory consumption Report from $(hostname)\nServer Time : $(date +"%d%b%Y %T")\n\n\$free -m output\n$mem\n\n%MEM %CPU USER\tCMD" > $resource_usage_info
        ps -eo pmem,pcpu,user,cmd | sed '1d' | sort -nr >> $resource_usage_info
        caption=$(<$msg_caption)
        telegram_send
        rm -f $resource_usage_info
        rm -f $msg_caption
        sleep 900 #stop executing script for 15 minutes
    fi 
    sleep 10
done

Create Server Bot Communicate with Telegram


If you use Telegram regularly, you’ve probably heard of bots. They’re like regular accounts, you can talk to them or have them in groups and they’ll do all sorts of useful things for you.
In this post, I present you the shell bot! Unlike other bots, this one is self-hosted: you use it by running your own instance of the bot in your server.
This handy bot runs commands on demand and sends the live output, allowing you to interact at any time. It can even run graphical apps!
The shell bot running some simple commands.
The shell bot running some simple commands.
It was made as an example for the botgram framework, but I’ve been using it for some time and it’s proven to be very useful. When you’re on mobile and just want to issue a few commands, talking to a bot is probably more convenient than opening an SSH app and connecting to the server.
Graphical application example, alsamixer in this case.
Graphical application example, alsamixer in this case.
It has creative uses as well: I once was troubleshooting a server problem with some friends, so we added the bot to the group and started playing around. Everyone could see what was going on, and suggest possible causes or things to try.
If you want to have your own shell bot, this post will guide you through the process of creating the Telegram bot itself, and installing the necessary software.

Create your bot

First of all, you need to register a Telegram bot. Don’t worry, it only takes some seconds. Click here to talk to the BotFather, then say /newbot and you’ll be asked some things (name, username, etc.). At the end, you’ll be given an authentication tokenthat looks like the following:
123456:ABC-DEF1234ghIkl-zyx57W2v1u123ew11

Preparations

Edit: The bot now lives in its own repository and the installation process has been simplified. If you followed a previous version of this post, remove it and follow these steps from the start.
The following should be done on the computer you want to run commands on. First of all, make sure you have Node.JS installed (you can verify by running npm -v). You’ll also need a working compiler and git:
sudo apt-get install build-essential git
Clone the project and install dependencies:
git clone https://github.com/botgram/shell-bot.git
cd shell-bot
npm install
Then run the bot for the first time, to create the configuration file:
node server
It will ask you a set of questions, including the auth token that you got from the BotFather, and will ask that you talk to your bot (so it can know who his owner is).

Run it!

Now that we have everything set up, we can run the shell bot:
node server
Try saying /run uname -a for example, or say /help to learn about the available commands.

Autostart

Once you’ve played around, you may want the bot to start automatically on boot, and respawn if it crashes. For that, let’s install forever:
sudo npm install -g forever
Then, from your /etc/rc.local or an init script, call:
forever start /path/to/shell-bot/server.js
Also, it’s a good idea to talk to the BotFather and say /setcommands to define a list of commands your bot accepts. You’ll find this list in commands.txt; just paste the contents when asked. You may also want to change the bot’s profile photo and description.
Did you find the bot useful? Got any suggestions? Feel free to tell me about it in the comments!

ChatOps Skype Integration Useful for Custom Bots

The notion behind Sameroom can feel complicated at first. To assist, we’ve produced a series of videos that’ll help you make sense of what our service is, and how to make the most of it. :-)



For Integration on Corporate Network : https://sameroom.io/enterprise-admins.pdf

Voice command image file and controlling electronics


With the introduction of the Raspberry Pi B+, a lot of people 
are finding image files aren't working unless updated first. Because 
of this, I went ahead and made a fresh image file for voice command 
on Raspbian that is A/B/B+ compatible.

You can download it at:

https://mega.co.nz/#!MM8W1JxR!4PlZ_1-dumasDUCYRI4LuiBwEJgtqhfoin0R8ls90NQ

I've also taken the liberty of putting wiringPi and pilight on it. This means 
it's easier than ever to control electronics with your voice
 (I'll post more on that later).
You can read more about it at the hackaday projects page here.

And here is a quick video demo: