File show_SAPHanaSR_attributes of Package SAPHanaSR.2773

#!/usr/bin/perl
#
# SAPHanaSR-showAttr
# (c) 2014 SUSE Linux Products GmbH, Nuremberg, Germany
# (c) 2015-2016 SUSE Linux GmbH, Nuremberg Germany
# Author: Fabian Herschel <fabian.herschel@suse.com>
# License: GPL v2+
my $Version="0.18.2016.02.16.1";
#
##################################################################
use POSIX;
use strict;
use Sys::Syslog;
use Sys::Hostname;
use File::Path;
use Getopt::Long;
use lib '/usr/share/SAPHanaSR/tests';
use SAPHanaSRTools;

###################################
## this part is not for scale out and currently NOT zero-config

my $ClusterNodes=2;
my $ClusterPrimaries=1;
my $ClusterSecondaries=1;
my %Name;
my %Host;
my $host = hostname();

my $varlib='/var/lib/SAPHanaTD';
my $testfile='SAPHanaTD.status';
my $testcount=0;
my $first_test=1;
my $sid="";
my @sids;
my $ino="";
my $sortBy="";
my $table_titleH = "Host";
#my %Name;
my %Host;
my %Site;
my %Global;
my %HName;
my %SName;
my %GName;
my $help;
my $version;
my $cibFile="";

sub init()
{
	my $result = GetOptions ("sid=s" => \@sids,
	                      "sort=s" => \$sortBy,
	                      "cib=s" => \$cibFile,
                          "version" => \$version,
                          "help" => \$help,
		 );
   return 0;
}

init();

if ( $help ) {
   printf "SAPHanaSR-showAttr {[--sid=<sid[:instNr]>]} [--sort=<SortBy>] [--cib=<OfflineCibFile>]\n";
   printf "";
   exit 0;
}
if ( $version ) {
   printf "%s\n", $Version;
   exit 0;
}

if ( $cibFile ne "" ) {
   printf "Using cib file %s\n", $cibFile;
}

sub max { # thanks to http://www.perlunity.de/perl/forum/thread_018329.shtml
 my $a = shift;
 my $b = shift;
 return $a > $b ? $a : $b;
}

sub read_cib($) {
    my $sid = shift();
    if ( $cibFile eq "" ) {
        printf "Open live cib\n";
        open CIB, "cibadmin -Ql |" or die "CIB could not be read from cluster";
    } else {
        open CIB, "<$cibFile" or die "CIB file $cibFile not found or not able to read it";
    }
    while (<CIB>) {
       chomp;
       my ($host, $name, $site, $value);
       if ( $_ =~ /cib-last-written="([^"]*)"/ ) {
          printf "CIB-time: %s\n", $1;
       }
       if ( $_ =~ /node_state id=".+" uname="([a-zA-Z0-9\-\_]+)" .*crmd="([a-zA-Z0-9\-\_]+)"/ ) {
           insertAttribute($sid, \%Host, \%HName, $1, "node_status", $2);
       }
       if ( $_ =~ /nvpair.*name="([a-zA-Z0-9\_\-]+_${sid}_([a-zA-Z0-9\-\_]+))"/ ) {
          $name=$1;
          if ( $_ =~ /id=.(status|nodes)-([a-zA-Z0-9\_\-]+)-/ ) {
             # found attribute in nodes forever and reboot store
             $host=$2;
             if ( $_ =~ /value="([^"]+)"/ ) {
                 $value=$1;
                 insertAttribute($sid, \%Host, \%HName, $host, $name, $value);
             }
          } elsif ( $_ =~ /id=.SAPHanaSR-[a-zA-Z0-9\_\-]+_site_[a-zA-Z0-9\-]+_([a-zA-Z0-9\_\-]+)/) {
             # found a site attribute
             $site=$1;
             if ( $name =~ /[a-zA-Z0-9\_\-]+_site_([a-zA-Z0-9\-]+)/ ) {
                $name = $1;
             }
             if ( $_ =~ /value="([^"]+)"/ ) {
                 $value=$1;
                 insertAttribute($sid, \%Site, \%SName, $site, $name, $value);
             }
          } elsif ( $_ =~ /id=.SAPHanaSR-[a-zA-Z0-9\_\-]+_glob_[a-zA-Z0-9\_\-]+/) {
             # found a global attribute
             $host="GLOBAL";
             if ( $name =~ /([a-zA-Z0-9\_\-]+)_glob_([a-zA-Z0-9\_\-]+)/ ) {
                $name = $2;
             }
             if ( $_ =~ /value="([^"]+)"/ ) {
                 $value=$1;
                 insertAttribute($sid, \%Global, \%GName, "global", $name, $value);
             }
          }
       }
    }
    close CIB;
}

if ( 0 == @sids ) {
    my $sid_ino_list;
       ( $sid_ino_list ) = get_sid_and_InstNr();
       @sids = split(",", $sid_ino_list);

}

foreach $sid (@sids) {
    ( $sid, $ino ) = split(":", $sid);
    $sid=tolower("$sid");
    %Host=();
    %HName=();
    read_cib($sid);
    get_hana_attributes($sid);
    if ( keys(%Host) == 0 ) {
        printf "No attributes found for SID=%s\n", $sid;
    } else {
        print_host_attr(\%Host,   \%HName, "Hosts",  $sortBy);
    }
}
openSUSE Build Service is sponsored by