shell 脚本监控磁盘容量,并配置发送邮件

shell 脚本监控磁盘容量,并配置发送邮件

要求

  检查日志挂载的磁盘,每隔一个小时测试一下该磁盘的使用率是否超过60%,如果超过立刻将 2 天之前的日志移动到远程服务器 172.18.0.100 服务器上,移动完成后,如果磁盘空间使用率依旧大于 70%, 则发邮件.

代码
  1. 配置centos 使其能够发送邮件
    首先生成邮箱账户的授权码,用来填入配置文件
    在这里插入图片描述
    在这里插入图片描述
    注意这里的password=xxxx,xxx就输入你生成的那一串字符
    [root@localhost ~]yum install mailx sendmail sshpass
    [root@localhost ~]#vi /etc/mail.rc
    set from=1921160095@qq.com
    set smtp=smtp.qq.com
    set smtp-auth-user=1921160095@qq.com
    set smtp-auth-password=XXXXXX                                                          
    set smtp-auth=login
    
  2. 编写脚本
    脚本需要设置两个变量一个是远程主机的ip,一个是远程主机的密码。
    有两种方法可以把文件放进远端服务器,一种是使用 xargs 传入,一种是使用while read 来读取find的查询结果。两种都可实现。
    [root@localhost ~]#vi df.sh 
    #!/bin/bash
    #
    #********************************************************************
    #Author:                liuhao
    #QQ:                    1921160095
    #Date:                  2019-12-26
    #FileName:             df.sh
    #Description:          The test script
    #Copyright (C):         2019 All rights reserved
    #********************************************************************
    remotehost=172.18.0.100
    remotepasswd=Liu123
    used=`df -h |  sed -rn '/^\/dev\/sdb/s@^([^ ]+).* ([0-9]+)%.*$@\2@p'`
    if [ $used -gt 6 ];then
            find /data/log/* -mtime +3 | xargs -n1 -I {} sshpass -p $remotepasswd  scp {} $remotehost:/root/
    #       find /data/log/* -mtime +3 | while read file;do
    #               sshpass -p $remotepasswd  scp $file $remotehost:/root/
    #       done 
            find /data/log/* -mtime +3 | xargs rm
            used2=`df -h |  sed -rn '/^\/dev\/sdb/s@^([^ ]+).* ([0-9]+)%.*$@\2@p'`
            if [ $used2 -gt 70 ];then
                    echo "磁盘空间超过百分之70,请检查此主机`hostname -I`"| mail -s "disk warring" 1921160095@qq.com
            fi
    fi
    
  3. 效果演示
    [root@localhost ~]#dd if=/dev/zero of=/data/log/bigfile bs=1M count=100
    100+0 records in
    100+0 records out
    104857600 bytes (105 MB) copied, 0.0646149 s, 1.6 GB/s
    [root@localhost ~]#dd if=/dev/zero of=/data/log/bigfile2 bs=1M count=100
    100+0 records in
    100+0 records out
    104857600 bytes (105 MB) copied, 0.0892862 s, 1.2 GB/s
    [root@localhost ~]#dd if=/dev/zero of=/data/log/bigfile3 bs=1M count=400
    400+0 records in
    400+0 records out
    419430400 bytes (419 MB) copied, 0.304679 s, 1.4 GB/s
    [root@localhost ~]#ll /data/log
    total 614416
    -rw-r--r--. 1 root root 104857600 Dec 26 14:59 bigfile
    -rw-r--r--. 1 root root 104857600 Dec 26 14:59 bigfile2
    -rw-r--r--. 1 root root 419430400 Dec 26 14:59 bigfile3
    drwx------. 2 root root     16384 Dec 26 13:20 lost+found
    [root@localhost ~]#df -h
    Filesystem               Size  Used Avail Use% Mounted on
    devtmpfs                 736M     0  736M   0% /dev
    tmpfs                    748M     0  748M   0% /dev/shm
    tmpfs                    748M   18M  730M   3% /run
    tmpfs                    748M     0  748M   0% /sys/fs/cgroup
    /dev/mapper/centos-root   17G  1.5G   16G   9% /
    /dev/sda1               1014M  136M  879M  14% /boot
    tmpfs                    150M     0  150M   0% /run/user/0
    /dev/sdb1                991M  603M  322M  66% /data/log
    [root@localhost ~]#date -s "+4day"
    Mon Dec 30 15:01:25 CST 2019
    [root@localhost ~]#sh df.sh 
    Loaded plugins: fastestmirror
    Loading mirror speeds from cached hostfile
    Package mailx-12.5-19.el7.x86_64 already installed and latest version
    Package sendmail-8.14.7-5.el7.x86_64 already installed and latest version
    Package sshpass-1.06-1.el7.x86_64 already installed and latest version
    Nothing to do
    /data/log/lost+found: not a regular file
    Killed by signal 1.
    rm: cannot remove ‘/data/log/lost+found’: Is a directory
    [root@localhost ~]#df -h
    Filesystem               Size  Used Avail Use% Mounted on
    devtmpfs                 736M     0  736M   0% /dev
    tmpfs                    748M     0  748M   0% /dev/shm
    tmpfs                    748M   18M  730M   3% /run
    tmpfs                    748M     0  748M   0% /sys/fs/cgroup
    /dev/mapper/centos-root   17G  1.5G   16G   9% /
    /dev/sda1               1014M  136M  879M  14% /boot
    tmpfs                    150M     0  150M   0% /run/user/0
    /dev/sdb1                991M  2.6M  922M   1% /data/log
    [root@localhost ~]#ll /data/log
    total 16
    drwx------. 2 root root 16384 Dec 26 13:20 lost+found
    [root@localhost ~]#
    
    #远程服务器
    [root@localhost ~]#hostname -I
    192.168.19.11 
    [root@localhost ~]#ll
    total 614428
    -rw-------. 1 root root      1377 Dec 26 14:35 anaconda-ks.cfg
    -rw-r--r--. 1 root root 104857600 Dec 26 15:01 bigfile
    -rw-r--r--. 1 root root 104857600 Dec 26 15:01 bigfile2
    -rw-r--r--. 1 root root 419430400 Dec 26 15:01 bigfile3
    -rw-r--r--. 1 root root     21702 Nov 13 21:44 init_env_191112.sh
    [root@localhost ~]#
    
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值