grep help?

Status
Not open for further replies.

danb35

Hall of Famer
Joined
Aug 16, 2011
Messages
15,504
I ran across what promises to be a handy script at https://serverfault.com/a/708808, which is supposed to monitor pool status and light the status LED for failed disks. It uses sas2ircu, so should at a minimum be able to support Supermicro chassis with SAS expander backplanes; it might well have broader applicability.

I've made two small changes to it: (1) I've set a variable for the directory where the script and its few data files will live, and (2) I've added an extension to the data file names to match the pool name (so the script can be used to monitor more than one pool).

Unfortunately, it isn't working; grep is complaining. The offending statements appear to be here:
drivelist=$(zpool status $1 | grep -E $'^\t ' | grep -vE "^\W+($1|NAME|mirror|logs|spares)" | sed -E $'s/^[\t ]+//;s/([a-z0-9]+).*/\\1/')
and here:
encaddr=$(echo "$saslist" | grep $sasaddr -B 2 | sed -E 'N;s/^.*: ([0-9]+)\n.*: ([0-9]+)/\1:\2/')

I can play with grep a little and have never touched sed, so this really looks like gibberish to me. Here's the complete script:

Code:
#! /usr/local/bin/bash
BASEDIR=/mnt/ssdpool/scripts

if [ ! "$1" ]; then
  echo "Usage: zpscan.sh pool "
  echo "Scan a pool, send email notification and activate leds of failed drives"
  exit
fi
if [ ! -d $BASEDIR/.sas2ircu ]; then
  mkdir $BASEDIR/.sas2ircu
  touch $BASEDIR/.sas2ircu/drives-$1
  touch $BASEDIR/.sas2ircu/locs-$1
fi
if [ "$2" ]; then
  email="$2"
else
  email="root"
fi
condition=$(/sbin/zpool status $1 | egrep -i '(DEGRADED|FAULTED|OFFLINE|UNAVAIL|REMOVED|FAIL|DESTROYED|corrupt|cannot|unrecover)')
if [ "${condition}" ]; then
  emailSubject="`hostname` - ZFS pool - HEALTH fault"
  mailbody=$(zpool status $1)
  echo "Sending email notification of degraded zpool $1"
  echo "$mailbody" | mail -s "Degraded Zpool $1 on hostname" $email
  drivelist=$(zpool status $1 | grep -E "(DEGRADED|FAULTED|OFFLINE|UNAVAIL|REMOVED|FAIL|DESTROYED)" | grep -vE "^\W+($1|NAME|mirror|logs|spares)" | sed -E $'s/.*was \/dev\/([0-9a-z]+)/\\1/;s/^[\t  ]+([0-9a-z]+)[\t ]+.*$/\\1/')
  echo "Locating failed drives."
  for drive in $drivelist;
  do
  record=$(grep -E "^$drive" $BASEDIR/.sas2ircu/drives-$1)
  location=$(echo $record | cut -f 3 -d " ")
  echo Locating: $record
  sas2ircu 0 locate $location ON
  if [ ! "$(egrep $location $BASEDIR/.sas2ircu/locs-$1)" ]; then
  echo $location >> $BASEDIR/.sas2ircu/locs-$1
  fi
  done
else
  echo "Saving drive list."
  drivelist=$(zpool status $1 | grep -E $'^\t  ' | grep -vE "^\W+($1|NAME|mirror|logs|spares)" | sed -E $'s/^[\t ]+//;s/([a-z0-9]+).*/\\1/')
  saslist=$(sas2ircu 0 display)
  printf "" > $BASEDIR/.sas2ircu/drives-$1
  for drive in $drivelist;
  do
  sasaddr=$(sg_vpd -i -q $drive 2>/dev/null | sed -E '2!d;s/,.*//;s/  0x//;s/([0-9a-f]{7})([0-9a-f])([0-9a-f]{4})([0-9a-f]{4})/\1-\2-\3-\4/')
  encaddr=$(echo "$saslist" | grep $sasaddr -B 2 | sed -E 'N;s/^.*: ([0-9]+)\n.*: ([0-9]+)/\1:\2/')
  echo $drive $sasaddr $encaddr >> $BASEDIR/.sas2ircu/drives-$1
  done

  for loc in $(cat $BASEDIR/.sas2ircu/locs-$1);
  do
  sas2ircu 0 locate $loc OFF
  done
  printf "" > $BASEDIR/.sas2ircu/locs-$1
