35 sub get_status($); |
35 sub get_status($); |
36 sub report($); |
36 sub report($); |
37 sub version($$); |
37 sub version($$); |
38 |
38 |
39 my $ME = basename $0; |
39 my $ME = basename $0; |
40 my $VERSION = "2.0"; |
40 my $VERSION = "2.1"; |
41 |
41 |
42 my %opt = ( |
42 my %opt = ( |
43 binary => "/usr/bin/rsnapshot", |
43 binary => "/usr/bin/rsnapshot", |
44 logfile => "/var/log/rsnapshot.log" |
44 logfile => "/var/log/rsnapshot.log", |
|
45 seconds => 86400, |
|
46 date => "yesterday" |
45 ); |
47 ); |
46 |
48 |
47 MAIN: { |
49 MAIN: { |
48 Getopt::Long::Configure('bundling'); |
50 Getopt::Long::Configure('bundling'); |
49 GetOptions( |
51 GetOptions( |
50 "b|binary=s" => \$opt{binary}, |
52 "b|binary=s" => \$opt{binary}, |
51 "l|logfile=s" => \$opt{logfile}, |
53 "l|logfile=s" => \$opt{logfile}, |
|
54 "s|seconds=i" => \$opt{seconds}, |
|
55 "d|date=s" => \$opt{date}, |
52 "h|help" => sub { pod2usage( -verbose => 1, -exitval => $ERRORS{OK} ) }, |
56 "h|help" => sub { pod2usage( -verbose => 1, -exitval => $ERRORS{OK} ) }, |
53 "m|man" => sub { pod2usage( -verbose => 2, -exitval => $ERRORS{OK} ) }, |
57 "m|man" => sub { pod2usage( -verbose => 2, -exitval => $ERRORS{OK} ) }, |
54 "V|version" => sub { version( $ME, $VERSION ); exit $ERRORS{OK}; } |
58 "V|version" => sub { version( $ME, $VERSION ); exit $ERRORS{OK}; } |
55 ) or pod2usage( -verbose => 1, -exitval => $ERRORS{CRITICAL} ); |
59 ) or pod2usage( -verbose => 1, -exitval => $ERRORS{CRITICAL} ); |
56 |
60 |
57 unless ( -x $opt{binary} ) { |
61 unless ( -x $opt{binary} ) { |
58 print |
62 print |
59 "RSNAPSHOT CRITICAL: $opt{binary} - not found or not executable\n"; |
63 "RSNAPSHOT CRITICAL: binary '$opt{binary}' - not found or not executable\n"; |
|
64 exit $ERRORS{CRITICAL}; |
|
65 } |
|
66 |
|
67 unless ( -e $opt{logfile} ) { |
|
68 print "RSNAPSHOT CRITICAL: logfile '$opt{logfile}' - don't exists\n"; |
|
69 exit $ERRORS{CRITICAL}; |
|
70 } |
|
71 |
|
72 if ( -z $opt{logfile} ) { |
|
73 print "RSNAPSHOT WARNING: logfile $opt{logfile} - has zero size\n"; |
|
74 exit $ERRORS{WARNING}; |
|
75 } |
|
76 |
|
77 if ( ( time() - ( stat( $opt{logfile} ) )[9] ) > $opt{seconds} ) { |
|
78 print |
|
79 "RSNAPSHOT CRITICAL: logfile $opt{logfile} - last modification is longer than $opt{seconds} seconds ago\n"; |
60 exit $ERRORS{CRITICAL}; |
80 exit $ERRORS{CRITICAL}; |
61 } |
81 } |
62 |
82 |
63 my $status = get_status( $opt{logfile} ); |
83 my $status = get_status( $opt{logfile} ); |
64 report($status); |
84 report($status); |
65 } |
85 } |
66 |
86 |
67 sub get_status($) { |
87 sub get_status($) { |
68 my $logfile = shift; |
88 my $logfile = shift; |
69 my $date = UnixDate( ParseDate("today"), "%d/%b/%Y" ); |
89 my $date = UnixDate( ParseDate( $opt{date} ), "%d/%b/%Y" ); |
70 my ( $found, $error ) = undef; |
90 my ( $error, $found_date ) = undef; |
|
91 |
|
92 unless ($date) { |
|
93 print "RSNAPSHOT CRITICAL: can't parse date [$opt{date}]\n"; |
|
94 exit $ERRORS{CRITICAL}; |
|
95 } |
71 |
96 |
72 open( FH, $logfile ) |
97 open( FH, $logfile ) |
73 or print "RSNAPSHOT CRITICAL: $logfile - $!\n" and exit $ERRORS{CRITICAL}; |
98 or print "RSNAPSHOT CRITICAL: $logfile - $!\n" and exit $ERRORS{CRITICAL}; |
74 while (<FH>) { |
99 while (<FH>) { |
75 unless ($found) { |
100 next unless (/^\[$date/); |
76 /^\[$date:.*ERROR/ or next; |
101 if (/^\[$date:.*ERROR/) { |
77 $found = 1; |
|
78 $error = $_; |
102 $error = $_; |
79 last; |
103 last; |
80 } |
104 } |
|
105 $found_date = 1; |
81 } |
106 } |
82 close(FH); |
107 close(FH); |
83 |
108 |
84 if ($found) { |
109 unless ($found_date) { |
85 print "RSNAPSHOT CRITICAL: $error\n"; |
110 $error = "can't find rsnapshot run from [$date]\n"; |
|
111 } |
|
112 |
|
113 if ($error) { |
|
114 print "RSNAPSHOT CRITICAL: $error"; |
86 exit $ERRORS{CRITICAL}; |
115 exit $ERRORS{CRITICAL}; |
87 } |
116 } |
88 else { |
117 else { |
89 print "RSNAPSHOT OK: no errors in $logfile\n"; |
118 print "RSNAPSHOT OK: no errors in $logfile\n"; |
90 exit $ERRORS{OK}; |
119 exit $ERRORS{OK}; |