Saturday, January 15, 2011

Bash getops - Quick & Easy

Ever wanted to create this super script with tonns of flags and parameters?
In this short tutorial I will show you how to do it in bash shell.

Let's say we have a script named:script.sh
That has 3 flags (-v for verbouse, -h for help, -i for ip address as input).
The two first parameters do not really get input, but more "tell the script how to behave", while the third parameter actually gets a value from user (ip address).

The "getopts" function is a very handful function when you’re working with script where parameters and flags have to be set.

Our script will be executed as (for example):
#./script.sh -v -i 10.100.1.2

This is the content of our short script:
#!/bin/bash 
usage() {
cat << EOF
-h help
-v verbouse mode
-i new ip address
EOF
} 
while getopts "i:hv" flag ; do
        case $flag in
                i ) IPADDR=$OPTARG;;
                h ) usage;;
                v ) VERB=1;exit 0;;
        esac
done
 
Pay attention to the options with a colon (“:”) after them need to have an argument set.
Yet if there is no column, then no argument is needed (-h, -v).

We combine while loop with "getopts" function, that will use case to determine which flags were used and what values has been assigned.

Thursday, January 6, 2011

Perl:Howto combine two arrays into hash

Hash is basically a double array that consists of keys & values. It's extremely useful when we search for some key that has been assigned with some variable.
For example let's say we have 2 files containing machines names & their serial numbers:

#cat file1
machine01
machine02
machine03

#cat file2
zxvsd
x123a
fsaz1

Now let's say we want to combine this data into one hash table, so whenever we refer to machine01 we will have immediate access to it's serial.
In Perl (v5.xx) the combined hash will look something like this:

#!/usr/bin/perl -w
use strict;
use warnings;
my @arr1=`cat file1`;
my @arr2=`cat file2`;
my %hash;
@hash{@arr1} = @arr2
Now if you iterate over %hash with "foreach" loop such as:
foreach (%hash) {
print "$_\n";
}

You will get:

machine01
zxvsd
machine02
x123a
machine3
fsaz1 
Ok, now when we know the basics let try do something more practical.
Let's say we have 2 huge arrays of machines vs serial numbers, and we
instantly want to find which serial number corresponds to what machine.
We could basically use "foreach" loop, but let's say performance is critical
In such case hash table is our best friend. 
Take a look at the following example:
 
#!/usr/bin/perl

my @machines=("server01","server02","server03");
my @serials=("1111","2222","3333");
 
#Define the hash... 
my %hash;
@hash{@machines}=@serials;

if (exists $hash{$ARGV[0]}) {
   print "Value for $ARGV[0] is: $hash{$ARGV[0]}\n";
 } else {
   print "$ARGV[0] Not found!\n";
 } 
 
What is going on here?
We defined two (short, just for sake of simplicity) arrays.
We defined a hash that combines them, it takes machines (server names)
as keys and serials as values.

Next we expect a user to give input as one argument.
If that argument exists in the hash the script will give output the 
corresponding value, if not it will say that such key wasn't found. 
 
Sample run: 
./hash_check.pl server01
Value for server01 is: 1111
  
./hash_check.pl server_foo
server_foo Not found! 

Sunday, January 2, 2011

Finding bad disk on NetApp filer

The fastest and easiest way to find a failed disk on your administered NetApp system is via command "sysconfig -d" (-d states for disks).

Let's see how it's done:

#ssh netapp001 "sysconfig -d"
Device          HA    SHELF BAY CHAN    Disk Vital Product Information
4a.16           4a    1   0     FC:B    3KS2XC8N000076209GBE
4a.17           4a    1   1     FC:B    3KS2WTJ7000076202GXS
4a.18           4a    1   2     FC:B    Not available.
4a.19           4a    1   3     FC:B    3KS2WNZG000076209GE5
4a.20           4a    1   4     FC:B    3KS2V198000076202GVQ
3a.21           3a    1   5     FC:A    3KS3EP1M00007627PNJQ
4a.22           4a    1   6     FC:B    3KS3B9DJ00007628QPQH
3a.23           3a    1   7     FC:A    3KS2WTT1000076202GXP
3a.24           3a    1   8     FC:A    3KS2WPY2000076209GY5
4a.25           4a    1   9     FC:B    3KS2XDAW000076209FUF
3a.26           3a    1   10    FC:A    3KS2YKV800007621UV85
4a.27           4a    1   11    FC:B    3KS2WPQR000076205BY7
3a.28           3a    1   12    FC:A    3KS2V6ZM000076200JT3
4a.29           4a    1   13    FC:B    3KS2XCVW00007620R4TQ


From the output we can easily see that Shelf1, Bay 2 has a bad disk.