In conjunction with this article on connecting PHP to the Oracle database, this time, combined with the rrdtool drawing tool, to draw some custom monitoring charts, although Oracle's EM has been perfect, however, you may not be able to monitor issues that are user-defined or concerned with the Business Environment Based on your business needs. In this case, you need to do it yourself.
First, use php to write a production script for the rrdtool database file.
- #! /Usr/bin/php-f
- <? Php
- Define ('wd ','/u01/app/oracle/awr ');
- $ Cs = $ _ SERVER ['argv'] [1];
- $ User = 'system ';
- $ Pass = 'oracle ';
- /* Open a new connection */
- $ Ds = oci_connect ($ user, $ pass, $ cs)
- Or die ("Cannot connect to Oracle Database". $ cs. "\ n ");
- /* Setting client nls environment */
- $ SQL = "alter session set nls_timestamp_format = 'Mm/DD/YY HH24: Mi '";
- $ Stmt = oci_parse ($ ds, $ SQL );
- Oci_execute ($ stmt );
- Oci_free_statement ($ stmt );
- /* Create directory that will contain in rrds (if not exists )*/
- If (! File_exists (WD. '/'. $ cs ))
- Mkdir (WD. '/'. $ cs );
- If (! File_exists (WD. '/'. $ cs. '/wait '))
- Mkdir (WD. '/'. $ cs. '/wait ');
- /* Function to create new RRDs */
- Function createRRD ($ name, $ interval, $ cs ){
- $ Hb = $ interval * 5; // heartbeat
- $ Cmd = "rrdtool create". WD. "/". $ cs. "/wait/$ {name}. rrd-s". $ interval ."\
- -B \ "now-3month \" DS: waits: DERIVE: $ hb: 0: U \
- DS: mswaited: DERIVE: $ hb: 0: U \
- RRA: AVERAGE: 0.5: 1: 1440 RRA: AVERAGE: 0.5: 30: 336 \
- RRA: AVERAGE: 0.5: 120: 372 RRA: AVERAGE: 0.5: 720: 730 \
- RRA: MIN: 0.5: 1: 1440 RRA: MIN: 0.5: 30: 336 \
- RRA: MIN: 0.5: 120: 372 RRA: MIN: 0.5: 720: 730 \
- RRA: MAX: 0.5: 1: 1440 RRA: MAX: 0.5: 30: 336 \
- RRA: MAX: 0.5: 120: 372 RRA: MAX: 0.5: 720: 730 \
- RRA: LAST: 0.5: 1: 1440 ";
- // Print $ cmd. "\ n ";
- Return passthru ($ cmd );
- }
- /* Take the snapshot frequency from dba_hist_wr_control
- To create the RDD with correct heartbeat value */
- $ SQL = 'select extract (hour from snap_interval) * 3600 +
- Extract (minute from snap_interval) * 60 as SEED from DBA_HIST_WR_CONTROL ';
- $ Stmt = oci_parse ($ ds, $ SQL );
- Oci_execute ($ stmt );
- $ Row = oci_fetch_assoc ($ stmt );
- $ Interval = $ row ['seed'];
- Unset ($ row );
- Oci_free_statement ($ stmt );
- /* Statement definition that will collect
- All snapshots for a certain wait event with more
- A certain amonut of time waited.
- Gathering all events cocould be time consuming and useless.
- I fetch rows ordered by event_name rather
- Then by date because I can update your values
- Into the same rrd with very few rrdupdate commands
- */
- $ SQL = 'select s. END_INTERVAL_TIME,
- G. EVENT_NAME, g. WAIT_CLASS, g. TOTAL_WAITS,
- Round (g. TIME_WAITED_MICRO/1000) MS
- From DBA_HIST_SNAPSHOT s,
- Dba_hist_bg_event_summary g,
- V $ instance I
- Where s. SNAP_ID = g. SNAP_ID and g. wait_class! = \ 'Idle \'
- And g. TIME_WAITED_MICRO> 100000
- And s. instance_number = I. instance_number
- And s. instance_number = g. instance_number
- Order by 2, 1 ';
- /* Default prefetch size (148) matches default snapshot retention (24hx7dd )*/
- $ Stmt = oci_parse ($ ds, $ SQL );
- Oci_set_prefetch ($ stmt, 148 );
- Oci_execute ($ stmt );
- $ I = 0;
- $ Oldevent = "";
- While ($ row = oci_fetch_assoc ($ stmt )){
- If ($ oldevent! = $ Row ['event _ name']) {
- // New event detected: WILL START A NEW UPDATE CMD
- If ($ I! = 0 & amp ;! Empty ($ cmd )){
- /* Not the first occurrence,
- I bet there's something in my buffer */
- Passthru ($ cmd );
- }
- $ CleanName = preg_replace ("([^ [: alnum:] _-])", "_", $ row ['event _ name']);
- // If there is no rrd for this event, I create a new one
- If (! File_exists (WD. "/". $ cs. "/wait/$ {cleanName}. rrd ")){
- CreateRRD ($ cleanName, $ interval, $ cs );
- }
- /*
- * I initialize a new update command. This string act as a buffer: I append signature
- * Values to be updated so I'll update them values in a single command line:
- * Less forks of rrdtool and less file opens: the whole update process has
- * Enormous improvement.
- */
- $ Precmd = "rrdtool update". WD. "/". $ cs. "/wait/$ {cleanName}. rrd ";
- $ Lastcmd = "rrdtool info". WD. "/". $ cs. "/wait/$ {cleanName}. rrd ".
- "| Grep last_update | awk '{print \ $ NF }'";
- $ Last = trim ('$ lastcmd ');
- Printf ("% s-last: % d \ n", $ row ['event _ name'], $ cleanName, $ last );
- $ I = 0;
- $ Cmd = $ precmd;
- $ Oldevent = $ row ['event _ name'];
- }
- $ Time = strtotime ($ row ['end _ INTERVAL_TIME ']);
- // Print "time:". $ time. "last:". $ last. "\ n ";
- If ($ time & gt; $ last ){
- $ Cmd. = "". $ time. ":". $ row ['total _ WAITS ']. ":". $ row ['Ms'];
- $ I ++;
- }
- If ($ I & gt; = 40 ){
- // When I reach 40 values per commandline I force
- // The update: next loop will reinitialize a new commandline.
- Passthru ($ cmd );
- $ Cmd = $ precmd;
- $ I = 0;
- }
- Unset ($ row );
- }
- If ($ I! = 0 ){
- /* One more update pending in my buffer */
- Passthru ($ cmd );
- }
- Oci_free_statement ($ stmt );
- Oci_close ($ ds );
- ?>
Run the preceding script to generate the rrd file.
- # Ll
- Total 3864
- -Rw-r -- 1 oracle oinstall 165244 Nov 24 Streams_AQ _ enqueue_blocked_on_low_memory.rrd
- -Rw-r -- 1 oracle oinstall 165244 Nov 24 buffer_busy_waits.rrd
- -Rw-r -- 1 oracle oinstall 165244 Nov 24 control_file_parallel_write.rrd
- -Rw-r -- 1 oracle oinstall 165244 Nov 24 23:28 control_file_sequential_read.rrd
- -Rw-r -- 1 oracle oinstall 165244 Nov 24 cursor _ pin_S_wait_on_X.rrd
- -Rw-r -- 1 oracle oinstall 165244 Nov 24 23:28 db_file_scattered_read.rrd
- -Rw-r -- 1 oracle oinstall 165244 Nov 24 23:28 db_file_sequential_read.r
- -Rw-r -- 1 oracle oinstall 165244 Nov 24 events_in_waitclass_Other.rrd
- -Rw-r -- 1 oracle oinstall 165244 Nov 24 latch _ cache_buffers_chains.rrd
- -Rw-r -- 1 oracle oinstall 165244 Nov 24 latch _ library_cache.rrd
- -Rw-r -- 1 oracle oinstall 165244 Nov 24 latch _ library_cache_lock.rrd
- -Rw-r -- 1 oracle oinstall 165244 Nov 24 latch _ redo_writing.rrd
- -Rw-r -- 1 oracle oinstall 165244 Nov 24 latch _ row_cache_objects.rrd
- -Rw-r -- 1 oracle oinstall 165244 Nov 24 latch _ shared_pool.rrd
- -Rw-r -- 1 oracle oinstall 165244 Nov 24 library_cache_load_lock.rrd
- -Rw-r -- 1 oracle oinstall 165244 Nov 24 library_cache_lock.rrd
- -Rw-r -- 1 oracle oinstall 165244 Nov 24 log_buffer_space.rrd
- -Rw-r -- 1 oracle oinstall 165244 Nov 24 log_file_parallel_write.rrd
- -Rw-r -- 1 oracle oinstall 165244 Nov 24 23:28 log_file_sequential_read.rrd
- -Rw-r -- 1 oracle oinstall 165244 Nov 24 log_file_single_write.rrd
- -Rw-r -- 1 oracle oinstall 165244 Nov 24 log_file_switch_completion.rrd
- -Rw-r -- 1 oracle oinstall 165244 Nov 24 log_file_sync.rrd
- -Rw-r -- 1 oracle oinstall 165244 Nov 24 OS _thread_startup.rrd
Use the rrdtool command to plot as follows: