welcome: please sign in
location: CheckDiskUsage

磁盘使用情况检测

需求

检查磁盘分区使用情况,剩余空间低时邮件警告

解决方案

引子 Bash with df/grep/awk

   1 #!/bin/bash
   2 
   3 # Modified from: http://www.unix.com/302287637-post2.html
   4 
   5 email="foo@example.com"
   6 #### check disk space on filesystems listed
   7 filesystems=(/ /boot /var /home)
   8 for fs in ${filesystems[@]}; do
   9         # grep's -m ensures only (at most) one line is fed to awk
  10         disk_space=`df -h | grep -m 1 -E $fs$ | awk '{print $(NF-1)}'`
  11         #disk_space=`df -h | awk '/'\\$fs'$/{print $(NF-1);exit}'`   # get rid of useless...
  12         max_space="90"
  13         disk_space=${disk_space:-$((max_space-1))}
  14         if [[ "${disk_space%'%'}" -ge "$max_space" ]]; then
  15                 sub="[FTP3 WARN] DISK SPACE ON $fs IS AT $disk_space"
  16                 df -h | mail -s "$sub" "$email"
  17         fi
  18 done
  19 unset filesystems fs disk_space max_space email sub
  20 ####
  21 

POSIX Shell

   1 #!/bin/sh
   2 email='foo@example.com'
   3 filesystems='/ /boot /var /home'
   4 limit=90
   5 
   6 used=$(df | sed '1d; s/\s\+/\t/g; s/%//' | cut -f5,6)
   7 urgentfs=
   8 for fs in $filesystems; do
   9     percent=$(echo "$used" | grep '\s'$fs'$' | sed 1q | cut -f1)
  10     if [ -n "$percent" ] && [ "$percent" -ge "$limit" ]; then
  11         urgentfs="$urgentfs $fs"
  12     fi
  13 done
  14 
  15 if [ -n "$urgentfs" ]; then
  16     title="$(hostname) WARNING:$urgentfs over $limit"
  17     df -h | mail -s "$title" "$email"
  18 fi

Bash without sed/grep/awk

   1 #!/bin/bash
   2 
   3 ## max avail percent before alert: 10%
   4 MAX_PERCENT=90
   5 ## minimal avail space before alert: 10G
   6 MIN_AVAIL=$((1024*1024*10))
   7 
   8 df -P | while read dev total used avail percent mountpoint; do
   9     ## skip the first line
  10     if [[ $dev == "Filesystem" ]]; then continue; fi
  11     ## remove % from percent number
  12     percent=${percent%%%}
  13     if [[ $avail -lt $MIN_AVAIL || $percent -gt $MAX_PERCENT ]]; then
  14         echo "space too low: $dev on $mountpoint, avail: $avail, percent:
  15 $percent%"
  16     fi
  17 done

注:将其中echo的那一行换成发邮件的命令,同时也可以在那一步过滤掉一些不关心的文件系统。

POSIX Shell with stat

   1 #!/bin/sh
   2 : ${email:='foo@example.com'}
   3 : ${filesystems:='/ /boot /var /home'}
   4 : ${limit:=10}
   5 
   6 set --
   7 for fs in $filesystems; do
   8     percent=$(( $(stat -f "$fs" -c "100 * %f / %b") ))
   9     if [ "$percent" -le "$limit" ]; then
  10         set -- "$@" "$fs"
  11     fi
  12 done
  13 
  14 if [ "$#" -gt 0 ]; then
  15     title="$(hostname) WARNING: $@ free space below $limit%"
  16     df -h | mail -s "$title" "$email"
  17 fi

这是 ftp3 上目前部署的脚本 checkdf.sh

注:可以在调用脚本的时候利用环境变量影响它们的值。比如调试的时候可以用:email=bar@localhost ./checkdf.sh

C

NOTE: 还有些问题~~

   1 #include <stdio.h>
   2 #include <sys/vfs.h>
   3 
   4 #define RECIPIENT "ray"
   5 
   6 int main(void)
   7 {
   8     const char* watch_dirs[] = {"/var", "/data", NULL};
   9     const char* (*dir);
  10     struct statfs buf;
  11     for (dir = watch_dirs; *dir; ++dir) {
  12         if (statfs(*dir, &buf) < 0) {
  13             perror(*dir);
  14             return 1;
  15         }
  16         if (buf.f_blocks * 0.1 > buf.f_bfree ||
  17                 buf.f_files * 0.1 > buf.f_ffree) {
  18             char cmd[2012];
  19             snprintf(cmd, 2012, "mail -s 'WARNING: low disk space on %s' %s", *dir, RECIPIENT);
  20             popen(cmd, "w");
  21         }
  22         return 0;
  23     }
  24 }

Perl

来自http://www.cyberciti.biz/tips/howto-write-perl-script-to-monitor-disk-space.html

未测试……

   1 #!/usr/bin/perl
   2 # Available under BSD License. See url for more info:
   3 # http://www.cyberciti.biz/tips/howto-write-perl-script-to-monitor-disk-space.html
   4 use strict;
   5 use warnings;
   6 use Filesys::DiskSpace;
   7 
   8 # file system to monitor
   9 my $dir = "/home";
  10 
  11 # warning level
  12 my $warning_level=10;
  13 
  14 # email setup
  15 my $to='admin@yourdomain.com';
  16 my $from='webmaster@YOURDOMAIN.COM';
  17 my $subject='Low Disk Space';
  18 
  19 # get df
  20 my ($fs_type, $fs_desc, $used, $avail, $fused, $favail) = df $dir;
  21 
  22 # calculate
  23 my $df_free = (($avail) / ($avail+$used)) * 100.0;
  24 
  25 # compare
  26 if ($df_free < $warning_level) {
  27 my $out = sprintf("WARNING Low Disk Space on $dir : %0.2f%% ()\n",$df_free);
  28 
  29 # send email using UNIX/Linux sendmail
  30 open(MAIL, "|/usr/sbin/sendmail -t");
  31 
  32 ## Mail Header
  33 print MAIL "To: $to\\n";
  34 print MAIL "From: $from\\n";
  35 print MAIL "Subject: $subject\\n";
  36 
  37 ## Mail Body
  38 print MAIL $out;
  39 
  40 close(MAIL);
  41 }

参考

[1] 邮件列表 https://groups.google.com/forum/#!topic/thu-opensource-mirror-admin/bJ06tY1z9Nw

CheckDiskUsage (2012-04-27 11:19:54由xiaq编辑)