mirror of
https://github.com/munin-monitoring/contrib.git
synced 2025-07-22 02:51:03 +00:00
refactoring - split a single regex matching to three matchings
* one line regular expression matching to multiline text is a bit hard to picture * apply regular expressions to each line * first line should like "active raid1 sda1[0] sdc1[2] sdb1[1]" * second line should like "123456 blocks super 1.2 [2/2] [UU]" * third line will appear when the array is in action
This commit is contained in:
parent
2e0acaca2b
commit
2670e4dc26
1 changed files with 45 additions and 11 deletions
|
@ -30,18 +30,53 @@ if ( $ARGV[0] and $ARGV[0] eq "config" ) {
|
||||||
}
|
}
|
||||||
|
|
||||||
open(my $mdstat, "/proc/mdstat");
|
open(my $mdstat, "/proc/mdstat");
|
||||||
my $text;
|
my(@text) = <$mdstat>;
|
||||||
{
|
# contents of <$mdstat> may be changed at next reading, so fetch the contents at a time
|
||||||
local($/);
|
|
||||||
$text = <$mdstat>;
|
|
||||||
}
|
|
||||||
close($mdstat);
|
close($mdstat);
|
||||||
|
|
||||||
# Should look like "active raid1 sda1[0] sdc1[2] sdb1[1]"
|
my($devinfo_re, $devstat_re, $action_re) = (
|
||||||
# Interestingly, swap is presented as "active (auto-read-only)"
|
'(md\d+)\s+:\s+active\s+(\(auto-read-only\)\s+|)(\w+)\s+(.*)',
|
||||||
while ($text =~ /(md\d+)\s+:\s+active\s+(\(auto-read-only\)\s+|)(\w+)\s+(.*)\n.*\[(\d+)\/(\d+)]\s+\[(\w+)]\n(.*(check|resync)\s=\s+(\d+\.\d+)%|.*\n)/ ) {
|
'.*\[(\d+)\/(\d+)]\s+\[(\w+)]',
|
||||||
my($dev,$dummy,$type,$members,$nmem,$nact,$status,$dummy2,$dummy3,$proc) = ($1,$2,$3,$4,$5,$6,$7,$8,$9,$10);
|
'(.*(check|resync)\s=\s+(\d+\.\d+)%)',
|
||||||
# print "$text\nitem: $dev $type ($members) status=$status $proc\n";
|
);
|
||||||
|
|
||||||
|
my($dev, $type, $members, $nmem, $nact, $status, $proc);
|
||||||
|
while (@text) {
|
||||||
|
my $line = shift @text;
|
||||||
|
if ($line =~ /$devinfo_re/) {
|
||||||
|
# first line should like "active raid1 sda1[0] sdc1[2] sdb1[1]"
|
||||||
|
$dev = $1;
|
||||||
|
$type = $3;
|
||||||
|
$members = $4;
|
||||||
|
|
||||||
|
$line = shift @text;
|
||||||
|
if ($line =~ /$devstat_re/) {
|
||||||
|
# second line should like "123456 blocks super 1.2 [2/2] [UU]"
|
||||||
|
$nmem = $1;
|
||||||
|
$nact = $2;
|
||||||
|
$status = $3;
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
# sencond line did not exist on /proc/mdstat
|
||||||
|
next;
|
||||||
|
}
|
||||||
|
|
||||||
|
$line = shift @text;
|
||||||
|
if ($line =~ /$action_re/) {
|
||||||
|
# third line should like " [==>..................] check = 10.0% (12345/123456) finish=123min speed=12345/sec"
|
||||||
|
# this line will appear only when the array is in action
|
||||||
|
$proc = $3;
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
# array is not in action
|
||||||
|
unshift(@text, $line);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
# skip until first line is found
|
||||||
|
next;
|
||||||
|
}
|
||||||
|
|
||||||
if ( $ARGV[0] and $ARGV[0] eq "config" ) {
|
if ( $ARGV[0] and $ARGV[0] eq "config" ) {
|
||||||
print "$dev.label $dev\n";
|
print "$dev.label $dev\n";
|
||||||
print "$dev.info $type $members\n";
|
print "$dev.info $type $members\n";
|
||||||
|
@ -74,7 +109,6 @@ while ($text =~ /(md\d+)\s+:\s+active\s+(\(auto-read-only\)\s+|)(\w+)\s+(.*)\n.*
|
||||||
print $dev, "_rebuild.value $rpct\n";
|
print $dev, "_rebuild.value $rpct\n";
|
||||||
print $dev, "_check.value $cpct\n";
|
print $dev, "_check.value $cpct\n";
|
||||||
}
|
}
|
||||||
$text = $';
|
|
||||||
}
|
}
|
||||||
|
|
||||||
exit 0;
|
exit 0;
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue