I spoke with Mr. Peter Scott and he informed me that I would be ok to
cut/paste my code in
an email and post it to the list, so with that said here it is..
Please be open and honest, I am looking to speed up the script and make it
more efficient as well
Regards,
Ron
#!/usr/bin/perl -w
use Getopt::Long;
use Time::Local;
use strict;
use Benchmark;
use POSIX qw(strftime);
use Time::Local;
if ( @ARGV < 2 ) {
print "You must supply two command line options\n";
print "The run # and the sleeptime\n";
print "\n IE: $0 1 60\n";;
exit (1);
}
# This just times the script, it should take roughly 30 - 45 seconds to run
to completion
# Disable buffering of output
$| = 1;
# declare global variables for use
my ($smtx, $syscl, $usr, $sys, $wt, $idl, $CPUS, $avg1, $avg2, $avg3, @time,
%otime, $otime, $tmp, $stime, %stime, $oratime, $systime);
my (@mpstat, $OUT, $OFN, $ldavg, $STAT, $SFAN, $header, @lookFor, %results,
$results, $clients, $pricing, @output, %lookFor);
my ($get_errors_timer, $tt, $RTDLOGDIR, $RTDUSER, $RTDPASS, $RTD_ORACLE_SID,
$sessions, $active, $VQUSER, $VQPASS, $lwtime);
my ($TARGET_HOST, @sessions,@active);
# A timer so I can limit when the get_errors() runs
my $time = time() + 30;
# oracle related values, used to gather connect times
$RTDLOGDIR = $ENV{APPSPATH} . "/logs/runtime_logs/";
$RTDUSER="rtdiag";
$RTDPASS="byteme";
$RTD_ORACLE_SID="VALUTEST";
$VQUSER="VQ3994";
$VQPASS="VQUIX04";
$TARGET_HOST="mc0300uv004";
if ( $ENV{NODE} =~ /NodeA/ ) { $VQUSER="VQ3993"; }
# Initialy zero out all the hash values
foreach my $keys (keys %lookFor ) { $results{$keys} = 0; }
# Start some benchmarking stuff
$tt = new Benchmark;
sub abort () {
# set currnt timer time
my $t3 = new Benchmark;
# find the processing time
my $te = timediff($t3, $tt);
# report the processing time
print OFN "elapsed time\t: " . timestr($te) . "\n";
close(SFN);
close(OFN);
exit(1);
}
sub timer () {
if ( time() > $time ) {
return 1;
} else {
return 0;
}
}
%SIG = (
HUP => \&abort, # just because it seems only reasonable!
INT => \&abort,
QUIT => \&abort, # can kids inherit these? should they?
TERM => \&abort,
);
# cleanup routine called every iteration through the get_errors()
# to 0 out all values and hopefully reclaim memory...
sub cleanup () {
@output = ();
%otime = ();
%stime = ();
@mpstat = ();
%results = ();
@sessions = ();
@active = ();
foreach my $keys (keys %lookFor ) { $results{$keys} = 0; }
}
# setup some output files
my $host = `hostname`;
my $date = `date +%b%d`;
chomp( $host);
chomp( $date );
# Define the output files used
$OUT = $ENV{APPSPATH} . "/trg/ltt/scripts/" . $host . "_" . $date .
"_run$ARGV[0]_$$.out";
$STAT = $ENV{APPSPATH} . "/trg/ltt/scripts/" . $host . "_" . $date .
"_run$ARGV[0]_$$.xls";
# BIG UGLY hash of current errors we are tracking for each SLT
# please add new elements to the bottom of the hash, also ensure
# that the print_stat_header() and the generate_statistic_output()
# are updated accordingly
# yeah yeah yeah, I could remove the => ; but why they look so so pretty ;-P
%lookFor = (
"ORA-" => "ORACLE errors (various
Oracle errors)",
"Fault 2-001" => "Fault 2-001 Host/server down or
unresponsive",
"Fault 2-002" => "Fault 2-002 no orbix daemon or
unresponsive",
"Fault 2-003" => "Fault 2-003 Can not bind to
Authorization Object (ACF2/Message Broker)",
"Fault 2-004" => "Fault 2-004 Can not bind to
Contract Object (CMS/Get Q/Message Broker)",
"Fault 2-005" => "Fault 2-005 Can not bind to
Order Object (Order Submit/Message Broker)",
"Fault 2-006" => "Fault 2-006 Authorization Object
fault (ACF2/Message Broker)",
"Fault 2-007" => "Fault 2-007 Contract Object
fault (CMS/Get Q/Message Broker)",
"Fault 2-008" => "Fault 2-008 Order Object fault
(Order Submit/Message Broker)",
"Fault 2-009" => "Fault 2-009 CHI 3270 session
fault (Customer Information/TRG)",
"Fault 2-010" => "Fault 2-010 CAPS 3270 session
fault (Trade-in Information/TRG)",
"Fault 2-011" => "Fault 2-011 Sale Range 3270
session fault (Negotiated Credit Info/TRG)",
"Fault 2-012" => "Fault 2-012 Oracle DBMS
offline",
"Fault 2-013" => "Fault 2-013 Oracle DBMS is out
of critical resource",
"Fault 2-015" => "Fault 2-015 Factory Server
failed to start Session Server",
"Fault 2-016" => "Fault 2-016 CAPS Broker failure
(Serial, Order Information/TRG)",
"Fault 2-017" => "Fault 2-017 CAPS Broker fault
(Serial, Order Information/TRG)",
"Fault 2-018" => "Fault 2-018 Pooling Broker
failure (Pooling Information/TRG)",
"Fault 2-019" => "Fault 2-019 Pooling Broker fault
(Pooling Information/TRG)",
"Get Q Error" => "Get Q Error (Message Broker).",
"Bind" => "Bind to Factory failure",
"SystemError" => "SystemError",
"SystemException" => "SystemException",
"Communication" => "Communication failure",
"ORB" => "ORB problem",
"Test App Finished" => "Number of clients Finished"
);
# This begins the output of the statistical data for graphical manipulation
# print the Excel column header
sub print_stat_header () {
print SFN "\n";
print SFN
"DateTimeStamp,ORAn,F2001,F2002,F2003,F2004,F2005,F2006,F2007,F2008,F2009,F2
010,F2011,F2012,F2013,F2015,F2016,";
print SFN
"F2017,F2018,F2019,FactoryFailure,SystemError,SystemException,CommunicationF
ailure,ORBProblem,GetQError,";
print SFN "# of clients still running,# of PricingSessions still running,#
of clients Finished Test,smtx / syscl,user %,";
print SFN "sys %,wt %,idl %,current load average,current load
average,current load average,input,errs,output,errs,colls,";
print SFN "input,errs,output,errs,colls\n";
}
# This will print out the generated stats in CSV format for importing into
# Excel for graphical manipulation
sub generate_statistic_output () {
printf SFN "%s,",strftime( "%m/%d/%Y %H:%M:%S", localtime());
printf SFN "%d,%d,%d,%d,", $results{'ORA-'},$results{'Fault
2-001'},$results{'Fault 2-002'},$results{'Fault 2-003'};
printf SFN "%d,%d,%d,%d,", $results{'Fault 2-004'},$results{'Fault
2-005'},$results{'Fault 2-006'},$results{'Fault 2-007'};
printf SFN "%d,%d,%d,%d,", $results{'Fault 2-008'},$results{'Fault
2-009'},$results{'Fault 2-010'},$results{'Fault 2-011'};
printf SFN "%d,%d,%d,%d,", $results{'Fault 2-012'},$results{'Fault
2-013'},$results{'Fault 2-015'},$results{'Fault 2-016'};
printf SFN "%d,%d,%d,", $results{'Fault 2-017'},$results{'Fault
2-018'},$results{'Fault 2-019'};
printf SFN "%d,%d,%d,%d,",
$results{'Bind'},$results{'SystemError'},$results{'SystemException'},$result
s{'Communication'};
printf SFN "%d,%d,%d,%d,%d", $results{'ORB'},$results{'Get Q
Error'},$clients,0,$results{'Test App Finished'};
printf SFN ",%1.2f", $smtx / $syscl;
printf SFN ",%1.2f", $usr / ($usr + $sys + $wt + $idl);
printf SFN ",%1.2f", $sys / ($usr + $sys + $wt + $idl);
printf SFN ",%1.2f", $wt / ($usr + $sys + $wt + $idl);
printf SFN ",%1.2f,%1.2f,%1.2f,%1.2f", $idl / ($usr + $sys + $wt + $idl),
$avg1,$avg2,$avg3;
# Padding output data until I figure out a way to capture `netstat -I hem0
60` output
# cleanly/correctly
printf SFN ",%d,%d,%d,%d,%d", 0,0,0,0,0;
printf SFN ",%d,%d,%d,%d,%d\n", 0,0,0,0,0;
}
# mpstat example output
# Column numbers and example mpstat output
# 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14
15
#CPU minf mjf xcal intr ithr csw icsw migr smtx srw syscl usr sys wt
idl
# 10 17 0 42 101 101 69 0 10 2 0 63 0 14 1
85
# 11 1 0 14 101 101 162 0 5 114 0 303 1 2 0
98
# 14 2 0 0 101 101 62 0 4 3 0 74 0 0 0
100
# 15 29 0 156 151 146 129 4 5 44 0 19609 10 36 3
52
# 18 0 0 545 401 201 116 0 4 15 0 6466 3 15 1
81
# 19 14 0 11 102 100 167 2 6 5 0 3345 1 7 0
91
# This sub-routine will take the output of mpstat (see above) and parse
# it out to generate required statistical data.
# Please be very careful with this sub-routine, an error here
# could cause you a world of hurt later!!
sub get_mpstats () {
$smtx = $syscl = $usr = $sys = $wt = $idl = 0;
@mpstat = qx(mpstat 5 2 | sed 1,`mpstat|egrep -c '[0-9]|CPU'`d);
foreach (@mpstat) {
# skip the header output of mpstat
next if /CPU/;
# remove the leading spaces in output
s/^\s+//;
# seperate the output by spaces and pull certain columns
(see above)
# out for use
my ($A, $B, $C, $D, $E, $F ) = ( split ( /\s+/))
[9,11,12,13,14,15];
# begin the calculation of the stats
$smtx += $A;
$syscl += $B;
$usr += $C;
$sys += $D;
$wt += $E;
$idl += $F;
}
}
sub get_load_average () {
# the previous way to get this information was from parsing the
"top"
# output, IMHO that is senseless when you can just use uptime and
have
# the date right there with little parsing needs.
$ldavg = `uptime`;
# remove linefeeds from the output
chomp($ldavg);
# remove the leadeing space from the output
$ldavg =~ s/^\s+//;
# parse the data on spaces and store the data
# from the output
($avg1,$avg2,$avg3) = ( split ( /\s+/,$ldavg)) [9,10,11];
# remove the ugly ","
$avg1 =~ s/,//;
$avg2 =~ s/,//;
$avg3 =~ s/,//;
}
sub get_oracle_time () {
# Lets get the time it takes to connect to oracle
my ( $key, $value );
$oratime = qx(
(timex sqlplus $RTDUSER/$RTDPASS\@RTD_ORACLE_SID <<-!>/dev/null
quit
!
) 2>&1);
# remove lineffeds from the output
$oratime =~ tr/\n//s;
# removeing any linefeeds at the begining of a line
$oratime =~ tr/^\n//;
# remove any leading speaces
$oratime =~ s/^\s+//;
# split the date into two variables
($key,$value) = split(/\s+/,$oratime);
# populate the hash, the key will be the first column (real, usr and
sys)
# the value will be the corresponding data from the output
$otime{$key} = $value;
}
sub get_client_time () {
# Lets get the time it takes to connect to oracle and get a single worksheet
my ( $key, $value );
my $CMD = $ENV{APPSPATH} . "/trg/pricing/active/bin/scr_client";
$systime = qx (
(timex $CMD 0 $TARGET_HOST <<-!>/dev/null
_VALUEQX
$VQUSER
$VQPASS
y
lw
gw
054100
gwh
qq
!
) 2>&1);
# remove lineffeds from the output
$systime =~ tr/\n//s;
# removeing any linefeeds at the begining of a line
$systime =~ tr/^\n//;
# remove any leading speaces
$systime =~ s/^\s+//;
# split the date into two variables
($key, $value) = split( /\s+/, $systime);
# populate the hash, the key will be the first column (real, usr and
sys)
# the value will be the corresponding data from the output
$stime{$key} = $value;
}
sub get_errors () {
# Here is the big dady of sub-routines
# it looks small, but man oh man is it key
# to the sucess of this script
# start a timer for this sub-routine
my $t0 = new Benchmark;
# ensure each value key of results is
# 0 when we start, if not we get major errors
foreach my $key ( keys %lookFor ) { $results{$key} = 0; }
# run the perl script that forks 3 egreps on the
# logs and parse the data real time.
open(COM, "parse.pl |") || die("fork failed: \l$!\n");
while (<COM>) {
# increment the number of occurances we see
# for a certain error.
# this will become our error count for
# statistical and reporting output
foreach my $test (keys %lookFor) {
if ( $test eq "Communication" ) {
$results{$test}++ if ( $_ =~ /$test/ && $_
=~ /failure/);
next;
}
if ( $test eq "Bind" ) {
$results{$test}++ if ($_ =~ /$test/ && $_ =~
/Factory/ && $_ =~ /failed/);
next;
}
if ( $test eq "SystemException" ) {
$results{$test}++ if($_ =~ /$test/ && $_ !~
/UnsavedChangesError/);
next;
}
if ( $test eq "SystemError" ) {
$results{$test}++ if($_ =~ /$test/ && $_ !~
/foo/);
next;
}
if ( $test eq "ORB" ) {
$results{$test}++ if($_ =~ /$test/ && $_ !~
/UnsavedChangesError/);
next;
}
$results{$test}++ if /$test/;
}
}
close(COM);
# set the current timer time
my $t1 = new Benchmark;
# see how long this sub-routine took
$get_errors_timer = timediff($t1, $t0);
}
# genaric output seperator
sub print_line () {
print OFN "------------------------------------------------\n";
}
# loop forever and ever!
# this will keeps us going while the SLT is running
open SFN, ">>$STAT" || die "unable to open $STAT: $!";
open OFN, ">>$OUT" || die "unable to open $OUT: $!";
my $oldfh = select(OFN); $| = 1; select($oldfh);
my $oldfh2 = select(SFN); $| = 1; select($oldfh2);
&print_stat_header;
while ( 1 ) {
my $t2 = new Benchmark;
my $parse = 0;
printf OFN "%s,",strftime( "%m/%d/%Y %H:%M:%S", localtime())
# start bench marking this script
&get_mpstats;
&get_load_average;
&print_line;
print OFN scalar localtime() . "\n";
&print_line;
my $top = `top -d1 20`;
print OFN "top processes....\n$top\nmpstats....\n@mpstat\n";
&print_line;
$clients = `ps -ef | grep scr_client | grep -v grep | wc -l`;
print OFN "Errors during SLT:\n";
# Okay, we will _ONLY_ run the get_errors() if the time limit has elapsed
# TODO: make the time interval a passed in arg
if ( timer() ) {
&cleanup;
&get_errors;
&generate_statistic_output;
$parse =1;
}
foreach my $error (sort keys %lookFor) {
# lets ONLY report an error if we have one.
# is there a real need to report 0 errors???
if ( ($results{$error} > 0) && ( $error ne "Test App
Finished" ) ) {
printf OFN "%-60s\n\t%-4d\n",
$lookFor{$error},$results{$error};
}
}
# Report total clients still running
printf OFN "%-60s\n\t%-4d\n", 'Number of clients Still
Running',$clients;
# Report total clients finished
printf OFN "%-60s\n\t%-4d\n", $lookFor{'Test App
Finished'},$results{'Test App Finished'};
&print_line;
print OFN "Done\n";
&print_line;
# set currnt timer time
# find the processsign time
# report the time the egrep(s) took
my $left = $time - time();
my $mins = ($left / 60) % 60;
my $secs = $left % 60;
# report the processing time
my $t4 = new Benchmark;
my $tf = timediff($t4, $t2);
print OFN "$0\t: " . timestr($tf) . "\n";
my $etime = timeit(1, \&get_errors);
print OFN "egrep(s)\t: " . timestr($etime) . "\n";
printf OFN "Time until next egrep: %d:%2d\n", $mins, $secs;
# sleep X amount of seconds before we start processign again
# this is a command line option passed into the script
sleep $ARGV[1];
if ( $parse == 1 ) {
$time = time() + 30;
}
}
# close our stat output
--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]