How to iterate the loop for a matched pattern and print the count using awk

Viewed 78

i have below multipath output.

mig_1 (3624a9370908ba6a5e68a464600014087) dm-11 PURE,FlashArray
size=5.0T features='0' hwhandler='0' wp=rw
`-+- policy='queue-length 0' prio=1 status=active
  |- 0:0:2:3 sdeo 129:0  active ready running
  |- 0:0:3:3 sdep 129:16 active ready running
  |- 1:0:2:3 sdeq 129:32 active ready running
  `- 1:0:3:3 sder 129:48 active ready running
mig_2 (3624a9370908eb6a5f69a4646000140bb) dm-10 PURE,FlashArray
size=7.0T features='0' hwhandler='0' wp=rw
`-+- policy='queue-length 0' prio=1 status=active
  |- 0:0:2:2 sdkg 66:320 active ready running
  |- 0:0:3:2 sdkh 66:336 active ready running
  |- 1:0:2:2 sdki 66:352 active ready running
  `- 1:0:3:2 sdkj 66:368 active ready running

Would like to check how many active paths exist for each LUN. If the LUN is not having /active ready running/ it should decrease the count. Tried the below way, but it is not providing the required result. It is providing the total count for all the LUN's, also size is coming in different line.

 cat multi | awk '/dm-1/ {print $1"\t"$2} ; /^size/ {print $1} ; /active ready running/ {a++} END {print a}'
mig_1   (3624a9370908ba6a5e68a464600014087)
size=5.0T
mig_2   (3624a9370908eb6a5f69a4646000140bb)
size=7.0T
8

Expected output like below:

Disk  #1:  mig_1  (3624a9370908ba6a5e68a464600014087)  size=5.0T  active_paths:4
Disk  #2:  mig_2  (3624a9370908eb6a5f69a4646000140bb)  size=7.0T  active_paths:4

Kindly advise how to achieve the above output.

2 Answers

With your shown samples(only), please try following awk code. Written and tested in GNU awk, should work in any awk version.

awk '
/^mig_[0-9]+/{
  if(found && val1 && val2 && count){
    print "Disk  #" ++count2,val1,val2,"active_paths:"count
    count=val1=val2=found=""
  }
  val1=$1 OFS $2
  found=1
  next
}
found && /^size=[0-9]+/{
  val2=$1
  next
}
found && /active ready running/{
  count++
}
END{
  if(found && val1 && val2 && count){
    print "Disk  #" ++count2,val1,val2,"active_paths:"count
  }
}
'   Input_file

Standing on the shoulders of giants, my answer is based on @RavinderSingh13's answer. I pointed out in the comments that aside from being great awk code, it will not print a line for devices with no active paths, thus missing devices with all-paths-down. Being a storage support engineer for over 5 years and a storage admin for just over two, I know first-hand that having a line with "0" active paths is probably the most critical. So, I revised RavinderSingh13's awk a little to account for this. I also slimmed it down by cutting away some of the unnecessary bits

awk '
/^mig_[0-9]+/{
  if(found){
    if(!active){
      active="ALL PATHS DOWN"
    }
    print "Disk  #" ++count,val1,val2,"active_paths:"active
    active=val1=val2=""
  }
  else{
    found=1
  }
  val1=$1 OFS $2
  next
}
/^size=[0-9]+/{
  val2=$1
  next
}
/active ready running/{
  active++
}
END{
  if(!active){
    active="ALL PATHS DOWN"
  }
  print "Disk  #" ++count,val1,val2,"active_paths:"active
}
' mpath.txt

mpath.txt (note that I don't have access to a SAN or server with multipathed devices, let alone one with non-active/healthy paths, so I made up the output "failed faulty stopped" - regardless, these arbitrary strings have no impact on my awk code modifications - it will work on any path line in the output that is not strictly "active ready running")

mig_1 (3624a9370908ba6a5e68a464600014087) dm-11 PURE,FlashArray
size=5.0T features='0' hwhandler='0' wp=rw
`-+- policy='queue-length 0' prio=1 status=active
  |- 0:0:2:3 sdeo 129:0  active ready running
  |- 0:0:3:3 sdep 129:16 failed faulty stopped
  |- 1:0:2:3 sdeq 129:32 active ready running
  `- 1:0:3:3 sder 129:48 active ready running
mig_2 (3624a9370908eb6a5f69a4646000140bb) dm-10 PURE,FlashArray
size=7.0T features='0' hwhandler='0' wp=rw
`-+- policy='queue-length 0' prio=1 status=active
  |- 0:0:2:2 sdkg 66:320 failed faulty stopped
  |- 0:0:3:2 sdkh 66:336 failed faulty stopped
  |- 1:0:2:2 sdki 66:352 failed faulty stopped
  `- 1:0:3:2 sdkj 66:368 failed faulty stopped
mig_1 (3624a9370908ba6a5e68a464600014087) dm-11 PURE,FlashArray
size=5.0T features='0' hwhandler='0' wp=rw
`-+- policy='queue-length 0' prio=1 status=active
  |- 0:0:2:3 sdeo 129:0  active ready running
  |- 0:0:3:3 sdep 129:16 active ready running
  |- 1:0:2:3 sdeq 129:32 active ready running
  `- 1:0:3:3 sder 129:48 active ready running
mig_2 (3624a9370908eb6a5f69a4646000140bb) dm-10 PURE,FlashArray
size=7.0T features='0' hwhandler='0' wp=rw
`-+- policy='queue-length 0' prio=1 status=active
  |- 0:0:2:2 sdkg 66:320 active ready running
  |- 0:0:3:2 sdkh 66:336 active ready running
  |- 1:0:2:2 sdki 66:352 active ready running
  `- 1:0:3:2 sdkj 66:368 active ready running

I hope my modifications are welcome and I hope RavinderSingh13 gets 98% of the credit.

Final Output:

...

Disk  #1 mig_1 (3624a9370908ba6a5e68a464600014087) size=5.0T active_paths:3
Disk  #2 mig_2 (3624a9370908eb6a5f69a4646000140bb) size=7.0T active_paths:ALL PATHS DOWN
Disk  #3 mig_1 (3624a9370908ba6a5e68a464600014087) size=5.0T active_paths:4
Disk  #4 mig_2 (3624a9370908eb6a5f69a4646000140bb) size=7.0T active_paths:4
Related