Scripts – contig_devs.pl

Print Friendly

DISCLAIMER:

This script is as is. I give no warranty it will work as you expected on your system. You are running it at your own risk. If something goes wrong because of this script, I bear no responsibility.

Language


Solaris 10
server# bash –version
GNU bash, version 3.00.16(1)-release (sparc-sun-solaris2.10)
Copyright (C) 2004 Free Software Foundation, Inc.
server# perl -v
This is perl, v5.8.4 built for sun4-solaris-64int
(with 33 registered patches, see perl -V for more detail)

Purpose:

You have several device numbers in hexadecimal format – some as a range, some as single. You want to find out which devs are in sequence, and how many in a single sequence, and total number of devs.
The device numbers given at command line must be hexadecimal (script will treat them as hexadecimal). Specify the devs with “:” if they are in range. Separate all devs by a comma. Example would be: 12AB:13AB,1123,5C6:5D7

Usage


server# contig_devs.pl 469a:469C,47BE,0FC3:0FDA,02C0:039b

  Devices        Count
===========     =======
  469A:469C          3
  47BE               1
  FC3:FDA           24
  2C0:39B          220
===========     =======
  Total Devs       248

server# cat contig_devs.pl
#!/usr/perl5/5.8.4/bin/perl

#################################################################################
# A script to process given device numbers in hexadecimal format and present them
# on screen in a contiguous range along with their counts
# Written by : Ketan Patel
#################################################################################

use lib '/net/loncdcemcp3/stadm/perl/lib';
use strict;
use Getopt::Long;

my %mightyinfo;

#=============================================================================================
# Sub: usage - to show the help message
#=============================================================================================
sub usage {
  print "usage:    ./contig_devs.pl dev1:dev15,dev71,dev81:dev91,dev101\n\n";
  print "NOTE:   Enter the device number without : in them (1234 instead of 12:34)\n";
  exit;
}
#=============================================================================================

#MAIN BODY

my $leng = $#ARGV;

#If no arguments are provided, display the usage
if ( $leng eq -1 ) {
  usage;
}

#Save all devices separated by comma in an array and then in a hash of dec_hex devnum
my @devsarray = split(/,/,$ARGV[0]);

foreach my $dev_temp ( @devsarray ) {
  if ( $dev_temp =~ /:/ ) {
    my ( $firstdev, $lastdev ) = split (/:/, $dev_temp );
    my $firstdev_dec = hex ($firstdev);
    my $lastdev_dec  = hex ($lastdev);

    for my $dev_dec ( $firstdev_dec..$lastdev_dec ) {
      # Convert it back into hexadecimal format such as 3F05
      my $dev_hex = sprintf ( "%X", $dev_dec );
      $mightyinfo{dec_hex_v}->{$dev_dec} = $dev_hex;
    }
  } else {
    my $firstdev_dec = hex ($dev_temp);
    $mightyinfo{dec_hex_v}->{$firstdev_dec} = $dev_temp;
  }
}

print "\n";
# Go through sorted dec-hex hash, and prepare range and count the numbers and display on the screen
my ($devices, $count);
format OUTPUT =
  @<<<<<<<<<<<< @>>>>>
  $devices,     $count;
.

$devices = "Devices";
$count   = "Count";
$~="OUTPUT";
write;
print "===========     =======\n";

my $cur_range_count = 0;
my $prev_range_count = 0;
my $dev_count   = 0;
my $is_firstdev = "yes";
my $total_devs  = keys %{ $mightyinfo{dec_hex_v} };
my ( $nextdev_dec, $curdev_dec, $prevdev_dec, $range_firstdev_hex, $range_lastdev_hex );

