Tuesday, 4 June 2019

Perl one liner to extract LUNid and disk alias from /etc/multipath.conf file

Introduction:

We may run into situations wherein we need to fetch the LUN id and alias mapping for disks under multipath on a Linux machine. Obtaining this data manually would prove to be cumbersome. One way to fetch this data would be to use the combination of grep and paste commands. But I felt that my Perl was getting a bit rusty so I decided to go the Perl way.

First take a look at the entries from the sample file.

        multipath {
                wwid                    36000d3100008f20000000000000001f4
                alias                   dvd-rhel5-2-64
        }
        multipath {
                wwid                    36000d3100008f20000000000000001f6
                alias                   dvd-rhel5-2-32
        }
        multipath {
                wwid                    36000d3100008f20000000000000003e2
                alias                   dvd-rhel4-7-32
        }

The above output shows the multipath stanzas for a couple of disks. We are basically interested in the wwid and alias section. To extract the required information we will be using the below combination of two Perl one liners.

[root@sahil-lab1 ~]# cat mpath.cf  | perl -ne 'print if(/wwid|alias/);' | perl -pne 'if($.%2){s/\n/\t/;}'
                wwid                    36000d3100008f2000000000000000356                       alias                   aleppo
                wwid                    36000d3100008f2000000000000000a3a                       alias                   dc2tst
                wwid                    36000d3100008f2000000000000000b02                       alias                   dc1tst
                wwid                    36000d3100008f20000000000000003cf                       alias                   algiers
                wwid                    36000d3100008f2000000000000000397                       alias                   algiers_local
                wwid                    36000d3100008f200000000000000004b                       alias                   chicago
                wwid                    36000d3100008f200000000000000004c                       alias                   chicago_mysql
                wwid                    36000d3100008f200000000000000004d                       alias                   chicago_local
                wwid                    36000d3100008f200000000000000004e                       alias                   chicago_assets
                wwid                    36000d3100008f20000000000000001f4                       alias                   dvd-rhel5-2-64
                wwid                    36000d3100008f20000000000000001f6                       alias                   dvd-rhel5-2-32
                wwid                    36000d3100008f20000000000000003e2                               alias                   dvd-rhel4-7-32
[root@sahil-lab1 ~]#

You could further add an additional Perl one liner to print only the alias and LUN id as shown below.

[root@sahil-lab1~]# cat mpath.cf  | perl -ne 'print if(/wwid|alias/);' | perl -pne 'if($.%2){s/\n/\t/;}' | perl -F"\s+" -lane 'print "$F[4]  $F[2]"' 
 aleppo  36000d3100008f2000000000000000356
dc2tst  36000d3100008f2000000000000000a3a
dc1tst  36000d3100008f2000000000000000b02
algiers  36000d3100008f20000000000000003cf
algiers_local  36000d3100008f2000000000000000397
chicago  36000d3100008f200000000000000004b
chicago_mysql  36000d3100008f200000000000000004c
chicago_local  36000d3100008f200000000000000004d
chicago_assets  36000d3100008f200000000000000004e
dvd-rhel5-2-64  36000d3100008f20000000000000001f4
dvd-rhel5-2-32  36000d3100008f20000000000000001f6
dvd-rhel4-7-32  36000d3100008f20000000000000003e2
[root@sahil-lab1~]#


Explanation:

The first one liner simply prints lines containing the strings wwid or alias.
The next one liner loops over the content piped from the previous one liner and uses $. variable denoting the line number. If the remainder of the division of the line number by 2 is not 0 i,e. the line is odd, then the new line after the end of the line gets replaced by a tab thereby combining the even and odd numbered lines together. 
The last one liner invokes the awk like functionality available with Perl one liners. The -F flag in conjunction with -a flag allow us to split lines based on a delimiter and the individual strings in the line get stored in an array variable named @F and we can extract the fields by using the scalar elements that make up the @F array.


Conclusion:

I'm sure there are easier and perhaps more compact versions of Perl one liners out there to accomplish this task. I would appreciate any suggestions and feedback on this approach of extracting the required fields from the /etc/multipath.conf file.

No comments:

Post a Comment

Using capture groups in grep in Linux

Introduction Let me start by saying that this article isn't about capture groups in grep per se. What we are going to do here with gr...