Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
branch: master
Fetching contributors…

Octocat-spinner-32-eaf2f5

Cannot retrieve contributors at this time

file 160 lines (125 sloc) 3.037 kb
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159
#!/usr/bin/perl -w

=head1 NAME

oraAlertLog.pl

=head1 SYNOPSIS

Find and tail the Oracle alert log.
See http://tardate.blogspot.com/2007/04/find-and-tail-oracle-alert-log.html for discussion.

$Id: oraAlertLog.pl,v 1.8 2007/04/22 23:27:04 paulg Exp $

=head1 DESCRIPTION

USAGE:
perl oraAlertLog.pl OPTIONS

OPTIONS:
[-i] show information about the Oracle environment and log location
[-f] tail the alert log
[-h] help

=head1 AUTHOR

Paul Gallagher gallagher.paul@gmail.com
http://tardate.blogspot.com

=cut

use strict;
use Carp;
use IO::Handle;
use DBI;
use DBD::Oracle qw(:ora_session_modes);
use Env qw(ORACLE_SID ORACLE_HOME);
use Getopt::Std;
use File::Basename;

my $VERSION = 1.0;

my ($scriptBase, $scriptPath, $scriptSuffix) = fileparse($0, qr/\..*/);

my %option = ();
getopts('ifh', \%option);

if ($option{i}) {
info();
}
elsif ($option{f}) {
tailLog();
}
else {
usage();
}

1;


sub usage
{
my ($msg) = @_;
if ($msg) {
print "\nWARNING: $msg\n";
}
    print <<END_OF_USAGE;

PURPOSE: Find and tail the Oracle alert log.

USAGE:
perl oraAlertLog.pl OPTIONS

OPTIONS:
[-i] show information about the Oracle environment and log location
[-f] tail the alert log
[-h] help

END_OF_USAGE

print "Version: $VERSION ".'Last modified: $Date: 2007/04/22 23:27:04 $'."\n";
    exit;
}


sub getAlertLogName
{
my $alertlog="";
use vars qw($cachedalertlog);
$cachedalertlog="";

if (!defined(${ORACLE_SID}) || !(${ORACLE_SID} =~ /\S/) ) {
print STDERR "ORACLE environment not available.\n\n";
exit(1);
}

my $cacheFile="${scriptPath}${scriptBase}.${ORACLE_SID}.conf";

my $skipdb=0;
my $dbh = DBI->connect('dbi:Oracle:', '', '',
{ora_session_mode => ORA_SYSDBA , RaiseError => 0, PrintError => 0, AutoCommit => 0}) or
$skipdb=1;
$skipdb or
my $sth = $dbh->prepare(q{SELECT value FROM v$parameter WHERE name = 'background_dump_dest'}) or
$skipdb=1;
if (!$skipdb) {
$sth->execute;
my $row = $sth->fetchrow_hashref;
$sth->finish;
$alertlog=$row->{VALUE};
$dbh->disconnect;
}

if ( $alertlog =~ /\S/ ) {
$alertlog .= "/alert_${ORACLE_SID}.log";
}
else {
print "ORACLE not available.Checking for cached settings..\n";
open( my $fh, "<", $cacheFile) or croak("Problem opening conf file $cacheFile : $!");
while (<$fh>) {
chomp;
if (/^\s*alertlog\s*=/i) {
s/.*=\s*//;
$alertlog= $_;
}
}
close $fh;
}
if ( $alertlog !~ /\S/ ) {
croak( "Could not determine alert log location.");
}
else {
open( my $fh, ">", $cacheFile) or croak("Problem opening cache file $cacheFile : $!");
print $fh "alertlog=${alertlog}\n";
close($fh);
}
return $alertlog;
}


sub info
{
my $fn = getAlertLogName();
print "ORACLE_HOME = " . $ORACLE_HOME . "\n";
print "ORACLE_SID = " . $ORACLE_SID . "\n";
print "ALERT LOG = " . $fn . "\n";
}


sub tailLog
{
my $fn = getAlertLogName();
my $log;
open (LOGFILE, $fn) or croak("can't open $fn: $!");
for (;;) {
while (<LOGFILE>) { print }
sleep 1;
LOGFILE->clearerr();
}
}
Something went wrong with that request. Please try again.