Subversion Repositories DevTools

Rev

Rev 5404 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
5398 dpurdie 1
#! /usr/bin/perl
2
########################################################################
3
# Copyright (c) VIX TECHNOLOGY (AUST) LTD
4
#
5
# Module name   : blatNagios.pl
6
# Module type   : Nagios Plug in
7
# Compiler(s)   : Perl
8
# Environment(s): jats
9
#
10
# Description   : A Nagios plugin to interface to BLAT data
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_help = 0;
35
my $opt_target;
36
my $opt_rundir = 'run';
37
my @opt_data;
5399 dpurdie 38
my @opt_text;
5404 dpurdie 39
my @opt_warn;
5398 dpurdie 40
my %stats;
41
my $exitMsg = "OK";
5399 dpurdie 42
my @exitMsgTxt;
5398 dpurdie 43
my @perfData;
5404 dpurdie 44
my $setWarning;
45
my $setCritical;
5398 dpurdie 46
 
47
#-------------------------------------------------------------------------------
48
# Function        : Main Entry
49
#
50
# Description     :
51
#
52
# Inputs          :
53
#
54
# Returns         :
55
#
56
my $result = GetOptions (
57
                "help:+"        => \$opt_help,          # flag, multiple use allowed
58
                "manual:3"      => \$opt_help,          # flag
59
                "verbose:+"     => \$opt_verbose,       # flag
60
                "target=s"      => \$opt_target,        # String
61
                "rundir=s"      => \$opt_rundir,        # String
5399 dpurdie 62
                "data=s"        => sub{push @opt_data, split(/\s*,\s*/,$_[1])},          # Strings
63
                "text=s"        => sub{push @opt_text, split(/\s*,\s*/,$_[1])},          # Strings
5404 dpurdie 64
                "warn=s"        => sub{push @opt_warn, split(/\s*,\s*/,$_[1])},          # Strings
5398 dpurdie 65
                );
66
 
67
#
68
#   Process help and manual options
69
#
70
pod2usage(-verbose => 0, -message => "Version: $VERSION")  if ($opt_help == 1  || ! $result);
71
pod2usage(-verbose => 1)  if ($opt_help == 2 );
72
pod2usage(-verbose => 2)  if ($opt_help > 2);
73
 
74
# Validate options
75
Error ("Target machine not specified") unless ($opt_target);
76
 
77
# Determine the 'run' directory
78
unless ($opt_rundir =~ m~^/~)
79
{
80
    $opt_rundir = catdir($FindBin::Bin , $opt_rundir);
81
}
82
Error ("Rundir not found: $opt_rundir") unless -d $opt_rundir;
83
 
84
#
85
# See if the named BLAT target is running
86
#   It must have a PID pid file
87
#
88
Error('BLAT daemon not running') unless testPid('blat');
89
Error('Transfer daemon not running') unless testPid($opt_target);
90
 
91
#
92
# Read in the statistics file
93
#   Format is is key:data
94
# 
95
my $statsFile = catfile( $opt_rundir, $opt_target . '.stats');
96
Warning('No Statistics available') unless -f $statsFile;
97
open my $fh, $statsFile || Error("Cannot read statistics");
98
while (<$fh>)
99
{
100
    m~(.*):(.*)~;
101
    $stats{lc($1)} = $2;
102
}
5404 dpurdie 103
close $fh;
5398 dpurdie 104
 
105
#
106
#   Determine the Nagios State. Will be CRITICAL if
107
#       state is NOT OK
108
#       timeStamp is more than 5 minutes old
109
#       
110
unless (defined $stats{state} && $stats{state} eq 'OK')
111
{
5404 dpurdie 112
    $setCritical = 1;
5398 dpurdie 113
    $exitMsg = $stats{state} || 'Unknown state' ;
114
}
115
 
116
my $dataAge = time() - $stats{timestamp};
117
if ($dataAge > (10 * 60))
118
{
5404 dpurdie 119
    $setCritical = 1;
5398 dpurdie 120
    $exitMsg = 'Data too old(Secs):' . $dataAge;
121
}
122
 
123
#
5404 dpurdie 124
#   Insert Text data - not perf data.
5399 dpurdie 125
#   This will be displayed as a part of the output
126
#
127
foreach my $item (@opt_text)
128
{
129
    push @exitMsgTxt, $item . '=' . getDataItem($item); 
130
}
131
#
5398 dpurdie 132
#   Insert the required performance data
133
#   
5399 dpurdie 134
foreach my $item (@opt_data)
5398 dpurdie 135
{
5399 dpurdie 136
    push @perfData, $item . '=' . getDataItem($item); 
5398 dpurdie 137
}
138
 
139
#
5404 dpurdie 140
#   Process warning thresholds
141
#
142
foreach my $item (@opt_warn)
143
{
144
    my ($Name, $warn, $critical) = split(/:/, $item);
145
    my $name = lc $Name;
146
    my $isCritical;
147
    if (exists $stats{$name})
148
    {
149
        my $value = int($stats{$name});
150
        if (defined $critical)
151
        {
152
            if ($value > int($critical) )
153
            {
154
                $isCritical = 1;
155
                $setCritical = 1;
156
                push @exitMsgTxt, $Name . ' is Critical'; 
157
 
158
            }
159
        }
160
        if (! $isCritical)
161
        {
162
            if (defined $warn )
163
            {
164
                if ($value > int($warn) )
165
                {
166
                    $setWarning = 1;
167
                    push @exitMsgTxt, $Name . ' is Warning'; 
168
                }
169
            }
170
            else
171
            {
172
                push @exitMsgTxt, "$item bad format";
173
                $setWarning = 1; 
174
            }
175
        }
176
    }
177
    else
178
    {
179
        push @exitMsgTxt, "$Name not known";
180
        $setWarning = 1; 
181
    }
182
}
183
 
