Implement your own server monitor in 10 minutes

need

Recently, I need to add a monitor to my server. The purpose is to monitor the server's memory, CPU, and disk occupancy. If the resource occupancy is too high, I can send a reminder to myself. The current mainstream platforms generally provide emails, short messages, and even Provide WeChat reminders, but these reminders contain too much noise (mixed with all kinds of irrelevant social information), I just need to receive an early warning from the server. Since the server environment is not complicated, mainstream and monitoring platforms are not considered (after all, it is quite complicated to build).

Choose a product

There are many products that support incoming (that is, by calling the API provided by the application to forward our custom messages to the application), I plan to use JBox , because it provides Android, and iOS client support and is open source, so what are the requirements later You can add it yourself (the most important thing is that it is very simple to use, the API document has only one interface, and there is basically no learning cost).

Get started

Follow the JBox tutorial , first create a new custom integration and get a Webhook url

http://jbox.jiguang.cn/v1/message/dxwYTMfrC8GRhU5/vwlrqCegmp  //注意:这里填写自己集成的 Webhook url,每个集成的 Webhook 都不一样。

First write our monitoring script, here I wrote two scripts

#内存监控脚本  monitor_memory.sh
webhook="http://jbox.jiguang.cn:80/v1/message/dxwYTMfrC8GRhU5/vwlrqCegmp" #注意:这里填写自己集成的 Webhook url
#告警阈值30G,少于则告警,频率 30分钟 检查一次
 normal=30

#取得总内存  

#取得内存分页数  

freemk=`vmstat 5 2 | tail -n 1 | awk '{print $5}'`;  
 #每一页是4K ,所以乘以4                              

freemm=`expr $freemk \* 4`;    
 #转换为 G                                                          

freemem=`echo $freemm/1024/1024|bc`;                                          

echo `date +%Y%m%d%H%M`"  Memory:" "M" all $freemem"G" avail;

if [ $freemem -lt $normal ]

then

    echo "当前内存"$freemem"G,少于"$normal"G"        #打印告警信息    这里可以插入短信库,发送至手机
    title="内存告警!!"
    message="当前内存"$freemem"G,少于"$normal"G"
    memoryAlertJson='{"title":"'${title}'"'',"message":"'${message}'"}'
    echo $memoryAlertJson

# 这里发送预警,该条消息会转发到 JBOx app
    curl -H "Content-Type: application/json" -X POST -d $memoryAlertJson $webhook
fi
# 磁盘监控脚本 monitor_disk.sh
webhook="http://jbox.jiguang.cn:80/v1/message/dxwYTMfrC8GRhU5/vwlrqCegmp"
normal=10 #当超过 10% 这个值时产生告警,这里因为测试 所以设得很低,这个可以根据自己的需求来增加

DiskPercent=`df |grep -w / |awk '{print $5}'|awk -F '%' '{print $1}'`;
echo $DiskPercent;
if [ $normal -lt $DiskPercent ] 
    then
    echo "硬盘 使用率告警"
    title="硬盘 使用率告警!!"
    message="当前使用率"$DiskPercent"%,大于"$normal"%"
    DiskAlertJson='{"title":"'${title}'"'',"message":"'${message}'"}'
    echo $DiskAlertJson
# 这里发送预警,该条消息会转发到 JBOx app
    curl -H "Content-Type: application/json" -X POST -d $DiskAlertJson $webhook
fi

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
# 一分钟执行一次脚本
* * * * * /bin/bash /home/ubuntu/monitor_memory.sh >>/home/ubuntu/moniter_memory.log
* * * * * /bin/bash /home/ubuntu/monitor_disk.sh >>/home/ubuntu/monitor_disk.log


Author: HuminiOS - Aurora

Original: 10 minutes to implement a own server monitor

Know the column: Aurora Daily

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325529180&siteId=291194637