check_backup.pl 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. #! /usr/bin/perl -wT
  2. # (c)2001 Patrick Greenwell, Stealthgeeks, LLC. (patrick@stealthgeeks.net)
  3. # Licensed under the GNU GPL
  4. # http://www.gnu.org/licenses/gpl.html
  5. # check_backup: Checks a directory to see if at least one file was
  6. # created within a specified period of time that is of equal to or greater
  7. # than a given size.
  8. # Version 1.0
  9. # Last Updated: 9/12/01
  10. BEGIN {
  11. if ($0 =~ m/^(.*?)[\/\\]([^\/\\]+)$/) {
  12. $runtimedir = $1;
  13. $PROGNAME = $2;
  14. }
  15. }
  16. require 5.004;
  17. use strict;
  18. use Getopt::Long;
  19. use vars qw($opt_H $opt_d $opt_s $opt_t $verbose $PROGNAME);
  20. use lib $main::runtimedir;
  21. use utils qw($TIMEOUT %ERRORS &print_revision &usage &support &is_error);
  22. sub help ();
  23. sub print_help ();
  24. sub print_usage ();
  25. sub version ();
  26. sub display_res($$);
  27. my ($filesize, $answer) = ();
  28. my $state = $ERRORS{'UNKNOWN'};
  29. # Directory to check.
  30. my $dir = "/backup/";
  31. # Time period(in seconds)
  32. my $within = "3600";
  33. # Minimum size of file (in bytes)
  34. my $minsize = "40000000";
  35. delete @ENV{'PATH', 'IFS', 'CDPATH', 'ENV', 'BASH_ENV'};
  36. Getopt::Long::Configure('bundling', 'no_ignore_case');
  37. GetOptions
  38. ("V|version" => \&version,
  39. "h|help" => \&help,
  40. "v|verbose" => \$verbose,
  41. "d|directory=s" => \$opt_d,
  42. "s|minsize=s" => \$opt_s,
  43. "t|timeout=s" => \$opt_t,
  44. );
  45. ($opt_s) || ($opt_s = shift) || usage("Minimum File size not specified\n");
  46. usage("File size must be numeric value") unless ($opt_s =~ m/^[0-9]+$/);
  47. (($opt_t) && ($TIMEOUT = $opt_t)) || ($TIMEOUT = 120);
  48. usage("TIMEOUT must be numeric value") unless ($TIMEOUT =~ m/^[0-9]+$/);
  49. # Don't hang if there are timeout issues
  50. $SIG{'ALRM'} = sub {
  51. print ("ERROR: No response from ftp server (alarm)\n");
  52. exit $ERRORS{'UNKNOWN'};
  53. };
  54. alarm($TIMEOUT);
  55. # Do stuff
  56. my $time = time;
  57. opendir(THISDIR, "$dir") or die "Can't open directory! $!";
  58. my @allfiles = grep !/^\./, readdir THISDIR;
  59. closedir THISDIR;
  60. while (my $file = $dir . pop @allfiles){
  61. my ($size, $mtime) = (stat($file))[7,9];
  62. if (((my $a = ($time - $mtime)) <= $within) and ($size >= $opt_s)){
  63. display_res("OK: File $file is <= $within and >=$opt_s bytes.\n","OK");
  64. }
  65. }
  66. # If we got here nothing matched....
  67. display_res("CRITICAL: No files in $dir are <= $within and >= $minsize.", "CRITICAL");
  68. exit;
  69. sub print_usage () {
  70. print "Usage: $PROGNAME -s <minimum file size in bytes> -t <timeout> \n";
  71. }
  72. sub print_help () {
  73. print_revision($PROGNAME,'$ Revision: 1.0 $ ');
  74. print_usage();
  75. support();
  76. }
  77. sub version () {
  78. print_revision($PROGNAME,'$ Revision: 1.0 $ ');
  79. exit $ERRORS{'OK'};
  80. }
  81. sub help () {
  82. print_help();
  83. exit $ERRORS{'OK'};
  84. }
  85. sub display_res ($$) {
  86. my ($answer, $state) = @_;
  87. print $answer;
  88. exit $ERRORS{$state};
  89. }