需求
最近需要給自己的服務(wù)器添加監(jiān)控器聪铺,目的是監(jiān)控服務(wù)器的內(nèi)存化焕、CPU、磁盤占用率铃剔,資源占用率過高的話能給自己發(fā)個提醒撒桨,當(dāng)前主流的平臺一般會提供郵件、短息键兜、甚至?xí)峁┪⑿盘嵝逊锢啵贿^這類提醒包含的噪音太多了(夾雜著各種無關(guān)的社交信息),我只是單純的需要接收到服務(wù)器的預(yù)警普气。由于服務(wù)器環(huán)境并不復(fù)雜谜疤,所以不考慮主流的與監(jiān)控平臺(畢竟搭建起來還是挺復(fù)雜的)。
選擇產(chǎn)品
有很多產(chǎn)品支持 incoming(就是通過調(diào)用應(yīng)用提供的 API 把我們自定義的消息轉(zhuǎn)發(fā)送該應(yīng)用)现诀,我打算使用 JBox 夷磕,因為它提供了 Android、和 iOS 客戶端支持而且是開源的所以后期有什么需求都可以自己加上去(還有一點最主要的是使用起來非常簡單仔沿,API 文檔只有一個接口坐桩,基本沒有學(xué)習(xí)成本)。
著手操作
按照 JBox 教程 來于未,首先新建一個自定義集成撕攒,獲得一個 Webhook url
http://jbox.jiguang.cn/v1/message/dxwYTMfrC8GRhU5/vwlrqCegmp //注意:這里填寫自己集成的 Webhook url陡鹃,每個集成的 Webhook 都不一樣。
首先編寫我們的監(jiān)控腳本抖坪,這里我寫了兩個腳本
#內(nèi)存監(jiān)控腳本 monitor_memory.sh
webhook="http://jbox.jiguang.cn:80/v1/message/dxwYTMfrC8GRhU5/vwlrqCegmp" #注意:這里填寫自己集成的 Webhook url
#告警閾值30G萍鲸,少于則告警,頻率 30分鐘 檢查一次
normal=30
#取得總內(nèi)存
#取得內(nèi)存分頁數(shù)
freemk=`vmstat 5 2 | tail -n 1 | awk '{print $5}'`;
#每一頁是4K 擦俐,所以乘以4
freemm=`expr $freemk \* 4`;
#轉(zhuǎn)換為 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 "當(dāng)前內(nèi)存"$freemem"G,少于"$normal"G" #打印告警信息 這里可以插入短信庫脊阴,發(fā)送至手機(jī)
title="內(nèi)存告警!蚯瞧!"
message="當(dāng)前內(nèi)存"$freemem"G,少于"$normal"G"
memoryAlertJson='{"title":"'${title}'"'',"message":"'${message}'"}'
echo $memoryAlertJson
# 這里發(fā)送預(yù)警嘿期,該條消息會轉(zhuǎn)發(fā)到 JBOx app
curl -H "Content-Type: application/json" -X POST -d $memoryAlertJson $webhook
fi
# 磁盤監(jiān)控腳本 monitor_disk.sh
webhook="http://jbox.jiguang.cn:80/v1/message/dxwYTMfrC8GRhU5/vwlrqCegmp"
normal=10 #當(dāng)超過 10% 這個值時產(chǎn)生告警,這里因為測試 所以設(shè)得很低,這個可以根據(jù)自己的需求來增加
DiskPercent=`df |grep -w / |awk '{print $5}'|awk -F '%' '{print $1}'`;
echo $DiskPercent;
if [ $normal -lt $DiskPercent ]
then
echo "硬盤 使用率告警"
title="硬盤 使用率告警B窈稀备徐!"
message="當(dāng)前使用率"$DiskPercent"%,大于"$normal"%"
DiskAlertJson='{"title":"'${title}'"'',"message":"'${message}'"}'
echo $DiskAlertJson
# 這里發(fā)送預(yù)警,該條消息會轉(zhuǎn)發(fā)到 JBOx app
curl -H "Content-Type: application/json" -X POST -d $DiskAlertJson $webhook
fi
我把這兩個腳本加在 crontab 執(zhí)行計劃里面
$ 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
# 一分鐘執(zhí)行一次腳本
* * * * * /bin/bash /home/ubuntu/monitor_memory.sh >>/home/ubuntu/moniter_memory.log
* * * * * /bin/bash /home/ubuntu/monitor_disk.sh >>/home/ubuntu/monitor_disk.log