3 |
# Author: Dobrica Pavlinusic, dpavlin@rot13.org |
# Author: Dobrica Pavlinusic, dpavlin@rot13.org |
4 |
# Description: monitor sap servers using sapinfo from RFCSDK |
# Description: monitor sap servers using sapinfo from RFCSDK |
5 |
# |
# |
6 |
# Usage: sap.monitor [-h ashost filter] [-s sysnr filter] |
# Usage: sap.monitor [-[hH] ashost only/ignore] [-[sS] sysnr only/ignore] |
7 |
# |
# |
8 |
|
# e.g. sap.monitor -s 20 will scan only hosts with sysnr == 20 |
9 |
|
# sap.monitor -S 20 will scan only hosts with sysnr != 20 |
10 |
|
|
11 |
|
# configuration file in /usr/local/etc/sap-mon.conf describes which |
12 |
|
# hosts (ashost) and systems (sysnr) you want to check. |
13 |
|
# |
14 |
|
# format of line is: |
15 |
|
# |
16 |
|
# ashost [tab|space] sysnr # optional comment |
17 |
|
# |
18 |
|
# you can spacify host as hostname (sap01) or with sap routers in-between |
19 |
|
# to test routers too (/H/saprtr/H/sap01) |
20 |
|
|
21 |
use strict; |
use strict; |
22 |
use Getopt::Std; |
use Getopt::Std; |
24 |
# change paths here if you want to |
# change paths here if you want to |
25 |
my $CONFIG = "/usr/local/etc/sap-mon.conf"; |
my $CONFIG = "/usr/local/etc/sap-mon.conf"; |
26 |
my $SAPINFO = "/usr/local/bin/sapinfo"; |
my $SAPINFO = "/usr/local/bin/sapinfo"; |
27 |
|
# number of tries to repeat sapinfo if it fails first time |
28 |
|
my $repeat = 3; |
29 |
|
|
30 |
my %opts; |
my %opts; |
31 |
getopt('h:s:', \%opts); |
getopt('h:s:H:S:', \%opts); |
32 |
|
|
33 |
my @config; |
my @config; |
34 |
open(C, $CONFIG) || die "sap-mon.conf: $!"; |
open(C, $CONFIG) || die "sap-mon.conf: $!"; |
48 |
chomp; |
chomp; |
49 |
s/#.+$//g; # nuke comments |
s/#.+$//g; # nuke comments |
50 |
s/^\s+$//g; # remove empty lines |
s/^\s+$//g; # remove empty lines |
51 |
my ($ashost,$sysnr) = split(/\t+/,$_,2); |
my ($ashost,$sysnr,undef) = split(/\s+/,$_,3); |
52 |
if ($ashost && $ashost ne "" && $sysnr && $sysnr ne "" && |
if ($ashost && $ashost ne "" && $sysnr && $sysnr ne "" && |
53 |
(($opts{h} && $ashost =~ m/$opts{h}/) || not $opts{h}) && |
(($opts{h} && $ashost =~ m/$opts{h}/) || not $opts{h}) && |
54 |
(($opts{s} && $sysnr =~ m/$opts{s}/) || not $opts{s}) ) { |
(($opts{s} && $sysnr =~ m/$opts{s}/) || not $opts{s}) && |
55 |
my $output = `$SAPINFO trace=0 ashost=$ashost sysnr=$sysnr`; |
(($opts{H} && $ashost !~ m/$opts{H}/) || not $opts{H}) && |
56 |
$output =~ m/System ID\s+(\w+)/; |
(($opts{S} && $sysnr !~ m/$opts{S}/) || not $opts{S}) ) { |
57 |
my $sys_id = $1 || ""; |
my $ret = 1; |
58 |
if ($? != 0) { |
my $loop = 0; |
59 |
|
my $output; |
60 |
|
my $sys_id; |
61 |
|
for(my $i=0; $i<$repeat; $i++) { |
62 |
|
$output = `$SAPINFO trace=0 ashost=$ashost sysnr=$sysnr`; |
63 |
|
undef $sys_id; |
64 |
|
if ($output =~ m/System\s+ID\s+(\w+)/i) { |
65 |
|
$sys_id = $1; |
66 |
|
last; |
67 |
|
} |
68 |
|
# print "$loop: $ashost $sysnr $ret\n"; |
69 |
|
$loop++; |
70 |
|
sleep 5; |
71 |
|
} |
72 |
|
if (! $sys_id) { |
73 |
push @failed, "$ashost ($sysnr)"; |
push @failed, "$ashost ($sysnr)"; |
74 |
$fail_msg .= $output; |
$fail_msg .= $output; |
75 |
} else { |
} else { |
81 |
my $exit = 0; |
my $exit = 0; |
82 |
|
|
83 |
if (@failed) { |
if (@failed) { |
84 |
print "FAILED HOSTS: ",join(", ",@failed),"\n\n"; |
print join(", ",@failed)," FAILED\n\n"; |
85 |
print "$fail_msg\n"; |
print "$fail_msg\n"; |
86 |
$exit = 1; |
$exit = 1; |
87 |
} |
} |
88 |
|
|
89 |
print "CHECKED HOSTS (which are OK): ",join(", ",@ok),"\n\n"; |
print "ALL OK\nCHECKED HOSTS (which are OK): ",join(", ",@ok),"\n\n"; |
90 |
|
|
91 |
rmdir "/tmp/sap$$" || die "can't rmdir in /tmp/sap$$: $!"; |
rmdir "/tmp/sap$$" || die "can't rmdir in /tmp/sap$$: $!"; |
92 |
|
|