zoukankan      html  css  js  c++  java
  • 10 分钟实现一个自己的server监控器

    需求

    近期须要给自己的server加入监控器。目的是监控server的内存、CPU、磁盘占用率,资源占用率过高的话能给自己发个提醒。当前主流的平台通常会提供邮件、短息、甚至会提供微信提醒,只是这类提醒包括的噪音太多了(夹杂着各种无关的社交信息),我仅仅是单纯的须要接收到server的预警。由于server环境并不复杂,所以不考虑主流的与监控平台(毕竟搭建起来还是挺复杂的)。

    选择产品

    有非常多产品支持 incoming(就是通过调用应用提供的 API 把我们自己定义的消息转发送该应用)。我打算使用 JBox ,由于它提供了 Android、和 iOS client支持并且是开源的所以后期有什么需求都能够自己加上去(另一点最基本的是使用起来非常easy。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


    作者:HuminiOS - 极光

    原文:10 分钟实现一个自己的server监控器

    知乎专栏:极光日报

  • 相关阅读:
    泡泡一分钟:Stabilize an Unsupervised Feature Learning for LiDAR-based Place Recognition
    解决Eclipse闪退问题的方法总结
    微信小程序 --- e.currentTarget.dataset.id 获取不到值
    微信小程序的json遍历
    获取个人微信信息
    小程序开发
    Oracle IF-ELSE 条件判断结构
    Oracle存储过程总结
    etl的表输入时精度问题
    Oracle 创建普通用户,并赋予权限
  • 原文地址:https://www.cnblogs.com/yutingliuyl/p/7327660.html
Copyright © 2011-2022 走看看