#!/bin/perl 

# bslogget
# AUTHOR: Brent Parish
# DATE WRITTEN: 01/20/02
# LAST UPDATE: 02/20/02
# VERSION: 1.1

# Standard disclaimers apply - Use at your own risk!  Author and author's employer are
# not responsible for any negative impact that might result from its use.
# Please read through the script and make sure that you understand all
# the potential effects.  Please take all the necessary precautions (backups are a GOOD thing!!)

# 
# The purpose of this script is to retrieve log files
# from remote servers and display in the browser

# You will need to set up the BBUSER account to ssh to remote machines
# without prompting for a password!

# This script expects key=value pairs of 
#   machine=TARGET_MACHINE
#   log=SETOOLKIT   - or -  log=SYSLOG
# see setoolkit.pl script for an example.

print "Content-type: text/html\n\n";

$ENV{'PATH'} = '/bin:/usr/bin:/usr/local/bin';

if ($ENV{'REQUEST_METHOD'} eq 'GET') {
    @pairs = split(/&/, $ENV{'QUERY_STRING'});
} elsif ($ENV{'REQUEST_METHOD'} eq 'POST') {
    read(STDIN, $buffer, $ENV{'CONTENT_LENGTH'});
    @pairs = split(/&/, $buffer);
} else {
    print 'invalid request_method';
    exit;
}

foreach $pair (@pairs) {
        local($name, $value) = split(/=/, $pair);
        $name =~ tr/+/ /;
        $name =~ s/%([a-fA-F0-9][a-fA-F0-9])/pack("C", hex($1))/eg;
        $value =~ tr/+/ /;
        $value =~ s/%([a-fA-F0-9][a-fA-F0-9])/pack("C", hex($1))/eg;
        $Form{$name} = $value;
}

if (!defined $Form{'machine'}) {
   print 'Invalid host name';
   exit;
} else {
   $machine = $1 if ($Form{'machine'} =~ m/(.*)/);
   chomp($machine);
}

if (!defined $Form{'log'}) {
   print 'Invalid log file';
   exit;
} else {
   $logfile = $Form{'log'};
   chomp($logfile);
}

$logfile = '/var/adm/sa/monitor.log' if ($logfile eq 'setoolkit');
$logfile = '/var/adm/messages' if ($logfile eq 'syslog');

@content = `/usr/local/bin/ssh $machine "cat $logfile"`;

print "<BODY>\n";
print "<PRE>";
print @content;
print "</PRE><CENTER>";

# YOU WILL NEED TO SET THIS PATH FOR THE "BACK" BUTTON

print '<FORM ACTION="/bb/html/'.$machine.'.se.html">';
print "<INPUT TYPE='SUBMIT' VALUE='BACK'>\n";
print '</FORM></CENTER>';

