监控磁盘使用率的shell脚本

简介:

本脚本来自有学习阿铭的博文学习:
公司监控最基本的一般都要监控磁盘的使用情况,否则将导致业务上的事故。
一般监控要求如下:每分钟都要扫描一下磁盘的状况。
当磁盘空间使用率或者inode使用率高于90%的情况,就需要报警。
并把统计使用率超过90%的分区的所有的子目录的,按照大小依次排列,把前3的目录名称发给到邮箱。
第一次未处理,30分钟后在一次。

#!/bin/bash
#用途:监控磁盘的使用情况。
#作者:Caron maktini
#日期:2018年10月18日
#版本:v0.1
#把脚本名字存在变量l-name
l_name=`echo $0 | awk -F '/' 'print $NF'`
#定义收件人的邮箱
mail_user=admin@admin.com

#定义检查磁盘的空间使用率函数
chk_sp()
{
    df -m | sed '1d' | awk -F '% | +' '$5>90 {print $7,$5}'>/tmp/chk_sp.log
    n=`wc -l /tmp/chk_sp.log | awk 'print $1'`
    if [ $n -gt 0 ]
    then 
      tag=1
      for d in `awk '{print $1}' /tmp/chk_sp.log`
      do 
           find $d -type d | sed '1d' | xargs du -sm | sort -nr | head -3
      done >/tmp/most_sp.txt
   fi
               
}

#定义检查inode使用率函数

chk_in()
{
  df -i | sed `1d` | awk -F '% | +' '$5>90 {print $7,$5}'>/tmp/chk_in.log
    n=`wc -l /tmp/chk_in.log | awk '{print $1}'`
    if [ $n -gt 0 ]
    then 
        tag=2
    fi
 }

#定义告警函数

m_mail(){
    log=$1
    t_s=`date +%s`
    t_s2=`data -d "1 hours ago" +%s`
    if [ ! -f /tmp/$log ]
    then
        #创建$log文件
        touch /tmp/$log
        #增加a权限,只允许追加内容,不允许更改或删除
        chattr +a /tmp/$log
        #第一次告警,可以直接写入1小时以前的时间戳
        echo $t_s2 >> /tmp/$log
     fi
    #无论#log文件是否刚刚创建,都需要查看最后一行的时间戳
    t_s2=`tail -l /tmp/$log | awk '{print $1}'`
    # 取出最后一行及上次告警的时间戳,立即写入当期的时间戳
    echo $t_s >>/tmp/$log
    #取两次时间戳差值
    v=$[ $t_s-$t_s2 ]
    #如果差值超过100,立即发送邮件。
    if [ $v -gt 1800 ]
    then
      #发邮件,其中$2为mail函数的第二个函数,这里为一个文件
      python  mail.py $mail_user "磁盘使用率超过90%"
      #定义技数器临时文件,并写入0
      echo "0" > /tmp/$log.count
     else
      #如果技数器临时文件不存在,需要创建并写入0
      if [ ! -f /tmp/$log.count }
      then
         echo "0" > /tmp/$log.count 

      fi

      nu=`cat /tmp/$log.count`
      #30分钟内每发生1次告警,计算器加1
      nu2=$[ $nu+1 ]
      echo $nu2>/tmp/$log.count
      #当告警次数超过30次,需要再次发油件
      if [ $nu2 -gt 30 ]
      then 
          python mail.py $mail_user "磁盘使用率90%持续30分钟了" "`cat $2`" 2>/dev/null
          #第二次告警后,将计算器再次从0开始
          echo "0" > /tmp/$log.count
      fi
 fi 
}
#把进程数大于0.则说明上次的脚本还未执行完
if [ $p_n -gt 0 ]
then
    exit

fi
 

chk_sp
chk_in

if [ $tag == 1 ]
then
    m_mail chk_sp /tmp/most_sp.txt
elif [ $tag == 2 ]
then
    m_mail chk_in /tmp/chk_in.log
fi 
相关文章
|
23天前
|
Java Shell Linux
使用 sh -x 进行 shell 脚本调试
使用 sh -x 进行 shell 脚本调试
34 9
使用 sh -x 进行 shell 脚本调试
|
4天前
|
监控 关系型数据库 MySQL
优秀的网络工程师,早就偷偷收藏了这9 个实用 Shell 脚本!
优秀的网络工程师,早就偷偷收藏了这9 个实用 Shell 脚本!
|
13天前
|
存储 Shell 应用服务中间件
[ansible]wget批量调用shell脚本
[ansible]wget批量调用shell脚本
|
16天前
|
运维 监控 Shell
掌握100个开箱即用的Shell脚本~(附PDF)
Shell脚本是实现Linux系统管理及自动化运维所必备的重要工具。许多其它岗位的小伙伴也经常使用Shell脚本来实现某项需求。 今天分享《100个shell脚本案例》,共有55页,支持文字搜索定位,代码清晰可复制。
|
23天前
|
Shell 测试技术 Linux
Shell 脚本循环遍历日志文件中的值进行求和并计算平均值,最大值和最小值
Shell 脚本循环遍历日志文件中的值进行求和并计算平均值,最大值和最小值
29 3
|
1月前
|
Shell Linux C语言
|
23天前
|
Shell Linux
Shell 脚本编程学习
Shell 脚本编程学习
22 0
|
1月前
|
JavaScript 前端开发 Shell
Shell 脚本编程保姆级教程(上)
Shell 脚本编程保姆级教程(上)
|
1月前
|
Shell 网络安全
shell脚本 配饰ssh
【7月更文挑战第15天】
23 4
|
1月前
|
网络协议 Shell Linux
Shell脚本配置Centos静态ip地址
这是一个用于在CentOS上设置静态IP的Shell脚本摘要: - 脚本交互式获取用户输入的IP地址、子网掩码、网关和DNS。 - 使用`sed`命令动态更新`/etc/sysconfig/network-scripts/ifcfg-ENS33`配置文件。 - 修改`BOOTPROTO`为`static`,并设置IP、NETMASK、GATEWAY和DNS1字段。 - 用`systemctl restart network`重启网络服务。 - 提示用户新配置的静态IP信息。