LoginHelp/GuideAbout TracPreferences
WikiTimelineRoadmapBrowse SourceView TicketsNew TicketSearchDownloads

source: check_dir/check_dir @ 1253

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

Fixed #91 and #92. New release not yet ready (packaging)

  • 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(
122            UNKNOWN,
123            q{Internal error: not enough parameters for 'verbose'} );
124    }
125
126    if ( !defined $level ) {
127        $level = 0;
128    }
129
130    if ( $options && $level < $options->verbose ) {
131        print $message; ## no critic (RequireCheckedSyscalls)
132    }
133
134    return;
135
136}
137
138##############################################################################
139# Usage     : @files = get_entries("directory_name")
140# Purpose   : reads the entries in the given directory
141# Returns   : number of entries
142# Arguments : dirname: the name of the directory to check
143# Throws    : n/a
144# Comments  : n/a
145# See also  : n/a
146sub get_entries {
147
148    my $dirname = shift;
149
150    #######################
151    # get directory listing
152
153    verbose "Opening $dirname\n", 1;
154
155    my $DIR;
156    opendir $DIR, $dirname
157      or exit_with_error( UNKNOWN, "Can't open $dirname: $OS_ERROR" );
158
159    # exclude .. and . from the count *and* recursion
160    my @files = grep { $_ ne '.' && $_ ne '..' } readdir $DIR;
161   
162    closedir $DIR
163      or exit_with_error( UNKNOWN, "Error closing $dirname: $OS_ERROR" );
164   
165    return @files;
166   
167}
168   
169
170
171##############################################################################
172# Usage     : check_dir("directory_name")
173# Purpose   : checks the number of files in the given directory
174# Returns   : n/a
175# Arguments : dirname: the name of the directory to check
176# Throws    : n/a
177# Comments  : n/a
178# See also  : n/a
179sub check_dir {
180
181    my $dirname = shift;
182    my $info;
183
184    ####################################
185    # check if the directory is readable
186
187    $info = stat $dirname
188      or exit_with_error( UNKNOWN,
189        "Can't read stat for $dirname: $OS_ERROR" );
190
191    my @files = get_entries( $dirname );
192    my $n = @files;
193   
194    #################
195    # Additional info
196    verbose "Directory '$dirname' has $n files\n";
197
198    #################
199    # Output (status)
200
201    $plugin->add_perfdata(
202        label     => $dirname,
203        value     => $n,
204        uom       => q{},
205        threshold => $threshold,
206    );
207
208    if ( defined $info_string ) {
209        $info_string = "$info_string, $dirname=$n";
210    }
211    else {
212        $info_string = "$dirname=$n";
213    }
214
215    $status = max( $status, $threshold->get_status($n) );
216
217    ###########
218    # Recursive
219    if ($options->get('recursive')) {
220        foreach my $file (@files) {
221            if ( -d "$dirname/$file" ) {
222                check_permissions( "$dirname/$file" );
223                check_dir("$dirname/$file");
224            }
225        }
226    }
227   
228    return;
229}
230
231##############################################################################
232# Usage     : check_permissions( $dirname )
233# Purpose   : checks if the directory is readable
234# Returns   : undef if OK or error message if the directory is not readable
235# Arguments : dirname: the name of the directory to check
236# Throws    : n/a
237# Comments  : n/a
238# See also  : n/a
239sub check_permissions {
240
241    my $dirname = shift;
242       
243    if ( !-d $dirname ) {
244        return "$dirname is not a directory";
245    }
246   
247    if ( !-r $dirname ) {
248        return "$dirname is not readable";
249    }
250   
251    if ( !-x $dirname ) {
252        return "$dirname is not executable";
253    }
254
255    return;
256
257}
258
259##############################################################################
260# Usage     : run();
261# Purpose   : main method
262# Returns   : n/a
263# Arguments : n/a
264# Throws    : n/a
265# Comments  : n/a
266# See also  : n/a
267sub run {
268
269    # initialization
270    $plugin = Nagios::Plugin->new( shortname => 'CHECK_DIR' );
271
272    $status    = 0;
273
274    ########################
275    # Command line arguments
276
277    $options = Nagios::Plugin::Getopt->new(
278        usage   => 'Usage: %s [OPTIONS]',
279        version => $VERSION,
280        url     => 'https://trac.id.ethz.ch/projects/nagios_plugins',
281        blurb   => 'cheks the number of files in one or more directories.',
282    );
283
284    $options->arg(
285        spec => 'dir|d=s@',
286        help => 'specify the directory (can be repeated)',
287        required => 1,
288    );
289
290    $options->arg(
291        spec => 'critical|c=s',
292        help => 'specify the critical number (or range) of files',
293        required => 1,
294    );
295
296    $options->arg(
297        spec => 'warning|w=s',
298        help => 'specify warning threshold (range) for the number of files',
299        required => 1,
300    );
301
302    $options->arg(
303        spec => 'recursive|r',
304        default => 0,
305        help => '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(
337                UNKNOWN,
338                "Error: $error" );
339        }
340    }
341                   
342    #############
343    # Process dir
344
345    foreach my $dirname (@{$options->get('dir')}) {
346        check_dir($dirname);
347    }
348
349    exit_with_error( $status, $info_string );
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