184
#
5398 dpurdie 185
# Prepare the output
5399 dpurdie 186
#   STATUS, Status Text, Performance Data
5398 dpurdie 187
# 
188
print($exitMsg);
5404 dpurdie 189
print (' - ',join(', ', @exitMsgTxt)) if (@exitMsgTxt);
5399 dpurdie 190
print ('|',join('; ', @perfData), ';') if (@perfData);
5398 dpurdie 191
print("\n");
5404 dpurdie 192
exit 2 if $setCritical;
193
exit 1 if $setWarning;
194
exit 0;
5398 dpurdie 195
 
196
#-------------------------------------------------------------------------------
5399 dpurdie 197
# Function        : getDataItem 
198
#
199
# Description     : Get an item of statistical data 
200
#
201
# Inputs          : $name       - Name of the item to get 
202
#
203
# Returns         : The value of the item or the text 'Unknown'
204
#
205
sub getDataItem
206
{
207
    my ($name) = @_;
208
    $name = lc $name;
209
    return 'Unknown' unless exists $stats{$name};
210
    return $stats{$name}; 
211
}
212
 
213
 
214
#-------------------------------------------------------------------------------
5398 dpurdie 215
# Function        : testPid 
216
#
217
# Description     : See if a PID exists    
218
#
219
# Inputs          : $name               - Name of pid file to examine
220
#
221
# Returns         : TRUE - looks OK
222
#
223
sub testPid
224
{
225
    my ($name) = @_;
226
    my $pidfile = catfile( $opt_rundir, $name . '.pid');
5399 dpurdie 227
    open (my $fh, $pidfile) || return 0;
5398 dpurdie 228
    my ($pid) = <$fh>;
229
    close $fh;
230
 
231
    if (defined $pid)
232
    {
233
        chomp $pid;
234
        $pid = int $pid;
235
        if ($pid > 0 )
236
        {
237
            # Brute force - unix specific check
238
            return 1 if -d catdir( '/proc', $pid);
239
        }
240
    }
241
    return 0;
242
}
243
 
244
 
245
#-------------------------------------------------------------------------------
246
# Function        : Error 
247
#
248
# Description     : Report an error to Nagios
249
#                   Returns an UNKNOWN exit code for Nagios
250
#
251
# Inputs          : Error string
252
#
253
# Returns         : Does not return
254
#
255
 
256
sub Error
257
{
258
    print("ERROR: @_\n");
259
    exit 3;
260
}
261
 
262
#-------------------------------------------------------------------------------
263
# Function        : Warning 
264
#
265
# Description     : Report an warning to Nagios
266
#                   Returns an WARNING exit code for Nagios
267
#
268
# Inputs          : Error string
269
#
270
# Returns         : Does not return
271
#
272
 
273
sub Warning
274
{
275
    print("Warning: @_\n");
276
    exit 1;
277
}
278
 
279
 
280
#-------------------------------------------------------------------------------
281
#   Documentation
282
#
283
 
284
=pod
285
 
286
=head1
287
 
288
blatNagios - Nagios Plugin for BLAT
289
 
290
=head1 SYNOPSIS
291
 
292
 blatNagios.pl [options]
293
 
294
 Options:
295
    -help              - brief help message
296
    -help -help        - Detailed help message
297
    -man               - Full documentation
298
    -target=name       - Target Machine name
299
    -rundir=path       - Alternate run directory
5399 dpurdie 300
    -data=item         - Data item to report as performance data. Multiple allowed
301
    -text=item         - Data item to report as text. Multiple allowed
5404 dpurdie 302
    -warn=item:vw:vc   - Report warnings for item. Multiple allowed
5398 dpurdie 303
 
304
=head1 OPTIONS
305
 
306
=over 8
307
 
308
=item B<-help>
309
 
310
Print a brief help message and exits.
311
 
312
=item B<-help -help>
313
 
314
Print a detailed help message with an explanation for each option.
315
 
316
=item B<-man>
317
 
318
Prints the manual page and exits.
319
 
320
=item B<-target=name>
321
 
322
The name of the target BLAT server. This will be used to locate the PID and STATS files
323
 
324
=item B<-rundir=path>
325
 
326
The path to the 'run' directory in which the targets PID and STATS files are stored.
327
 
328
If a relative path is provided, then it is relative to this script.
329
 
330
The default value if 'run'.
331
 
332
=item B<-data=item>
333
 
334
One or more data items to be returned as a part of the performance data.
335
 
336
Multiple items are comma seperated. The argument may be used more than once.
337
 
5399 dpurdie 338
=item B<-text=item>
339
 
340
One or more data items to be returned as a part of the text message.
341
 
342
Multiple items are comma seperated. The argument may be used more than once.
343
 
5404 dpurdie 344
=item B<-warn=item:vw:vc>
345
 
346
Report a Nagios error state if the value of the named item exceeds the specified value.
347
 
348
vw is the warning threshold. vc is the critical threshold.
349
 
350
Multiple items are comma seperated. The argument may be used more than once.
351
 
352
 
5398 dpurdie 353
=back
354
 
355
=head1 EXAMPLE
356
 
5793 dpurdie 357
perl blatNagios.pl -target=auawsaarc001 -data=txCount,delCount -data=linkErrors -warn=txCount:40:50
5398 dpurdie 358
 
359
=cut
360