fi



Any thoughts?
 

Arwen

MVP
Joined
May 17, 2014
Messages
3,611
I have seen differences between Linux scripts and Linux tools, as compared to other Unix flavors.
Basically, Linux tools, (Shells, Grep, Sed, etc...), are developed faster and may have features that
have not yet appeared elsewhere, like FreeBSD.

Testing just now, the first works with Linux, (using ZFSonLinux). But, it does barf on FreeBSD. I
spent a minute trying to get the correct syntax, but did not suceed. Don't have time right now.

Here are the versions of Grep on FreeNAS 9.10.x and recent Linux;
Code:
# grep -V
grep (GNU grep) 2.5.1-FreeBSD

Copyright 1988, 1992-1999, 2000, 2001 Free Software Foundation, Inc.
This is free software; see the source for copying conditions. There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.


> grep -V
grep (GNU grep) 2.27
Copyright (C) 2016 Free Software Foundation, Inc.
License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>.
This is free software: you are free to change and redistribute it.
There is NO WARRANTY, to the extent permitted by law.

Written by Mike Haertel and others, see <http://git.sv.gnu.org/cgit/grep.git/tree/AUTHORS>.
 

HolyK

Ninja Turtle
Moderator
Joined
May 26, 2011
Messages
654
I don't have SAS here. Could you paste how the zpool status <pool> looks like? Because the line with "drivelist" returns this for me:

[root@HolyNAS ~]# zpool status redmirror | grep -E $'^\t ' | grep -vE "^\W+(redmirror|NAME|mirror|logs|spares)" | sed -E $'s/^[\t ]+//;s/([a-z0-9]+).*/\\1/'
gptid
gptid


Which does not make much sense...
 

danb35

Hall of Famer
Joined
Aug 16, 2011
Messages
15,504
I don't believe SAS vs. SATA makes a difference. In my case, I have SATA disks on a SAS backplane and HBA.
Code:
  pool: tank
state: ONLINE
  scan: scrub in progress since Sun May 28 00:00:24 2017
  33.8T scanned out of 36.3T at 251M/s, 2h53m to go
  0 repaired, 93.12% done
config:

   NAME  STATE  READ WRITE CKSUM
   tank  ONLINE  0  0  0
	raidz2-0  ONLINE  0  0  0
	gptid/9a85d15f-8d5c-11e4-8732-0cc47a01304d  ONLINE  0  0  0
	gptid/9afa89ae-8d5c-11e4-8732-0cc47a01304d  ONLINE  0  0  0
	gptid/9b6cc00b-8d5c-11e4-8732-0cc47a01304d  ONLINE  0  0  0
	gptid/9c501d57-8d5c-11e4-8732-0cc47a01304d  ONLINE  0  0  0
	gptid/9cc41939-8d5c-11e4-8732-0cc47a01304d  ONLINE  0  0  0
	gptid/9d39e31d-8d5c-11e4-8732-0cc47a01304d  ONLINE  0  0  0
	raidz2-1  ONLINE  0  0  0
	gptid/f5b737a6-8e41-11e4-8732-0cc47a01304d  ONLINE  0  0  0
	gptid/7e2d9269-8a4e-11e5-bec2-002590de8695  ONLINE  0  0  0
	gptid/f68f4fa9-8e41-11e4-8732-0cc47a01304d  ONLINE  0  0  0
	gptid/f722e509-8e41-11e4-8732-0cc47a01304d  ONLINE  0  0  0
	gptid/56c2074c-657f-11e6-877d-002590caf340  ONLINE  0  0  0
	gptid/82d5cbf5-2a41-11e6-a151-002590caf340  ONLINE  0  0  0
	raidz2-2  ONLINE  0  0  0
	gptid/2c854638-212c-11e6-881c-002590caf340  ONLINE  0  0  0
	gptid/2dc4f155-212c-11e6-881c-002590caf340  ONLINE  0  0  0
	gptid/b7625ade-6980-11e6-877d-002590caf340  ONLINE  0  0  0
	gptid/1abefcac-acb9-11e6-8df3-002590caf340  ONLINE  0  0  0
	gptid/3d317e50-67dd-11e6-877d-002590caf340  ONLINE  0  0  0
	gptid/192328aa-fc8e-11e6-aef3-002590caf340  ONLINE  0  0  0

