用于监视空间和发送邮件的脚本

时间:2014-01-04 10:21:37

标签: perl

我想写一个perl脚本来监视多个文件系统的空间,如果它超过警告级别,它应该发送一个邮件。 EG:如果空间为98%且可用空间仅为2%^,则应发送邮件说该路径已满。请从此路径中删除一些不需要的文件。以下是代码:

#!/usr/bin/perl
# Pass in command line parameter "--test" to perform an email test.
use strict;
use warnings;
use Filesys::DiskSpace;
# ---------------------------
# Configuration section
# ---------------------------
# default warning level
my $default_warning_level=10;
# email setup
my $to='admin@yourdomain.com';
my $from='webmaster@YOURDOMAIN.COM';
my $subject='Low Disk Space';
# folders to check
sub perform_checks {
    &check_free_space("/", 30);
    &check_free_space("/mnt/data");
    &check_free_space("/mnt/storage");
    &check_free_space("/mnt/backup", 20);
}
# ---------------------------
# Main script
# ---------------------------
my $out = "";
# Check free space for folder and add to email message if it is below warning level.
# Parameters:
#   folder_to_check - The path of the filesystem to check.
#   warning_level - If the free disk space percentage is below this level,
#                   an email is sent.  If this parameter is omitted, the
#                   default level will be used.
sub check_free_space {
    my ($dir, $warning_level) = @_;
    # set warning level to default if not specified
    if(!defined($warning_level)) {
        $warning_level = $default_warning_level;
    }
    # get df
    my ($fs_type, $fs_desc, $used, $avail, $fused, $favail) = df $dir;
    # calculate
    my $df_free = ($avail / ($avail + $used)) * 100.0;
    # compare
    if (($df_free < $warning_level) || (defined($ARGV[0]) && ($ARGV[0] eq "--test"))) {
        # append to email
        $out .= sprintf("WARNING Low Disk Space on $dir : %0.2f%% ()\n",$df_free);
    }
}
&perform_checks;
# check if there are warnings to email
if($out ne "") {
    # send email using UNIX/Linux sendmail
    open(MAIL, "|/usr/sbin/sendmail -t");
    ## Mail Header
    print MAIL "To: $to\n";
    print MAIL "From: $from\n";
    print MAIL "Subject: $subject\n";
    ## Mail Body
    print MAIL $out;
}
close(MAIL);

但我在Citrix solaris环境中运行代码。我无法安装文件系统模块。你可以帮我写一个代码而不使用这个模块使用Filesys :: DiskSpace;。

1 个答案:

答案 0 :(得分:4)

当然可能,最简单的方法是解析系统的df输出。我不在Solaris上,因此我的df格式可能略有不同,您可能需要为Solaris df修改此格式。

## Since df gives %used, you can simply change your
## limit to be 90 instead of 10
my $default_warning_level=90;

## skipping the rest of the script

sub check_free_space {
    my ($dir, $warning_level) = @_;
    # set warning level to default if not specified
    if(!defined($warning_level)) {
        $warning_level = $default_warning_level;
    }
    # get df, on my system, this includes a usage percentage
    # which is what I guess you were calculating.
    my $res=`df $dir | tail -n 1`;
    my ($dev, $total, $used, $avail, $use_perc) = split(/\s+/,$res);

    # compare
    if (($df_free < $warning_level) || (defined($ARGV[0]) && ($ARGV[0] eq "--test"))) {
        # append to email
        $out .= sprintf("WARNING Low Disk Space on $dir : %0.2f%% ()\n",$avail);
    }
}

作为一般说明,除非您使用的是非常旧版本的Perl,否则不要在子例程中使用&,除非您真的知道自己在做什么。在大多数情况下,不需要&,并且经常会导致问题。有关详细信息,请参阅here