| 1 |
package Resmon::Module::OLDFILES; |
|---|
| 2 |
use Resmon::ExtComm qw/cache_command/; |
|---|
| 3 |
use vars qw/@ISA/; |
|---|
| 4 |
use File::Find; |
|---|
| 5 |
@ISA = qw/Resmon::Module/; |
|---|
| 6 |
|
|---|
| 7 |
# Checks for files in a directory older than a certain time |
|---|
| 8 |
# Parameters: |
|---|
| 9 |
# minutes : how old can the files be before we alarm |
|---|
| 10 |
# checkmount : check to make sure the directory is mounted first |
|---|
| 11 |
# (only enable if the dir you are checking is the mountpoint of |
|---|
| 12 |
# a filesystem) |
|---|
| 13 |
# filecount : how many old files will we allow before alarming. If this is not |
|---|
| 14 |
# set, then we will alarm if any files are old. |
|---|
| 15 |
# Example: |
|---|
| 16 |
# |
|---|
| 17 |
# OLDFILES { |
|---|
| 18 |
# /test/dir : minutes => 5, filecount => 2, checkmount => 1 |
|---|
| 19 |
# /other/dir : minutes => 60 |
|---|
| 20 |
# } |
|---|
| 21 |
|
|---|
| 22 |
my $minutes; |
|---|
| 23 |
my $oldcount = 0; |
|---|
| 24 |
|
|---|
| 25 |
sub handler { |
|---|
| 26 |
my $arg = shift; |
|---|
| 27 |
my $dir = $arg->{'object'}; |
|---|
| 28 |
$minutes = $arg->{'minutes'}; |
|---|
| 29 |
my $filecount = $arg->{'filecount'} || 0; |
|---|
| 30 |
my $checkmount = $arg->{'checkmount'} || 0; |
|---|
| 31 |
|
|---|
| 32 |
# Check to make sure the directory is mounted first |
|---|
| 33 |
if ($checkmount) { |
|---|
| 34 |
my $output = cache_command("df -k", 600); |
|---|
| 35 |
my ($line) = grep(/$dir\s*/, split(/\n/, $output)); |
|---|
| 36 |
if($line !~ /(\d+)%/) { |
|---|
| 37 |
return "BAD", "0 dir not mounted"; |
|---|
| 38 |
} |
|---|
| 39 |
} |
|---|
| 40 |
|
|---|
| 41 |
# Then look for old files |
|---|
| 42 |
find(\&wanted, $dir); |
|---|
| 43 |
if ($oldcount <= $filecount) { |
|---|
| 44 |
return "OK", "$oldcount files"; |
|---|
| 45 |
} else { |
|---|
| 46 |
return "BAD", "$oldcount files"; |
|---|
| 47 |
} |
|---|
| 48 |
} |
|---|
| 49 |
|
|---|
| 50 |
sub wanted { |
|---|
| 51 |
-f $_ && -M $_ > ($minutes / 1440) && $oldcount++; |
|---|
| 52 |
} |
|---|
| 53 |
|
|---|
| 54 |
1; |
|---|