
最近需要给自己的服务器添加监控器,目的是监控服务器的内存、 CPU 、磁盘占用率,资源占用率过高的话能给自己发个提醒,当前主流的平台一般会提供邮件、短息、甚至会提供微信提醒,不过这类提醒包含的噪音太多了(夹杂着各种无关的社交信息),我只是单纯的需要接收到服务器的预警。由于服务器环境并不复杂,所以不考虑主流的与监控平台(毕竟搭建起来还是挺复杂的)。
有很多产品支持 incoming (就是通过调用应用提供的 API 把我们自定义的消息转发送该应用),我打算使用 JBox ,因为它提供了 Android 、和 iOS 客户端支持而且是开源的所以后期有什么需求都可以自己加上去(还有一点最主要的是使用起来非常简单, API 文档只有一个接口,基本没有学习成本)。
按照 JBox 教程 来,首先新建一个自定义集成,获得一个 Webhook url
http://jbox.jiguang.cn/v1/message/dxwYTMfrC8GRhU5/vwlrqCegmp //注意:这里填写自己集成的 Webhook url ,每个集成的 Webhook 都不一样。 首先编写我们的监控脚本,这里我写了两个脚本
#内存监控脚本 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 我把这两个脚本加在 crontab 执行计划里面 $ 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 

1 goophy 2016 年 12 月 16 日 看到“阀值”忍不住回复一下,应该是阈值-_-! |
3 HuminiOS OP 貌似发错地方了 -_-! |
4 nopunk 2016 年 12 月 16 日 发错节点了吧。 另外请问下楼主 如果想几秒回一次 crontab 时间参数填什么? |
6 KenChoi 2016 年 12 月 19 日 666 |
7 jpush 2017 年 3 月 27 日 优秀! |
8 Aceyclee 2017 年 4 月 26 日 棒! |