Subversion Repositories DevTools

Rev

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