#!/usr/bin/perl -w # # mngsvr.pl # A cgi script to help with process management on development servers. # NOT RECOMMENDED FOR INSTALLATION ON A PRODUCTION SERVER # # This script and it counterpart, dirmon.pl, should not be used on an open server. # The server should be secured by at least IP and User restrictions. Possibilities for # user restrictions: For IIS, use NT username integration, for Netscape, use LDAP. # # amonotod June 2000 # amonotod@netscape.net # # This script is distributed "as is". No warranty of any kind is # expressed or implied. You use at your own risk. The author # will not be liable for data loss, damages, loss of profits or # any other kind of loss while using or misusing this software. use strict; no strict 'vars'; no strict 'refs'; my @snd2s = ("AdminSta1", "AdminSta2", "AdminSta3", "DevGuy1", "DevGuy2"); #Computer names to send messages to my $actndir = "c:/winnt/system32/jobcntrl"; my %choices = ( "A", ["Restart Web Server", "rstnetscape", "The Web service on SERVER will be restarted in 30 seconds.", "The Web service on SERVER has been restarted."], "B", ["Shutdown Web Server and restart in 10 minutes", "netscape", "The Web service on SERVER will be shutdown in 30 seconds. It will restart in 10 minutes.", "The Web service on SERVER has been restarted."], "C", ["Restart ColdFusion and Web Servers", "cf", "The ColdFusion and Web services on SERVER will be shutdown in 30 seconds.", "The Coldfusion and Web services on SERVER have been restarted."], "D", ["Stop ColdFusion Server", "cfstop", "The ColdFusion service on SERVER will be shutdown in 30 seconds.", "The Coldfusion service on SERVER has been shutdown."], "E", ["Start ColdFusion Server", "cfstart", "The ColdFusion service on SERVER will be restarted in 30 seconds.", "The Coldfusion service on SERVER has been restarted."], "F", ["Restart ColdFusion and SQL Servers", "sql", "The SQL and ColdFusion services on SERVER will be shutdown in 30 seconds.", "The SQL and Coldfusion services on SERVER have been restarted."], "G", ["Restart All the Above but not NT", "rstall", "All services on SERVER will be shutdown in 30 seconds.", "The services on SERVER have been restarted."], "H", ["Restart the Entire Server", "nt", "SERVER will be shutdown and restarted in 30 seconds."] ); # %choices= ("X", ["Description", "name", "The XXX service on Server will be restarted in 30 seconds.", "The XXX service on SERVER has been restarted."]) # For each entry above, you'll have an entry below. #@arrayname = (waittime, "stop command", "start command", "parameter", "parameter",...) @rstnetscape = (5, "net stop /y", "net start", "Netscape Enterprise Server 3.6"); @netscape = (600, "net stop /y", "net start", "Netscape Enterprise Server 3.6"); @cf = (5, "net stop /y", "net start", "Netscape Enterprise Server 3.6 ", "Cold Fusion Application Server", "Cold Fusion Executive"); @cfstop = (5, "net stop /y", "quit", "Cold Fusion Application Server", "Cold Fusion Executive"); @cfstart = (5, "net start", "quit", "Cold Fusion Executive", "Cold Fusion Application Server"); @sql = (5, "net stop /y", "net start", "Cold Fusion Application Server", "Cold Fusion Executive", "MSDTC", "SQLServerAgent", "MSSQLServer"); @rstall = (5, "net stop /y", "net start", "Netscape Enterprise Server 3.6 ", "Cold Fusion Application Server", "Cold Fusion Executive", "MSDTC", "SQLServerAgent", "MSSQLServer"); @nt = (0, "shutdown /l /r /y /c", "quit", ""); chdir "c:/"; if ((lc $ARGV[0]) eq "svcs") { #print "Doing services\n"; $cmdparam = uc $ARGV[1]; $cmdparam =~ s /\,//; #print "$cmdparam \n"; &dwnmsg; sleep 30; &do_cmd; sleep 5; &upmsg; } else { &html_content; if ($FORM{mng}) { &log_cmd; &open_html; &submit_reply; &close_html; } elsif ($FORM{confirm}) { &open_html; &print_reply; &confirm; &close_html; } else { &open_html; &print_query; &close_html; } } exit; sub print_query { print '
'."\n"; print ' '; print ' '."\n"; print ' '."\n"; foreach (sort keys %choices){ print ' '."\n"; } print ' '."\n"; print '
'."\n"; print ' Use this form to manage server processes on SERVER.'."\n"; print '
'."\n"; print ' '. $choices{$_}[0] ."
\n"; print '
'."\n"; print '
'."\n"; print '
'."\n"; print " The following info will be recorded in the log:
\n User: $ENV{remote_user}
\n IP: $ENV{remote_host}
\n"; #foreach (keys %FORM){ # print "$_ = $FORM{$_} \n"; #} } sub html_content{ #print "Checking post content\n"; read (STDIN, $QUERY_STRING, $ENV{'CONTENT_LENGTH'}); @NameValuePairs = split (/&/, $QUERY_STRING); foreach $NameValue (@NameValuePairs){ my ($Name, $Field) = split (/=/, $NameValue); $Field =~ tr/+/ /; $Field =~ s/%([\dA-Fa-f][\dA-Fa-f])/ pack ("C", hex ($1))/eg; $Name =~ tr/+/ /; $Name =~ s/%([\dA-Fa-f][\dA-Fa-f])/ pack ("C", hex ($1))/eg; $FORM{$Name} = $Field; } } sub upmsg { foreach (@snd2s) { $snd = "net send $_ " .'"'. $choices{$cmdparam}[3] .'"'; @sndres = `$snd`; } } sub dwnmsg { foreach (@snd2s) { $snd = "net send $_ " .'"'. $choices{$cmdparam}[2] .'"'; @sndres = `$snd`; } } sub open_html{ print "Content-type: text/html\n\n"; print "\n\n Server Process Management\n\n\n
\n"; print "

