grep -i dbhost "/usr/sap/SID/SYS/profile/DEFAULT.PFL"| awk '{print $3}'| uniq
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28
use strict; use warnings; use 5.010; my $file = "/usr/sap/SID/SYS/profile/DEFAULT.PFL"; my $search = 'dbhost'; open my $fh, '<', $file or die "open($file,ro) failed: $!"; my %seen; LINE: while ( my $line = <$fh> ) { # skip lines without search string next LINE if index( lc($line), lc($search), 0 ) < 0; # extract the third field of line my $third = ( split( m/\s+/, $line, ) )[2]; # keep the fields uniq $seen{$third}++; } close $fh; # show the third fields of matching lines, but only once say join( "\n", keys %seen );
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26
use strict; use warnings; use 5.010; my $file = "/usr/sap/SID/SYS/profile/DEFAULT.PFL"; my $search = 'dbhost'; open my $fh, '<', $file or die "open($file,ro) failed: $!"; my $seen = ' '; LINE: while ( my $line = <$fh> ) { # skip lines without search string next LINE if index( lc($line), lc($search), 0 ) < 0; # extract the third field of line my $third = ( split( ' ', $line, ) )[2] // ''; # keep the fields uniq say $third unless $third eq $seen; $seen = $third; } close $fh;
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26
sub DBhost { my $file = "/usr/sap/$_[0]/SYS/profile/DEFAULT.PFL"; my $search = 'dbhost'; open my $fh, '<', $file or die "open($file,ro) failed: $!"; my $seen = ' '; LINE: while ( my $line = <$fh> ) { # skip lines without search string next LINE if index( lc($line), lc($search), 0 ) < 0; # extract the third field of line my $third = ( split( ' ', $line, ) )[2] // ''; # keep the fields uniq $third unless $third eq $seen; $seen = $third; } return $seen; }
say $third unless $third eq $seen;
$third unless $third eq $seen;
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52
use strict; use warnings; use 5.010; sub extract_dbhosts { my @files = @_; my @dbhosts; my %seen; my $search = 'sapdbhost'; # keep it lower case # https://help.sap.com/saphelp_nw70ehp1/helpdata/en/c4/3a6136505211d189550000e829fbbd/frameset.htm FILE: for my $file ( @files ) { open my $fh, '<', $file or die "open($file,ro) failed: $!"; LINE: while ( my $line = <$fh> ) { # skip not matching lines next LINE if index( lc($line), $search, 0 ) < 0; # get value of assignment my ( $dbhost ) = (split m{\s*=\s*}, $line )[1]; # remove whitespaces (newlines) from dbhost $dbhost =~ s/\s+//g; # add db hosts in order of occurence push @dbhosts, $dbhost if not $seen{$dbhost}++; # assuming only one dbhost per file; otherwise comment out the "next FILE;" next FILE; } close $fh; } # return list of db hosts in order of occurence return @dbhosts; } ### use case: # get all profile files; SID is replaced with * my @profile_files = glob( "/usr/sap/*/SYS/profile/DEFAULT.PFL" ); # search through all profile files and get db_hosts my @db_hosts = extract_dbhosts( @profile_files );
1 2 3 4 5 6 7
FILE: for my $file ( @files ) { open my $fh, '<', $file or die "open($file,ro) failed: $!"; push @dbhosts, map { /$search\s*=\s*(\S+)/i && ! $seen{$1}++ ? ($1) : () } <$fh>; close $fh; }
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147
use warnings; use strict; use File::Find; use File::Basename; use Config; use Sys::Hostname; use IPC::Open2; use feature 'say'; my ($variant, $comp, $patch, $OSinfoSum); my (@OSinfoSum, @ListInstances ); my $host = hostname; my @ExcludeHosts = qw ( arch backup -clone -rep); ######################################################################################################## # exclude some hosts from collect ######################################################################################################## ######################################################################################################## # test the oS ######################################################################################################## my $OSinfo = "$host"." : "."$Config{osname}"." : "."$Config{osvers}" ; #print "$OSinfo\n" ; ######################################################################################################### # test the saphostctrl (SAP-System) and use it for Kernel and patchlevel (SID: Instance number : Kernel : Patchlevel ) ######################################################################################################### #@ListInstances = `/usr/sap/hostctrl/exe/saphostctrl -function ListInstances | sed 's/ Inst Info : //'` ; if ( -e "/usr/sap/hostctrl/exe/saphostctrl") { @ListInstances = `/usr/sap/hostctrl/exe/saphostctrl -function ListInstances | grep -vE "DAA|DAB" |sed 's/,//g' | awk '{print \$4,":",\$6,":"}` ; chomp (@ListInstances); } else { my $info = $OSinfo." : : : : :"; push @OSinfoSum, $info; } foreach (@ListInstances) { #print "$_\n"; my $UpperSID = substr($_,0,3); my $Instance = substr($_,6,2); my $LowerSID = lc $UpperSID; my $sidadm = $LowerSID."adm"; my $ListInstances = $_; #print "$UpperSID\n"; #print "$LowerSID\n"; #print "$SIDadm\n"; # delete all OS mails #system ("su - $SIDadm -c /"echo /'d */' | mail -N/"") system ( "su - $sidadm -c cat /dev/null > /var/spool/mail/$sidadm" ); my $DBhost = &DBhost($UpperSID); my $disp = `su - $sidadm -c which disp+work 2>/dev/null`; if ( $? == 0 ) { #print "$disp \n"; #print "$sidadm\n"; #my @SapKernel_Info = `su - $SIDadm -c $disp 2>/dev/null`; my $pid = open(PH, "{ su - $sidadm -c disp+work; } 2>/dev/null |") or die $!; my @SapKernel_Info; while (<PH>) { #print "output = $_"; push @SapKernel_Info, $_; } #print join "\n", @SapKernel_Info; foreach (@SapKernel_Info) { if (/kernel make variant/) { $variant = (split)[-1]; } elsif (/compilation mode/) { $comp = (split)[-1]; } elsif (/patch number/) { $patch = (split)[-1]; } } my $info = $OSinfo." : ".$ListInstances." ".$variant." : ".$comp." : ".$patch." : ".$DBhost; push @OSinfoSum, $info; undef $variant; undef $comp; undef $patch; } else { my $info = $OSinfo." : ".$ListInstances." : : "; push @OSinfoSum, $info; } } ######################################################################################################### # get with saphostctrl DB type , version and patchlevel ######################################################################################################### #my @ListDatabaseSystems = `/usr/sap/hostctrl/exe/saphostctrl -function ListDatabaseSystems | egrep -i "Database name|Instance name" | sed 's/^[ \t]*//'|sed 's/^ +//'` ; #print "\n\n @ListInstances"; print join "\n", @OSinfoSum; print "\n"; #print "\n\n @ListDatabaseSystems"; sub DBhost { my $file = "/usr/sap/$_[0]/SYS/profile/DEFAULT.PFL"; my $search = 'dbhost'; open my $fh, '<', $file or die "open($file,ro) failed: $!"; my $seen = ' '; LINE: while ( my $line = <$fh> ) { # skip lines without search string next LINE if index( lc($line), lc($search), 0 ) < 0; # extract the third field of line my $third = ( split( ' ', $line, ) )[2] // ''; # keep the fields uniq $third unless $third eq $seen; $seen = $third; } return $seen; } exit
perl -anE 'm/dbhost/i && !$seen{$F[2]}++ && say $F[2]' bla.PFL
1 2 3 4 5 6 7 8 9
#!/usr/bin/perl use warnings; use strict; sub getDaShit { my @data = `grep -i dbhost "/usr/sap/SID/SYS/profile/DEFAULT.PFL"| awk '{print $3}'| uniq`; return @data; }