#!/usr/bin/perl -w
#
#NAME
#  webprocs - remote monitor for web (www) processes
#
#SYNOPSIS
#  http://host.com.ain:port/dir/webprocs
#
#REQUIRES
#	push @INC, '.';
#	require 'Vopt.pm';
#	require 'taintsubs.pm';
#
#DESCRIPTION
#  This is a web debugging and troubleshooting tool.  It's not a good idea to
#  install  this  in  a "production" web server's directories.  But it's very
#  useful for fixing problems while debugging changes to your web site. So to
#  use it, you should link it into a cgi directory temporarily, and remove it
#  when you've fixed things up.
#
#OPTIONS
#
#FILES
#
#BUGS
#  Runs with the web server's permissions, so it can do anything to your  web
#  site.  Don't leave it visible to the outside world.
#
#SEE ALSO
#
#AUTHOR John Chambers <jc@trillian.mit.edu>
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - #

$| = 1;
$exitstat = 0;
($P = $0) =~ s".*/"";
$V = $ENV{"V_$P"} || $ENV{"D_$P"} || 2;	# Verbose level.

use CGI;
use CGI::Carp 'fatalsToBrowser';
use diagnostics;

print "Content-type: text/html\n\n";
print "<html><head><title>Web Process Monitor $0</title></head>\n";
print "<body><center>Web Process Monitor $0</center>\n";

print "<hr>Here are our form elements:\n";
$query = new CGI;
@names = $query->param();

# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - #
if ($V>1) {			# Display our parameters
	print "<dl compact>\n";
	for $n (sort @names) {
		if (@vals = $query->param($n)) {
			for $v (@vals) {print "<dt>$n<dd>\"$v\"\n"}
		} elsif ($v = $query->param($n)) {
			print "<dt>$n<dd>\"$v\"\n";
		} else {
			print "<dt>$n<dd>(NO VALUE)\n";
		}
	}
	print "</dl>\n";
}

# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - #
if ($V>2) {			# Display our environment
	print "<hr>Here are our environment variables:\n";
	print "<dl>\n";
	for $e (sort keys %ENV) {
		$v = $ENV{$e};
		print "<dt>$e<dd>$v\n";
	}
	print "</dl>\n";
}

# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - #
# The following code handles being called with parameters that tell us to do  #
# something interesting or useful.  There's lots of room for expansion here.  #
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - #

unless (defined($sig = $query->param('s'))) {	# s= gives us a signal
	print "No signal supplied.<br>\n" if $V>1;
}
unless (defined($pid = $query->param('p'))) {	# p= gives us a process id
	print "No PID supplied.<br>\n" if $V>1;
}
unless (defined($pat = $query->param('P'))) {	# P= gives us a perl pattern
	print "No PAT supplied.<br>\n" if $V>1;
}
unless (defined($cmd = $query->param('c'))) {	# c= gives us a command (tricky to do right!)
	print "No cmd supplied.<br>\n" if $V>1;
}

if (defined($sig) && defined($pid)) {
	print  "<hr>\n" if $V>1;
	print  "kill -$sig $pid<br>\n" if $V>1;
	system "/bin/kill -$sig $pid";
	print "Exit status was $?\<br>n if $V>1";
}

if (defined($cmd)) {
	print "<hr>\n" if $V>1;
	print "$cmd<br>\n" if $V>1;
	system $cmd;
	print "Exit status was $?<br>\n" if $V>1;
}

print "<hr></body></html>\n";