Server Process Management

\n"; } sub close_html{ print "\n
\n\n\n"; } sub print_reply { print "\n \n \n"; print ' '."\n"; print ' '."\n"; print "
\n To confirm your decision, please re-check your choice, then submit the form.\n
'."\n"; print ' '."\n".' Confirm? '. $choices{$FORM{confirm}}[0] ."
\n"; print '
'."\n"; print ' '."
\n"; print '
\n"; } sub submit_reply { print "\n Your request has been submitted. You will get notification when it has completed.
\n"; print ' After you recieve notification, you may view the results of your request.
' . "\n"; print ' Alternatively, you may go back to the Server Resource Page' . "\n"; #print "\n Your request has been submitted. You will get notification when it has completed.\n"; } sub confirm { my $actncmd = $actndir . $choices{$FORM{mng}}[1]; my $actnfile = "d:/web/jobs/webcmd.txt"; #print "
File is $actnfile
\n"; #print "Command to run is $actncmd
\n"; print "Job submitted by $ENV{remote_user}, from $ENV{remote_host}
\n"; } sub log_cmd { my $actncmd = "c:/winnt/system32/jobcntrl/". $choices{$FORM{mng}}[1]; my $actnfile = "d:/web/jobs/webcmd.txt"; if ( open (CMDFILE, "> $actnfile") ){ print CMDFILE "$FORM{mng}, submitted by $ENV{remote_user}, from $ENV{remote_host}\n"; close CMDFILE; } } sub do_cmd { $chore = $choices{$cmdparam}[1]; #foreach (@$chore) { # print "$_ \n" #} $cnt = @$chore; $inc = 3; #print "$cnt \t $inc\n"; while ($inc < $cnt) { my $actncmd = "$$chore[1] ".'"'. "$$chore[$inc]".'"'; print "$actncmd \n"; @cmdres = `$actncmd`; foreach (@cmdres) { push @results, $_; } $inc++; } $inc--; if ($$chore[2] ne "quit") { sleep $$chore[0]; while ($inc > 2) { my $actncmd = "$$chore[2] ".'"'. "$$chore[$inc]".'"'; print "$actncmd \n"; @cmdres = `$actncmd`; foreach (@cmdres) { push @results, $_; } $inc--; } } #print "$cnt \n"; my $rsltfile = "d:/web/docs/resource-stuff/jobresult.txt"; #@results = `$actncmd`; if ( open (RSLTFILE, "> $rsltfile") ){ print RSLTFILE " ". localtime() ." \n"; foreach (@results) { chomp; if ($_ ne "") { print RSLTFILE "$_ \n"; } } close RSLTFILE; } #if ($$chore[2] eq "quit") { exit; } undef @rstnetscape, @netscape, @cf, @sql, @rstall, @nt; }