forked from HariSekhon/Nagios-Plugins
-
Notifications
You must be signed in to change notification settings - Fork 0
/
check_disk_write.pl
executable file
·89 lines (71 loc) · 2.63 KB
/
check_disk_write.pl
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
#!/usr/bin/perl -T
# nagios: -epn
#
# Author: Hari Sekhon
# Date: 2013-07-24 18:28:09 +0100 (Wed, 24 Jul 2013)
#
# https://github.com/harisekhon/nagios-plugins
#
# License: see accompanying LICENSE file
#
$DESCRIPTION = "Nagios Plugin to check a disk is writable and functioning properly by writing a tiny canary file with unique generated contents and then reading it back to make sure it was written properly.
Useful to detect I/O errors and disks that have been re-mounted read-only as often happens when I/O errors in the disk subsystem are detected by the kernel.";
$VERSION = "0.1";
use strict;
use warnings;
BEGIN {
use File::Basename;
use lib dirname(__FILE__) . "/lib";
}
use HariSekhonUtils;
use File::Spec;
use File::Temp;
my $dir;
%options = (
"d|directory=s" => [ \$dir, "Directory to write the canary file to. Set this to a directory on the mount point of the disk you want to check" ],
);
get_options();
defined($dir) or usage "directory not specified";
$dir = File::Spec->rel2abs($dir); # also canonicalizes, but sets "." if $dir undefined
$dir = validate_directory($dir);
my $random_string = sprintf("%s %s %s", $progname, time, random_alnum(20));
vlog_option "random string", "'$random_string'\n";
set_timeout();
$status = "OK";
my $fh;
vlog2 "creating canary file";
try {
$fh = File::Temp->new(TEMPLATE => "$dir/${progname}_XXXXXXXXXX");
};
catch_quit "failed to create canary file in $dir";
my $filename = $fh->filename;
vlog2 "canary file created: '$filename'\n";
vlog2 "writing random string to canary file";
try {
print $fh $random_string;
};
catch_quit "failed to write random string to canary file '$filename'";
vlog2 "wrote canary file\n";
vlog3 "seeking to beginning of canary file";
try {
seek($fh, 0, 0) or quit "CRITICAL", "failed to seek to beginning of canary file '$filename': $!";
};
catch_quit "failed to seek to beginning of canary file '$filename'";
vlog3 "seeked back to start of canary file\n";
my $contents = "";
my $bytes;
vlog2 "reading contents of canary file back";
try {
$bytes = read($fh, $contents, 100);
};
catch_quit "failed to read back from canary file '$filename'";
vlog2 "$bytes bytes read back from canary file\n";
vlog3 "contents = '$contents'\n";
vlog3 "comparing random string written to contents of canary file";
if($contents eq $random_string){
vlog2 "random string written and contents read back match OK\n";
} else {
quit "CRITICAL", "canary file I/O error in $dir (written => read contents differ: '$random_string' vs '$contents')";
}
$msg = "canary file I/O written => read back $bytes bytes successfully in $dir, unique contents verified";
quit $status, $msg;