LoginHelp/GuideAbout TracPreferences
WikiTimelineRoadmapBrowse SourceView TicketsNew TicketSearchDownloads

source: check_dir/check_dir @ 1259

Revision 1259, 8.3 KB checked in by corti, 3 years ago (diff)

Fixed minor issues with perlcritic

  • Property svn:executable set to *
  • Property svn:keywords set to Id Revision HeadURL Source Date
Line 
1#!perl
2
3package main;
4
5# RCS information (required by Perl::Critic)
6# enable substitution with:
7#   $ svn propset svn:keywords "Id Revision HeadURL Source Date"
8#
9#   $Id$
10#   $Revision$
11#   $HeadURL$
12#   $Date$
13
14use 5.00800;
15
16use strict;
17use warnings;
18
19use English qw(-no_match_vars);
20
21use Nagios::Plugin;
22use Nagios::Plugin::Range;
23use Nagios::Plugin::Getopt;
24use Nagios::Plugin::Threshold;
25
26our $VERSION = '3.0.0';
27
28# IMPORTANT: Nagios plugins could be executed using embedded perl in this case
29#            the main routine would be executed as a subroutine and all the
30#            declared subroutines would therefore be inner subroutines
31#            This will cause all the global lexical variables not to stay shared
32#            in the subroutines!
33#
34# All variables are therefore declared as package variables...
35#
36## no critic (ProhibitPackageVars)
37use vars qw(
38  $n
39  $plugin
40  $options
41  $status
42  $info_string
43  $threshold
44);
45## use critc
46
47# the script is declared as a package so that it can be unit tested
48# but it should not be used as a module
49if ( !caller ) {
50    run();
51}
52
53##############################################################################
54# subroutines
55
56##############################################################################
57# Usage     : exit_with_error( $status, $message)
58# Purpose   : if a plugin object is available exits via ->nagios_exit
59#             otherwise prints to the shell and exit normally
60# Returns   : n/a
61# Arguments : n/a
62# Throws    : n/a
63# Comments  : n/a
64# See also  : n/a
65sub exit_with_error {
66
67    my $status  = shift;
68    my $message = shift;
69
70    if ($plugin) {
71        $plugin->nagios_exit( $status, $message );
72    }
73    else {
74        #<<<
75        print "Error: $message"; ## no critic (RequireCheckedSyscalls)
76        #>>>
77        exit $status;
78    }
79
80    return;
81
82}
83
84##############################################################################
85# Usage     : max($i, $j)
86# Purpose   : returns the maximum of two integers
87# Returns   : the maximum of two integers
88# Arguments : the integers to compare
89# Throws    : n/a
90# Comments  : n/a
91# See also  : n/a
92sub max {
93
94    my ( $i, $j ) = @_;
95
96    if ( $i > $j ) {
97        return $i;
98    }
99    else {
100        return $j;
101    }
102
103}
104
105##############################################################################
106# Usage     : verbose("some message string", $optional_verbosity_level);
107# Purpose   : write a message if the verbosity level is high enough
108# Returns   : n/a
109# Arguments : message : message string
110#             level   : options verbosity level
111# Throws    : n/a
112# Comments  : n/a
113# See also  : n/a
114sub verbose {
115
116    # arguments
117    my $message = shift;
118    my $level   = shift;
119
120    if ( !defined $message ) {
121        exit_with_error( UNKNOWN,
122            q{Internal error: not enough parameters for 'verbose'} );
123    }
124
125    if ( !defined $level ) {
126        $level = 0;
127    }
128
129    if ( $options && $level < $options->verbose ) {
130        print $message;    ## no critic (RequireCheckedSyscalls)
131    }
132
133    return;
134
135}
136
137##############################################################################
138# Usage     : @files = get_entries("directory_name")
139# Purpose   : reads the entries in the given directory
140# Returns   : number of entries
141# Arguments : dirname: the name of the directory to check
142# Throws    : n/a
143# Comments  : n/a
144# See also  : n/a
145sub get_entries {
146
147    my $dirname = shift;
148
149    #######################
150    # get directory listing
151
152    verbose "Opening $dirname\n", 1;
153
154    my $DIR;
155    opendir $DIR, $dirname
156      or exit_with_error( UNKNOWN, "Can't open $dirname: $OS_ERROR" );
157
158    # exclude .. and . from the count *and* recursion
159    my @files = grep { $_ ne q{.} && $_ ne q{..} } readdir $DIR;
160
161    closedir $DIR
162      or exit_with_error( UNKNOWN, "Error closing $dirname: $OS_ERROR" );
163
164    return @files;
165
166}
167
168##############################################################################
169# Usage     : check_dir("directory_name")
170# Purpose   : checks the number of files in the given directory
171# Returns   : n/a
172# Arguments : dirname: the name of the directory to check
173# Throws    : n/a
174# Comments  : n/a
175# See also  : n/a
176sub check_dir {
177
178    my $dirname = shift;
179    my $info;
180
181    ####################################
182    # check if the directory is readable
183
184    $info = stat $dirname
185      or exit_with_error( UNKNOWN, "Can't read stat for $dirname: $OS_ERROR" );
186
187    my @files = get_entries($dirname);
188    my $n     = @files;
189
190    #################
191    # Additional info
192    verbose "Directory '$dirname' has $n files\n";
193
194    #################
195    # Output (status)
196
197    $plugin->add_perfdata(
198        label     => $dirname,
199        value     => $n,
200        uom       => q{},
201        threshold => $threshold,
202    );
203
204    if ( defined $info_string ) {
205        $info_string = "$info_string, $dirname=$n";
206    }
207    else {
208        $info_string = "$dirname=$n";
209    }
210
211    $status = max( $status, $threshold->get_status($n) );
212
213    ###########
214    # Recursive
215    if ( $options->get('recursive') ) {
216        foreach my $file (@files) {
217            if ( -d "$dirname/$file" ) {
218                my $error = check_permissions("$dirname/$file");
219                if ($error) {
220                    exit_with_error( UNKNOWN, "Error: $error" );
221                }
222                check_dir("$dirname/$file");
223            }
224        }
225    }
226
227    return;
228}
229
230##############################################################################
231# Usage     : check_permissions( $dirname )
232# Purpose   : checks if the directory is readable
233# Returns   : undef if OK or error message if the directory is not readable
234# Arguments : dirname: the name of the directory to check
235# Throws    : n/a
236# Comments  : n/a
237# See also  : n/a
238sub check_permissions {
239
240    my $dirname = shift;
241
242    if ( !-d $dirname ) {
243        return "$dirname is not a directory";
244    }
245
246    if ( !-r $dirname ) {
247        return "$dirname is not readable";
248    }
249
250    if ( !-x $dirname ) {
251        return "$dirname is not executable";
252    }
253
254    return;
255
256}
257
258##############################################################################
259# Usage     : run();
260# Purpose   : main method
261# Returns   : n/a
262# Arguments : n/a
263# Throws    : n/a
264# Comments  : n/a
265# See also  : n/a
266sub run {
267
268    # initialization
269    $plugin = Nagios::Plugin->new( shortname => 'CHECK_DIR' );
270
271    $status = 0;
272
273    ########################
274    # Command line arguments
275
276    $options = Nagios::Plugin::Getopt->new(
277        usage   => 'Usage: %s [OPTIONS]',
278        version => $VERSION,
279        url     => 'https://trac.id.ethz.ch/projects/nagios_plugins',
280        blurb   => 'cheks the number of files in one or more directories.',
281    );
282
283    $options->arg(
284        spec     => 'dir|d=s@',
285        help     => 'specify the directory (can be repeated)',
286        required => 1,
287    );
288
289    $options->arg(
290        spec     => 'critical|c=s',
291        help     => 'specify the critical number (or range) of files',
292        required => 1,
293    );
294
295    $options->arg(
296        spec     => 'warning|w=s',
297        help     => 'specify warning threshold (range) for the number of files',
298        required => 1,
299    );
300
301    $options->arg(
302        spec    => 'recursive|r',
303        default => 0,
304        help =>
305          'perform recursive traversal (checks individual dirs not the total)',
306    );
307
308    $options->getopts();
309
310    #############################
311    # Sanity checks: command line
312
313    my $critical_range =
314      Nagios::Plugin::Range->parse_range_string( $options->get('critical') );
315    if ( !$critical_range->is_set() ) {
316        exit_with_error( UNKNOWN, 'Could not parse "critical"' );
317    }
318
319    my $warning_range =
320      Nagios::Plugin::Range->parse_range_string( $options->get('warning') );
321    if ( !$critical_range->is_set() ) {
322        exit_with_error( UNKNOWN, 'Could not parse "warning"' );
323    }
324
325    $threshold = Nagios::Plugin::Threshold->set_thresholds(
326        warning  => $warning_range,
327        critical => $critical_range,
328    );
329
330    ##########################
331    # Sanity checks: directory
332
333    foreach my $dirname ( @{ $options->get('dir') } ) {
334        my $error = check_permissions($dirname);
335        if ($error) {
336            exit_with_error( UNKNOWN, "Error: $error" );
337        }
338    }
339
340    #############
341    # Process dir
342
343    foreach my $dirname ( @{ $options->get('dir') } ) {
344        check_dir($dirname);
345    }
346
347    exit_with_error( $status, $info_string );
348
349    return;
350
351}
352
3531;
Note: See TracBrowser for help on using the repository browser.
 

Wichtiger Hinweis:
Diese Website wird in älteren Versionen von Netscape ohne graphische Elemente dargestellt. Die Funktionalität der Website ist aber trotzdem gewährleistet. Wenn Sie diese Website regelmässig benutzen, empfehlen wir Ihnen, auf Ihrem Computer einen aktuellen Browser zu installieren. Weitere Informationen finden Sie auf
folgender Seite.

Important Note:
The content in this site is accessible to any browser or Internet device, however, some graphics will display correctly only in the newer versions of Netscape. To get the most out of our site we suggest you upgrade to the newer browser.
More information

 
© 2005 ETH Zürich | Imprint | 26.09.2007 | Version 0.12
top