Subversion Repositories DevTools

Rev

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