1.必须函数:
[root@localhost ~]# df
文件系统 1K-块 已用 可用 已用% 挂载点
/dev/mapper/VolGroup00-LogVol00
11109328 2154636 8381252 21% /
/dev/sda1 101086 11999 83868 13% /boot
tmpfs 1037748 0 1037748 0% /dev/shm
[root@localhost ~]# df | sed -n ‘/\/$/p‘
11109328 2154636 8381252 21% /
/$:表示以斜杠(/)结尾($)的行
[root@localhost ~]# df | sed -n ‘/\/$/p‘ | gawk ‘{print $4}‘
21%
$4:表示打印出第四个字段
[root@localhost ~]# df | sed -n ‘/\/$/p‘ | gawk ‘{print $4}‘|sed ‘s/%//‘
21
s:替换
s/%//:表示将百分号替换成空
2.创建脚本
创建一个脚本,提示当/的使用空间超过10%的时候,向ley的用户发送一封报警邮件
#!/bin/bash
#monitor available disk space
s=`df | sed -n ‘/\/$/p‘ | gawk ‘{print $4}‘|sed ‘s/%//‘`
if [ $s -ge 10 ]
then
echo "$s% userd" | mail -s "Disk warning" ley
fi
~
ley用户必须是在系统中存在的用户
******************************************************************************************
[ley@localhost ~]$ mail 查看邮件
Mail version 8.1 6/6/93. Type ? for help.
"/var/spool/mail/ley": 5 messages 5 new
>N 1 root@localhost.local Wed Nov 5 17:02 16/654 "Disk warning"
N 2 root@localhost.local Wed Nov 5 17:02 16/654 "Disk warning"
N 3 root@localhost.local Wed Nov 5 17:04 16/654 "Disk warning"
N 4 root@localhost.local Wed Nov 5 17:04 16/654 "Disk warning"
N 5 root@localhost.local Wed Nov 5 17:04 16/654 "Disk warning"
&
Message 1: 第一条信息
From root@localhost.localdomain Wed Nov 5 17:02:16 2014
Date: Wed, 5 Nov 2014 17:02:16 +0800
From: root <root@localhost.localdomain>
To: ley@localhost.localdomain
Subject: Disk warning
21% userd
本文出自 “linux运维分享” 博客,请务必保留此出处http://liangey.blog.51cto.com/9097868/1572228
原文地址:http://liangey.blog.51cto.com/9097868/1572228