Error in Scripting to monitor tablespace in Oracle DB


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Error in Scripting to monitor tablespace in Oracle DB
# 1  
Old 05-31-2012
Error in Scripting to monitor tablespace in Oracle DB

Buddies, I am writing the below script 'tab.sh' to monitor the tablespaces in Database:-
-------------------------------
Code:
export ORACLE_HOME=/home/oracle/app/oracle/product/11.2.0/dbhome_2
export PATH=$PATH:$ORACLE_HOME/bin
export ORACLE_SID=orcl
sqlplus system/oracle as sysdba <<EOF 
spool /home/oracle/tablespace_analysis.log
set lines 2000
set pagesize 100
select a.tablespace_name as "tablespace",allocated as "size in gb", (a.allocated - nvl(b.freespace,0)) as "used space in gb",nvl(b.freespace,0) as "freespace in gb", (a.allocated - nvl(b.freespace,0))*100/a.allocated "percentage used",
100 - (a.allocated - nvl(b.freespace,0))*100/a.allocated "Percentage free"
from
(select tablespace_name, sum(bytes)/1024/1024/1024 allocated from dba_data_files group by tablespace_name ) a,
(select tablespace_name, sum(bytes)/1024/1024/1024 freespace from dba_free_space group by tablespace_name ) b
where a.tablespace_name = b.tablespace_name(+)
and (a.allocated - nvl(b.freespace,0))*100/a.allocated >85
order by 6 desc ;

spool off
EOF     
exit 0

________________________

However while executing the script, I am getting the below error:-

SQL> SQL> SQL> SQL> SP2-0042: unknown command "EOF" - rest of line ignored.

Any idea will be highly appreciated.

Last edited by joeyg; 05-31-2012 at 01:01 AM.. Reason: Please wrap code and data with CodeTags
# 2  
Old 05-31-2012
How about this ?
Code:
export ORACLE_HOME=/home/oracle/app/oracle/product/11.2.0/dbhome_2
export PATH=$PATH:$ORACLE_HOME/bin
export ORACLE_SID=orcl
echo "
spool /home/oracle/tablespace_analysis.log
set lines 2000
set pagesize 100
select a.tablespace_name as "tablespace",allocated as "size in gb",
(a.allocated - nvl(b.freespace,0)) as "used space in gb",nvl(b.freespace,0) as "freespace in gb",
(a.allocated - nvl(b.freespace,0))*100/a.allocated "percentage used",100 - (a.allocated - nvl(b.freespace,0))*100/a.allocated "Percentage free" 
from
(select tablespace_name, sum(bytes)/1024/1024/1024 allocated from dba_data_files group by tablespace_name ) a,
(select tablespace_name, sum(bytes)/1024/1024/1024 freespace from dba_free_space group by tablespace_name ) b
where a.tablespace_name = b.tablespace_name(+)
and (a.allocated - nvl(b.freespace,0))*100/a.allocated >85
order by 6 desc ;
spool off;
exit" | sqlplus system/oracle as sysdba
   
exit 0

This User Gave Thanks to pravin27 For This Post:
# 3  
Old 05-31-2012
Thanks a lot Pravin. However echo commands in that way is producing some errors in my SQL code which is not there otherwise, so need to think in different ways
# 4  
Old 05-31-2012
Try with single quote
Code:
echo '
set lines 2000
set pagesize 100
select a.tablespace_name as "tablespace",allocated as "size in gb",
(a.allocated - nvl(b.freespace,0)) as "used space in gb",nvl(b.freespace,0) as "freespace in gb",
(a.allocated - nvl(b.freespace,0))*100/a.allocated "percentage used",100 - (a.allocated - nvl(b.freespace,0))*100/a.allocated "Percentage free"
from
(select tablespace_name, sum(bytes)/1024/1024/1024 allocated from dba_data_files group by tablespace_name ) a,
(select tablespace_name, sum(bytes)/1024/1024/1024 freespace from dba_free_space group by tablespace_name ) b
where a.tablespace_name = b.tablespace_name(+)
and (a.allocated - nvl(b.freespace,0))*100/a.allocated >85
order by 6 desc ;
spool off;
exit' | sqlplus system/oracle as sysdba

This User Gave Thanks to pravin27 For This Post:
# 5  
Old 05-31-2012
Quote:
Originally Posted by pravin27
Try with single quote
Code:
echo '
set lines 2000
set pagesize 100
select a.tablespace_name as "tablespace",allocated as "size in gb",
(a.allocated - nvl(b.freespace,0)) as "used space in gb",nvl(b.freespace,0) as "freespace in gb",
(a.allocated - nvl(b.freespace,0))*100/a.allocated "percentage used",100 - (a.allocated - nvl(b.freespace,0))*100/a.allocated "Percentage free"
from
(select tablespace_name, sum(bytes)/1024/1024/1024 allocated from dba_data_files group by tablespace_name ) a,
(select tablespace_name, sum(bytes)/1024/1024/1024 freespace from dba_free_space group by tablespace_name ) b
where a.tablespace_name = b.tablespace_name(+)
and (a.allocated - nvl(b.freespace,0))*100/a.allocated >85
order by 6 desc ;
spool off;
exit' | sqlplus system/oracle as sysdba

Perfect dear, thanks a million!
Dude just one advice more, can you suggest any good book for learning shell scripting .
# 6  
Old 05-31-2012
There are lot of books available in the market, I used the below book when i started learning Shell script.
Amazon.com: Unix Shell Programming (3rd Edition) (9780672324901): Stephen G. Kochan, Patrick Wood: Books

Another way is to follow this forum........you will learn surely.
Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. Programming

Tablespace growth trend

Hi experts, I have the below details with me.How to calculate the tablespace growth between two dates. sample data(have data upto 1 year): INSTANCE_NAME DATE TABLESPACE_NAME MB_ALLOC MB_FREE MB_USED PCT_FREE PCT_USED MAX ---------------- ---------... (1 Reply)
Discussion started by: navsan420
1 Replies

2. Shell Programming and Scripting

Script to check Oracle tablespace

I'm new to unix script, and I need to check the tablespaces daily, here is the script(quite simple), but it does not work. Did I missed something ? Please guide me, Many thanks ! #!/bin/sh echo "ORA TABLEASPACE:" sqlplus system/oracle as sysdba; set pagesize 9999; set linesize 132; ... (5 Replies)
Discussion started by: dehetoxic
5 Replies

3. Shell Programming and Scripting

Help please! Oracle scripting

Hi All, I'm completely new to shell scripting and i've found some old code (10 years), which would be suitable for me to facilitate database shutdowns throughout our systems. I'm on AIX 6 and i'm using 11gr2 Oracle. The problems seems to be with while read portion of this code: FLAG_SET=0... (2 Replies)
Discussion started by: Bluegenes
2 Replies

4. Shell Programming and Scripting

tablespace monitoring script

I have prepared the below script to monitor the tablespace and alert the users whenever it reaches a threshold limit. #!/bin/sh . /home/.profile sqlplus -s $LOGON << .eof > $scripts/check_tablespace.temp set pages 0 select tablespace_name, free_percent from ( SELECT... (4 Replies)
Discussion started by: svajhala
4 Replies

5. Shell Programming and Scripting

Help needed for shell scripting for oracle.

Hi, Please see contains both files created for automating the data from oracle through shell. 1)a_p.ksh #!/bin/ksh LOG=/home/A_P.log MESSAGE=/home/MESSAGE_A_P.txt mail_list=/home/AP_MAIL_LIST.txt data=/home/spooled_A_P.log echo "`date` Starting execution for A_P COUNT" > $LOG ... (2 Replies)
Discussion started by: fidelis
2 Replies

6. Shell Programming and Scripting

error in script path for tablespace usage

Hi , I am trying to run this script in crontab but I get errors. When I run it explicitly like ./monitor_tblsp from another location then it runs fine . I am messing somewhere with paths but I don't know where and how . Please help . Here is the error part ./monitor_tblsp: touch: not... (2 Replies)
Discussion started by: capri_drm
2 Replies

7. Shell Programming and Scripting

Shell Script to find the tablespace size in oracle.

Hi, I need to execute a script to find the tablespace size in oracle.But i get an error.:confused: Script Executed:- #!/bin/ksh ORACLE_SID= oracelinstance ORACLE_HOME= oracle path PATH=$ORACLE_HOME/bin export ORACLE_SID ORACLE_HOME PATH sqlplus... (4 Replies)
Discussion started by: vighna
4 Replies

8. Shell Programming and Scripting

Shell Scripting with Oracle...

Hi guys, I have searched all the forums and searched the net as much as possible, but I carn't find any tutorials about shell scripting with Oracle. Has anyone come across any? Thanks for reading and in advance of posts, B14... (2 Replies)
Discussion started by: B14speedfreak
2 Replies

9. Shell Programming and Scripting

Unix and Oracle scripting

Hello all, I have a script that is fully functional and satisfactory in oracle. I recently started spooling it's output into unix for processing and manipulation. I do have a few problems however: The output of some columns are altered in the unix output: 1) On oracle, the date comes with a... (7 Replies)
Discussion started by: Khoomfire
7 Replies
Login or Register to Ask a Question