John’s Oracle Experiences

My everyday experiences with Oracle products

Archive for the ‘Scripting’ Category

ORA-01031: insufficient privileges when using EXP

Posted by John Paul van Helvoort on May 20, 2009

Using an export OS script with a command similar to this:

exp “‘/ AS SYSDBA’” full=y file=export_SID.dmp log=export_SID.log statistics=none

This error suddenly came around.

EXP-00056: ORACLE error 1031 encountered
ORA-01031: insufficient privileges

After researching this error, i found that when the TWO_TASK parameter is set, Oracle tries to connect via Oracle Net.
This then will return an error while connecting using $OPS login of “/” , which is done in my case.

after unset this variable the problem was solved !

unset TWO_TASK

Posted in Database, Linux, Scripting | Leave a Comment »

How to recall system reboot history

Posted by John Paul van Helvoort on April 23, 2009

One easy way to get some reboot history of a system is running the following script :

for wtmp in `ls -t /var/log/wtmp*`; do last reboot -f $wtmp; done

Here is an example of it :

xxx:~ # for wtmp in `ls -t /var/log/wtmp*`; do last reboot -f $wtmp; done
reboot system boot 2.6.16.60-0.21-s Sun Apr 19 23:43 (3+11:51)
reboot system boot 2.6.16.60-0.21-s Fri Apr 17 11:38 (2+11:41)
reboot system boot 2.6.16.60-0.21-s Thu Apr 16 12:28 (21:19)
reboot system boot 2.6.16.60-0.21-s Thu Apr 16 11:01 (00:-32)

wtmp begins Thu Apr 16 11:01:27 2009
xxx:~ #

Posted in Linux, Scripting | Leave a Comment »

Cleanup old snapshot data in Oracle 9i RDBMS

Posted by John Paul van Helvoort on February 3, 2009

When you are monitoring a Oracle 9i database with Oracle Enterprise Manager.
You might run into the problem that old snapshot statistics do not have a retension periode and therefor make a tablespace grow bigger then expected.

To somewhat control the growth of this snapshot data. Here is a easy script that will clearout old ( older then 1 month ) snapshot data in you monitored 9i database.
This script assumes that statspack is installed under schema owner perfstat.

declare
   -- Determine snapshots to be deleted.
   cursor c_snap is
      select snap_id
      from perfstat.STATS$SNAPSHOT
      where trunc(snap_time) < add_months(sysdate, -1);
   l_teller integer default 0;
begin
   for r_snap in c_snap loop
      delete from perfstat.STATS$SNAPSHOT
      where snap_id = r_snap.snap_id;
      l_teller := l_teller + 1;
      -- commit after every 10 records
      if mod(l_teller, 10) = '0' then
         commit;
      end if;
   end loop;
   -- commit last snapshot delete
   commit;
end;
/

Here you can see the amount of records that will be deleted when running this cleanup script.

select count(snap_id)
from perfstat.STATS$SNAPSHOT
where trunc(snap_time) < add_months(sysdate, -1);

I used this script by scheduling this job to be executed every week.

Posted in Database, Scripting | Leave a Comment »

Start and Stop scripts for your Oracle environment

Posted by John Paul van Helvoort on January 8, 2009

Whenever i am asked to implement start and stop scripts for an Oracle environment. I use these good old scripts to support the need for most systems.
These scripts use profiles for every ORACLE_HOME and ORACLE_SID.
Beside the start and stop purpose , they can also be used to just “set” the Oracle environment right for whatever you like to do.

So here is what i do;
First i create the profile files for every environment :

Environment file for an infrastrcuture DATABASE , profile_SID

PATH=/home/oracle/bin:/usr/local/bin:/usr/bin:/usr/X11R6/bin:/bin:/usr/games:/opt/gnome/bin:/opt/kde3/bin       ; export PATH
ORACLE_HOME=/u00/app/oracle/product/10.1.4/idm                                                                  ; export ORACLE_HOME
ORACLE_SID=SID                                                                                                  ; export ORACLE_SID
PATH=$ORACLE_HOME/bin:$PATH                                                                                     ; export PATH
SCRIPT_DIR=/home/oracle/scripts                                                                                 ; export SCRIPT_DIR

