#!/usr/bin/perl -w # # check_bbackup.pl - nagios plugin # # Copyright (C) 2012 William R Thomas # # This program is free software; you can redistribute it and/or # modify it under the terms of the GNU General Public License # as published by the Free Software Foundation; either version 2 # of the License, or (at your option) any later version. # # This program is distributed in the hope that it will be useful, # but WITHOUT ANY WARRANTY; without even the implied warranty of # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the # GNU General Public License for more details. # # You should have received a copy of the GNU General Public License # along with this program; if not, write to the Free Software # Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. # # # Report bugs to: corvar@theonering.net # # 10.29.2012 Version 1.0 use strict; use Getopt::Long; &Getopt::Long::config('auto_abbrev','no_ignore_case'); my %ERRORS = ('UNKNOWN' , '-1', 'OK' , '0', 'WARNING', '1', 'CRITICAL', '2'); my $status; my $TIMEOUT = 30; my $accountsfile = "/etc/boxbackup/bbstored/accounts.txt"; my $bbstoreaccts = "/usr/sbin/bbstoreaccounts"; my $state = "OK"; my $statefile="check_bbackup.db"; my $statedir="/var/cache/nagios"; if(-d "/var/cache/nagios3") { $statedir="/var/cache/nagios3"; } my $warn = 86400; my $critical = 172900; my $warnpercent = 80; my $critpercent = 90; my $hostname; my $host; my $stateful; my $answer; my @output; my %last_sample; sub usage { printf "\nMissing arguments!\n"; printf "\n"; printf "Perl boxbackup plugin for Nagios\n"; printf "monitors host for boxbackup clients who have not checked in\n"; printf "usage: \n"; printf "check_reboot.pl -d -s -w -c -W -C \n"; printf "Copyright (C) 2012 William R Thomas\n"; printf "check_bbackup.pl comes with ABSOLUTELY NO WARRANTY\n"; printf "This programm is licensed under the terms of the "; printf "GNU General Public License\n(check source code for details)\n"; printf "\n\n"; exit $ERRORS{"UNKNOWN"}; } $status = GetOptions("statefile=s",\$statefile, "dir=s",\$statedir, "warn=i",\$warn, "critical=i",\$critical, "Warnpercent=i",\$warnpercent, "Critpercent=i",\$critpercent); if ($status == 0) { &usage; } $stateful = $statedir."/".$statefile; if (! -f $stateful) { open (O, ">$stateful"); close (O); } if (!open (IN, "$stateful")) { print "ERROR: could not open state file $stateful\n"; exit $ERRORS{"UNKNOWN"}; } while (defined ($host = )) { if ($host =~ /^(\S+) (\d+) (\d+)/) { $last_sample{$1}{"marker"} = $2; $last_sample{$1}{"lastcheck"} = $3; } } close (IN); if (!open (ACCT, "$accountsfile")) { print "ERROR: could not open accounts file $accountsfile\n"; exit $ERRORS{"UNKNOWN"}; } while() { my ($marker,$used); my $acctnum = (split(/:/,$_))[0]; open(BBST,$bbstoreaccts." info $acctnum |"); while() { if(/ Client store marker: (\d+)/) { $marker = $1; } if(/\s+Used:\s+\d+\s+blocks,\s+\d+\.\d+ ..,\s+(\d+)%/) { $used = $1; # Used: 8083932 blocks, 30.84 GB, 22% |*** | } } close(BBST); if(!$marker) { $state = 'WARNING'; next; } if(!$used) { $state = 'WARNING'; next; } if($last_sample{$acctnum}{"marker"}) { if($last_sample{$acctnum}{"marker"} != $marker) { $last_sample{$acctnum}{"marker"} = $marker; $last_sample{$acctnum}{"lastcheck"} = time; } } else { $last_sample{$acctnum}{"marker"} = $marker; $last_sample{$acctnum}{"lastcheck"} = time; } if( ($last_sample{$acctnum}{"lastcheck"} < (time - $warn)) || $used > $warnpercent ) { $answer .= $acctnum.": "; if($last_sample{$acctnum}{"lastcheck"} < (time - $critical) ) { $state = 'CRITICAL'; $answer .= "OLD ".(time - $last_sample{$acctnum}{"lastcheck"})." "; } elsif( $last_sample{$acctnum}{"lastcheck"} < (time - $warn)) { $state = 'WARNING'; $answer .= "old ".(time - $last_sample{$acctnum}{"lastcheck"})." "; } if( $used > $critpercent ) { $state = 'CRITICAL'; $answer .= "SIZE ".$used."% "; } elsif($used > $warnpercent) { $state = 'WARNING'; $answer .= "size ".$used."% "; } $answer .= "; "; } else { $answer .= $acctnum.": ok ".(time - $last_sample{$acctnum}{"lastcheck"})." ".$used."% ; "; } } close(ACCT); if (!open (OUT, ">$stateful")) { print "ERROR: could not open state file $statefile\n"; exit $ERRORS{"UNKNOWN"}; } foreach my $k (sort keys %last_sample) { print OUT "$k ".$last_sample{$k}{"marker"}." ".$last_sample{$k}{"lastcheck"}."\n"; } close (OUT); print ("$state: $answer"); exit $ERRORS{$state};