diff options
author | Holger Weiss <holger@zedat.fu-berlin.de> | 2013-09-29 22:03:24 (GMT) |
---|---|---|
committer | Holger Weiss <holger@zedat.fu-berlin.de> | 2013-09-29 22:03:24 (GMT) |
commit | 0b6423f9c99d9edf8c96fefd0f6c453859395aa1 (patch) | |
tree | 1c2b6b21704a294940f87c7892676998d8371707 /web/attachments/181129-check_rsync | |
download | site-0b6423f9c99d9edf8c96fefd0f6c453859395aa1.tar.gz |
Import Nagios Plugins site
Import the Nagios Plugins web site, Cronjobs, infrastructure scripts,
and configuration files.
Diffstat (limited to 'web/attachments/181129-check_rsync')
-rw-r--r-- | web/attachments/181129-check_rsync | 146 |
1 files changed, 146 insertions, 0 deletions
diff --git a/web/attachments/181129-check_rsync b/web/attachments/181129-check_rsync new file mode 100644 index 0000000..e7537ee --- /dev/null +++ b/web/attachments/181129-check_rsync | |||
@@ -0,0 +1,146 @@ | |||
1 | #! /usr/bin/perl -w | ||
2 | # | ||
3 | # check_rsync - Check Rsync and modules availability | ||
4 | # | ||
5 | # Copyright (C) 2006 Thomas Guyot-Sionnest <tguyot@gmail.com> | ||
6 | # | ||
7 | # This program is free software; you can redistribute it and/or | ||
8 | # modify it under the terms of the GNU General Public License | ||
9 | # as published by the Free Software Foundation; either version 2 | ||
10 | # of the License, or (at your option) any later version. | ||
11 | # | ||
12 | # This program is distributed in the hope that it will be useful, | ||
13 | # but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
14 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
15 | # GNU General Public License for more details. | ||
16 | # | ||
17 | # You should have received a copy of the GNU General Public License | ||
18 | # along with this program; if not, write to the Free Software | ||
19 | # Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. | ||
20 | # | ||
21 | |||
22 | use POSIX; | ||
23 | use strict; | ||
24 | use Getopt::Long; | ||
25 | |||
26 | # Make sure this module is installed or IPC::Cmd::run won't work as expected. | ||
27 | require IPC::Run; | ||
28 | |||
29 | use IPC::Cmd qw[run]; | ||
30 | use vars qw($opt_H $opt_p $opt_m); | ||
31 | use vars qw($PROGNAME); | ||
32 | use lib "/usr/local/nagios/libexec"; | ||
33 | use utils qw($TIMEOUT %ERRORS); | ||
34 | |||
35 | $PROGNAME = "check_rsync"; | ||
36 | $ENV{'PATH'}=''; | ||
37 | $ENV{'BASH_ENV'}=''; | ||
38 | $ENV{'ENV'}=''; | ||
39 | |||
40 | Getopt::Long::Configure('bundling'); | ||
41 | GetOptions ( | ||
42 | "H=s" => \$opt_H, "hostname=s" => \$opt_H, | ||
43 | "p=s" => \$opt_p, "port=s" => \$opt_p, | ||
44 | "m=s@" => \$opt_m, "module=s@" => \$opt_m ); | ||
45 | |||
46 | unless (defined($opt_H)){ | ||
47 | print "Usage: $PROGNAME -H <host> [-p <port>] [-m <module>[,<user>,<password>] [-m <module>[,<user>,<password>]...]]\n"; | ||
48 | exit $ERRORS{'UNKNOWN'}; | ||
49 | } | ||
50 | |||
51 | my $host = $opt_H; | ||
52 | my $port = defined($opt_p) ? $opt_p : 873; | ||
53 | |||
54 | # Create an array for each -m arguments and store them in @modules | ||
55 | my @modules; | ||
56 | if (defined($opt_m)) { | ||
57 | for(@$opt_m) { | ||
58 | push @modules, [ split(/,/) ]; | ||
59 | } | ||
60 | } | ||
61 | |||
62 | # Just in case of problems, let's not hang Nagios | ||
63 | $SIG{'ALRM'} = sub { | ||
64 | print "CRITICAL: Rsync timed out\n"; | ||
65 | exit $ERRORS{"CRITICAL"}; | ||
66 | }; | ||
67 | |||
68 | # Rsync arguments | ||
69 | my $source = "rsync://$host"; | ||
70 | |||
71 | alarm($TIMEOUT); | ||
72 | |||
73 | # Get a list of modules to see if rsync is up | ||
74 | my $command = "/usr/bin/rsync --port=$port $source"; | ||
75 | my (undef, $error_code, undef, $stdout_buf, $stderr_buf ) = | ||
76 | run( command => $command, verbose => 0 ); | ||
77 | |||
78 | #Turn off alarm | ||
79 | alarm(0); | ||
80 | |||
81 | my $realerr = $error_code >> 8; | ||
82 | report_error("Rsync command $command failed with error " . $realerr) if ($realerr != 0); | ||
83 | |||
84 | # If one or more -m, check if these modules exists first... | ||
85 | if (@modules) { | ||
86 | |||
87 | # IPC::Run have the bad habbit of randomly concatenating multiple lines | ||
88 | # in the same array element. | ||
89 | my @result; | ||
90 | for (@$stdout_buf) { | ||
91 | push(@result, split(/\n/)); | ||
92 | } | ||
93 | |||
94 | foreach my $mod (@modules) { | ||
95 | my $match = 0; | ||
96 | for (@result) { | ||
97 | $match = 1 if (/^$$mod[0]\s/); | ||
98 | } | ||
99 | report_error("Module $$mod[0] not found") if ($match == 0); | ||
100 | } | ||
101 | } else { # else just return OK | ||
102 | print "OK: Rsync is up\n"; | ||
103 | exit $ERRORS{'OK'}; | ||
104 | } | ||
105 | |||
106 | # Check each -m aruments... | ||
107 | for my $arg (@modules) { | ||
108 | if (defined($$arg[1]) and defined($$arg[2])) { | ||
109 | $source = "rsync://$$arg[1]" . '@' . "$host/$$arg[0]"; | ||
110 | $ENV{'RSYNC_PASSWORD'} = $$arg[2]; | ||
111 | } else { | ||
112 | $source = "rsync://$host/$$arg[0]"; | ||
113 | } | ||
114 | |||
115 | alarm($TIMEOUT); | ||
116 | |||
117 | # Get a file listing of the root of the module | ||
118 | undef $error_code; # Better safe than sorry... | ||
119 | $command = "/usr/bin/rsync --port=$port $source"; | ||
120 | (undef, $error_code, undef, $stdout_buf, $stderr_buf ) = | ||
121 | run( command => $command, verbose => 0 ); | ||
122 | |||
123 | #Turn off alarm | ||
124 | alarm(0); | ||
125 | |||
126 | $realerr = $error_code >> 8; | ||
127 | report_error("Rsync command failed on module $$arg[0] with error " . $realerr) if ($realerr != 0); | ||
128 | } | ||
129 | |||
130 | if (@modules > 0) { | ||
131 | print "OK: Rsync is up with ", scalar(@modules), " module tested\n" if (@modules == 1); | ||
132 | print "OK: Rsync is up with ", scalar(@modules), " modules tested\n" if (@modules > 1); | ||
133 | exit $ERRORS{'OK'}; | ||
134 | } else { # We hould never end up here :) | ||
135 | print "UNKNOWN: The unexpected occured (bug?)\n"; | ||
136 | exit $ERRORS{'UNKNOWN'}; | ||
137 | } | ||
138 | |||
139 | # Report error passed as one string, print rsync messages to STDERR | ||
140 | sub report_error { | ||
141 | my $report = shift; | ||
142 | print "CRITICAL: $report\n"; | ||
143 | print STDERR @$stderr_buf; | ||
144 | exit $ERRORS{'CRITICAL'}; | ||
145 | } | ||
146 | |||