Environment file for an INFRASTRUCTURE , profile_IDM

PATH=/home/oracle/bin:/usr/local/bin:/usr/bin:/usr/X11R6/bin:/bin:/usr/games:/opt/gnome/bin:/opt/kde3/bin       ; export PATH
ORACLE_HOME=/u00/app/oracle/product/10.1.4/idm                                                                  ; export ORACLE_HOME
ORACLE_SID=idm101                                                                                               ; export ORACLE_SID
PATH=$ORACLE_HOME/bin:$PATH                                                                                     ; export PATH
SCRIPT_DIR=/home/oracle/scripts                                                                                 ; export SCRIPT_DIR

Environment file for a MIDTIER , profile_MID

PATH=/home/oracle/bin:/usr/local/bin:/usr/bin:/usr/X11R6/bin:/bin:/usr/games:/opt/gnome/bin:/opt/kde3/bin       ; export PATH
ORACLE_HOME=/u00/app/oracle/product/10.1.2/mid                                                                  ; export ORACLE_HOME
ORACLE_SID=mid101                                                                                               ; export ORACLE_SID
PATH=$ORACLE_HOME/bin:$ORACLE_HOME/opmn/bin:$PATH                                                               ; export PATH
SCRIPT_DIR=/home/oracle/scripts

After creating the profiles we are ready to start using the actual start and stop scripts which use these profiles to set the right environment.

Start script for an INFRASTRUCTURE , start_idm101.sh

#######################################################################
#
# Script for starting an infrastructure
#
# Author : John Paul van Helvoort
# dd     : 9-12-2008
#
#######################################################################

. /home/oracle/scripts/profile_SID

lsnrctl start

sqlplus /nolog <<EOF
conn / as sysdba
startup
exit
EOF

. /home/oracle/scripts/profile_IDM

$ORACLE_HOME/opmn/bin/opmnctl startall

$ORACLE_HOME/bin/emctl start iasconsole

Stop script for an INFRASTRUCTURE , stop_idm101.sh

#######################################################################
#
# Script for starting an infrastructure
#
# Author : John Paul van Helvoort
# dd     : 9-12-2008
#######################################################################

. /home/oracle/scripts/profile_IDM

$ORACLE_HOME/bin/emctl stop iasconsole

$ORACLE_HOME/opmn/bin/opmnctl stopall

. /home/oracle/scripts/profile_SID

lsnrctl stop

sqlplus /nolog <<EOF
conn / as sysdba
shutdown immediate
exit
EOF

Start script for a MIDTIER , start_mid101.sh

#######################################################################
#
# Script for starting an midtier
#
# Author : John Paul van Helvoort
# dd     : 9-12-2008
#######################################################################

. /home/oracle/scripts/profile_MID

$ORACLE_HOME/opmn/bin/opmnctl startall

$ORACLE_HOME/bin/emctl start iasconsole

Stop script for MIDTIER , stop_mid101.sh

#######################################################################
#
# Script for starting an  midtier
#
# Author : John Paul van Helvoort
# dd     : 9-12-2008
#######################################################################

. /home/oracle/scripts/profile_MID

$ORACLE_HOME/bin/emctl stop iasconsole

$ORACLE_HOME/opmn/bin/opmnctl stopall

The scripts to start and stop the Oracle components are ready. If you like to use these scripts to start and stop your Oracle environment upon booting and shutting the system.
You can follow up on the next script.

Create a file called /etc/init.d/oracle as the user root.
Put this in the script ( please check the path to the scripts called for here )

#! /bin/sh
#
# Author: John Paul van Helvoort
#
# /etc/rc.d/oracle
#
#

