用于检查已用磁盘空间的bash脚本

时间:2014-10-31 21:16:35

标签: bash

我写了一个bash脚本来检查分区使用的磁盘空间百分比,并在超过某个限制时发出警告。它适用于某些服务器但不适用于其他服务器

#!/bin/bash                                                                                                                                                                                         
source /etc/profile

# Devices to check                                                                                                                                                                                                                                                                                                                             
devnames=`df | awk '{print $1}'|tail -n +2`
email="myemail"
threshold=95
for devname in $devnames
do
   if  [[ -z "$devname" ]] || [[ "$devname" = "tmpfs" ]]; then
       continue
   fi
#                                                                                                                                                                                                   
   let p=`df -k $devname | grep -v ^File | awk '{printf ("%i", 100 - $4*100 / $2); }'`
#   echo "$devname $p"                                                                                                                                                                              

  if [ $p -ge $threshold ]
  then
    df -h $devname | mail -s "$devname is low on space" $email
  fi
done

这是有问题的服务器上的df结果

 df                                      
Filesystem      1K-blocks       Used  Available Use% Mounted on
rootfs           20317384    5138584   14154856  27% /
/dev/root        20317384    5138584   14154856  27% /
devtmpfs         32968916        304   32968612   1% /dev
tmpfs            33002348          0   33002348   0% /dev/shm
/dev/sda4      1902183556 1617154956  188403296  90% /home
/dev/sdb4      1901315252 1675315368  129418612  93% /disk2
/dev/sdc4      1902183556  336514784 1469043468  19% /disk3
/dev/sdd4      1902183556 1348238780  457319472  75% /disk4
/dev/root        20317384    5138584   14154856  27% /var/named/chroot/etc/named
/dev/root        20317384    5138584   14154856  27% /var/named/chroot/var/named
/dev/root        20317384    5138584   14154856  27% /var/named/chroot/etc/named.conf
/dev/root        20317384    5138584   14154856  27% /var/named/chroot/etc/named.rfc1912.zones
/dev/root        20317384    5138584   14154856  27% /var/named/chroot/etc/rndc.key
/dev/root        20317384    5138584   14154856  27% /var/named/chroot/usr/lib64/bind
/dev/root        20317384    5138584   14154856  27% /var/named/chroot/etc/named.iscdlv.key
/dev/root        20317384    5138584   14154856  27% /var/named/chroot/etc/named.root.key

这是错误:

 /home/util/check_disk_storages.sh       
df: `rootfs': No such file or directory
df: no file systems processed
/home/util/check_disk_storages.sh: line 15: let: p=: syntax error: operand expected (error token is "=")
/home/util/check_disk_storages.sh: line 18: [: -ge: unary operator expected
df: `/dev/root': No such file or directory
df: no file systems processed
/home/util/check_disk_storages.sh: line 15: let: p=: syntax error: operand expected (error token is "=")
/home/util/check_disk_storages.sh: line 18: [: -ge: unary operator expected
df: `devtmpfs': No such file or directory
df: no file systems processed
/home/util/check_disk_storages.sh: line 15: let: p=: syntax error: operand expected (error token is "=")
/home/util/check_disk_storages.sh: line 18: [: -ge: unary operator expected
df: `/dev/root': No such file or directory
df: no file systems processed
/home/util/check_disk_storages.sh: line 15: let: p=: syntax error: operand expected (error token is "=")
df: `/dev/root': No such file or directory
df: no file systems processed
/home/util/check_disk_storages.sh: line 15: let: p=: syntax error: operand expected (error token is "=")
df: `/dev/root': No such file or directory
df: no file systems processed
/home/util/check_disk_storages.sh: line 15: let: p=: syntax error: operand expected (error token is "=")
df: `/dev/root': No such file or directory
df: no file systems processed
/home/util/check_disk_storages.sh: line 15: let: p=: syntax error: operand expected (error token is "=")
df: `/dev/root': No such file or directory
df: no file systems processed
/home/util/check_disk_storages.sh: line 15: let: p=: syntax error: operand expected (error token is "=")
df: `/dev/root': No such file or directory
df: no file systems processed
/home/util/check_disk_storages.sh: line 15: let: p=: syntax error: operand expected (error token is "=")
df: `/dev/root': No such file or directory
df: no file systems processed
/home/util/check_disk_storages.sh: line 15: let: p=: syntax error: operand expected (error token is "=")
df: `/dev/root': No such file or directory
df: no file systems processed
/home/util/check_disk_storages.sh: line 15: let: p=: syntax error: operand expected (error token is "=")

我该如何解决这个问题?

1 个答案:

答案 0 :(得分:1)

试试这个:只有2个小改动。不要在第15行使用 let ,并在第18行使用

#!/bin/bash                                                                                                                                                                                         
source /etc/profile

# Devices to check                                                                                                                                                                                                                                                                                                                             
devnames=`df | awk '{print $1}'|tail -n +2`
email="myemail"
threshold=95
for devname in $devnames
do
   if  [[ -z "$devname" ]] || [[ "$devname" = "tmpfs" ]]; then
       continue
   fi
#                                                                                                                                                                                                   
   p=`df -k $devname | grep -v ^File | awk '{printf ("%i", 100 - $4*100 / $2); }'`
#   echo "$devname $p"                                                                                                                                                                              

  if [[ $p -ge $threshold ]]
  then
    df -h $devname | mail -s "$devname is low on space" $email
  fi
done