demand

Recently need to add a server of its own monitor, the purpose is to monitor the server memory, CPU, disk usage, the resource occupancy rate is too high, you can give your send a reminder, the current mainstream platform will generally provide mail, short of breath, even WeChat reminder, but this kind of reminder contains too much noise (no mixed with a variety of social information), I simply need to receive an alert from the server. Since the server environment is not complex, mainstream and monitoring platforms are not considered (after all, it is quite complicated to set up).

Select a product

There are a lot of products that support incoming (which is to forward our custom messages to the app by calling the API provided by the app), and I’m going to use JBox, Because it provides Android, iOS client support and is open source, so you can add any requirements later (and the most important thing is that it is very simple to use, the API documentation only has one interface, basically no learning cost).

To begin operations

Follow the JBox tutorial and start by creating a new custom integration to get a Webhook URL

http:/ / jbox. Jiguang. Cn/v1 / message/dxwYTMfrC8GRhU5 vwlrqCegmp / / note: write your own integration Webhook urls, each integration Webhook is different.Copy the code

Let’s start by writing our monitoring scripts. Here I’ve written two scripts

# memory monitoring script monitor_memory. Sh # webhook = "http://jbox.jiguang.cn:80/v1/message/dxwYTMfrC8GRhU5/vwlrqCegmp" note: Here fill in the Webhook URL # alarm threshold of its own integration 30G, less than the alarm, Check normal frequency 30 minutes = 30 # # to obtain the total memory number get paging freemk = ` vmstat 5 2 | tail - n 1 | awk '{print $5}' `; # each page is 4K, so multiply by 4 freemm= 'expr $freemk \* 4'; Converting # G freemem = ` echo $| BC ` freemm / 1024/1024; echo `date +%Y%m%d%H%M`" Memory:" "M" all $freemem"G" avail; If [$freemem-lt $normal] then echo "current memory "$freemem"G, less than "$normal"G Message = "the memory" $freemem "G, less than $" normal" G "memoryAlertJson = '{" title" : "' ${title} ""," message ":" '${message} "}' echo $memoryAlertJson $memoryAlertJson $memoryAlertJson $memoryAlertJson application/json" -X POST -d $memoryAlertJson $webhook fiCopy the code
# disk monitoring script monitor_disk. Sh webhook = "http://jbox.jiguang.cn:80/v1/message/dxwYTMfrC8GRhU5/vwlrqCegmp" normal = 10 # when more than 10% This value is generated when the alarm, because the test here So set too low, it can according to their own requirements to increase DiskPercent = ` df | grep - w / | awk '{print $5}' | awk -f '%' '{print $1}' `; echo $DiskPercent; If [$normal -lt $DiskPercent] then echo "Disk usage alarm" title=" Disk usage alarm!! Message = "current use" $DiskPercent "%, is greater than $" normal" % "DiskAlertJson = '{" title" : "' ${title} ""," message ":" '${message} "}' echo JBOx app curl -h "content-type: application/json" -x POST -d $DiskAlertJson $webhook fiCopy the code

I added these two scripts to the crontab execution plan $crontab -e

# Edit this file to introduce tasks to be run by cron. # # Each task to run has to be defined through a single line # indicating with different fields when the task will be run # and what command to run for the task # # To define the time  you can provide concrete values for # minute (m), hour (h), day of month (dom), month (mon), # and day of week (dow) or use '*' in these fields (for 'any').# # Notice that tasks will be started based on the cron's  system # daemon's notion of time and timezones. # # Output of the crontab jobs (including errors) is sent through # email to the user the crontab file belongs to (unless redirected). # # For example, you can run a backup of all your user accounts # at 5 a.m every week with: # 0 5 * * 1 tar -zcf /var/backups/home.tgz /home/ # # For more information see the manual pages of crontab(5) and Cron (8) # # m h dom mon dow Command # Execute script once a minute * * * * * /bin/bash /home/ubuntu/monitor_memory.sh >>/home/ubuntu/moniter_memory.log * * * * * /bin/bash /home/ubuntu/monitor_disk.sh >>/home/ubuntu/monitor_disk.logCopy the code