summary | shortlog | log | commit | commitdiff | tree
raw | patch | inline | side by side (parent: 162faf8)
raw | patch | inline | side by side (parent: 162faf8)
author | Ton Voon <tonvoon@users.sourceforge.net> | |
Thu, 15 Dec 2005 15:17:49 +0000 (15:17 +0000) | ||
committer | Ton Voon <tonvoon@users.sourceforge.net> | |
Thu, 15 Dec 2005 15:17:49 +0000 (15:17 +0000) |
Added tests
git-svn-id: https://nagiosplug.svn.sourceforge.net/svnroot/nagiosplug/nagiosplug/trunk@1297 f882894a-f735-0410-b71e-b25c423dba1c
git-svn-id: https://nagiosplug.svn.sourceforge.net/svnroot/nagiosplug/nagiosplug/trunk@1297 f882894a-f735-0410-b71e-b25c423dba1c
plugins-scripts/check_file_age.pl | patch | blob | history | |
plugins-scripts/t/check_file_age.t | [new file with mode: 0644] | patch | blob |
index 43038fae6151d47c0828d725bb2260902db0cc62..0215c0f6c2f020970ad1795e97b7e44fbabf2f1a 100755 (executable)
$opt_f = shift unless ($opt_f);
if (! $opt_f) {
- print "No file specified\n";
+ print "FILE_AGE UNKNOWN: No file specified\n";
exit $ERRORS{'UNKNOWN'};
}
-# Examine the file.
-unless (-f $opt_f) {
- print "$opt_f: File not found\n";
- exit $ERRORS{'UNKNOWN'};
+# Check that file exists (can be directory or link)
+unless (-e $opt_f) {
+ print "FILE_AGE CRITICAL: File not found - $opt_f\n";
+ exit $ERRORS{'CRITICAL'};
}
$st = File::stat::stat($opt_f);
$result = 'WARNING';
}
-print "$result - $opt_f is $age seconds old and $size bytes\n";
+print "FILE_AGE $result: $opt_f is $age seconds old and $size bytes\n";
exit $ERRORS{$result};
sub print_usage () {
print "Copyright (c) 2003 Steven Grimm\n\n";
print_usage();
print "\n";
- print " <secs> File must be no more than this many seconds old\n";
- print " <size> File must be at least this many bytes long\n";
+ print " <secs> File must be no more than this many seconds old (default: warn 240 secs, crit 600)\n";
+ print " <size> File must be at least this many bytes long (default: crit 0 bytes)\n";
print "\n";
support();
}
diff --git a/plugins-scripts/t/check_file_age.t b/plugins-scripts/t/check_file_age.t
--- /dev/null
@@ -0,0 +1,88 @@
+#!/usr/bin/perl -w -I ..
+#
+# check_file_age tests
+#
+# $Id$
+#
+
+use strict;
+use Test::More tests => 15;
+use NPTest;
+
+my $successOutput = '/^FILE_AGE OK: /';
+my $warningOutput = '/^FILE_AGE WARNING: /';
+my $criticalOutput = '/^FILE_AGE CRITICAL: /';
+my $unknownOutput = '/^FILE_AGE UNKNOWN: /';
+
+my $result;
+my $temp_file = "/tmp/check_file_age.tmp";
+my $temp_link = "/tmp/check_file_age.link.tmp";
+
+unlink $temp_file, $temp_link;
+
+$result = NPTest->testCmd(
+ "./check_file_age"
+ );
+cmp_ok( $result->return_code, '==', 3, "Missing parameters" );
+like ( $result->output, $unknownOutput, "Output for unknown correct" );
+
+$result = NPTest->testCmd(
+ "./check_file_age -f $temp_file"
+ );
+cmp_ok( $result->return_code, '==', 2, "File not exists" );
+like ( $result->output, $criticalOutput, "Output for file missing correct" );
+
+write_chars(100);
+$result = NPTest->testCmd(
+ "./check_file_age -f $temp_file"
+ );
+cmp_ok( $result->return_code, '==', 0, "File is new enough" );
+like ( $result->output, $successOutput, "Output for success correct" );
+
+sleep 2;
+
+$result = NPTest->testCmd(
+ "./check_file_age -f $temp_file -w 1"
+ );
+cmp_ok( $result->return_code, '==', 1, "Warning for file over 1 second old" );
+like ( $result->output, $warningOutput, "Output for warning correct" );
+
+$result = NPTest->testCmd(
+ "./check_file_age -f $temp_file -c 1"
+ );
+cmp_ok( $result->return_code, '==', 2, "Critical for file over 1 second old" );
+like ( $result->output, $criticalOutput, "Output for critical correct" );
+
+$result = NPTest->testCmd(
+ "./check_file_age -f $temp_file -c 1000 -W 100"
+ );
+cmp_ok( $result->return_code, '==', 0, "Checking file size" );
+
+$result = NPTest->testCmd(
+ "./check_file_age -f $temp_file -c 1000 -W 101"
+ );
+cmp_ok( $result->return_code, '==', 1, "One byte too short" );
+
+$result = NPTest->testCmd(
+ "./check_file_age -f $temp_file -c 1000 -C 101"
+ );
+cmp_ok( $result->return_code, '==', 2, "One byte too short - critical" );
+
+symlink $temp_file, $temp_link or die "Cannot create symlink";
+$result = NPTest->testCmd("./check_file_age -f $temp_link -c 10");
+cmp_ok( $result->return_code, '==', 0, "Works for symlinks" );
+unlink $temp_link;
+
+unlink $temp_file;
+mkdir $temp_file or die "Cannot create directory";
+$result = NPTest->testCmd("./check_file_age -f $temp_file -c 1");
+cmp_ok( $result->return_code, '==', 0, "Works for directories" );
+rmdir $temp_file;
+
+
+sub write_chars {
+ my $size = shift;
+ open F, "> $temp_file" or die "Cannot write to $temp_file";
+ print F "A" x $size;
+ close F;
+}