diff options
| author | Ton Voon <tonvoon@users.sourceforge.net> | 2005-12-15 15:17:49 +0000 |
|---|---|---|
| committer | Ton Voon <tonvoon@users.sourceforge.net> | 2005-12-15 15:17:49 +0000 |
| commit | 73b77a44c43960b6fcf8b3c29c1016ba1940aa89 (patch) | |
| tree | a8adb7ae427b4cf7b7c6c4f35de9fc0a918140d4 | |
| parent | 162faf883a864a94c0f75ca0e21360cbd001e0f3 (diff) | |
| download | monitoring-plugins-73b77a44c43960b6fcf8b3c29c1016ba1940aa89.tar.gz | |
Allow directories and links to be tested by check_file_age. Sanitise output.
Added tests
git-svn-id: https://nagiosplug.svn.sourceforge.net/svnroot/nagiosplug/nagiosplug/trunk@1297 f882894a-f735-0410-b71e-b25c423dba1c
| -rwxr-xr-x | plugins-scripts/check_file_age.pl | 16 | ||||
| -rw-r--r-- | plugins-scripts/t/check_file_age.t | 88 |
2 files changed, 96 insertions, 8 deletions
diff --git a/plugins-scripts/check_file_age.pl b/plugins-scripts/check_file_age.pl index 43038fae..0215c0f6 100755 --- a/plugins-scripts/check_file_age.pl +++ b/plugins-scripts/check_file_age.pl | |||
| @@ -67,14 +67,14 @@ if ($opt_h) { | |||
| 67 | $opt_f = shift unless ($opt_f); | 67 | $opt_f = shift unless ($opt_f); |
| 68 | 68 | ||
| 69 | if (! $opt_f) { | 69 | if (! $opt_f) { |
| 70 | print "No file specified\n"; | 70 | print "FILE_AGE UNKNOWN: No file specified\n"; |
| 71 | exit $ERRORS{'UNKNOWN'}; | 71 | exit $ERRORS{'UNKNOWN'}; |
| 72 | } | 72 | } |
| 73 | 73 | ||
| 74 | # Examine the file. | 74 | # Check that file exists (can be directory or link) |
| 75 | unless (-f $opt_f) { | 75 | unless (-e $opt_f) { |
| 76 | print "$opt_f: File not found\n"; | 76 | print "FILE_AGE CRITICAL: File not found - $opt_f\n"; |
| 77 | exit $ERRORS{'UNKNOWN'}; | 77 | exit $ERRORS{'CRITICAL'}; |
| 78 | } | 78 | } |
| 79 | 79 | ||
| 80 | $st = File::stat::stat($opt_f); | 80 | $st = File::stat::stat($opt_f); |
| @@ -91,7 +91,7 @@ elsif (($opt_w and $age > $opt_w) or ($opt_W and $size < $opt_W)) { | |||
| 91 | $result = 'WARNING'; | 91 | $result = 'WARNING'; |
| 92 | } | 92 | } |
| 93 | 93 | ||
| 94 | print "$result - $opt_f is $age seconds old and $size bytes\n"; | 94 | print "FILE_AGE $result: $opt_f is $age seconds old and $size bytes\n"; |
| 95 | exit $ERRORS{$result}; | 95 | exit $ERRORS{$result}; |
| 96 | 96 | ||
| 97 | sub print_usage () { | 97 | sub print_usage () { |
| @@ -106,8 +106,8 @@ sub print_help () { | |||
| 106 | print "Copyright (c) 2003 Steven Grimm\n\n"; | 106 | print "Copyright (c) 2003 Steven Grimm\n\n"; |
| 107 | print_usage(); | 107 | print_usage(); |
| 108 | print "\n"; | 108 | print "\n"; |
| 109 | print " <secs> File must be no more than this many seconds old\n"; | 109 | print " <secs> File must be no more than this many seconds old (default: warn 240 secs, crit 600)\n"; |
| 110 | print " <size> File must be at least this many bytes long\n"; | 110 | print " <size> File must be at least this many bytes long (default: crit 0 bytes)\n"; |
| 111 | print "\n"; | 111 | print "\n"; |
| 112 | support(); | 112 | support(); |
| 113 | } | 113 | } |
diff --git a/plugins-scripts/t/check_file_age.t b/plugins-scripts/t/check_file_age.t new file mode 100644 index 00000000..372b9f2e --- /dev/null +++ b/plugins-scripts/t/check_file_age.t | |||
| @@ -0,0 +1,88 @@ | |||
| 1 | #!/usr/bin/perl -w -I .. | ||
| 2 | # | ||
| 3 | # check_file_age tests | ||
| 4 | # | ||
| 5 | # $Id$ | ||
| 6 | # | ||
| 7 | |||
| 8 | use strict; | ||
| 9 | use Test::More tests => 15; | ||
| 10 | use NPTest; | ||
| 11 | |||
| 12 | my $successOutput = '/^FILE_AGE OK: /'; | ||
| 13 | my $warningOutput = '/^FILE_AGE WARNING: /'; | ||
| 14 | my $criticalOutput = '/^FILE_AGE CRITICAL: /'; | ||
| 15 | my $unknownOutput = '/^FILE_AGE UNKNOWN: /'; | ||
| 16 | |||
| 17 | my $result; | ||
| 18 | my $temp_file = "/tmp/check_file_age.tmp"; | ||
| 19 | my $temp_link = "/tmp/check_file_age.link.tmp"; | ||
| 20 | |||
| 21 | unlink $temp_file, $temp_link; | ||
| 22 | |||
| 23 | $result = NPTest->testCmd( | ||
| 24 | "./check_file_age" | ||
| 25 | ); | ||
| 26 | cmp_ok( $result->return_code, '==', 3, "Missing parameters" ); | ||
| 27 | like ( $result->output, $unknownOutput, "Output for unknown correct" ); | ||
| 28 | |||
| 29 | $result = NPTest->testCmd( | ||
| 30 | "./check_file_age -f $temp_file" | ||
| 31 | ); | ||
| 32 | cmp_ok( $result->return_code, '==', 2, "File not exists" ); | ||
| 33 | like ( $result->output, $criticalOutput, "Output for file missing correct" ); | ||
| 34 | |||
| 35 | write_chars(100); | ||
| 36 | $result = NPTest->testCmd( | ||
| 37 | "./check_file_age -f $temp_file" | ||
| 38 | ); | ||
| 39 | cmp_ok( $result->return_code, '==', 0, "File is new enough" ); | ||
| 40 | like ( $result->output, $successOutput, "Output for success correct" ); | ||
| 41 | |||
| 42 | sleep 2; | ||
| 43 | |||
| 44 | $result = NPTest->testCmd( | ||
| 45 | "./check_file_age -f $temp_file -w 1" | ||
| 46 | ); | ||
| 47 | cmp_ok( $result->return_code, '==', 1, "Warning for file over 1 second old" ); | ||
| 48 | like ( $result->output, $warningOutput, "Output for warning correct" ); | ||
| 49 | |||
| 50 | $result = NPTest->testCmd( | ||
| 51 | "./check_file_age -f $temp_file -c 1" | ||
| 52 | ); | ||
| 53 | cmp_ok( $result->return_code, '==', 2, "Critical for file over 1 second old" ); | ||
| 54 | like ( $result->output, $criticalOutput, "Output for critical correct" ); | ||
| 55 | |||
| 56 | $result = NPTest->testCmd( | ||
| 57 | "./check_file_age -f $temp_file -c 1000 -W 100" | ||
| 58 | ); | ||
| 59 | cmp_ok( $result->return_code, '==', 0, "Checking file size" ); | ||
| 60 | |||
| 61 | $result = NPTest->testCmd( | ||
| 62 | "./check_file_age -f $temp_file -c 1000 -W 101" | ||
| 63 | ); | ||
| 64 | cmp_ok( $result->return_code, '==', 1, "One byte too short" ); | ||
| 65 | |||
| 66 | $result = NPTest->testCmd( | ||
| 67 | "./check_file_age -f $temp_file -c 1000 -C 101" | ||
| 68 | ); | ||
| 69 | cmp_ok( $result->return_code, '==', 2, "One byte too short - critical" ); | ||
| 70 | |||
| 71 | symlink $temp_file, $temp_link or die "Cannot create symlink"; | ||
| 72 | $result = NPTest->testCmd("./check_file_age -f $temp_link -c 10"); | ||
| 73 | cmp_ok( $result->return_code, '==', 0, "Works for symlinks" ); | ||
| 74 | unlink $temp_link; | ||
| 75 | |||
| 76 | unlink $temp_file; | ||
| 77 | mkdir $temp_file or die "Cannot create directory"; | ||
| 78 | $result = NPTest->testCmd("./check_file_age -f $temp_file -c 1"); | ||
| 79 | cmp_ok( $result->return_code, '==', 0, "Works for directories" ); | ||
| 80 | rmdir $temp_file; | ||
| 81 | |||
| 82 | |||
| 83 | sub write_chars { | ||
| 84 | my $size = shift; | ||
| 85 | open F, "> $temp_file" or die "Cannot write to $temp_file"; | ||
| 86 | print F "A" x $size; | ||
| 87 | close F; | ||
| 88 | } | ||
