From 55c60873a76c0e95737785a5d78516f9beef7f33 Mon Sep 17 00:00:00 2001 From: Antoine Beaupre Date: Tue, 13 Jan 2009 16:08:28 -0500 Subject: rewrite nagios check scripts in perl --- files/checkbackups.pl | 101 ++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 101 insertions(+) create mode 100755 files/checkbackups.pl (limited to 'files/checkbackups.pl') diff --git a/files/checkbackups.pl b/files/checkbackups.pl new file mode 100755 index 0000000..24632d1 --- /dev/null +++ b/files/checkbackups.pl @@ -0,0 +1,101 @@ +#!/usr/bin/perl -w + +# This script is designed to check a backup directory populated with +# subdirectories named after hosts, within which there are backups of various +# types. +# +# Example: +# /home/backup: +# foo.example.com +# +# foo.example.com: +# rdiff-backup .ssh +# +# rdiff-backup: +# root home rdiff-backup-data usr var +# +# There are heuristics to determine the backup type. Currently, the following +# types are supported: +# +# rdiff-backup: assumes there is a rdiff-backup/rdiff-backup-data/backup.log file +# duplicity: assumes there is a dup subdirectory, checks the latest file +# dump files: assumes there is a dump subdirectory, checks the latest file +# +# This script returns output suitable for send_nsca to send the results to +# nagios and should therefore be used like this: +# +# checkbackups.sh | send_nsca -H nagios.example.com + +use Getopt::Std; + +# XXX: taken from utils.sh from nagios-plugins-basic +my $STATE_OK=0; +my $STATE_WARNING=1; +my $STATE_CRITICAL=2; +my $STATE_UNKNOWN=3; +my $STATE_DEPENDENT=4; + +our $opt_d = "/backup"; +our $opt_c = 48 * 60 * 60; +our $opt_w = 24 * 60 * 60; + +if (!getopts('d:c:w:')) { + print < ] [ -c ] [ -w ] +EOF + ; + exit(); +} + +my $backupdir= $opt_d; +my $crit = $opt_c; +my $warn = $opt_w; + +# XXX: this should be a complete backup registry instead +my @hosts=qx{ls $backupdir}; + +chdir($backupdir); +foreach my $host (@hosts) { + chomp($host); + my $flag=""; + my $type="unknown"; + if (-d $host) { + # guess the backup type and find a proper stamp file to compare + # XXX: this doesn't check if the backup was actually successful + # XXX: the backup type should be part of the machine registry + if (-d "$host/rdiff-backup") { + $flag="$host/rdiff-backup/rdiff-backup-data/backup.log"; + $type="rdiff"; + } elsif (-d "$host/dump") { + $flag="$host/dump/" . `ls -tr $host/dump | tail -1`; + chomp($flag); + $type="dump"; + } elsif (-d "$host/dup") { + $flag="$host/dup"; + $type="duplicity"; + } else { + printf "$host\tbackups\t$STATE_UNKNOWN\tunknown system\n"; + next; + } + my @stats = stat($flag); + if (not @stats) { + printf "$host\tbackups\t$STATE_UNKNOWN\tcannot stat flag $flag\n"; + next; + } + my $t = time(); + my $delta = $t - $stats[9]; + my $state = $STATE_UNKNOWN; + if ($delta > $crit) { + $state = $STATE_CRITICAL; + } elsif ($delta > $warn) { + $state = $STATE_WARNING; + } elsif ($delta >= 0) { + $state = $STATE_OK; + } + print "$host\t"; + print "backups\t$state"; + print "\t$delta seconds old\n"; + } else { + printf "$host\tbackups\t$STATE_UNKNOWN\tno directory\n"; + } +} -- cgit v1.2.3 From 981c43068254a2c49a677bb7c66c61c5c0f99943 Mon Sep 17 00:00:00 2001 From: Antoine Beaupre Date: Fri, 17 Apr 2009 13:01:26 -0400 Subject: try to refactor to detect vserver backups --- files/checkbackups.pl | 23 ++++++++++++++++------- 1 file changed, 16 insertions(+), 7 deletions(-) (limited to 'files/checkbackups.pl') diff --git a/files/checkbackups.pl b/files/checkbackups.pl index 24632d1..adc4379 100755 --- a/files/checkbackups.pl +++ b/files/checkbackups.pl @@ -59,6 +59,8 @@ foreach my $host (@hosts) { chomp($host); my $flag=""; my $type="unknown"; + my @vservers = (); + my $state = $STATE_UNKNOWN; if (-d $host) { # guess the backup type and find a proper stamp file to compare # XXX: this doesn't check if the backup was actually successful @@ -66,6 +68,11 @@ foreach my $host (@hosts) { if (-d "$host/rdiff-backup") { $flag="$host/rdiff-backup/rdiff-backup-data/backup.log"; $type="rdiff"; + $vserver_dir = "$host/rdiff-backup/var/lib/vservers"; + if (opendir(DIR, $vserver_dir)) { + @vservers = grep { /^\./ && -f "$vserver_dir/$_" } readdir(DIR); + closedir DIR; + } } elsif (-d "$host/dump") { $flag="$host/dump/" . `ls -tr $host/dump | tail -1`; chomp($flag); @@ -74,17 +81,16 @@ foreach my $host (@hosts) { $flag="$host/dup"; $type="duplicity"; } else { - printf "$host\tbackups\t$STATE_UNKNOWN\tunknown system\n"; + $message = "unknown system"; next; } my @stats = stat($flag); if (not @stats) { - printf "$host\tbackups\t$STATE_UNKNOWN\tcannot stat flag $flag\n"; + $message = "cannot stat flag $flag"; next; } my $t = time(); my $delta = $t - $stats[9]; - my $state = $STATE_UNKNOWN; if ($delta > $crit) { $state = $STATE_CRITICAL; } elsif ($delta > $warn) { @@ -92,10 +98,13 @@ foreach my $host (@hosts) { } elsif ($delta >= 0) { $state = $STATE_OK; } - print "$host\t"; - print "backups\t$state"; - print "\t$delta seconds old\n"; + $message = "$delta seconds old"; } else { - printf "$host\tbackups\t$STATE_UNKNOWN\tno directory\n"; + $message = "no directory"; + } +} continue { + printf "$host\tbackups\t$state\t$message\n"; + foreach my $vserver (@vservers) { + printf "$vserver\tbackups\t$state\t$message\n"; } } -- cgit v1.2.3 From 9aa5fb318ffe3bf976d4e456c2754ca6b97dc9e8 Mon Sep 17 00:00:00 2001 From: Antoine Beaupre Date: Fri, 17 Apr 2009 13:01:54 -0400 Subject: detect multiple vserver locations --- files/checkbackups.pl | 25 +++++++++++++++++-------- 1 file changed, 17 insertions(+), 8 deletions(-) (limited to 'files/checkbackups.pl') diff --git a/files/checkbackups.pl b/files/checkbackups.pl index adc4379..f46920b 100755 --- a/files/checkbackups.pl +++ b/files/checkbackups.pl @@ -35,6 +35,11 @@ my $STATE_CRITICAL=2; my $STATE_UNKNOWN=3; my $STATE_DEPENDENT=4; +# gross hack: we look into subdirs to find vservers +my @vserver_dirs = qw{/var/lib/vservers /vservers}; +# even worse: hardcode a suffix to the vserver name to get a FQDN +my $dom_sufx = ".koumbit.net"; + our $opt_d = "/backup"; our $opt_c = 48 * 60 * 60; our $opt_w = 24 * 60 * 60; @@ -55,12 +60,14 @@ my $warn = $opt_w; my @hosts=qx{ls $backupdir}; chdir($backupdir); -foreach my $host (@hosts) { +my ($state, $message, @vservers, $host); +foreach $host (@hosts) { chomp($host); my $flag=""; my $type="unknown"; - my @vservers = (); - my $state = $STATE_UNKNOWN; + @vservers = (); + $state = $STATE_UNKNOWN; + $message = "???"; if (-d $host) { # guess the backup type and find a proper stamp file to compare # XXX: this doesn't check if the backup was actually successful @@ -68,10 +75,12 @@ foreach my $host (@hosts) { if (-d "$host/rdiff-backup") { $flag="$host/rdiff-backup/rdiff-backup-data/backup.log"; $type="rdiff"; - $vserver_dir = "$host/rdiff-backup/var/lib/vservers"; - if (opendir(DIR, $vserver_dir)) { - @vservers = grep { /^\./ && -f "$vserver_dir/$_" } readdir(DIR); - closedir DIR; + foreach my $vserver_dir (@vserver_dirs) { + $dir = "$host/rdiff-backup$vserver_dir"; + if (opendir(DIR, $dir)) { + @vservers = grep { /^[^\.]/ && -d "$dir/$_" } readdir(DIR); + closedir DIR; + } } } elsif (-d "$host/dump") { $flag="$host/dump/" . `ls -tr $host/dump | tail -1`; @@ -105,6 +114,6 @@ foreach my $host (@hosts) { } continue { printf "$host\tbackups\t$state\t$message\n"; foreach my $vserver (@vservers) { - printf "$vserver\tbackups\t$state\t$message\n"; + printf "$vserver$dom_sufx\tbackups\t$state\t$message\n"; } } -- cgit v1.2.3 From 0c604312144f476bb35a9539e351daefb3f05a6c Mon Sep 17 00:00:00 2001 From: Antoine Beaupre Date: Fri, 17 Apr 2009 13:08:53 -0400 Subject: note which is the parent --- files/checkbackups.pl | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'files/checkbackups.pl') diff --git a/files/checkbackups.pl b/files/checkbackups.pl index f46920b..df73ceb 100755 --- a/files/checkbackups.pl +++ b/files/checkbackups.pl @@ -114,6 +114,6 @@ foreach $host (@hosts) { } continue { printf "$host\tbackups\t$state\t$message\n"; foreach my $vserver (@vservers) { - printf "$vserver$dom_sufx\tbackups\t$state\t$message\n"; + printf "$vserver$dom_sufx\tbackups\t$state\t$message, same as parent: $host\n"; } } -- cgit v1.2.3 From 3c80f2328ed686b3b9565a4cced50e3e91c6af1b Mon Sep 17 00:00:00 2001 From: Antoine Beaupre Date: Fri, 17 Apr 2009 13:09:26 -0400 Subject: drop the dom_sufx hack, calculate based on the parent's domain instead --- files/checkbackups.pl | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) (limited to 'files/checkbackups.pl') diff --git a/files/checkbackups.pl b/files/checkbackups.pl index df73ceb..5e75b85 100755 --- a/files/checkbackups.pl +++ b/files/checkbackups.pl @@ -37,8 +37,6 @@ my $STATE_DEPENDENT=4; # gross hack: we look into subdirs to find vservers my @vserver_dirs = qw{/var/lib/vservers /vservers}; -# even worse: hardcode a suffix to the vserver name to get a FQDN -my $dom_sufx = ".koumbit.net"; our $opt_d = "/backup"; our $opt_c = 48 * 60 * 60; @@ -113,6 +111,8 @@ foreach $host (@hosts) { } } continue { printf "$host\tbackups\t$state\t$message\n"; + my @dom_sufx = split(/\./, $host); + my $dom_sufx = join('.', @dom_sufx[1,-1]); foreach my $vserver (@vservers) { printf "$vserver$dom_sufx\tbackups\t$state\t$message, same as parent: $host\n"; } -- cgit v1.2.3 From 6ed0d0399c42187ec3ed0ef54c39a9c1b70aff91 Mon Sep 17 00:00:00 2001 From: Antoine Beaupre Date: Mon, 27 Apr 2009 15:02:03 -0400 Subject: add missing comma --- files/checkbackups.pl | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'files/checkbackups.pl') diff --git a/files/checkbackups.pl b/files/checkbackups.pl index 5e75b85..5748f75 100755 --- a/files/checkbackups.pl +++ b/files/checkbackups.pl @@ -114,6 +114,6 @@ foreach $host (@hosts) { my @dom_sufx = split(/\./, $host); my $dom_sufx = join('.', @dom_sufx[1,-1]); foreach my $vserver (@vservers) { - printf "$vserver$dom_sufx\tbackups\t$state\t$message, same as parent: $host\n"; + printf "$vserver.$dom_sufx\tbackups\t$state\t$message, same as parent: $host\n"; } } -- cgit v1.2.3 From e7ac16329b76d8a17fa7b247d55d3c3f7c9f2e10 Mon Sep 17 00:00:00 2001 From: Antoine Beaupre Date: Tue, 16 Jun 2009 10:51:29 -0400 Subject: parse rdiff-backup logfile for the last real successful backup instead of just relying on the backup logfile timestamp --- files/checkbackups.pl | 41 ++++++++++++++++++++++++++++++++--------- 1 file changed, 32 insertions(+), 9 deletions(-) (limited to 'files/checkbackups.pl') diff --git a/files/checkbackups.pl b/files/checkbackups.pl index 5748f75..2084e86 100755 --- a/files/checkbackups.pl +++ b/files/checkbackups.pl @@ -41,10 +41,11 @@ my @vserver_dirs = qw{/var/lib/vservers /vservers}; our $opt_d = "/backup"; our $opt_c = 48 * 60 * 60; our $opt_w = 24 * 60 * 60; +our $opt_v = 0; -if (!getopts('d:c:w:')) { +if (!getopts('d:c:w:v')) { print < ] [ -c ] [ -w ] +Usage: $0 [ -d ] [ -c ] [ -w ] -v EOF ; exit(); @@ -63,16 +64,33 @@ foreach $host (@hosts) { chomp($host); my $flag=""; my $type="unknown"; + my $extra_msg=""; @vservers = (); $state = $STATE_UNKNOWN; $message = "???"; if (-d $host) { # guess the backup type and find a proper stamp file to compare - # XXX: this doesn't check if the backup was actually successful # XXX: the backup type should be part of the machine registry + my $last_bak; if (-d "$host/rdiff-backup") { $flag="$host/rdiff-backup/rdiff-backup-data/backup.log"; $type="rdiff"; + if (open(FLAG, $flag)) { + while () { + if (/StartTime ([0-9]*).[0-9]* \((.*)\)/) { + $last_bak = $1; + $extra_msg = ' [backup.log]'; + $opt_v && print STDERR "found timestamp $1 ($2) in backup.log\n"; + } + } + if (!$last_bak) { + $message = "cannot parse backup.log for a valid timestamp"; + next; + } + } else { + $opt_v && print STDERR "cannot open backup.log\n"; + } + close(FLAG); foreach my $vserver_dir (@vserver_dirs) { $dir = "$host/rdiff-backup$vserver_dir"; if (opendir(DIR, $dir)) { @@ -81,23 +99,28 @@ foreach $host (@hosts) { } } } elsif (-d "$host/dump") { + # XXX: this doesn't check backup consistency $flag="$host/dump/" . `ls -tr $host/dump | tail -1`; chomp($flag); $type="dump"; } elsif (-d "$host/dup") { + # XXX: this doesn't check backup consistency $flag="$host/dup"; $type="duplicity"; } else { $message = "unknown system"; next; } - my @stats = stat($flag); - if (not @stats) { - $message = "cannot stat flag $flag"; - next; + if (!defined($last_bak)) { + my @stats = stat($flag); + if (not @stats) { + $message = "cannot stat flag $flag"; + next; + } + $last_bak = $stats[9]; } my $t = time(); - my $delta = $t - $stats[9]; + my $delta = $t - $last_bak; if ($delta > $crit) { $state = $STATE_CRITICAL; } elsif ($delta > $warn) { @@ -105,7 +128,7 @@ foreach $host (@hosts) { } elsif ($delta >= 0) { $state = $STATE_OK; } - $message = "$delta seconds old"; + $message = "$delta seconds old$extra_msg"; } else { $message = "no directory"; } -- cgit v1.2.3 From 4b24e2921731446d0f311176264efe41fa05f2b8 Mon Sep 17 00:00:00 2001 From: Antoine Beaupre Date: Thu, 18 Jun 2009 12:11:41 -0400 Subject: add -o flag to treat only one backup, add rsync.log detection --- files/checkbackups.pl | 37 ++++++++++++++++++++++++------------- 1 file changed, 24 insertions(+), 13 deletions(-) (limited to 'files/checkbackups.pl') diff --git a/files/checkbackups.pl b/files/checkbackups.pl index 2084e86..c621109 100755 --- a/files/checkbackups.pl +++ b/files/checkbackups.pl @@ -42,10 +42,11 @@ our $opt_d = "/backup"; our $opt_c = 48 * 60 * 60; our $opt_w = 24 * 60 * 60; our $opt_v = 0; +our $opt_o; -if (!getopts('d:c:w:v')) { +if (!getopts('d:c:w:vo')) { print < ] [ -c ] [ -w ] -v +Usage: $0 [ -d ] [ -c ] [ -w ] [ -o ] [ -v ] EOF ; exit(); @@ -55,25 +56,35 @@ my $backupdir= $opt_d; my $crit = $opt_c; my $warn = $opt_w; -# XXX: this should be a complete backup registry instead -my @hosts=qx{ls $backupdir}; +my @hosts; +if (defined($opt_o)) { + @hosts=qx{hostname -f}; +} else { + # XXX: this should be a complete backup registry instead + @hosts=qx{ls $backupdir}; +} chdir($backupdir); my ($state, $message, @vservers, $host); foreach $host (@hosts) { chomp($host); + if ($opt_o) { + $dir = $backupdir; + } else { + $dir = $host; + } my $flag=""; my $type="unknown"; my $extra_msg=""; @vservers = (); $state = $STATE_UNKNOWN; $message = "???"; - if (-d $host) { + if (-d $dir) { # guess the backup type and find a proper stamp file to compare # XXX: the backup type should be part of the machine registry my $last_bak; - if (-d "$host/rdiff-backup") { - $flag="$host/rdiff-backup/rdiff-backup-data/backup.log"; + if (-d "$dir/rdiff-backup") { + $flag="$dir/rdiff-backup/rdiff-backup-data/backup.log"; $type="rdiff"; if (open(FLAG, $flag)) { while () { @@ -92,21 +103,21 @@ foreach $host (@hosts) { } close(FLAG); foreach my $vserver_dir (@vserver_dirs) { - $dir = "$host/rdiff-backup$vserver_dir"; + $dir = "$dir/rdiff-backup$vserver_dir"; if (opendir(DIR, $dir)) { @vservers = grep { /^[^\.]/ && -d "$dir/$_" } readdir(DIR); closedir DIR; } } - } elsif (-d "$host/dump") { + } elsif (-d "$dir/dump") { # XXX: this doesn't check backup consistency - $flag="$host/dump/" . `ls -tr $host/dump | tail -1`; + $flag="$dir/dump/" . `ls -tr $dir/dump | tail -1`; chomp($flag); $type="dump"; - } elsif (-d "$host/dup") { + } elsif (-r "$dir/rsync.log") { # XXX: this doesn't check backup consistency - $flag="$host/dup"; - $type="duplicity"; + $flag="$dir/rsync.log"; + $type="rsync"; } else { $message = "unknown system"; next; -- cgit v1.2.3 From 9df16ed173e45462f6b0a55d0267b31d5a3021f9 Mon Sep 17 00:00:00 2001 From: Antoine Beaupre Date: Thu, 25 Jun 2009 11:45:54 -0400 Subject: detect properly duplicity backups --- files/checkbackups.pl | 5 +++++ 1 file changed, 5 insertions(+) (limited to 'files/checkbackups.pl') diff --git a/files/checkbackups.pl b/files/checkbackups.pl index c621109..be6bf70 100755 --- a/files/checkbackups.pl +++ b/files/checkbackups.pl @@ -114,6 +114,11 @@ foreach $host (@hosts) { $flag="$dir/dump/" . `ls -tr $dir/dump | tail -1`; chomp($flag); $type="dump"; + } elsif (-d "$dir/dup") { + # XXX: this doesn't check backup consistency + $flag="$dir/dup/" . `ls -tr $dir/dup | tail -1`; + chomp($flag); + $type="dup"; } elsif (-r "$dir/rsync.log") { # XXX: this doesn't check backup consistency $flag="$dir/rsync.log"; -- cgit v1.2.3 From de010623a8594900b9661db9c0dc8707a1e402fe Mon Sep 17 00:00:00 2001 From: Antoine Beaupre Date: Thu, 24 Sep 2009 11:16:05 -0400 Subject: fix vserver lookup script --- files/checkbackups.pl | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) (limited to 'files/checkbackups.pl') diff --git a/files/checkbackups.pl b/files/checkbackups.pl index be6bf70..80fc07f 100755 --- a/files/checkbackups.pl +++ b/files/checkbackups.pl @@ -103,10 +103,13 @@ foreach $host (@hosts) { } close(FLAG); foreach my $vserver_dir (@vserver_dirs) { - $dir = "$dir/rdiff-backup$vserver_dir"; - if (opendir(DIR, $dir)) { - @vservers = grep { /^[^\.]/ && -d "$dir/$_" } readdir(DIR); + $vsdir = "$dir/rdiff-backup$vserver_dir"; + if (opendir(DIR, $vsdir)) { + @vservers = grep { /^[^\.]/ && -d "$vsdir/$_" } readdir(DIR); + $opt_v && print STDERR "found vservers $vsdir: @vservers\n"; closedir DIR; + } else { + $opt_v && print STDERR "no vserver in $vsdir\n"; } } } elsif (-d "$dir/dump") { -- cgit v1.2.3