oracle how to capture user login information, such as SID, IP address, etc.

You can use login triggers, such as

  CREATE OR REPLACE TRIGGER tr_login_record

  AFTER logon ON DATABASE

  DECLARE

  miUserSid NUMBER;

  mtSession v$session%ROWTYPE;

  CURSOR cSession(iiUserSid IN NUMBER) IS

  SELECT * FROM v$session

  WHERE sid=iiUserSid;

  BEGIN

  SELECT sid INTO miUserSid FROM v$mystat WHERE rownum<=1;

  OPEN cSession(miUserSid);

  FETCH cSession INTO mtSession;

  --if user exists then insert data

  IF cSession%FOUND THEN

  INSERT INTO log$information(login_user,login_time,ip_adress,ausid,terminal,

  osuser,machine,program,sid,serial#)

  VALUES(ora_login_user,SYSDATE,SYS_CONTEXT ('USERENV','IP_ADDRESS'),

  userenv('SESSIONID'),

  mtSession.Terminal,mtSession.Osuser,

  mtSession.Machine,mtSession.Program,

  mtSession.Sid,mtSession.Serial#);

  ELSE

  --if user don't exists then return error

  sp_write_log('Session Information Error:'||SQLERRM);

  CLOSE cSession;

  raise_application_error(-20099,'Login Exception',FALSE);

  END IF;

  CLOSE cSession;

  EXCEPTION

  WHEN OTHERS THEN

  sp_write_log('Login Trigger Error:'||SQLERRM);

  END tr_login_record;

  In the above triggers need to note the following

  1, the user has permission object query v_ $ session and v_ $ mystat can support the explicit authorization under sys.

  2, sp_write_log was originally a log writing process can be replaced with their own needs, such as null Skip.

  3, you must create a log table record $ information logon information before you create the trigger.

  

  [Q] how to capture the entire database DDL statements or that object structure changes and modifications

  [A] DDL triggers may be employed, such as

  CREATE OR REPLACE TRIGGER tr_trace_ddl

  AFTER DDL ON DATABASE

  DECLARE

  sql_text ora_name_list_t;

  state_sql ddl$trace.ddl_sql%TYPE;

  BEGIN

  FOR i IN 1..ora_sql_txt(sql_text) LOOP

  state_sql := state_sql||sql_text(i);

  END LOOP;

  INSERT INTO ddl$trace(login_user,ddl_time,ip_address,audsid,

  schema_user,schema_object,ddl_sql)

  VALUES(ora_login_user,SYSDATE,userenv('SESSIONID'),

  sys_context('USERENV','IP_ADDRESS'),

  ora_dict_obj_owner,ora_dict_obj_name,state_sql);

  EXCEPTION

  WHEN OTHERS THEN

  sp_write_log('Capture DDL Excption:'||SQLERRM);

  END tr_trace_ddl;

  When create more trigger points to note

  1, you must create a ddl $ trace table to record the record ddl

2, sp_write_log was originally a log writing process can be replaced with their own needs, such as null Skip.

Guess you like

Origin www.cnblogs.com/fanweisheng/p/11113418.html