简体   繁体   English

Perl 脚本未找到最新文件

[英]Perl Script did not find the newest file

I use the following perl script from "https://exchange.nagios.org/directory/Plugins/Operating-Systems/Linux/Check-Newest-files-age-and-size-in-Diredtory/" But in these script is an Error.我使用来自“https://exchange.nagios.org/directory/Plugins/Operating-Systems/Linux/Check-Newest-files-age-and-size-in-Diredtory/”的以下 perl 脚本但在这些脚本中一个错误。 The script is not showing the newest file.该脚本未显示最新文件。 Can someone find the mistake?有人能找出错误吗? In the comments of the site have wrote somebody, that in line 22 the mistake is.在该网站的评论中有人写道,第 22 行中的错误是。 I can't find it: Here is the code:我找不到它:这是代码:

# Check that file exists (can be directory or link)
unless (-d $opt_f) {
        print "FILE_AGE CRITICAL: Folder not found - $opt_f\n";
        exit $ERRORS{'CRITICAL'};
}

my $list = opendir DIRHANDLE, $opt_f or die "Cant open directory: $!";

while ($_ = readdir(DIRHANDLE))
{
    $file=sprintf("%s/%s",$opt_f,$_);
    $attrs = stat("$file");
    $diff = time()-$attrs->mtime;
    if($temp == 0)
    {
        #$temp=$diff;
        $new=$file;
    }
    if($_ ne "." && $_ ne "..")
    {
        if($diff<$temp)
        {
            $temp=$diff;
            $new=$_;
        }
        else
        {
            $temp=$diff; $new=$_;
        }
    }
}

$st = File::stat::stat($opt_f."/".$new);
$age = time - $st->mtime;
$size = $st->size;

Example: I have some files on a filer (backups in a.img File).示例:我在文件管理器上有一些文件(a.img 文件中的备份)。 I use this script, to check the newest file size.我使用这个脚本来检查最新的文件大小。 If I create a new folder with a new file, the check looks to the correct file.如果我使用新文件创建一个新文件夹,则检查会查找正确的文件。 But if I create a second file, the check looks to the old file anytime.但是如果我创建第二个文件,检查会随时查看旧文件。 If I create a third file, the check goes to the correct file.如果我创建第三个文件,检查将转到正确的文件。 The fourth file is wrong and the fifth file is correct again(and so on)第四个文件错了,第五个文件又对了(以此类推)

An easy (easier?) way to do this would be to use the built-in glob function to read the directory instead of opening it, and then use simple file tests to sort the files by creation or modification time:一个简单(更简单?)的方法是使用内置的glob function 来读取目录而不是打开它,然后使用简单的文件测试按创建或修改时间对文件进行排序:

   my @files = sort {-M($a) <=> -M($b)} glob "*"; # or -C for creation
   # $files[0] is the newest file

A list of file test operators is at https://users.cs.cf.ac.uk/Dave.Marshall/PERL/node69.html文件测试运算符列表位于https://users.cs.cf.ac.uk/Dave.Marshall/PERL/node69.html

Note that -C and -M relate to when the script started , so for long-running or daemon scripts you might need to do something a bit different.请注意, -C-M与脚本启动的时间有关,因此对于长时间运行或守护程序脚本,您可能需要做一些不同的事情。

You want to find the earliest mtime , so we're talking about a simple comparison of the previously-found earlier mtime with the mtime of the current file.您想找到最早的mtime ,因此我们正在讨论将先前找到的较早 mtime 与当前文件的 mtime 进行简单比较。 But there's so much code beyond that in what you posted...and the first thing you do with the value you want to compare is change it?但是在您发布的内容之外还有很多代码......您对要比较的值做的第一件事就是更改它? What?什么?

Let's start over.让我们重新开始。

my $earliest_mtime = -1;
my $earliest_qfn;
while (defined( my $fn = readdir($dh) )) {
   next if $fn =~ /^\.\.?\z/;

   my $qfn = "$dir_qfn/$fn";
   my $stats = stat($qfn)
      or warn("Can't stat \"$qfn\": $!\n"), next;

   my $mtime = $stats->mtime;
   if ($mtime < $earliest_mtime) {
      $earliest_mtime = $mtime;
      $earliest_qfn = $qfn;
   }
}

if (defined($earliest_qfn)) {
   say $earliest_qfn;
}

The biggest issue with the script seems to be that line 12 calls the core version of stat but line 13 expects the output to be that of File::stat::stat().该脚本的最大问题似乎是第 12 行调用了 stat 的核心版本,但第 13 行预计 output 是 File::stat::stat() 的。 I suspect that testing for '.'我怀疑测试“。” or '..' should be done at the top of the while loop and all the variables should be defined before they are used.或 '..' 应该在 while 循环的顶部完成,并且所有变量都应该在使用之前定义。

As Jeremy has said, you're better off sorting an array of the files and pushing/poping the first/last value, depending on what you're looking for.正如杰里米所说,您最好对文件数组进行排序并推送/弹出第一个/最后一个值,具体取决于您要查找的内容。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM