Oracle定时查询结果输出到指定的log文件

      最近有个监控项目需要采集数据库信息,原来方案是写个sql脚本,每个脚本放一个查询语句然后通过操作系统层su到oracle用户通过sqlpus执行这个.sql,然后加到crontab定时执行。但是这个问题有个弊端,就是建立的连接太多,假如每5分钟采集一次数据那意味着5分钟通过sqlplus登陆数据库再退出。后来想着怎么在数据库层每5分钟执行下查询而不是操作系统频繁的通过sqlplus登陆,具体思路写个存储过程通过存储过程把查询结果输出到指定的日志文件。再通过job定时调度。
1、建立dir授予相应权限
SQL> create or replace directory monitor as '/oracle/monitor';
SQL> grant read,write on directory monitor to system;
Grant succeeded.
SQL> grant read,write on directory monitor to sys;   
Grant succeeded.
2、创建procedure
SQL> create or replace procedure pro_inst_status as
  2    v_tmp         varchar2(60);
  3    v_out_file    utl_file.file_type;
  4    v_file_name   varchar2(30);
  5  begin
  6    v_file_name := 'inst_status.log';
  7    v_out_file := utl_file.fopen('MONITOR',v_file_name,'w');
  8    if (utl_file.is_open(v_out_file)) then  
  9       select to_char(sysdate,'yyyy-mm-dd hh24:mi:ss')||',  '||instance_name||',  '||status
 10       into v_tmp from v$instance;
 11       utl_file.put_line(v_out_file, v_tmp);
 12    else
 13       raise_application_error(-20001,'Inst file Open Failure!');
 14    end if;
 15    utl_file.fclose(v_out_file);
 16  end pro_inst_status;
 17    /

Procedure created.
3、执行procedure
SQL> exec pro_inst_status;
BEGIN pro_inst_status; END;

*
ERROR at line 1:
ORA-29283: invalid file operation
ORA-06512: at "SYS.UTL_FILE", line 536
ORA-29283: invalid file operation
ORA-06512: at "SYS.PRO_INST_STATUS", line 7
ORA-06512: at line 1
报错因为没有给dir建目录。开建。。
[root@recover ~]# cd /oracle/
[root@recover oracle]# mkdir monitor
[root@recover oracle]# chown -R oracle:oinstall monitor/

SQL> exec pro_inst_status;

PL/SQL procedure successfully completed.

SQL> exec pro_inst_status;

PL/SQL procedure successfully completed.
4、查看生成日志
SQL> exit
Disconnected from Oracle Database 11g Enterprise Edition Release 11.2.0.4.0 - 64bit Production
With the Partitioning, OLAP, Data Mining and Real Application Testing options
[oracle@recover oracle]$ cd monitor/
[oracle@recover monitor]$ ls -ltr
total 4
-rw-r--r-- 1 oracle oinstall 33 Aug 31 13:26 inst_status.log
[oracle@recover monitor]$ cat inst_status.log
2016-08-31 13:26:04,  idb,  OPEN
[oracle@recover monitor]$ sqlplus / as sysdba

SQL*Plus: Release 11.2.0.4.0 Production on Wed Aug 31 13:27:17 2016
Copyright (c) 1982, 2013, Oracle.  All rights reserved.
Connected to:
Oracle Database 11g Enterprise Edition Release 11.2.0.4.0 - 64bit Production
With the Partitioning, OLAP, Data Mining and Real Application Testing options
SQL> exec pro_inst_status;
PL/SQL procedure successfully completed.
SQL> exit
Disconnected from Oracle Database 11g Enterprise Edition Release 11.2.0.4.0 - 64bit Production
With the Partitioning, OLAP, Data Mining and Real Application Testing options
[oracle@recover monitor]$ cat inst_status.txt
2016-08-31 13:27:21,  idb,  OPEN
[oracle@recover monitor]$ sqlplus / as sysdba
SQL*Plus: Release 11.2.0.4.0 Production on Wed Aug 31 13:36:53 2016
Copyright (c) 1982, 2013, Oracle.  All rights reserved.

Connected to:
Oracle Database 11g Enterprise Edition Release 11.2.0.4.0 - 64bit Production
With the Partitioning, OLAP, Data Mining and Real Application Testing options
5、建job做定时作业
SQL> variable monitor_job number;
SQL> begin
  2  dbms_job.submit(:monitor_job,'pro_inst_status;',sysdate,'sysdate+1/288');
  3  end;
  4  /

PL/SQL procedure successfully completed.

SQL> desc dba_jobs
 Name                                      Null?    Type
 ----------------------------------------- -------- ----------------------------
 JOB                                       NOT NULL NUMBER
 LOG_USER                                  NOT NULL VARCHAR2(30)
 PRIV_USER                                 NOT NULL VARCHAR2(30)
 SCHEMA_USER                               NOT NULL VARCHAR2(30)
 LAST_DATE                                          DATE
 LAST_SEC                                           VARCHAR2(8)
 THIS_DATE                                          DATE
 THIS_SEC                                           VARCHAR2(8)
 NEXT_DATE                                 NOT NULL DATE
 NEXT_SEC                                           VARCHAR2(8)
 TOTAL_TIME                                         NUMBER
 BROKEN                                             VARCHAR2(1)
 INTERVAL                                  NOT NULL VARCHAR2(200)
 FAILURES                                           NUMBER
 WHAT                                               VARCHAR2(4000)
 NLS_ENV                                            VARCHAR2(4000)
 MISC_ENV                                           RAW(32)
 INSTANCE                                           NUMBER

SQL> select job, what from dba_jobs;

       JOB WHAT
---------- ----------------------------------------------------------------------------------------------------
      4001 wwv_flow_cache.purge_sessions(p_purge_sess_older_then_hrs => 24);
      4002 wwv_flow_mail.push_queue(wwv_flow_platform.get_preference('SMTP_HOST_ADDRESS'),wwv_flow_platform.get
           _preference('SMTP_HOST_PORT'));

         7 pro_inst_status;
         6 dbms_refresh.refresh('"I6000_SYS"."SYS_V_USERROLEORGRS"');

6、运行job
SQL> begin
  2  dbms_job.run(7);
  3  end;
  4   /

PL/SQL procedure successfully completed.

7、查看运行结果
[root@recover monitor]# tail -f inst_status.log
2016-08-31 13:45:45,  idb,  OPEN
tail: inst_status.log: file truncated
2016-08-31 13:50:45,  idb,  OPEN
tail: inst_status.log: file truncated
2016-08-31 13:55:46,  idb,  OPEN
tail: inst_status.log: file truncated
2016-08-31 14:00:46,  idb,  OPEN
tail: inst_status.log: file truncated
2016-08-31 14:05:46,  idb,  OPEN
tail: inst_status.log: file truncated
2016-08-31 14:10:46,  idb,  OPEN

原文地址:https://www.cnblogs.com/datalife/p/5825799.html