case "$1" in
    start)
        echo -n "Starting Oracle Components"
        echo ""
        /bin/su - oracle -c "/home/oracle/scripts/start_idm101.sh"
        /bin/sleep 10
        /bin/su - oracle -c "/home/oracle/scripts/start_mid101.sh"
        ;;
    stop)
        echo -n "Shutting Oracle Components"
        /bin/su - oracle -c "/home/oracle/scripts/stop_mid101.sh"
        /bin/sleep 10
        /bin/su - oracle -c "/home/oracle/scripts/stop_idm101.sh"
        ;;
    try-restart)

        ;;
    restart)
        $0 stop
        $0 start

        ;;
    *)
        echo "Usage: $0 {start|stop|status|try-restart|restart}"
        exit 1
        ;;
esac
exit

After this you need to create symbolic links from the correct runlevel directory to this /etc/init.d/oracle file.
Say your server uses runlevel 3 ( you can check this by viewing /etc/inittab on SLES ).

Go to /etc/init.d/rc3.d

ln -s /etc/init.d/oracle S99oracle
( S99 , being the last process to be started by the server )
ln -s /etc/init.d/oracle K01oracle
( K01 , being the first process to be killed by the server )

Posted in Application Server, Database, Scripting | Leave a Comment »

Recieve and Insert an Email into an Oracle Database using PHP

Posted by John Paul van Helvoort on July 15, 2008

Recently i have been challanged with the question as where i would recieve mail using postfix and insert this email in my oracle Database 10g. There are many ways of handling this question as for me , i choose to use PHP scripting to do the trick.

The PHP script has the ability to filter out the information by To, From, Subject and Message. After all parts are filtered out, we can start to create our insert statement.
The PHP language is powerfull as also the extensions for supporting Oracle databases. Mainly there are 2 ways of handling Oracle connection from PHP. The OCI protocol which stands for Oracle Call-Interface and the ORA protocol which use the Normal Oracle function.

As for now i choose to use the OCI protocol as it seems to give me a more stable connection to my Oracle database.

Here i will show you the code i use to recieve an Email through postfix. ( add this code to your aliases file for recieving mail on reciever@YOUR-DOMAIN.COM , remember to run newaliases to update your aliases.db !)

##########################################################
# PIPE Mail naar Oracle DB Poster
##########################################################
reciever:    "|/etc/postfix/post_mail_reciever.php"

After recieving an email , the email is passed into our script where it will be filtered and inserted into the database. The script i use to do this looks like this : ( copy paste this as /etc/postfix/post_mail_reciever.php ) Make sure you provide enough rights for the script to be executed by user “nobody”.

#!/usr/local/bin/php

< ?php

###############################################
# Read from stdin
###############################################

$fd = fopen("php://stdin", "r");
$email = "";
while (!feof($fd)) {
$email .= fread($fd, 1024);
}
fclose($fd);

###############################################
# Handle email
###############################################
$lines = explode("\n", $email);

###############################################
# Empty vars
###############################################

$from = "";
$subject = "";
$headers = "";
$message = "";
$splittingheaders = true;

###############################################
# Email in vars
###############################################

for ($i=0; $i < count($lines); $i++) {
     if ($splittingheaders) {
     // this is a header
     $headers .= $lines[$i]."\n";

     // look out for special headers
    if (preg_match("/^Subject: (.*)/", $lines[$i], $matches)) {
        $subject = $matches[1];
    }
    if (preg_match("/^From: (.*)/", $lines[$i], $matches)) {
        $from = $matches[1];
    }
  }
  else
  {
     // not a header, but message
     $message .= $lines[$i]."\n";
  }

  if (trim($lines[$i])=="") {
     // empty line, header section has ended
     $splittingheaders = false;
  }
}

###############################################
# Replace Chars in String
###############################################

$message = str_replace("'" , "\"" , $message);

################## TEMP ################
# Debug mail , for storing recieved mail
########################################