errors: No known data errors


Edit: Yes, all those gptids are actually indented. Stupid formatting. Don't know if it's this forum or my terminal app.
upload_2017-5-30_18-58-12.png
 

danb35

Hall of Famer
Joined
Aug 16, 2011
Messages
15,504
Which does not make much sense...
I'm guessing that a "normal" pool would have members like ada0, ada1, ada2, etc., without the / separator in the gptid designators FreeNAS uses.
 

danb35

Hall of Famer
Joined
Aug 16, 2011
Messages
15,504
I don't have SAS here. Could you paste how the zpool status <pool> looks like? Because the line with "drivelist" returns this for me:
It's really doing the same for me:
Code:
➜  ~ zpool status testpool | grep -E $'^\t  ' | grep -vE "^\W+(testpool|NAME|mirror|logs|spares)" | sed -E $'s/^[\t ]+//;s/([a-z0-9]+).*/\\1/'
gptid
gptid


However, with a handy little script from @turvas, it's looking better:
Code:
➜  ~ zpool status testpool | /mnt/ssdpool/scripts/fixer.sh | grep -E $'^\t  ' | grep -vE "^\W+(testpool|NAME|mirror|logs|spares)" | sed -E $'s/^[\t ]+//;s/([a-z0-9]+).*/\\1/'
da19p2
da20p2
 

danb35

Hall of Famer
Joined
Aug 16, 2011
Messages
15,504
This is getting strange.
Code:
[root@freenas2 /mnt/ssdpool/scripts]# ./zpscan.sh testpool
Saving drive list.
Usage: grep [OPTION]... PATTERN [ FILE ]...
Try `grep --help' for more information.
Usage: grep [OPTION]... PATTERN [ FILE ]...
Try `grep --help' for more information.
[root@freenas2 /mnt/ssdpool/scripts]#

...but...
Code:
[root@freenas2 /mnt/ssdpool/scripts]# POOL=testpool;drivelist=$(zpool status $POOL | /mnt/ssdpool/scripts/fixer.sh | grep -E $'^\t  ' | grep -vE "^\W+($POOL|NAME|mirror|logs|spares)" | sed -E $'s/^[\t ]+//;s/([a-z0-9]+).*/\\1/')
[root@freenas2 /mnt/ssdpool/scripts]# echo $drivelist
da19p2 da20p2

It's probably something obvious I'm missing, but it's making my brain hurt right now.
 

danb35

Hall of Famer
Joined
Aug 16, 2011
Messages
15,504
OK, making some progress. Apparently those damned white-label drives are causing trouble again:
Code:
[root@freenas2 /mnt/ssdpool/scripts]# sg_vpd -i -q da13
  0x5000c5008cd437e4
  0x5003048000e57b99
[root@freenas2 /mnt/ssdpool/scripts]# sg_vpd -i -q da20
  << bad NAA nibble, expected 2, 3, 5 or 6, got 0>>
 00  00 00 00 00 00 00 00 00  ........
  0x5003048000e57ba0

da13 is a Seagate NAS disk that's part of my main pool. da20 is a white-label 6 TB disk that's one of the two disks comprising testpool.

I also expect that, even with fixer.sh, there's further work that will need to be done, as da13p2 won't map to any slot in the enclosure (and sg_vpd da13p2 returns an error as well).

So, for testing purposes, and using a disk that does work with sg_vpd, I created a pool from the CLI: zpool create onedisk da18. I then tried the script again:
Code:
[root@freenas2] /mnt/ssdpool/scripts# ./zpscan.sh onedisk
Saving drive list.
[root@freenas2] /mnt/ssdpool/scripts#

It seems to have worked:
Code:
[root@freenas2] /mnt/ssdpool/scripts# ll .sas2ircu/
total 19
drwxr-xr-x  2 root  wheel  6 Jul  5 21:57 ./
drwxr-xr-x  4 root  wheel  12 Jul  5 22:03 ../
-rw-r--r--  1 root  wheel  30 Jul  5 22:03 drives-onedisk
-rw-r--r--  1 root  wheel  14 Jul  5 21:32 drives-testpool
-rw-r--r--  1 root  wheel  0 Jul  5 22:03 locs-onedisk
-rw-r--r--  1 root  wheel  0 Jul  5 21:32 locs-testpool
[root@freenas2] /mnt/ssdpool/scripts# cat .sas2ircu/drives-onedisk
da18 5003048-0-00e5-7b9e 2:18

Only problem is that, since onedisk only has one disk, I can't offline a disk to see how well the script works in that case. Baby steps...
 

Stux

MVP
Joined
Jun 2, 2016
Messages
4,419
With grep and sed processes the important thing is to work out what they are trying to do. An approach to debugging this would be to pipe things in and out of text files at each stage so you can see what the input in and out is. Then you can work on getting the grep to work and then the sed etc.

It may be simpler to reimplement the logic in awk for example
 

carlos juan

Dabbler
Joined
Sep 12, 2014
Messages
38
OK, making some progress. Apparently those damned white-label drives are causing trouble again:
Code:
[root@freenas2 /mnt/ssdpool/scripts]# sg_vpd -i -q da13
  0x5000c5008cd437e4
  0x5003048000e57b99
[root@freenas2 /mnt/ssdpool/scripts]# sg_vpd -i -q da20
  << bad NAA nibble, expected 2, 3, 5 or 6, got 0>>
00  00 00 00 00 00 00 00 00  ........
  0x5003048000e57ba0

da13 is a Seagate NAS disk that's part of my main pool. da20 is a white-label 6 TB disk that's one of the two disks comprising testpool.

I also expect that, even with fixer.sh, there's further work that will need to be done, as da13p2 won't map to any slot in the enclosure (and sg_vpd da13p2 returns an error as well).

So, for testing purposes, and using a disk that does work with sg_vpd, I created a pool from the CLI: zpool create onedisk da18. I then tried the script again:
Code:
[root@freenas2] /mnt/ssdpool/scripts# ./zpscan.sh onedisk
Saving drive list.
[root@freenas2] /mnt/ssdpool/scripts#

It seems to have worked:
Code:
[root@freenas2] /mnt/ssdpool/scripts# ll .sas2ircu/
total 19
drwxr-xr-x  2 root  wheel  6 Jul  5 21:57 ./
drwxr-xr-x  4 root  wheel  12 Jul  5 22:03 ../
-rw-r--r--  1 root  wheel  30 Jul  5 22:03 drives-onedisk
-rw-r--r--  1 root  wheel  14 Jul  5 21:32 drives-testpool
-rw-r--r--  1 root  wheel  0 Jul  5 22:03 locs-onedisk
-rw-r--r--  1 root  wheel  0 Jul  5 21:32 locs-testpool
[root@freenas2] /mnt/ssdpool/scripts# cat .sas2ircu/drives-onedisk
da18 5003048-0-00e5-7b9e 2:18

Only problem is that, since onedisk only has one disk, I can't offline a disk to see how well the script works in that case. Baby steps...

I have days triying to make that script work, but as a noob, no dice :( did you made it work at the end??? have you sg3_utils installed in your freenas box? I would love to hear how you made this work at the end... I couldnt!
 

danb35

Hall of Famer
Joined
Aug 16, 2011
Messages
15,504
No, I don't have it working yet, and haven't given it much time lately. I would like to get this functionality working, but it sounds like it's going to be somewhat complicated. I should probably try to step through it on a Linux box first, just to follow along with what it's actually doing.
 

danb35

Hall of Famer
Joined
Aug 16, 2011
Messages
15,504
have you sg3_utils installed in your freenas box?
The only utility from that that seems to be used by the script (either yours or mine) is sg_vpd, which is part of the base FreeNAS install. I haven't compared the two scripts to see how they differ, but they look like they're at least similar in concept.
 

carlos juan

Dabbler
Joined
Sep 12, 2014
Messages
38
The only utility from that that seems to be used by the script (either yours or mine) is sg_vpd, which is part of the base FreeNAS install. I haven't compared the two scripts to see how they differ, but they look like they're at least similar in concept.
Thank you! This helps a lot, I will learn some grep, arw basics and try to make it work, if I do will get back here and post it. If you do it first, please post your results :)

Sent from my XT1095 using Tapatalk
 

danb35

Hall of Famer
Joined
Aug 16, 2011
Messages
15,504
For anyone who might be watching (and care), I seem to have it working. Now resource-ified here, with the script uploaded rather than inline, and a few tweaks. Further discussion probably belongs in the associated discussion thread.
 
Status
Not open for further replies.
Top