Subversion Repositories DevTools

Rev

Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
5534 dpurdie 1
#! /usr/bin/perl
2
########################################################################
3
# Copyright (c) VIX TECHNOLOGY (AUST) LTD
4
#
5
# Module name   : quarantineNagios.pl
6
# Module type   : Nagios Plug in
7
# Compiler(s)   : Perl
8
# Environment(s): jats
9
#
10
# Description   : A Nagios plugin to interface to quarantine tool
11
#                 This is a simple stand alone Perl Program
12
#                 It does not require the JATS environment
13
#
14
# Usage         : See POD at the end of this file
15
#
16
#......................................................................#
17
 
18
 
19
require 5.008_002;
20
use strict;
21
use warnings;
22
use Pod::Usage;
23
use Getopt::Long;
24
use File::Basename;
25
use Data::Dumper;
26
use File::Spec::Functions;
27
use FindBin;                                # Determine the current directory
28
 
29
#
30
#   Globals
31
#
32
my $VERSION = "1.0.0";                      # Update this
33
my $opt_verbose = 1;
34
my $opt_statsFile;
35
my $opt_help = 0;
36
my @opt_data;
37
my @opt_text;
38
my @opt_warn;
39
my %stats;
40
my $exitMsg = "OK";
41
my @exitMsgTxt;
42
my @perfData;
43
my $setWarning;
44
my $setCritical;
45
 
46
#-------------------------------------------------------------------------------
47
# Function        : Main Entry
48
#
49
# Description     :
50
#
51
# Inputs          :
52
#
53
# Returns         :
54
#
55
my $result = GetOptions (
56
                "help:+"        => \$opt_help,          # flag, multiple use allowed
57
                "manual:3"      => \$opt_help,          # flag
58
                "verbose:+"     => \$opt_verbose,       # flag
59
                "statsFile=s"   => \$opt_statsFile,     # flag
60
                "data=s"        => sub{push @opt_data, split(/\s*,\s*/,$_[1])},          # Strings
61
                "text=s"        => sub{push @opt_text, split(/\s*,\s*/,$_[1])},          # Strings
62
                "warn=s"        => sub{push @opt_warn, split(/\s*,\s*/,$_[1])},          # Strings
63
                );
64
 
65
#
66
#   Process help and manual options
67
#
68
pod2usage(-verbose => 0, -message => "Version: $VERSION")  if ($opt_help == 1  || ! $result);
69
pod2usage(-verbose => 1)  if ($opt_help == 2 );
70
pod2usage(-verbose => 2)  if ($opt_help > 2);
71
 
72
#
73
#   Locate the statistics file
74
#   Its location is configied within the jats_quarantine
75
#
76
 
77
Error("No statistics file specified") unless defined($opt_statsFile);
78
Error("No statistics file: $opt_statsFile") unless -f $opt_statsFile;
79
 
80
#
81
# Read in the statistics file
82
#   Format is is key:data
83
# 
84
Warning('No Statistics available') unless -f $opt_statsFile;
85
open my $fh, $opt_statsFile || Error("Cannot read statistics");
86
while (<$fh>)
87
{
88
    m~(.*):(.*)~;
89
    $stats{lc($1)} = $2;
90
}
91
close $fh;
92
 
93
#
94
#   Determine the Nagios State. Will be CRITICAL if
95
#       state is NOT OK
96
#       timeStamp is more than 25 hours old
97
# 
98
unless (defined $stats{state} && $stats{state} eq 'OK')
99
{
100
    $setCritical = 1;
101
    $exitMsg = $stats{state} || 'Unknown state' ;
102
}
103
 
104
my $dataAge = time() - $stats{timestamp};
105
if ($dataAge > (25 * 60 * 60))
106
{
107
    $setCritical = 1;
108
    $exitMsg = 'Data too old(Secs):' . $dataAge;
109
}
110
 
111
#
112
#   Insert Text data - not perf data.
113
#   This will be displayed as a part of the output
114
#
115
foreach my $item (@opt_text)
116
{
117
    push @exitMsgTxt, $item . '=' . getDataItem($item); 
118
}
119
#
120
#   Insert the required performance data
121
#   
122
foreach my $item (@opt_data)
123
{
124
    push @perfData, $item . '=' . getDataItem($item); 
125
}
126
 
