Benjamin Renard commited on 2015-11-12 14:17:05
Showing 1 changed files, with 181 additions and 0 deletions.
| ... | ... |
@@ -0,0 +1,181 @@ |
| 1 |
+#!/usr/bin/perl |
|
| 2 |
+# |
|
| 3 |
+# check_backuppc_du: a Nagios plugin to check the compressed disk usage of BackupPC hosts |
|
| 4 |
+# |
|
| 5 |
+# Tested against BackupPC 3.2.1 and Nagios 3 |
|
| 6 |
+# <http://backuppc.sourceforge.net> |
|
| 7 |
+# <http://nagios.org> |
|
| 8 |
+# |
|
| 9 |
+# AUTHORS |
|
| 10 |
+# Benjamin Renard <brenard@easter-eggs.com> |
|
| 11 |
+# Emmanuel Lacour <elacour@easter-eggs.com> |
|
| 12 |
+# |
|
| 13 |
+# Fork from check_backuppc 1.1.0 write by Seneca Cunningham |
|
| 14 |
+# <tetragon@users.sourceforge.net>. |
|
| 15 |
+# |
|
| 16 |
+# COPYRIGHT |
|
| 17 |
+# Copyright (C) 2014 Easter-eggs |
|
| 18 |
+# |
|
| 19 |
+# This program is free software; you can redistribute it and/or modify |
|
| 20 |
+# it under the terms of the GNU General Public License as published by |
|
| 21 |
+# the Free Software Foundation; either version 2 of the License, or |
|
| 22 |
+# (at your option) any later version. |
|
| 23 |
+# |
|
| 24 |
+# This program is distributed in the hope that it will be useful, |
|
| 25 |
+# but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
| 26 |
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|
| 27 |
+# GNU General Public License for more details. |
|
| 28 |
+# |
|
| 29 |
+# You should have received a copy of the GNU General Public License |
|
| 30 |
+# along with this program; if not, write to the Free Software |
|
| 31 |
+# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
|
| 32 |
+# |
|
| 33 |
+ |
|
| 34 |
+use strict; |
|
| 35 |
+no utf8; |
|
| 36 |
+ |
|
| 37 |
+# Nagios |
|
| 38 |
+use lib "/usr/lib/nagios/plugins"; |
|
| 39 |
+use utils qw(%ERRORS $TIMEOUT); |
|
| 40 |
+use POSIX qw(strftime difftime); |
|
| 41 |
+use Getopt::Long; |
|
| 42 |
+Getopt::Long::Configure('bundling');
|
|
| 43 |
+ |
|
| 44 |
+# BackupPC |
|
| 45 |
+use lib "/usr/share/backuppc/lib"; |
|
| 46 |
+use BackupPC::Lib; |
|
| 47 |
+ |
|
| 48 |
+my $version = '1.0'; |
|
| 49 |
+# Default quota (GB) |
|
| 50 |
+my $quota = 100; |
|
| 51 |
+my $opt_V = 0; |
|
| 52 |
+my $opt_h = 0; |
|
| 53 |
+my $host; |
|
| 54 |
+my @hostlist; |
|
| 55 |
+my %Status; |
|
| 56 |
+my $statusCode = 'OK'; |
|
| 57 |
+ |
|
| 58 |
+ |
|
| 59 |
+# Process options |
|
| 60 |
+my $goodOpt = GetOptions( |
|
| 61 |
+ 'q=f' => \$quota, 'quota=f' => \$quota, |
|
| 62 |
+ 'V' => \$opt_V, 'version' => \$opt_V, |
|
| 63 |
+ 'h' => \$opt_h, 'help' => \$opt_h, |
|
| 64 |
+ 'H=s' => \$host, 'hostname=s' => \$host, |
|
| 65 |
+ 'L=s' => \@hostlist, 'hostlist=s' => \@hostlist, |
|
| 66 |
+ ); |
|
| 67 |
+ |
|
| 68 |
+if ($opt_V) |
|
| 69 |
+{
|
|
| 70 |
+ print "check_backuppc_du - " . $version . "\n"; |
|
| 71 |
+ exit $ERRORS{'OK'};
|
|
| 72 |
+} |
|
| 73 |
+if ($opt_h or not $goodOpt) |
|
| 74 |
+{
|
|
| 75 |
+ print "check_backuppc_du - " . $version . "\n"; |
|
| 76 |
+ print "A Nagios plugin to check on BackupPC backup status.\n\n"; |
|
| 77 |
+ print "Options:\n"; |
|
| 78 |
+ print " --hostname,-H perl regexp to match hostnames\n"; |
|
| 79 |
+ print " --quota,-q quota size (GB)\n"; |
|
| 80 |
+ print " --version,-V display plugin version\n"; |
|
| 81 |
+ print " --help,-h display this message\n\n"; |
|
| 82 |
+ exit $ERRORS{'OK'} if $goodOpt;
|
|
| 83 |
+ exit $ERRORS{'UNKNOWN'};
|
|
| 84 |
+} |
|
| 85 |
+ |
|
| 86 |
+# Connect to BackupPC |
|
| 87 |
+my $server; |
|
| 88 |
+if (!($server = BackupPC::Lib->new)) |
|
| 89 |
+{
|
|
| 90 |
+ print "BACKUPPC CRITICAL - Couldn't connect to BackupPC\n"; |
|
| 91 |
+ exit $ERRORS{'CRITICAL'};
|
|
| 92 |
+} |
|
| 93 |
+my %Conf = $server->Conf(); |
|
| 94 |
+ |
|
| 95 |
+$server->ChildInit(); |
|
| 96 |
+ |
|
| 97 |
+my $err = $server->ServerConnect($Conf{ServerHost}, $Conf{ServerPort});
|
|
| 98 |
+if ($err) |
|
| 99 |
+{
|
|
| 100 |
+ print("BACKUPPC UNKNOWN - Can't connect to server ($err)\n");
|
|
| 101 |
+ exit $ERRORS{'UNKNOWN'};
|
|
| 102 |
+} |
|
| 103 |
+ |
|
| 104 |
+# query the BackupPC server for host status |
|
| 105 |
+my $status_raw = $server->ServerMesg('status hosts');
|
|
| 106 |
+my $hosts_infos = $server->HostInfoRead(); |
|
| 107 |
+ |
|
| 108 |
+# undump the output... BackupPC uses Data::Dumper |
|
| 109 |
+eval $status_raw; |
|
| 110 |
+ |
|
| 111 |
+# Allow hostname separeted by comma |
|
| 112 |
+@hostlist = split(/,/,join(',',@hostlist));
|
|
| 113 |
+ |
|
| 114 |
+my $per_host_detail; |
|
| 115 |
+my $total_usage = 0; |
|
| 116 |
+my $total_count = 0; |
|
| 117 |
+my $host_count = 0; |
|
| 118 |
+foreach my $hostname ( sort(keys %Status) ) {
|
|
| 119 |
+ |
|
| 120 |
+ next if $hostname =~ /^\s*$/; |
|
| 121 |
+ next if $hostname =~ /^ /; |
|
| 122 |
+ next if ( $host && $hostname !~ m/$host/ ); |
|
| 123 |
+ next if ( scalar(@hostlist) > 0 && not ($hostname ~~ @hostlist) ); |
|
| 124 |
+ |
|
| 125 |
+ my %HostStatus = %{$Status{$hostname}};
|
|
| 126 |
+ my %host_conf = %{$server->ConfigDataRead($hostname)};
|
|
| 127 |
+ $HostStatus{BackupsDisable} = $host_conf{BackupsDisable};
|
|
| 128 |
+ next if ( $HostStatus{BackupsDisable} );
|
|
| 129 |
+ next if ($HostStatus{'type'} eq 'archive');
|
|
| 130 |
+ |
|
| 131 |
+ $host_count++; |
|
| 132 |
+ |
|
| 133 |
+ # Backups |
|
| 134 |
+ my @Backups = $server->BackupInfoRead($hostname); |
|
| 135 |
+ |
|
| 136 |
+ # Get aggregate of compressed used size (octets) |
|
| 137 |
+ my $full_size = 0; |
|
| 138 |
+ my $backups_count = 0; |
|
| 139 |
+ foreach my $Backup (sort {$Backups[$a]->{num} cmp $Backups[$b]->{num}} @Backups) {
|
|
| 140 |
+ if ( $full_size == 0 ) {
|
|
| 141 |
+ $full_size += $Backup->{sizeExistComp};
|
|
| 142 |
+ $full_size += $Backup->{sizeNewComp};
|
|
| 143 |
+ } else {
|
|
| 144 |
+ $full_size += $Backup->{sizeNewComp};
|
|
| 145 |
+ } |
|
| 146 |
+ $backups_count++; |
|
| 147 |
+ } |
|
| 148 |
+ |
|
| 149 |
+ $total_usage += $full_size; |
|
| 150 |
+ $total_count += $backups_count; |
|
| 151 |
+ |
|
| 152 |
+ # Convert to Go |
|
| 153 |
+ $full_size = sprintf("%0.2f", $full_size / 1024 / 1024 / 1024);
|
|
| 154 |
+ |
|
| 155 |
+ # Output size |
|
| 156 |
+ $per_host_detail .= "$hostname: $backups_count backups / $full_size Go\n"; |
|
| 157 |
+} |
|
| 158 |
+ |
|
| 159 |
+unless ( $host_count ) {
|
|
| 160 |
+ print "BACKUPPC UNKNOWN - no host matching $host\n"; |
|
| 161 |
+ exit $ERRORS{'UNKNOWN'};
|
|
| 162 |
+} |
|
| 163 |
+ |
|
| 164 |
+$total_usage = sprintf("%0.2f", $total_usage / 1024 / 1024 / 1024);
|
|
| 165 |
+ |
|
| 166 |
+if ( $total_usage >= $quota ) {
|
|
| 167 |
+ print "BACKUPPC CRITICAL - $total_usage Go used / $quota Go allocated\n"; |
|
| 168 |
+ $statusCode = 'CRITICAL'; |
|
| 169 |
+} elsif ( $total_usage >= $quota * 0.90 ) {
|
|
| 170 |
+ print "BACKUPPC WARNING - $total_usage Go used / $quota Go allocated\n"; |
|
| 171 |
+ $statusCode = 'WARNING'; |
|
| 172 |
+} else {
|
|
| 173 |
+ print "BACKUPPC OK - $total_usage Go used / $quota Go allocated\n"; |
|
| 174 |
+} |
|
| 175 |
+ |
|
| 176 |
+print "Total of $total_count backups with cumulative compressed size of $total_usage Go for $host_count active hosts\n"; |
|
| 177 |
+print $per_host_detail; |
|
| 178 |
+ |
|
| 179 |
+ |
|
| 180 |
+exit $ERRORS{$statusCode};
|
|
| 181 |
+ |
|
| 0 | 182 |