foreach my $curdev_dec ( sort ( keys % { $mightyinfo{dec_hex_v} } ) ) {
#  print "DEBUG - processing device $curdev_dec ($mightyinfo{dec_hex_v}->{$curdev_dec})\n";
  # Start counting total number of devs processed
  $dev_count   = $dev_count + 1;

  if ( "$is_firstdev" eq "yes" ) {
    # Set values for range (in case it is a range) - cur_range_count, range_firstdev_hex
    $cur_range_count = $cur_range_count + 1;
    $range_firstdev_hex = $mightyinfo{dec_hex_v}->{$curdev_dec};

    # Check whether only single dev is given ($total_devs = 1). If so, display the output, else flag up it is 
    # not a first dev anymore
    if ( $dev_count == $total_devs ) {
      $devices = $range_firstdev_hex;
      $count   = $cur_range_count;
      $~="OUTPUT";
      write;
    } else {
      # First dev is processed, more than one devs are supplied, no more first dev
      $is_firstdev = "no";

      # Current dev will become previous dev for next processing
      $prevdev_dec    = $curdev_dec;
    }

  } else {
    # Find out what decimal dev should be next in the range
    my $nextdev_dec = $prevdev_dec + 1;
    if ( "$nextdev_dec" eq "$curdev_dec" ) {
      $cur_range_count = $cur_range_count + 1;
    } else {
      $prev_range_count = $cur_range_count;
      $cur_range_count = 1;
    }

    # Check whether the device is last
    if ( $dev_count == $total_devs ){

      # check whether dev is a solo dev (not part of a range) - two writes - one for previous range or solo, 
      # and second for current solo
      if ( $cur_range_count == 1 ) {

        # writes for previous dev
        # print previous dev according to whether it is a range or solo
        if ( $prev_range_count > 1 ) {
          # print previous dev - it is a range
          $range_lastdev_hex = $mightyinfo{dec_hex_v}->{$prevdev_dec};
          $devices = "$range_firstdev_hex".":"."$range_lastdev_hex";
          $count   = $prev_range_count;
          $~="OUTPUT";
          write;
          $prev_range_count = 0;

        } else {
          # print previous dev - it is a solo
          $range_firstdev_hex = $mightyinfo{dec_hex_v}->{$prevdev_dec};
          $devices = $range_firstdev_hex;
          $count   = 1;
          $~="OUTPUT";
          write;
        }
        # writes for previous dev complete

        # print current dev which is solo
        $range_firstdev_hex = $mightyinfo{dec_hex_v}->{$curdev_dec};
        $devices = $range_firstdev_hex;
        $count   = 1;
        $~="OUTPUT";
        write;

      } else {
        # dev is part of a range
        $range_lastdev_hex = $mightyinfo{dec_hex_v}->{$curdev_dec};
        $devices = "$range_firstdev_hex".":"."$range_lastdev_hex";
        $count   = $cur_range_count;
        $~="OUTPUT";
        write;
      }
    } else {
      # curdev_dec is not last, there are more devs to follow - so carry on processing them

      # curdev_dec is not part of a range - so print the previous dev (which might be range or a solo)
      if ( $cur_range_count == 1 ) {
        # writes for previous dev
        # print previous dev according to whether it is a range or solo
        if ( $prev_range_count > 1 ) {
          # print previous dev - it is a range
          $range_lastdev_hex = $mightyinfo{dec_hex_v}->{$prevdev_dec};
          $devices = "$range_firstdev_hex".":"."$range_lastdev_hex";
          $count   = $prev_range_count;
          $~="OUTPUT";
          write;
          $prev_range_count = 0;

        } else {
          # print previous dev - it is a solo
          $range_firstdev_hex = $mightyinfo{dec_hex_v}->{$prevdev_dec};
          $devices = $range_firstdev_hex;
          $count   = 1;
          $~="OUTPUT";
          write;
        }
        # writes for previous dev complete

        # New range has started, so set the range_firstdev_hex
        $range_firstdev_hex = $mightyinfo{dec_hex_v}->{$curdev_dec};
      }
      $prevdev_dec = $curdev_dec;
    }
  }
}

#print the final count
print "===========     =======\n";
$devices = "Total Devs";
$count   = $total_devs;
$~="OUTPUT";
write;

print "\n";

, , ,

sanaswati
No comments yet.

Leave a Reply

*