summaryrefslogtreecommitdiffstats
path: root/contrib/check_backup.pl
blob: 703d24249de4123443c7bf144a46b177ed7929ba (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
#! /usr/bin/perl -wT

# (c)2001 Patrick Greenwell, Stealthgeeks, LLC. (patrick@stealthgeeks.net)
# Licensed under the GNU GPL
# http://www.gnu.org/licenses/gpl.html

# check_backup: Checks a directory to see if at least one file was
# created within a specified period of time that is of equal to or greater
# than a given size.

# Version 1.0
# Last Updated: 9/12/01


BEGIN {
	if ($0 =~ m/^(.*?)[\/\\]([^\/\\]+)$/) {
		$runtimedir = $1;
		$PROGNAME = $2;
	}
}

require 5.004;
use strict;
use Getopt::Long;
use vars qw($opt_H $opt_d $opt_s $opt_t $verbose $PROGNAME);
use lib $main::runtimedir;
use utils qw($TIMEOUT %ERRORS &print_revision &usage &support &is_error);

sub help ();
sub print_help ();
sub print_usage ();
sub version ();
sub display_res($$);
my ($filesize, $answer) = ();
my $state = $ERRORS{'UNKNOWN'};

# Directory to check.
my $dir = "/backup/";

# Time period(in seconds)
my $within = "3600";

# Minimum size of file (in bytes)
my $minsize = "40000000";

delete @ENV{'PATH', 'IFS', 'CDPATH', 'ENV', 'BASH_ENV'};

Getopt::Long::Configure('bundling', 'no_ignore_case');
GetOptions
	("V|version"     => \&version,
	 "h|help"        => \&help,
	 "v|verbose"     => \$verbose,
         "d|directory=s" => \$opt_d,
         "s|minsize=s"   => \$opt_s,
         "t|timeout=s"   => \$opt_t,
         );

($opt_s) || ($opt_s = shift) || usage("Minimum File size not specified\n");
usage("File size must be numeric value") unless ($opt_s =~ m/^[0-9]+$/);

(($opt_t) && ($TIMEOUT = $opt_t)) || ($TIMEOUT = 120);
usage("TIMEOUT must be numeric value") unless ($TIMEOUT =~ m/^[0-9]+$/);

# Don't hang if there are timeout issues
$SIG{'ALRM'} = sub {
	print ("ERROR: No response from ftp server (alarm)\n");
	exit $ERRORS{'UNKNOWN'};
};
alarm($TIMEOUT);

# Do stuff

my $time = time;
 
opendir(THISDIR, "$dir") or die "Can't open directory! $!";
my @allfiles = grep !/^\./, readdir THISDIR;
closedir THISDIR;
while (my $file = $dir . pop @allfiles){
        my ($size, $mtime) = (stat($file))[7,9];
        if  (((my $a = ($time - $mtime)) <= $within) and ($size >= $opt_s)){
                display_res("OK: File $file is <= $within and >=$opt_s bytes.\n","OK");        
        }
}
 
# If we got here nothing matched.... 
display_res("CRITICAL: No files in $dir are <= $within and >= $minsize.", "CRITICAL");

exit;

sub print_usage () {
	print "Usage: $PROGNAME -s <minimum file size in bytes> -t <timeout> \n";
}

sub print_help () {
	print_revision($PROGNAME,'$ Revision: 1.0 $ ');
	print_usage();
	support();
}

sub version () {
	print_revision($PROGNAME,'$ Revision: 1.0 $ ');
	exit $ERRORS{'OK'};
}

sub help () {
	print_help();
	exit $ERRORS{'OK'};
}

sub display_res ($$) {
        my ($answer, $state) = @_;
        print $answer;
        exit $ERRORS{$state};
}