Sunday, September 2, 2012

Perl script to check Kernel parameter differences between 2 Linux machines

This Perl script can be used for checking the Kernel Parameters difference between 2 Linux machines.
All you got to do is, take the ‘sysctl –a’ output into a file from each Linux machine that you wish to compare and give it as an input for this script like shown below:
Upon execution, you need to choose option 1 or 2.  ‘1’ is for listing out all the kernel parameter values of both servers in tabular form and ‘2’ for listing out only the parameters that are either differing or non-exist on other server.
 # perl  <scriptname>.pl  <first_server_kernel.txt>  <second_server_kernel.txt>



#!/usr/bin/perl
# Description   :       This script can be used to list out of the differences in the Kernel Settings between 2 Linux Servers.        
#                       It takes 2 files (consists of sysctl -a output) as input, upon execution it asks for User's choice either      
#                       to List out all the Kernel parameter of 2 Servers or to list out only the Differences.                        
# Version       :       1.1                                                                                                            
# Execution method :    Manual (by any normal user)                                                                                    

if( @ARGV != 2 )
{
   print "\nUsage: $0 <First-file> <Second-file>\n\n";
   print "First-file should contain 'sysctl -a' output taken from first Linux Machine\n\n";
   print "Second-file should contain 'sysctl -a' output taken from Second Linux Machine\n\n";
   exit 1;
}

open(File1, "$ARGV[0]" );
open(File2, "$ARGV[1]" );

my @first_file = <File1>;
my @second_file = <File2>;
my %output1, %output2, %kerparm;
my ($cnt1,$cnt2) = (0,0);

print "\nEnter '1' for Listing all Kernel parameters or '2' to list only the Differences : ";
my $choice=<STDIN>;
chomp($choice);

system $^O eq 'MSWin32' ? 'cls' : 'clear';
printf("%-50s %-25s %-50s\n\n", "KERNEL PARAMETER", "FIRST SERVER", "SECOND SERVER");

foreach $i(@first_file) {
        $i =~ s/\s+$//;
        @array1 = split(" = ",$i);
        $output1{$array1[0]} = $array1[1];
        $cnt1++;
        $kerparm{$array1[0]} = $cnt1;
}

foreach $j(@second_file) {
        $j =~ s/\s+$//;
        @array2 = split(" = ",$j);
        $output2{$array2[0]} = $array2[1];
        $cnt2++;
        $kerparm{$array2[0]} = $cnt2;
}

foreach $key (sort keys %kerparm)
{
  $output1{$key} = "" unless defined $output1{$key};
  $output2{$key} = "" unless defined $output2{$key};
  if($choice eq '1') {
          printf("%-50s %-25s %-50s\n", $key,$output1{$key},$output2{$key}); }
  elsif($choice eq '2') {
          printf("%-50s %-25s %-50s\n", $key,$output1{$key},$output2{$key}) if ($output1{$key} ne $output2{$key});
  }
}

1 comment:

  1. Play the best casino games of 2021 - JT Hub
    Our 남양주 출장안마 casino reviews, including 삼척 출장마사지 details about all 구미 출장샵 the top-rated 화성 출장샵 video slots and video poker providers, A great casino 정읍 출장안마 games site is the best you'll

    ReplyDelete

Popular Posts

About Me

My photo
The intent of this blog is to share my work experience and spread some smart solutions on Linux to System Administrators. I'm hoping the solutions shared in this Blog would be helpful and come as a handy for Viewers. Brief about me: I have 18+ years work experience in System and Cloud Administration domain, primarily works on VMware Cloud Products (vSphere, vCloud Director, vRealize Automation, NSX Adv. Load Balancer, vROps).