#!/usr/local/bin/perl use lib ("/export/home/sybase/src/perl/perlmods/lib","/export/home/sybase/src/perl/perlmods/lib/perl5/site_perl/5.6.1/sun4-solaris") ; require "ctime.pl"; use Sybase::DBlib; require 'getopts.pl'; select(STDOUT); $| = 1; # make unbuffered $usage = "usage $0 -UPSIsnt\nU,P,S,I Same as 'isql' switches\n - field separation character, default ','\n - number of samples to take, default '5'\n - number of seconds between samples, default '1'\n"; &Getopts('U:P:S:I:s:n:t:dhH') || die "$usage"; die "$usage" if ($opt_h); die "$usage" if ($opt_H); $opt_U = 'sa' unless $opt_U; $opt_P = '' unless $opt_P; $opt_S = 'SYBASE' unless $opt_S; $opt_I = "/opt/sybase/interfaces" unless $opt_I; $opt_s = "," unless $opt_s; $opt_n = "5" unless $opt_n; $opt_t = "1" unless $opt_t; # # Prompt silently for ASE Password if necessary # if ( $opt_P eq " " ) { print STDERR "\nPassword: "; system("stty -echo"); chop($opt_P = ); system("stty echo"); } ## Use the perferred Interfaces file dbsetifile($opt_I); ## Set the message and error handling routines &dbmsghandle (\&message_handler); &dberrhandle (\&error_handler); ## Use the perferred Interfaces file &dbsetifile($opt_I); ## Login in to the ASE Server $dbh1 = new Sybase::DBlib "$opt_U", "$opt_P", "$opt_S", "sqlsar.pl"; die "Unable to connect to $opt_S\n" unless defined($dbh1); $now = time; while ($opt_n > 0) { $now = time; @rs = exec_sql($dbh1,"select suser_name(suid),case fid when 0 then spid else fid end,db_name(dbid),object_name(id,dbid) as obj_na me,convert(varchar,status) as status,stmtnum,linenum,cmd from master..sysprocesses where suid 0 and cmd 'AWAITING COMMAND' and spid \@\@spid"); foreach (@rs) { print "$now" . $opt_s . "$_\n"; } print "\n" if $opt_d; system("sleep $opt_t"); $opt_n--; } $now = time; exit; ###################### End of Main ####################### ### ### callbacks ### sub message_handler { my ($db, $message, $state, $severity, $text, $server, $procedure, $line) = @_; # Don't display 'informational' messages: if ($severity > 10) { print STDERR ("Sybase message ", $message, ", Severity ", $severity, ", state ", $state); print STDERR ("\nServer `", $server, "'") if defined ($server); print STDERR ("\nProcedure `", $procedure, "'") if defined ($procedure); print STDERR ("\nLine ", $line) if defined ($line); print STDERR ("\n ", $text, "\n\n"); # &dbstrcpy returns the command buffer. if(defined($db)) { my ($lineno, $cmdbuff) = (1, undef); my $row; $cmdbuff = &Sybase::DBlib::dbstrcpy($db); foreach $row (split (/\n/, $cmdbuff)) { print STDERR (sprintf ("%5d", $lineno ++), "> ", $row, "\n"); } } } elsif ($message == 0) { print STDERR ($text, "\n"); } 0; } sub error_handler { my ($db, $severity, $error, $os_error, $error_msg, $os_error_msg) = @_; # Check the error code to see if we should report this. if ($error != SYBESMSG) { print STDERR ("Sybase error: ", $error_msg, "\n"); print STDERR ("OS Error: ", $os_error_msg, "\n") if defined ($os_error_msg); } INT_CANCEL; } ### ### exec_sql routine to execute SQL statement and store results (if any) into array ### Note: Don't use for large result sets! sub exec_sql { my($db,$sql,$sep)=@_; # local copy parameters my(@res, @data); $sep = $opt_s unless $sep; # provide default for sep @res = (); # clear result array $db->dbcmd($sql); # pass sql to server $db->dbsqlexec; # execute sql while($db->dbresults != NO_MORE_RESULTS) { # copy all results while (@data = $db->dbnextrow) { push(@res,join($sep,@data)); } } @res; # return the result array }