--- sap.monitor 2002/07/10 08:31:21 1.2 +++ sap.monitor 2002/07/17 10:18:14 1.6 @@ -2,11 +2,26 @@ # File: sap.monitor # Author: Dobrica Pavlinusic, dpavlin@rot13.org # Description: monitor sap servers using sapinfo from RFCSDK +# +# Usage: sap.monitor [-[hH] ashost only/ignore] [-[sS] sysnr only/ignore] +# +# e.g. sap.monitor -s 20 will scan only hosts with sysnr == 20 +# sap.monitor -S 20 will scan only hosts with sysnr != 20 use strict; +use Getopt::Std; + +# change paths here if you want to +my $CONFIG = "/usr/local/etc/sap-mon.conf"; +my $SAPINFO = "/usr/local/bin/sapinfo"; +# number of tries to repeat sapinfo if it fails first time +my $repeat = 3; + +my %opts; +getopt('h:s:H:S:', \%opts); my @config; -open(C,"/usr/local/etc/sap-mon.conf") || die "sap-mon.conf: $!"; +open(C, $CONFIG) || die "sap-mon.conf: $!"; @config = ; close(C); @@ -24,11 +39,25 @@ s/#.+$//g; # nuke comments s/^\s+$//g; # remove empty lines my ($ashost,$sysnr) = split(/\t+/,$_,2); - if ($ashost && $ashost ne "" && $sysnr && $sysnr ne "") { - my $output = `/usr/local/bin/sapinfo trace=0 ashost=$ashost sysnr=$sysnr`; + if ($ashost && $ashost ne "" && $sysnr && $sysnr ne "" && + (($opts{h} && $ashost =~ m/$opts{h}/) || not $opts{h}) && + (($opts{s} && $sysnr =~ m/$opts{s}/) || not $opts{s}) && + (($opts{H} && $ashost !~ m/$opts{H}/) || not $opts{H}) && + (($opts{S} && $sysnr !~ m/$opts{S}/) || not $opts{S}) ) { + my $ret = 1; + my $loop = 0; + my $output; + for(my $i=0; $i<$repeat; $i++) { + $output = `$SAPINFO trace=0 ashost=$ashost sysnr=$sysnr`; + $ret &= $?; +# print "$loop: $ashost $sysnr $ret\n"; + last if ($ret == 0); + $loop++; + sleep 5; + } $output =~ m/System ID\s+(\w+)/; my $sys_id = $1 || ""; - if ($? != 0) { + if ($ret != 0) { push @failed, "$ashost ($sysnr)"; $fail_msg .= $output; } else { @@ -40,12 +69,12 @@ my $exit = 0; if (@failed) { - print "FAILED HOSTS: ",join(", ",@failed),"\n\n"; + print join(", ",@failed)," FAILED\n\n"; print "$fail_msg\n"; $exit = 1; } -print "CHECKED HOSTS (which are OK): ",join(", ",@ok),"\n\n"; +print "ALL OK\nCHECKED HOSTS (which are OK): ",join(", ",@ok),"\n\n"; rmdir "/tmp/sap$$" || die "can't rmdir in /tmp/sap$$: $!";