#!/opt/perl5/bin/perl
#
# This perl script displays on a single, self-refreshing web page
# the most important parameters of multiply sap systems. By taking a
# look at this page, most of the problems can be noticed 
# immediately. The displayed data:
#  system status(up/down)
#  user number per app. server 
#  session number per app. server
#  average response time per app. server
#  number of active locks
#  number of update entries
#  number of abap dumps (today)
#  number of error messages in the system log (today)
#
#  The web page gets refreshed once every three minutes.
#  The perl script uses SAP's rfc2abap to submit
#  the data collector abap (the source code: pr# 93b is in a
#  unix text file) in the different systems. A cpic type user
#  has to exist in those systems to run the abap.
#  The script was used to monitor 14 SAP systems parallel.
#
print "Content-type:text/html\n\n";
use CGI;
print "<HTML><BODY style=\"font-size: small\">";
print "<META http-equiv=\"refresh\" content=\"180;URL=sysmon.cgi\">";
#
# Print out the time of the last screen refresh
#
$datum = `date`;
@f = split /\s/, $datum;
print "@f[3] @f[1] @f[2] @f[5]<BR><HR><BR>";
#
# Print a main header
#
print "<table border=\"1\" width=\"900\" height=\"28\" bgcolor=\"#0000FF\"";
print "cellspacing=\"0\"<tr>";
print "<td width=\"614\" align=\"center\"><strong><font color=\"#FFFFFF\">";
print "SYSTEM DEPENDENT DATA</font></strong></td>";
print "<td width=\"386\" align=\"center\"><strong><font color=\"#FFFFFF\">";
print "INSTANCE DEPENDENT DATA</font></strong></td>";
print "</tr></table>";
#
# Print the column headings
#
@h=("SYSTEM","STATUS","SYSLOG ERRORS TODAY","ABAPDUMPS TODAY","LOCKS","UPDATE EN
TRIES","INSTANCE","USERS","SESSIONS","AVERAGE D. RESPONSE TIME");
print "<table border=\"1\" width=\"900\" height=\"28\" cellspacing=\"0\"";
print "<tr>";
for ($x = 0; $x < @h; $x++) {
   print "<td width=\"75\" bgcolor=\"#00FFFF\" align=\"center\">";
   print "<small><small><small>@h[$x]</small></small></small></td>";
}
print "</tr>";
#
# List of the systems to be monitored(SID, client, host, system number)
# This portion of the script has to be customized
#
@s = (
   ["DEV", "090", "host1",  "00"],
   ["PRD", "100", "host2",  "01"],
   ["QAS", "110", "host3",  "02"],
   ["EDU", "120", "host4",  "03"]
);
#
# Loop on the SAP systems
#
for $k ( @s ) {
#
# Check, that the sysytem is available
#
$stat = `./sapinfo -3 -h@$k[2] -s@$k[3] -g@$k[2] -xsapgw@$k[3] 2>/dev/null`;
    if ($stat =~ /SAP/) {
#
# Get the data from the SAP system
# Customize the username and password
#
       $data = `./rfc2abap -d@$k[0] -uusername -ppassword -c@$k[1] -h@$k[2] -s@$k[3]
 -g@$k[2] -xsapgw@$k[3] -f zpoplog|grep -v Connected`;
    }
    else {
#
# When the system is down:
#
        $data = "Q AAA - - - - - - - - &";
    }
       $sid = @$k[0];
       @d = split /\s/, $data;
       for ($x = 0; $x < @d; $x++) {
         if (@d[$x] eq "Q") {
# Begin of table row
             print "<tr>";
         }
         elsif (@d[$x] eq "@") {
# Empty cell
             print "<td width=\"100\"></td>";
         }
# End of table row
         elsif (@d[$x] eq "&") {
             print "</tr>";
         }
         else {
            if ($x eq 2) {
# Set the status cell's color and text
              if (@d[1] eq $sid){
                $stat = "UP";
                $color = "#00FF00";
              }
              else {
                $stat = "DOWN";
                $color = "#FF0000";
              }
# Print the status cell
              print "<td width=\"100\" bgcolor=$color align=\"center\">";
              print "<small><small><small>$stat</small></small></small></td>";
            }
            if ($x eq 1) {
               $string = $sid;
            }
            else {
               $string = @d[$x];
            }
# Print a regular cell
            print "<td width=\"100\" bgcolor=\"#FFFFFF\" align=\"center\">";
            print "<small><small><small>$string</small></small></small></td>";
          }
     }
}
print "</table>";

print "</BODY></HTML>";