Benjamin Renard commited on 2013-08-20 14:15:39
Showing 1 changed files, with 210 additions and 0 deletions.
... | ... |
@@ -0,0 +1,210 @@ |
1 |
+#!/usr/bin/perl |
|
2 |
+# |
|
3 |
+# check_backuppc: a Nagios plugin to check the status of BackupPC |
|
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 |
+# |
|
12 |
+# Fork from check_backuppc 1.1.0 write by Seneca Cunningham |
|
13 |
+# <tetragon@users.sourceforge.net>. |
|
14 |
+# |
|
15 |
+# COPYRIGHT |
|
16 |
+# Copyright (C) 2013 Easter-eggs |
|
17 |
+# |
|
18 |
+# This program is free software; you can redistribute it and/or modify |
|
19 |
+# it under the terms of the GNU General Public License as published by |
|
20 |
+# the Free Software Foundation; either version 2 of the License, or |
|
21 |
+# (at your option) any later version. |
|
22 |
+# |
|
23 |
+# This program is distributed in the hope that it will be useful, |
|
24 |
+# but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
25 |
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|
26 |
+# GNU General Public License for more details. |
|
27 |
+# |
|
28 |
+# You should have received a copy of the GNU General Public License |
|
29 |
+# along with this program; if not, write to the Free Software |
|
30 |
+# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
|
31 |
+# |
|
32 |
+ |
|
33 |
+use strict; |
|
34 |
+no utf8; |
|
35 |
+ |
|
36 |
+# Nagios |
|
37 |
+use lib "/usr/lib/nagios/plugins"; |
|
38 |
+use utils qw(%ERRORS $TIMEOUT); |
|
39 |
+use POSIX qw(strftime difftime); |
|
40 |
+use Getopt::Long; |
|
41 |
+Getopt::Long::Configure('bundling'); |
|
42 |
+ |
|
43 |
+# BackupPC |
|
44 |
+use lib "/usr/share/backuppc/lib"; |
|
45 |
+use BackupPC::Lib; |
|
46 |
+ |
|
47 |
+my $version = '1.1.1'; |
|
48 |
+my $warnDaysOld = 2; |
|
49 |
+my $critDaysOld = 7; |
|
50 |
+my $verbose = 0; |
|
51 |
+my $opt_V = 0; |
|
52 |
+my $opt_h = 0; |
|
53 |
+my $goodOpt = 0; |
|
54 |
+my @ownerOnly; |
|
55 |
+my @hostsDesired; |
|
56 |
+my @hostsExcluded; |
|
57 |
+ |
|
58 |
+# Process options |
|
59 |
+$goodOpt = GetOptions( |
|
60 |
+ 'v+' => \$verbose, 'verbose+' => \$verbose, |
|
61 |
+ 'c=f' => \$critDaysOld, 'critical=f' => \$critDaysOld, |
|
62 |
+ 'w=f' => \$warnDaysOld, 'warning=f' => \$warnDaysOld, |
|
63 |
+ 'o=s' => \@ownerOnly, 'owner=s' => \@ownerOnly, |
|
64 |
+ 'V' => \$opt_V, 'version' => \$opt_V, |
|
65 |
+ 'h' => \$opt_h, 'help' => \$opt_h, |
|
66 |
+ 'H=s' => \@hostsDesired, 'hostname=s' => \@hostsDesired, |
|
67 |
+ 'x=s' => \@hostsExcluded, 'exclude=s' => \@hostsExcluded); |
|
68 |
+ |
|
69 |
+@hostsDesired = () if $#hostsDesired < 0; |
|
70 |
+@hostsExcluded = () if $#hostsExcluded < 0; |
|
71 |
+ |
|
72 |
+if ($opt_V) |
|
73 |
+{ |
|
74 |
+ print "check_backuppc - " . $version . "\n"; |
|
75 |
+ exit $ERRORS{'OK'}; |
|
76 |
+} |
|
77 |
+if ($opt_h or not $goodOpt) |
|
78 |
+{ |
|
79 |
+ print "check_backuppc - " . $version . "\n"; |
|
80 |
+ print "A Nagios plugin to check on BackupPC backup status.\n\n"; |
|
81 |
+ print "Options:\n"; |
|
82 |
+ print " --hostname,-H only check the specified host\n"; |
|
83 |
+ print " --exclude,-x do not check the specified host\n"; |
|
84 |
+ print " --owner,-o do only hosts of specified user\n"; |
|
85 |
+ print " --warning,-w days old of last good backup to cause a warning\n"; |
|
86 |
+ print " --critical,-c days old of last good backup to be critical\n"; |
|
87 |
+ print " --verbose,-v increase verbosity\n"; |
|
88 |
+ print " --version,-V display plugin version\n"; |
|
89 |
+ print " --help,-h display this message\n\n"; |
|
90 |
+ exit $ERRORS{'OK'} if $goodOpt; |
|
91 |
+ exit $ERRORS{'UNKNOWN'}; |
|
92 |
+} |
|
93 |
+if ($warnDaysOld > $critDaysOld) |
|
94 |
+{ |
|
95 |
+ print("BACKUPPC UNKNOWN - Warning threshold must be <= critical\n"); |
|
96 |
+ exit $ERRORS{'UNKNOWN'}; |
|
97 |
+} |
|
98 |
+ |
|
99 |
+# Connect to BackupPC |
|
100 |
+my $server; |
|
101 |
+if (!($server = BackupPC::Lib->new)) |
|
102 |
+{ |
|
103 |
+ print "BACKUPPC CRITICAL - Couldn't connect to BackupPC\n"; |
|
104 |
+ exit $ERRORS{'CRITICAL'}; |
|
105 |
+} |
|
106 |
+my %Conf = $server->Conf(); |
|
107 |
+ |
|
108 |
+$server->ChildInit(); |
|
109 |
+ |
|
110 |
+my $err = $server->ServerConnect($Conf{ServerHost}, $Conf{ServerPort}); |
|
111 |
+if ($err) |
|
112 |
+{ |
|
113 |
+ print("BACKUPPC UNKNOWN - Can't connect to server ($err)\n"); |
|
114 |
+ exit $ERRORS{'UNKNOWN'}; |
|
115 |
+} |
|
116 |
+ |
|
117 |
+my %Status; |
|
118 |
+ |
|
119 |
+# query the BackupPC server for host status |
|
120 |
+my $status_raw = $server->ServerMesg('status hosts'); |
|
121 |
+my $hosts_infos = $server->HostInfoRead(); |
|
122 |
+ |
|
123 |
+# undump the output... BackupPC uses Data::Dumper |
|
124 |
+eval $status_raw; |
|
125 |
+ |
|
126 |
+# check the dumped output |
|
127 |
+my $hostCount = 0; |
|
128 |
+my $errorLevel='OK'; |
|
129 |
+ |
|
130 |
+foreach my $host (@hostsDesired, @hostsExcluded) |
|
131 |
+{ |
|
132 |
+ if (not grep {/$host/} keys(%Status)) |
|
133 |
+ { |
|
134 |
+ print("BACKUPPC UNKNOWN - Unknown host ($host)\n"); |
|
135 |
+ exit $ERRORS{'UNKNOWN'}; |
|
136 |
+ } |
|
137 |
+} |
|
138 |
+ |
|
139 |
+my @problems; |
|
140 |
+ |
|
141 |
+# host status checks |
|
142 |
+foreach my $host (sort(keys(%Status))) |
|
143 |
+{ |
|
144 |
+ next if $host =~ /^ /; |
|
145 |
+ my $owner = $hosts_infos->{$host}->{user}; |
|
146 |
+ next if (@ownerOnly and not grep {/$owner/} @ownerOnly); |
|
147 |
+ my %host_conf = %{$server->ConfigDataRead($host)}; |
|
148 |
+ next if ( $host_conf{BackupsDisable} ); |
|
149 |
+ next if (@hostsDesired and not grep {/$host/} @hostsDesired); |
|
150 |
+ next if (@hostsExcluded and grep {/$host/} @hostsExcluded); |
|
151 |
+ next if ($Status{$host}{'type'} eq 'archive'); |
|
152 |
+ $hostCount++; |
|
153 |
+ # Debug |
|
154 |
+ if ($verbose == 2) |
|
155 |
+ { |
|
156 |
+ print "Host $host state " . $Status{$host}{'state'} . "\n"; |
|
157 |
+ print " with reason: " . $Status{$host}{'reason'} . "\n"; |
|
158 |
+ print " with error: " . $Status{$host}{'error'} . "\n"; |
|
159 |
+ print " with owner: $owner\n\n"; |
|
160 |
+ } |
|
161 |
+ # Check host error |
|
162 |
+ if ($Status{$host}{'error'}) |
|
163 |
+ { |
|
164 |
+ # Check connectivity errors with greater care |
|
165 |
+ if ($Status{$host}{'error'} ne 'ping too slow' && |
|
166 |
+ $Status{$host}{'error'} ne 'no ping response' && |
|
167 |
+ $Status{$host}{'error'} ne 'no ping response' && |
|
168 |
+ $Status{$host}{'error'} ne 'host not found' && |
|
169 |
+ $Status{$host}{'reason'} !~ /Reason_restore_failed/) { |
|
170 |
+ push @problems, "$host error : ".$Status{$host}{'error'}." / ".$Status{$host}{'reason'}; |
|
171 |
+ next; |
|
172 |
+ } |
|
173 |
+ } |
|
174 |
+ # Check last good backup time |
|
175 |
+ my $difftime=difftime(time(), $Status{$host}{'lastGoodBackupTime'}); |
|
176 |
+ my $diffdays=$difftime/(3600 * 24); |
|
177 |
+ $Status{$host}{'lastGoodBackupTime'} = $Status{$host}{'startTime'} if (not $Status{$host}{'lastGoodBackupTime'}); |
|
178 |
+ if ($difftime > ($critDaysOld * 3600 * 24)) |
|
179 |
+ { |
|
180 |
+ push @problems, "$host : last good backup have ".sprintf("%.1f",$diffdays)." days"; |
|
181 |
+ $errorLevel='CRITICAL'; |
|
182 |
+ } |
|
183 |
+ elsif ($difftime > ($warnDaysOld * 3600 * 24)) |
|
184 |
+ { |
|
185 |
+ push @problems, "$host : last good backup have ".sprintf("%.1f",$diffdays)." days"; |
|
186 |
+ $errorLevel='WARNING' if ($errorLevel eq 'OK'); |
|
187 |
+ } |
|
188 |
+} |
|
189 |
+ |
|
190 |
+my $problemTxt=""; |
|
191 |
+if (scalar(@problems) > 0) { |
|
192 |
+ if ($verbose > 0) { |
|
193 |
+ foreach my $pbl (@problems) { |
|
194 |
+ if ($problemTxt ne "") { |
|
195 |
+ $problemTxt.=" , "; |
|
196 |
+ } |
|
197 |
+ else { |
|
198 |
+ $problemTxt=" ( "; |
|
199 |
+ } |
|
200 |
+ $problemTxt.=$pbl; |
|
201 |
+ } |
|
202 |
+ $problemTxt.=" )"; |
|
203 |
+ } |
|
204 |
+ else { |
|
205 |
+ $problemTxt=" (".scalar(@problems)." problems)"; |
|
206 |
+ } |
|
207 |
+} |
|
208 |
+ |
|
209 |
+print "BACKUPPC $errorLevel$problemTxt\n"; |
|
210 |
+exit $ERRORS{$errorLevel}; |
|
0 | 211 |