127
#
128
#   Process warning thresholds
129
#
130
foreach my $item (@opt_warn)
131
{
132
    my ($Name, $warn, $critical) = split(/:/, $item);
133
    my $name = lc $Name;
134
    my $isCritical;
135
    if (exists $stats{$name})
136
    {
137
        my $value = int($stats{$name});
138
        if (defined $critical)
139
        {
140
            if ($value > int($critical) )
141
            {
142
                $isCritical = 1;
143
                $setCritical = 1;
144
                push @exitMsgTxt, $Name . ' is Critical'; 
145
 
146
            }
147
        }
148
        if (! $isCritical)
149
        {
150
            if (defined $warn )
151
            {
152
                if ($value > int($warn) )
153
                {
154
                    $setWarning = 1;
155
                    push @exitMsgTxt, $Name . ' is Warning'; 
156
                }
157
            }
158
            else
159
            {
160
                push @exitMsgTxt, "$item bad format";
161
                $setWarning = 1; 
162
            }
163
        }
164
    }
165
    else
166
    {
167
        push @exitMsgTxt, "$Name not known";
168
        $setWarning = 1; 
169
    }
170
}
171
 
172
#
173
# Prepare the output
174
#   STATUS, Status Text, Performance Data
175
# 
176
print($exitMsg);
177
print (' - ',join(', ', @exitMsgTxt)) if (@exitMsgTxt);
178
print ('|',join('; ', @perfData), ';') if (@perfData);
179
print("\n");
180
exit 2 if $setCritical;
181
exit 1 if $setWarning;
182
exit 0;
183
 
184
#-------------------------------------------------------------------------------
185
# Function        : getDataItem 
186
#
187
# Description     : Get an item of statistical data 
188
#
189
# Inputs          : $name       - Name of the item to get 
190
#
191
# Returns         : The value of the item or the text 'Unknown'
192
#
193
sub getDataItem
194
{
195
    my ($name) = @_;
196
    $name = lc $name;
197
    return 'Unknown' unless exists $stats{$name};
198
    return $stats{$name}; 
199
}
200
 
201
#-------------------------------------------------------------------------------
202
# Function        : Error 
203
#
204
# Description     : Report an error to Nagios
205
#                   Returns an UNKNOWN exit code for Nagios
206
#
207
# Inputs          : Error string
208
#
209
# Returns         : Does not return
210
#
211
 
212
sub Error
213
{
214
    print("ERROR: @_\n");
215
    exit 3;
216
}
217
 
218
#-------------------------------------------------------------------------------
219
# Function        : Warning 
220
#
221
# Description     : Report an warning to Nagios
222
#                   Returns an WARNING exit code for Nagios
223
#
224
# Inputs          : Error string
225
#
226
# Returns         : Does not return
227
#
228
 
229
sub Warning
230
{
231
    print("Warning: @_\n");
232
    exit 1;
233
}
234
 
235
 
236
#-------------------------------------------------------------------------------
237
#   Documentation
238
#
239
 
240
=pod
241
 
242
=head1
243
 
244
blatNagios - Nagios Plugin for BLAT
245
 
246
=head1 SYNOPSIS
247
 
248
 blatNagios.pl [options]
249
 
250
 Options:
251
    -help              - brief help message
252
    -help -help        - Detailed help message
253
    -man               - Full documentation
254
    -statsFile=path    - Path to the statistics file
255
    -data=item         - Data item to report as performance data. Multiple allowed
256
    -text=item         - Data item to report as text. Multiple allowed
257
    -warn=item:vw:vc   - Report warnings for item. Multiple allowed
258
 
259
=head1 OPTIONS
260
 
261
=over 8
262
 
263
=item B<-help>
264
 
265
Print a brief help message and exits.
266
 
267
=item B<-help -help>
268
 
269
Print a detailed help message with an explanation for each option.
270
 
271
=item B<-man>
272
 
273
Prints the manual page and exits.
274
 
275
=item B<-target=name>
276
 
277
The name of the target BLAT server. This will be used to locate the PID and STATS files
278
 
279
=item B<statsFile=path>
280
 
281
The path to the statisics file to be processed
282
 
283
Multiple items are comma seperated. The argument may be used more than once.
284
 
285
=item B<-text=item>
286
 
287
One or more data items to be returned as a part of the text message.
288
 
289
Multiple items are comma seperated. The argument may be used more than once.
290
 
291
=item B<-warn=item:vw:vc>
292
 
293
Report a Nagios error state if the value of the named item exceeds the specified value.
294
 
295
vw is the warning threshold. vc is the critical threshold.
296
 
297
Multiple items are comma seperated. The argument may be used more than once.
298
 
299
 
300
=back
301
 
302
=head1 EXAMPLE
303
 
304
perl quarantineNagios.pl -statsFile=quarantine.stats -data=Quarantine,TotalPackages -warn=QuarantineError:1:1 -warn=S3TransferError:1:1
305
 
306
=cut
307