V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
V2EX 提问指南
HuminiOS
V2EX  ›  问与答

10 分钟实现一个自己的服务器监控器

  •  
  •   HuminiOS · 2016-12-16 13:10:49 +08:00 · 2510 次点击
    这是一个创建于 2687 天前的主题,其中的信息可能已经有所发展或是发生改变。

    需求

    最近需要给自己的服务器添加监控器,目的是监控服务器的内存、 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
    

    8 条回复    2017-04-26 10:29:24 +08:00
    goophy
        1
    goophy  
       2016-12-16 13:17:48 +08:00
    看到“阀值”忍不住回复一下,应该是阈值-_-!
    HuminiOS
        2
    HuminiOS  
    OP
       2016-12-16 13:27:15 +08:00   ❤️ 1
    @goophy 眼神好犀利 -_-!
    HuminiOS
        3
    HuminiOS  
    OP
       2016-12-16 14:07:08 +08:00
    貌似发错地方了 -_-!
    nopunk
        4
    nopunk  
       2016-12-16 15:46:25 +08:00
    发错节点了吧。
    另外请问下楼主 如果想几秒回一次 crontab 时间参数填什么?
    HuminiOS
        5
    HuminiOS  
    OP
       2016-12-16 17:43:30 +08:00
    @nopunk 执行计划最快只能一分钟执行一次,,如果要几秒执行一次只能写个脚本循环跑了
    KenChoi
        6
    KenChoi  
       2016-12-19 14:40:52 +08:00
    666
    jpush
        7
    jpush  
       2017-03-27 17:39:51 +08:00
    优秀!
    Aceyclee
        8
    Aceyclee  
       2017-04-26 10:29:24 +08:00
    棒!
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   5302 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 27ms · UTC 09:16 · PVG 17:16 · LAX 02:16 · JFK 05:16
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.