We need to monitor all SQL-s coming from a certain Application Server to an Oracle DB.
For this, we are running the following query:
SELECT
h.sample_time,
u.username,
h.program,
h.module,
h.machine,
s.sql_text
FROM
DBA_HIST_ACTIVE_SESS_HISTORY h,
DBA_USERS u,
DBA_HIST_SQLTEXT s
WHERE sample_time >= SYSDATE - 0.1
AND h.user_id = u.user_id
AND h.sql_id = s.sql_iD
AND u.USERNAME = '<app_server_name>'
AND h.MACHINE = ('<app_schema_name>')
ORDER BY h.sample_time
Problem with this:
Missing OS User of the Application Server machine.
Could not find anything about OS User on views DBA_HIST_ACTIVE_SESS_HISTORY and DBA_HIST_SQLTEXT. (Meanwhile there is a column OSUSER on view V$SESSION - the non-history version of DBA_HIST_ACTIVE_SESS_HISTORY)
I took a look on columns of DBA %HIST% views:
select t.owner, t.table_name, t.column_name, t.data_type,
t.data_precision, t.data_scale
from dba_tab_cols t
where t.owner = 'SYS'
and t.table_name like '%DBA_HIST%'
and t.column_name like '%OS%'
order by t.table_name, t.COLUMN_NAME
but could not find anything related to OS User info.
Any idea of how I can have the OS User data on my first query ?
best regards
Altin
First, is there a reason why you aren't enabling auditing? That would be the only way to capture 100% of the queries by a given user.
Auditing isn't a perfect fit in all cases, particularly if the volume is excessive, so looking at ASH data does make sense as an alternative approach. But keep in mind that
dba_hist_active_sess_historyis only populated at an AWR snapshot interval (typically 1 hour apart, but configurable). YourSYSDATE - 0.1filter will only work if it faithfully fires each hour and the AWR interval is indeed an hour or less. Skip an hour or two and you'll miss your data. Also thedba_histversion is only 1 out of every 10 samples, so can miss a lot of small stuff. You are much better off querying the in-memory versiongv$active_session_history, which has 10x more entries and is real-time. You might want to set up something to hit this every minute, or every 10 minutes. It typically keeps its info for a few hours at least. That'd be a lot more accurate than thedba_histversion. Be aware that even this isn't 100% comprehensive. It gets its data by sampling the wait interface once per second. If a query executes in under a second and isn't repeatedly executed, you may not ever see it. But it's close enough for most purposes.You will not find
osuseranywhere in ASH data. If you want this, you will need to create a logon trigger that captures session info for every new session. You can save space by only capturing it if it's from the machine in question, whatever kind of filters make sense for you. But then you have a full set ofv$sessionattributes at your disposal within the trigger, plus additionalSYS_CONTEXTattributes. Write what you need to a log table keyed bysid,serial#,inst_id,logon_time. Later you can join ASH data (gv$active_session_history) to this log table usingsession_id/session_serial#/inst_idand add whatever additional attributes you have there for the session.