-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathiostat_errors.pl
More file actions
133 lines (121 loc) · 3.51 KB
/
Copy pathiostat_errors.pl
File metadata and controls
133 lines (121 loc) · 3.51 KB
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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
#!/usr/bin/perl -w
use Getopt::Std;
getopt('d');
if($opt_d)
{
$device = $opt_d;
$devFile = "$device";
}
$fileExist = 0;
if($status = isDeviceExist($device))
{
($device, $se, $he, $trn, $errors) = split(/\s+/, $status);
$se = $he = $trn = 0;
if($errors)
{
if(-f $devFile)
{
$fileExist = 1;
$firstLine = readFirstLine($devFile);
if($errors > $firstLine)
{
unshiftNewErrorsToFile($devFile, $errors);
print "CRITICAL: More errors found in device $device";
exit 2;
}
if($errors == $firstLine)
{
if($days = isModifiedToday($devFile))
{
if($days < 7)
{
print "Warn: $errors errors found in device $device";
exit 1;
}
print "OK: A week ago there were $errors errors...\n The log is going to be deleted\n";
exit 0;
}
print "CRITICAL: The $errors errors has been made today you need to take care of device: $device";
exit 2;
}
}
else
{
writeFile($devFile, $errors);
print "CRITICAL: $errors errors found in device $device";
exit 2;
}
}
print "OK: No errors in device $device";
exit 0;
}
else
{
if($fileExist)
{
unlink $devFile;
}
print "UNKNOWN: device $device doesn't exist";
exit 3;
}
sub isModifiedToday
{
$file = shift;
$num_seconds = time - (stat($file))[9];
$diff = $num_seconds;
$seconds = $diff % 60;
$diff = ($diff - $seconds) / 60;
$minutes = $diff % 60;
$diff = ($diff - $minutes) / 60;
$hours = $diff % 24;
$days = ($diff - $hours) / 24;
return $days;
}
sub unshiftNewErrorsToFile
{
$file = shift;
$error = shift;
open(FH, "$file");
@lines = <FH>;
close(FH);
open(FH,">$file");
unshift @lines, $error;
for($i=0; $i <=$#lines; $i++)
{
chomp $lines[$i];
print FH "$lines[$i]\n";
}
}
sub isDeviceExist
{
$device = shift;
@output = `iostat -e $device`;
if($#output < 2)
{
print "UNKNOWN: device $device is missing\n";
exit 3;
}
return $output[-1];
}
sub appendFile
{
$file = shift;
$content = @_;
open(FH, ">>$file");
print FH $content;
}
sub writeFile
{
$file = shift;
$content = $errors;
open(FH, ">$file");
print FH $errors;
}
sub readFirstLine
{
$file = shift;
open(FH, $file);
@lines = <FH>;
$line = shift@lines;
return $line
}