监控磁盘使用率的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 
相关文章
|
3天前
|
Java 关系型数据库 MySQL
Elasticsearch【问题记录 01】启动服务&停止服务的2类方法【及 java.nio.file.AccessDeniedException: xx/pid 问题解决】(含shell脚本文件)
【4月更文挑战第12天】Elasticsearch【问题记录 01】启动服务&停止服务的2类方法【及 java.nio.file.AccessDeniedException: xx/pid 问题解决】(含shell脚本文件)
28 3
|
6天前
|
运维 监控 Shell
利用Shell脚本编写局域网监控软件:实时监测主机连接情况
本文介绍了如何使用Shell脚本创建一个局域网监控工具,以实时检查主机连接状态。脚本包括扫描IP地址范围检测主机可达性及使用`netstat`监控ESTABLISHED连接。此外,还展示了如何每60秒将连接数数据自动提交到指定网站API,以便实时跟踪网络活动。这个自动化监控系统有助于提升网络安全性和故障排查效率。
26 0
|
7天前
|
Shell
Shell脚本之流程控制语句
Shell脚本之流程控制语句
|
8天前
|
JSON 运维 监控
训练shell常用脚本练习(三)
【4月更文挑战第14天】shell代码训练(三)
27 1
|
12天前
|
存储 弹性计算 Shell
ecs服务器shell常用脚本练习(十)
【4月更文挑战第11天】shell代码训练(十)
143 0
|
12天前
|
弹性计算 Shell Go
ecs服务器shell常用脚本练习(九)
【4月更文挑战第10天】shell代码训练(八)
136 0
|
23天前
|
弹性计算 Shell Perl
ecs服务器shell常用脚本练习(二)
【4月更文挑战第1天】shell代码训练(二)
106 1
|
26天前
|
Java Shell
SpringBoot启动脚本Shell
SpringBoot启动脚本Shell
17 0
|
16天前
|
弹性计算 Shell Linux
ecs服务器shell常用脚本练习(六)
【4月更文挑战第4天】shell代码训练(六)
109 0
|
21天前
|
弹性计算 Shell 应用服务中间件
ecs服务器shell常用脚本练习(四)
【4月更文挑战第4天】shell代码训练(四)
96 0