-2

以下は私を夢中にさせています。

わかりやすくするために、関数全体を提出します。ここでの目的は、Linux ミラー サブデバイスに障害が発生するたびに、またはサブデバイスが削除されたときにエラーをログに記録することです。毎回メッセージを 1 つだけ送信する必要があります。サブデバイスがミラーから完全に削除されておらず、障害状態にある場合、mdadm コマンドの出力には常に、removed の行と faulty の行が表示されます。この場合、削除された部分ではなく、サブデバイスの障害部分のみをログに記録したいだけです。

デバイスがミラーから削除されると、障害エラーはなくなり、削除されるだけです。この場合、削除されたエラーをログに記録する必要があります。

そのために、変数を使用し$myfaultyて出力を解析しmdadm --detail $md_devます。問題は、最初に来るコードの部分が 2 番目に評価されることです。

以下のコードが最初に来ます:

elsif ($subdevice_status =~ /faulty/) {
_msg("ERROR: MD device $device status $device_status, sub-device status $subdevice_status " );
_mylog('err', "ERROR: faulty MD $device $device_status: sub-device $subdevice ($subdevice_role): status $subdevice_status " );
  $myfaulty = 1;

そして、これは2番目に来ます:

elsif ($subdevice_status =~ /removed/ && $myfaulty != 1 ) {
 _msg("ERROR: MD device $device status $device_status, sub-device status $subdevice_status " );
 printf("The removed part: $myfaulty, $device, $raid_type \n");
 _mylog('err', "ERROR: MD device $device status $device_status, sub-device $subdevice_status " );

$myfaulty 変数の評価は、最初に 2 番目のコード ブロックで行われ、次に最初のコード ブロックで行われます。

何か案は?

ありがとう、ジョージ

サブコード

# checks all MD volumes it finds on the system
sub check_md() {
my $MDADMCMD = "";
my $device = "";
my $subdevice = "";
my $device_status = "";
my $subdevice_status = "";
my $raid_type = "";
my $subdevice_role = "";
my $raid_count = 0;
my $lines = 0;
my $myfaulty = 0;

    foreach(@mdadm_paths) {
            if( -e $_ ) { $MDADMCMD = $_; _debug("using mdadm in $MDADMCMD"); last; }
    }
    if($MDADMCMD eq "") { _msg("MD not configured on this system (mdadm not found) - "); return; }
    open MDSCAN, "-|", "$MDADMCMD --detail --scan --verbose 2>&1" || die "can't run: $!";
    while(<MDSCAN>) {
            $lines++;
            if($_ =~ /^ARRAY/) {
                    $_ =~ /^ARRAY\s+(\S+)\s+level=(\S+)\s+/;
                    $device = $1;
                    $raid_type = $2;
                    $raid_count++;
                    _debug("found MD device $device, raid-level $raid_type");
            open MDDETAIL, "-|", "$MDADMCMD --detail $device" || die "can't run: $!"; while(<MDDETAIL>) {
    printf($_);
                            if($_ =~ /\s+State\s+:\s+(.+)$/) { # md device status
                                    $device_status = $1;
                                    chomp($device_status);
                                    _debug("device status: $device_status");


                                    if($device_status !~ /clean$/) {
                                            change_status("CRITICAL");
                                            $md_status = "CRITICAL";

                            }

                             if($_ =~ /^\s+(\d+|-)\s+(\d+|-)\s+(\d+|-)\s+(\d+|-)\s+(\w+)\s+(\w+)\s+(\S+)\s*$/) {
                                    $subdevice_status = $5; $subdevice_role = $6; $subdevice = $7;
                                    _debug("device:$device - subdevice:$subdevice - role:$subdevice_role - status:$subdevice_status");
                                    if($subdevice_status !~ /active/) {
                                            change_status("CRITICAL");
                                            $md_status = "CRITICAL";
                                            _msg("MD $device, sub-device $subdevice ($subdevice_role): status $subdevice_status - ");
                                            if ($subdevice_role =~ /rebuilding/) {
                                            _mylog('warning', "WARNING: MD $device $device_status: sub-device $subdevice ($subdevice_role): status $subdevice_status " );
                                             }
                                            elsif ($subdevice_status =~ /faulty/) {
                                            _msg("ERROR: MD device $device status $device_status, sub-device status $subdevice_status " );
                                            _mylog('err', "ERROR: faulty MD $device $device_status: sub-device $subdevice ($subdevice_role): status $subdevice_status " );
                                            $myfaulty = 1;
                                            printf("Faulty is here:  $myfaulty, $subdevice_status, $raid_type \n");
                                            }
                                            elsif ($subdevice_status =~ /removed/ && $myfaulty != 1 ) {
                                                     _msg("ERROR: MD device $device status $device_status, sub-device status $subdevice_status " );
                                                     printf("The removed part: $myfaulty, $device, $raid_type \n");
                                                     _mylog('err', "ERROR: MD device $device status $device_status, sub-device $subdevice_status " );

                                            }
                                            else {
                                            _msg("MD $device, sub-device $subdevice ($subdevice_role): status $subdevice_status - ");
                                            _mylog('err', "ERROR: after faulty MD $device $device_status: sub-device $subdevice ($subdevice_role): status $subdevice_status " );
                                            }
                                    }
                                            else { _verbosemsg("MD $device, sub-device $subdevice ($subdevice_role): status OK - "); }
                             # Need to also catch subdevices that have been removed and don't show up anymore.
                    }
                    if ($_ =~ /^\s+(\d+|-)\s+(\d+|-)\s+(\d+|-)\s+(\d+|-)\s+(\w+)\s*$/) {
                                   $subdevice_status = $5;
                                     _debug("device:$device - subdevice:$subdevice - role:$subdevice_role - status:$subdevice_status");

                                            printf("The removed part: $myfaulty, $device, $raid_type \n");
                                    if ($subdevice_status =~ m/removed/ && $myfaulty != 1 ) {
                                            _msg("ERROR: MD device $device status $device_status, sub-device status $subdevice_status " );
                                            printf("The removed part: $myfaulty, $device, $raid_type \n");
                                            _mylog('err', "ERROR: MD device $device status $device_status, sub-device $subdevice_status " );
                                            }
                            }
                    } #while(<MDDETAIL>)
                    close(MDDETAIL);
            }
    }
    close(MDSCAN);
    # no md devices found, but command output wasn't empty
    if($raid_count == 0 && $lines > 0) { _msg("MD status is UNKNOWN (can't get configuration info) - "); }
    elsif($raid_count == 0) { _msg("MD not configured on this system - "); }
    elsif($md_status eq "OK" && not defined $verbOutput) { _msg("MD Status is OK - ");  }  }

mdadm --detail 出力

/dev/md0:
    Version : 0.90
Creation Time : Mon Mar  4 12:53:19 2013
 Raid Level : raid1
 Array Size : 521984 (509.84 MiB 534.51 MB)
Used Dev Size : 521984 (509.84 MiB 534.51 MB)
 Raid Devices : 2
Total Devices : 2
Preferred Minor : 0
Persistence : Superblock is persistent

Update Time : Thu Jul  4 16:27:46 2013
      State : clean, degraded
Active Devices : 1
Working Devices : 1
Failed Devices : 1
Spare Devices : 0

       UUID : 3a3bd078:31678889:9485a7cf:e1283d32
     Events : 0.438

Number   Major   Minor   RaidDevice State
   0       0        0        0      removed
   1       8       33        1      active sync   /dev/sdc1

   2       8       17        -      faulty spare   /dev/sdb1
4

2 に答える 2