# ECHO "----------HEADERS---------------------/n";
# ECHO $headers;
# ECHO "----------FROM------------------------/n";
# ECHO $from;
# ECHO "----------Subject---------------------/n";
# ECHO $subject;
# ECHO "----------MEssage----------------------/n";
# ECHO $message;
# ECHO "-------------------------------------";

#      $save_path='/tmp';
#
#      $date = date("H:i:s,d-m-Y");
#      $temp='file.tmp';
#      $dest= "MAIL_".$date;
#
#
#      $fp = fopen($save_path.'/'.$temp, "w", 0);
#      fputs($fp, $email);
#      fclose($fp);
#
#      rename($save_path.'/'.$temp,$save_path.'/'.$dest);
#
#######################################

###############################################
# Enviroment
###############################################

PutEnv("ORACLE_SID=ORCL");
PutEnv("ORACLE_HOME=/u00/oracle/product/10.2.0/db");
PutEnv("TNS_ADMIN=/u00/oracle/product/10.2.0/db/network/admin");

###############################################
# Mail Functions
###############################################

function send_mail($status , $errcode , $errmsg , $erroff , $errsql) {

      $mail = "error_mail@YOUR_DOMAIN.COM";

      ##############################
      # Create Error mail attachment
      ##############################
      $save_path='/etc/postfix';

      $temp='file.tmp';
      $dest='error.txt';

      $date = date("H:i:s d-m-Y");

      $fp = fopen($save_path.'/'.$temp, "w", 0);
      fputs($fp, "[".$date."] Error Code : ORA-".$errcode."\n\n");
      fputs($fp, "[".$date."] Error MSG  : ".$errmsg."\n");
      fputs($fp, "[".$date."] Error Pos  : ".$erroff."\n\n");
      fputs($fp, "[".$date."] Error SQL  : ".$errsql."\n\n");
      fclose($fp);

      rename($save_path.'/'.$temp,$save_path.'/'.$dest);
      ##############################

      $command = "mail -s $status $mail < /etc/postfix/error.txt";
      shell_exec($command);
}

###############################################
# Main
###############################################

$error = "";

$db = "ORCL";

$connect = OCILogOn("scott", "tiger", $db);

$query="begin scott.general.p_accept_mail_from_postfix('$from',sysdate,'$subject','$message'); end;";

if ($connect)
{
	## Parse Error catch
    	$parse = ociparse($connect, $query);
    	if (!$parse) {
	      $error = OCIError($connect);
	      send_mail("MAIL_PARSE_ERROR(ORCL)", $error['code'] ,$error['message'] , $error['offset'] , $error['sqltext']);
	}

    	ociexecute($parse);

    	## Execute Error catch
    	$error = OCIError($parse);
    	if($error){
      		 send_mail("MAIL_EXECUTE_ERROR(ORCL)",$error['code'] ,$error['message'] , $error['offset'] , $error['sqltext']);
    	}

	$committed = ocicommit($connect);

	## Commit Error catch
    	if (!$committed) {
      		$error = OCIError($committed);
      		send_mail("MAIL_COMMIT_ERROR(ORCL)",$error['code'] ,$error['message'] , $error['offset'] , $error['sqltext']);
    	}
    	# We can turn this on when we want to also monitor every successful processed Mail
        #else
    	#{
       	# send_mail("EM-Mail-Processed","OK" , "OK" , "OK" , "OK");
    	#}
    ocilogoff($connect);
   }
   else
   {
   	 $error = OCIError($connect);
    	 send_mail("URGENT-MAIL_DBCONNECT_ERROR(ORCL)", $error['code'] ,$error['message'] , $error['offset'] , $error['sqltext'] ); }

###### END ########
? >

As you can see there are alot of error control checks in there just to be sure an email is processed correctly. When ever there is a problem while parsing , executing or connecting against the Oracle database. A rich error report is send to an email adres of your choice for debug purposes.

Hope you will find benefit for using this script in your implementations.

Posted in Database, Scripting | Leave a Comment »