mdadm script

Orion Poplawski <[email protected]>
Newsgroups gmane.comp.log.logwatch.devel
Message-ID <[email protected]>
Couple comments on the mdadm script:

- I don't like scripts that *always* print something out with detail 0.

- mdadm --examine --scan outputs some funny devices on some of my machines:

ARRAY /dev/md/0 metadata=1.0 UUID=93d2aefb:2c402016:b825d44d:cd040a6e 
name=alexandria.cora.nwra.com:0
ARRAY /dev/md/5 metadata=1.1 UUID=fd717989:817f4441:90c97e46:a8544da4 
name=alexandria.cora.nwra.com:5
ARRAY /dev/md2 UUID=e3ffa7e3:0e09ccd8:3f1a1245:80507cc5
ARRAY /dev/md4 UUID=845fab3d:edb3e2be:43acf834:bcf1ba31
ARRAY /dev/md5 UUID=e3ab905a:c34c5991:0e157ac7:8ddbc516
ARRAY /dev/md3 UUID=d008359d:295ada4d:561c4e14:32809f19

These are not the device filenames used:
# ls /dev/md*
/dev/md0  /dev/md1  /dev/md2  /dev/md3  /dev/md4  /dev/md5

And doesn't match /etc/mdadm.conf:
ARRAY /dev/md0 level=raid1 num-devices=2 UUID=93d2aefb:2c402016:b825d44d:cd040a6e
ARRAY /dev/md1 level=raid1 num-devices=2 UUID=fd717989:817f4441:90c97e46:a8544da4
ARRAY /dev/md2 metadata=0.90 UUID=e3ffa7e3:0e09ccd8:3f1a1245:80507cc5
ARRAY /dev/md3 metadata=0.90 UUID=d008359d:295ada4d:561c4e14:32809f19
ARRAY /dev/md4 metadata=0.90 UUID=845fab3d:edb3e2be:43acf834:bcf1ba31
ARRAY /dev/md5 metadata=0.90 UUID=e3ab905a:c34c5991:0e157ac7:8ddbc516

I think the device names reported are an artifact of the install environment 
and the names assigned then.

I suppose the idea behind using the output of mdadm --examine --scan rather 
than /etc/mdadm.conf or /proc/mdstat is that it might pick up more 
unconfigured arrays and so forth?  I've changed it here to use /etc/mdadm.conf 
if it exists instead, and to use the output of mdadm if it doesn't.

- Let's parse the output of commands directly (why call awk and grep from perl? :)


-- 
Orion Poplawski
Technical Manager                     303-415-9701 x222
NWRA, Boulder Office                  FAX: 303-415-9702
3380 Mitchell Lane                       [email protected]
Boulder, CO 80301                   http://www.nwra.com

------------------------------------------------------------------------------
Master HTML5, CSS3, ASP.NET, MVC, AJAX, Knockout.js, Web API and
much more. Get web development skills now with LearnDevNow -
350+ hours of step-by-step video tutorials by Microsoft MVPs and experts.
SALE $99.99 this month only -- learn more at:
http://p.sf.net/sfu/learnmore_122812

_______________________________________________
Logwatch-devel mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/logwatch-devel
logwatch-mdadm.patch (text/x-patch, 6 KB)
Index: scripts/services/mdadm
===================================================================
--- scripts/services/mdadm	(revision 130)
+++ scripts/services/mdadm	(working copy)
@@ -26,80 +26,88 @@
 
 my $Detail = $ENV{'LOGWATCH_DETAIL_LEVEL'} || 0;
 
-# TODO: remove awk dependency
-my $devices = `mdadm --examine --scan 2>/dev/null| awk '{print \$2}'`;
+my @devices = ();
+# Sometimes mdadm --examine --scan reports device with different names than
+# what is in use.  Use /etc/mdadm.conf instead if it exists.
+if ( -f "/etc/mdadm.conf" ) {
+  open(MDADM,"< /etc/mdadm.conf");
+} else {
+  open(MDADM,"mdadm --examine --scan 2>/dev/null|");
+}
+while (<MDADM>) {
+  if (/^ARRAY/) {
+    push(@devices,(split())[1]);
+  }
+}
+close(MDADM);
 
