Published on 2020-11-04, by Javed Shaikh
Monitoring CPU and memory usage are one of the top todo checklist for a backend engineer. Sometimes you wont even notice when your server is down due to high CPU usage unless you login and manually check the system. In this tutorial, we are going to setup alarm for CPU Usage by most easy way possible(at least I think so) using a simple shell script and IFTTT web hooks.
š What is IFTTT and how to create an applet?
š How to check CPU usage of a system?
š How to make web request to IFTTT from your server?
š How to write a script to check CPU usage and schedule it on crontab?
IFTTT stands for If This, Then That. It is simple web based service that lets you connect your device, server or apps. It is used to create conditional statements or actions that triggered by the changes in your apps, devices or servers. So it helps automate your tasks for your web apps and devices. Lets get started.
First we need to create a free account and login to IFTTT
To use IFTTT, we need to create an applet (Free account can create up to 3 applets)
Go to the top right and click Create link. You should see similar to below page. Now click Add button on "If this add" link
On next page, You need to select a service. In this case we are going to use IFTTT web hook. So search for web hook and select it to set it up. Click "Receive a web request" and set the event name.
As shown below we are setting the event name as "high_cpu_usage" and then we will click "Create Trigger" to create the trigger.
So far we have created the trigger event, next we are going to create action service like what action needs to be taken if the above event is triggered. I am going to set EMAIL service so that an email will be generated when CPU usage will be high.
So click "Then That Add" and choose EMAIL service and set the subject and body of the email that will be sent to your account. Then Review and finish the setup.
At this moment we are done with setting up our IFTTT applet. Now we need to know the complete web URL of our applet so that we will make web request to notify cpu usage is high. To do that go to your account and lick my service, then webhooks and then 'documentation' page on the top right corner. You should see something like below.
We need to make a small change to add event name to the above URL. So our correct URL to make web request should look like this and this is how are going to make a web request to IFTTT.
1curl -X POST https://maker.ifttt.com/trigger/high_cpu_usage/with/key/boJOWPwAx6x2qjAWr3CUym
2
I have deleted webhook created for this tutorial. So please don't use the above key, Its not going to work š.
There are many ways to check CPU usage of a server. I have already posted a complete article for CPU Utilization and we are not going to cover in detail here. Please go and check my article if you are interested https://shaikhu.com/how-to-monitor-cpu-utilization-in-linux
For this tutorial, we are going to use VMSTAT command. This command will tell current CPU and memory usage of the system. Below command will tell CPU usage every second for 5 times. So with this command we can check CPU usage for last 5 seconds. We are going to use this entry in our next shell script to check the cpu usage.
1VMSTAT 1 5
2
Since we got the idea how to check CPU Usage lets start writing a simple script to monitor CPU usage. Here is how our script looks like.
1#!/usr/bin/bash
2#Set Threshold for CPU Usage
3THRESHOLD=90.0
4#CPU usage for last 5 seconds
5cpu_usage=$(vmstat 1 5 | awk '{if(NR > 2) {sum+=$15}} END {print 100-sum/5}')
6if [ $(echo "$cpu_usage > $THRESHOLD" | bc) -eq 1 ]
7then
8#SET ALARM using IFTTT webhook
9curl -X POST https://maker.ifttt.com/trigger/high_cpu_usage/with/key/dhFYYNRL4to_1k5O1NOUd3
10fi
11
Lets understand the above script.
Run this CPU usage script every 5 minutes.
With the help of crontab we are going to schedule this shell script ,so that script will be running every 5 minutes to monitor our CPU health.
1*/5 * * * * sh cpu_alert.sh
2
With this setting in crontab, our script will run every 5 minutes and check CPU usage. If you want to know more about how to set crontab and schedule jobs, then check my post How to use crontab .
If the cpu usage is higher than Threshold, it will make a web request to IFTTT and IFTTT will send us an email alert š. Now how easy that was!!!
This is how IFTTT email looks like when CPU usage reaches the threshold limit.
IFTTT is a very easy and also powerful web based service that can be used to connect your apps and devices. As shown in this tutorial, with just few lines of script, we have build a powerful tool to monitor our system's CPU usage. Same logic can be used and extended for memory usage as well. The amazing thing is that we are not writing codes to send an email. We are just making a web request conditionally to IFTTT and IFTTT is sending us the email alert.
Hope this tutorial will help you build some amazing apps using IFTTT. Let me know your thoughts in the comment section and dont forget to check my other How to tutorials listed below. Thanks!
We are going to build a CLI app using Python to change desktop wallpaper every given number of minutes. Every wallpaper downloaded from internet will be unique and our app will change the wallpaper based on the time we set.
2020-10-27
There are many ways to monitor and set alerts for your web server using third party apps but they don't come with free, be it Pingdom or Amazon's CloudWatch. In this post I am going to show how we can write and setup our own alert tool with just few lines of code
2020-10-29
In this post we are going to see how we can use Python to find the size of any file and folder. We will check how many files we have inside a folder and how many of them are empty files and how to delete those empty files.
2020-10-26
A process in computer term is a program being executed currently by a computer. Each process is unique and can be identified by its PID or process ID.
2020-10-24