2015-11-12 15:17:05 +01:00
|
|
|
#!/usr/bin/perl
|
|
|
|
#
|
|
|
|
# check_backuppc_du: a Nagios plugin to check the compressed disk usage of BackupPC hosts
|
|
|
|
#
|
2018-02-17 18:17:21 +01:00
|
|
|
# Tested against BackupPC 3 (3.2.1) and 4 (4.1.5) and Icinga (1.11.6)
|
|
|
|
# <https://backuppc.github.io/backuppc/>
|
|
|
|
# <https://www.icinga.com>
|
2015-11-12 15:17:05 +01:00
|
|
|
#
|
|
|
|
# AUTHORS
|
|
|
|
# Benjamin Renard <brenard@easter-eggs.com>
|
|
|
|
# Emmanuel Lacour <elacour@easter-eggs.com>
|
|
|
|
#
|
|
|
|
# COPYRIGHT
|
2018-02-17 18:17:21 +01:00
|
|
|
# Copyright (C) 2018 Easter-eggs
|
2015-11-12 15:17:05 +01:00
|
|
|
#
|
|
|
|
# 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
|
|
|
|
#
|
|
|
|
|
|
|
|
use strict;
|
|
|
|
no utf8;
|
|
|
|
|
|
|
|
# Nagios
|
|
|
|
use lib "/usr/lib/nagios/plugins";
|
|
|
|
use utils qw(%ERRORS $TIMEOUT);
|
|
|
|
use POSIX qw(strftime difftime);
|
|
|
|
use Getopt::Long;
|
|
|
|
Getopt::Long::Configure('bundling');
|
|
|
|
|
|
|
|
# BackupPC
|
|
|
|
use lib "/usr/share/backuppc/lib";
|
|
|
|
use BackupPC::Lib;
|
|
|
|
|
|
|
|
my $version = '1.0';
|
|
|
|
# Default quota (GB)
|
|
|
|
my $quota = 100;
|
|
|
|
my $opt_V = 0;
|
|
|
|
my $opt_h = 0;
|
|
|
|
my $host;
|
|
|
|
my @hostlist;
|
|
|
|
my %Status;
|
|
|
|
my $statusCode = 'OK';
|
|
|
|
|
|
|
|
|
|
|
|
# Process options
|
|
|
|
my $goodOpt = GetOptions(
|
2024-10-07 11:17:46 +02:00
|
|
|
'q=f' => \$quota, 'quota=f' => \$quota,
|
|
|
|
'V' => \$opt_V, 'version' => \$opt_V,
|
|
|
|
'h' => \$opt_h, 'help' => \$opt_h,
|
|
|
|
'H=s' => \$host, 'hostname=s' => \$host,
|
|
|
|
'L=s' => \@hostlist, 'hostlist=s' => \@hostlist,
|
|
|
|
);
|
2015-11-12 15:17:05 +01:00
|
|
|
|
|
|
|
if ($opt_V)
|
|
|
|
{
|
2024-10-07 11:17:46 +02:00
|
|
|
print "check_backuppc_du - " . $version . "\n";
|
|
|
|
exit $ERRORS{'OK'};
|
2015-11-12 15:17:05 +01:00
|
|
|
}
|
|
|
|
if ($opt_h or not $goodOpt)
|
|
|
|
{
|
2024-10-07 11:17:46 +02:00
|
|
|
print "check_backuppc_du - " . $version . "\n";
|
|
|
|
print "A Nagios plugin to check on BackupPC backup status.\n\n";
|
|
|
|
print "Options:\n";
|
|
|
|
print " --hostname,-H perl regexp to match hostnames\n";
|
|
|
|
print " --quota,-q quota size (GB)\n";
|
|
|
|
print " --version,-V display plugin version\n";
|
|
|
|
print " --help,-h display this message\n\n";
|
|
|
|
exit $ERRORS{'OK'} if $goodOpt;
|
|
|
|
exit $ERRORS{'UNKNOWN'};
|
2015-11-12 15:17:05 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
# Connect to BackupPC
|
|
|
|
my $server;
|
|
|
|
if (!($server = BackupPC::Lib->new))
|
|
|
|
{
|
2024-10-07 11:17:46 +02:00
|
|
|
print "BACKUPPC CRITICAL - Couldn't connect to BackupPC\n";
|
|
|
|
exit $ERRORS{'CRITICAL'};
|
2015-11-12 15:17:05 +01:00
|
|
|
}
|
|
|
|
my %Conf = $server->Conf();
|
|
|
|
|
|
|
|
$server->ChildInit();
|
|
|
|
|
|
|
|
my $err = $server->ServerConnect($Conf{ServerHost}, $Conf{ServerPort});
|
|
|
|
if ($err)
|
|
|
|
{
|
2024-10-07 11:17:46 +02:00
|
|
|
print("BACKUPPC UNKNOWN - Can't connect to server ($err)\n");
|
|
|
|
exit $ERRORS{'UNKNOWN'};
|
2015-11-12 15:17:05 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
# query the BackupPC server for host status
|
|
|
|
my $status_raw = $server->ServerMesg('status hosts');
|
|
|
|
my $hosts_infos = $server->HostInfoRead();
|
|
|
|
|
|
|
|
# undump the output... BackupPC uses Data::Dumper
|
|
|
|
eval $status_raw;
|
|
|
|
|
|
|
|
# Allow hostname separeted by comma
|
|
|
|
@hostlist = split(/,/,join(',',@hostlist));
|
|
|
|
|
|
|
|
my $per_host_detail;
|
|
|
|
my $total_usage = 0;
|
|
|
|
my $total_count = 0;
|
|
|
|
my $host_count = 0;
|
|
|
|
foreach my $hostname ( sort(keys %Status) ) {
|
|
|
|
|
2024-10-07 11:17:46 +02:00
|
|
|
next if $hostname =~ /^\s*$/;
|
|
|
|
next if $hostname =~ /^ /;
|
|
|
|
next if ( $host && $hostname !~ m/$host/ );
|
|
|
|
next if ( scalar(@hostlist) > 0 && not(grep /^\Q$hostname$/, @hostlist) );
|
|
|
|
|
|
|
|
my %HostStatus = %{$Status{$hostname}};
|
|
|
|
my %host_conf = %{$server->ConfigDataRead($hostname)};
|
|
|
|
$HostStatus{BackupsDisable} = $host_conf{BackupsDisable};
|
|
|
|
next if ( $HostStatus{BackupsDisable} eq 2);
|
|
|
|
next if ($HostStatus{'type'} eq 'archive');
|
|
|
|
|
|
|
|
$host_count++;
|
|
|
|
|
|
|
|
# Backups
|
|
|
|
my @Backups = $server->BackupInfoRead($hostname);
|
|
|
|
|
|
|
|
# Get aggregate of compressed used size (octets)
|
|
|
|
my $full_size = 0;
|
|
|
|
my $backups_count = 0;
|
|
|
|
foreach my $Backup (sort {$a->{num} cmp $b->{num}} @Backups) {
|
|
|
|
if ( $full_size == 0 ) {
|
|
|
|
$full_size += $Backup->{sizeExistComp};
|
|
|
|
$full_size += $Backup->{sizeNewComp};
|
|
|
|
} else {
|
|
|
|
$full_size += $Backup->{sizeNewComp};
|
|
|
|
}
|
|
|
|
$backups_count++;
|
|
|
|
}
|
|
|
|
|
|
|
|
$total_usage += $full_size;
|
|
|
|
$total_count += $backups_count;
|
|
|
|
|
|
|
|
# Convert to Go
|
|
|
|
$full_size = sprintf("%0.2f", $full_size / 1024 / 1024 / 1024);
|
|
|
|
|
|
|
|
# Output size
|
|
|
|
$per_host_detail .= "$hostname: $backups_count backups / $full_size Go\n";
|
2015-11-12 15:17:05 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
unless ( $host_count ) {
|
2024-10-07 11:17:46 +02:00
|
|
|
print "BACKUPPC UNKNOWN - no host matching $host\n";
|
|
|
|
exit $ERRORS{'UNKNOWN'};
|
2015-11-12 15:17:05 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
$total_usage = sprintf("%0.2f", $total_usage / 1024 / 1024 / 1024);
|
|
|
|
|
|
|
|
if ( $total_usage >= $quota ) {
|
2024-10-07 11:17:46 +02:00
|
|
|
print "BACKUPPC CRITICAL - $total_usage Go used / $quota Go allocated\n";
|
|
|
|
$statusCode = 'CRITICAL';
|
2015-11-12 15:17:05 +01:00
|
|
|
} elsif ( $total_usage >= $quota * 0.90 ) {
|
2024-10-07 11:17:46 +02:00
|
|
|
print "BACKUPPC WARNING - $total_usage Go used / $quota Go allocated\n";
|
|
|
|
$statusCode = 'WARNING';
|
2015-11-12 15:17:05 +01:00
|
|
|
} else {
|
2024-10-07 11:17:46 +02:00
|
|
|
print "BACKUPPC OK - $total_usage Go used / $quota Go allocated\n";
|
2015-11-12 15:17:05 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
print "Total of $total_count backups with cumulative compressed size of $total_usage Go for $host_count active hosts\n";
|
|
|
|
print $per_host_detail;
|
|
|
|
|
|
|
|
|
|
|
|
exit $ERRORS{$statusCode};
|
2024-10-07 11:17:46 +02:00
|
|
|
# vim:set sw=4 ts=4 sts=4 ft=perl expandtab:
|