-chomp($devices);
+foreach my $dev (@devices) {
+  my %mdhash;
 
-if ($devices ne '') {
-  foreach my $dev (split(/\n/,$devices)) {
-    my $md = `mdadm --misc --detail $dev | grep -v $dev`;
-    chomp($md);
+  open(MDADM,"mdadm --misc --detail $dev |");
+  while (<MDADM>) {
+    $mdhash{'level'} = $1 if ($_ =~ /Raid Level ?: ?(.*)$/);
+    $mdhash{'active'} = $1 if ($_ =~ /Active Devices ?: ?(.*)$/);
+    $mdhash{'working'} = $1 if ($_ =~ /Working Devices ?: ?(.*)$/);
+    $mdhash{'failed'} = $1 if ($_ =~ /Failed Devices ?: ?(.*)$/);
+    $mdhash{'spare'} = $1 if ($_ =~ /Spare Devices ?: ?(.*)$/);
+    $mdhash{'state'} = $1 if ($_ =~ /State ?: ?(.*)$/);
+    $mdhash{'rebuild'} = $1 if ($_ =~ /Rebuild Status ?: ?(.*)$/);
+    push(@{$mdhash{'good devices'}},$1) if ($_ =~ /sync .*(\/dev\/[\w\d\-\_]*)/);
+    push(@{$mdhash{'middle devices'}},$1) if ($_ =~ /rebuilding .*(\/dev\/[\w\d\-\_]*)/);
+    push(@{$mdhash{'bad devices'}},$1) if ($_ =~ /faulty .*(\/dev\/[\w\d\-\_]*)/);
+  }
+  close(MDADM);
 
-    my %mdhash;
-    foreach (split(/\n/,$md)) {
-      $mdhash{'level'} = $1 if ($_ =~ /Raid Level ?: ?(.*)$/);
-      $mdhash{'active'} = $1 if ($_ =~ /Active Devices ?: ?(.*)$/);
-      $mdhash{'working'} = $1 if ($_ =~ /Working Devices ?: ?(.*)$/);
-      $mdhash{'failed'} = $1 if ($_ =~ /Failed Devices ?: ?(.*)$/);
-      $mdhash{'spare'} = $1 if ($_ =~ /Spare Devices ?: ?(.*)$/);
-      $mdhash{'state'} = $1 if ($_ =~ /State ?: ?(.*)$/);
-      $mdhash{'rebuild'} = $1 if ($_ =~ /Rebuild Status ?: ?(.*)$/);
-      push(@{$mdhash{'good devices'}},$1) if ($_ =~ /sync .*(\/dev\/[\w\d\-\_]*)/);
-      push(@{$mdhash{'middle devices'}},$1) if ($_ =~ /rebuilding .*(\/dev\/[\w\d\-\_]*)/);
-      push(@{$mdhash{'bad devices'}},$1) if ($_ =~ /faulty .*(\/dev\/[\w\d\-\_]*)/);
-    }
-
-    if ($Detail <= 4) {
-      if (lc($mdhash{'state'}) =~ /clean|active/) {
-        print "$dev : $mdhash{'state'}\n";
-      } else {
-        print "$dev : $mdhash{'state'}\n";
-        if (@{$mdhash{'middle devices'}}) {
-          if (defined($mdhash{'rebuild'}) and ($mdhash{'rebuild'} ne '')) {
-            print "\tRebuilding status: $mdhash{'rebuild'}\n";
-          }
-          print "\tRebuilding @{$mdhash{'middle devices'}}\n";
+  if ($Detail <= 4) {
+    if (lc($mdhash{'state'}) =~ /clean|active/ and $Detail) {
+      print "$dev : $mdhash{'state'}\n";
+    } else {
+      print "$dev : $mdhash{'state'}\n";
+      if (@{$mdhash{'middle devices'}}) {
+        if (defined($mdhash{'rebuild'}) and ($mdhash{'rebuild'} ne '')) {
+          print "\tRebuilding status: $mdhash{'rebuild'}\n";
         }
-        if (@{$mdhash{'bad devices'}}) {
-          print "\tFailed @{$mdhash{'bad devices'}}\n";
-        }
+        print "\tRebuilding @{$mdhash{'middle devices'}}\n";
       }
+      if (@{$mdhash{'bad devices'}}) {
+        print "\tFailed @{$mdhash{'bad devices'}}\n";
+      }
     }
-    elsif($Detail <= 9) {
-      if (lc($mdhash{'state'}) =~ /clean|active/) {
-        print "$dev : $mdhash{'state'} - @{$mdhash{'good devices'}}\n";
-      } else {
-        print "$dev : $mdhash{'state'}\n";
-        if (@{$mdhash{'middle devices'}}) {
-          if (defined($mdhash{'rebuild'}) and ($mdhash{'rebuild'} ne '')) {
-            print "\tRebuilding status: $mdhash{'rebuild'}\n";
-          }
-          print "\t  Rebuilding : @{$mdhash{'middle devices'}}\n";
-        }
-        if (@{$mdhash{'bad devices'}}) {
-          print "\t      Failed : @{$mdhash{'bad devices'}}\n";
-        }
-        print "\t        Good : @{$mdhash{'good devices'}}\n";
-      }
+  }
+  elsif($Detail <= 9) {
+    if (lc($mdhash{'state'}) =~ /clean|active/) {
+      print "$dev : $mdhash{'state'} - @{$mdhash{'good devices'}}\n";
     } else {
       print "$dev : $mdhash{'state'}\n";
-      print "\t  Raid Level : $mdhash{'level'}\n";
-      print "\tGood Devices : @{$mdhash{'good devices'}}\n";
-      if (defined ($mdhash{'middle devices'}) and @{$mdhash{'middle devices'}}) {
+      if (@{$mdhash{'middle devices'}}) {
         if (defined($mdhash{'rebuild'}) and ($mdhash{'rebuild'} ne '')) {
           print "\tRebuilding status: $mdhash{'rebuild'}\n";
         }
         print "\t  Rebuilding : @{$mdhash{'middle devices'}}\n";
       }
-      if (defined($mdhash{'bad devices'}) and @{$mdhash{'bad devices'}}) {
+      if (@{$mdhash{'bad devices'}}) {
         print "\t      Failed : @{$mdhash{'bad devices'}}\n";
       }
-      if ($mdhash{'spare'} ne 0) {
-        print "\t      Spares : $mdhash{'spare'}\n";
+      print "\t        Good : @{$mdhash{'good devices'}}\n";
+    }
+  } else {
+    print "$dev : $mdhash{'state'}\n";
+    print "\t  Raid Level : $mdhash{'level'}\n";
+    print "\tGood Devices : @{$mdhash{'good devices'}}\n";
+    if (defined ($mdhash{'middle devices'}) and @{$mdhash{'middle devices'}}) {
+      if (defined($mdhash{'rebuild'}) and ($mdhash{'rebuild'} ne '')) {
+        print "\tRebuilding status: $mdhash{'rebuild'}\n";
       }
-      print "\n";
+      print "\t  Rebuilding : @{$mdhash{'middle devices'}}\n";
     }
+    if (defined($mdhash{'bad devices'}) and @{$mdhash{'bad devices'}}) {
+      print "\t      Failed : @{$mdhash{'bad devices'}}\n";
+    }
+    if ($mdhash{'spare'} ne 0) {
+      print "\t      Spares : $mdhash{'spare'}\n";
+    }
+    print "\n";
   }
 }
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.