Friday, February 14, 2020

Clone a PDB in Oracle 12c Multi tanent

Step 1: Let's create PDB_SOURCE for cloning
> create pluggable database pdb_source
  admin user pdb_source_admin identified by password
  role = (dba)
  default tablespace pdb_source_ts datafile '/mnt/san_storage/oradata/orcl12c/pdb_source/pdb_sts_datafile1.dbf' size 50M autoextend on
  file_name_convert = ('/oradata/orcl12c/pdbseed','/oradata/orcl12c/pdb_source');



Step 2: Open the PDB_SOURCE
> alter pluggable database pdb_source open;


Step 3: For coning PDB_SOURCE, first close the PDB_SOURCE and open it in read only mode.
> alter pluggable database pdb_source close;
> alter pluggable database pdb_source open read only;


Step 4: Clone the PDB_SOURCE to PDB_TARGET.
> create pluggable database pdb_target from pdb_source file_name_convert = ('/oradata/orcl12c/pdb_source','/oradata/orcl12c/pdb_target/');


Step 3: Open PDB_TARGET
> alter pluggable database pdb_target open;


To Find/Add/Remove Responsibilities assigned to a User in Bulk in ORACLE APPS

--------------------------------------------------------------------------------------------------------------
To get list of responsibilities assigned to a user
--------------------------------------------------------------------------
select fu.user_name,
  frt.responsibility_name,
  furg.start_date,
  furg.end_date
from fnd_user fu ,
  fnd_user_resp_groups_direct furg ,
  fnd_responsibility_vl frt
where fu.user_id                 = furg.user_id
and frt.responsibility_id        = furg.responsibility_id
and frt.application_id           = furg.responsibility_application_id
and nvl(furg.end_date,sysdate+1) > sysdate
and nvl(frt.end_date,sysdate +1) > sysdate
and fu.user_name                   = :p_user_name ---> fnd_user name is needed

--------------------------------------------------------------------------------------------------------------
To Add bulk of responsibilities to a user
--------------------------------------------------------------------------
set serveroutput on size 1000000;
    DECLARE
    v_user_name VARCHAR2 (100) := &1; ---> fnd_user name is needed
    v_responsibility_name VARCHAR2 (100);
    v_application_name VARCHAR2 (100) := NULL;
    v_responsibility_key VARCHAR2 (100) := NULL;
    v_security_group VARCHAR2 (100) := NULL;
    v_description VARCHAR2 (100) := NULL;
    CURSOR c1
    IS
    SELECT fa.application_short_name,
    fr.responsibility_key,
    frg.security_group_key,
    frt.description,
frt.responsibility_name
    FROM fnd_responsibility fr,
    fnd_application fa,
    fnd_security_groups frg,
    fnd_responsibility_tl frt
    WHERE fr.application_id = fa.application_id
    AND fr.data_group_id = frg.security_group_id
    AND fr.responsibility_id = frt.responsibility_id
    AND frt.LANGUAGE = USERENV ('LANG')
    AND frt.responsibility_name IN (
'responsibility1',
'responsibility2',
'responsibility3',
'responsibility4'
);
    BEGIN
    BEGIN
           FOR C IN C1
      LOOP
       BEGIN
        fnd_user_pkg.addresp (username => v_user_name,
        resp_app => c.application_short_name,
        resp_key => c.responsibility_key,
        security_group => c.security_group_key,
        description => c.description,
        start_date => SYSDATE,
        end_date => NULL);
        COMMIT;
        DBMS_OUTPUT.put_line ('Responsibility ' || c.responsibility_name || ' is attached to the user '|| v_user_name
        || ' Successfully');
            EXCEPTION
        WHEN OTHERS
      THEN
        DBMS_OUTPUT.put_line ('Issue while calling standard api' ||SQLERRM);
       END;
      END LOOP;
     EXCEPTION
     WHEN NO_DATA_FOUND
     THEN
      DBMS_OUTPUT.put_line ('NO data found while attaching responsibilty to the user and the error is '|| SQLERRM);
     WHEN OTHERS
     THEN
     DBMS_OUTPUT.put_line ('Error encountered while attaching responsibilty to the user and the error is '|| SQLERRM);
     END;
    END;
/

--------------------------------------------------------------------------------------------------------------------
To End date List of responsibilities assigned to a user
------------------------------------------------------------------------------
DECLARE
CURSOR c1
IS
SELECT fu.user_name,
fa.application_short_name,
frt.responsibility_name,
fr.responsibility_key,
fsg.security_group_key
FROM fnd_user_resp_groups_all ful,
fnd_user fu,
fnd_responsibility_tl frt,
fnd_responsibility fr,
fnd_security_groups fsg,
fnd_application fa

WHERE fu.user_id = ful.user_id
AND frt.responsibility_id = ful.responsibility_id
AND fr.responsibility_id = frt.responsibility_id
AND fsg.security_group_id = ful.security_group_id
AND fa.application_id = ful.responsibility_application_id
AND frt.language = 'US'
AND fu.user_name = :p_user_name ---> fnd_user name is needed

AND RESPONSIBILITY_NAME IN
('responsibility1',
'responsibility2',
'responsibility3',
'responsibility4');


BEGIN
FOR i IN c1
LOOP
BEGIN
fnd_user_pkg.delresp (username => i.user_name,
resp_app => i.application_short_name,
resp_key => i.responsibility_key,
security_group => i.security_group_key);
COMMIT;
DBMS_OUTPUT.
put_line (
i.responsibility_name || ' has been End Dated Successfully !!!');
EXCEPTION
WHEN OTHERS
THEN
DBMS_OUTPUT.
put_line (
'Inner Exception: '
|| ' - '
|| i.responsibility_key
|| ' - '
|| SQLERRM);
END;
END LOOP;

EXCEPTION

WHEN OTHERS
THEN
DBMS_OUTPUT.put_line ('Main Exception: ' || SQLERRM);

END;
----------------------------------------------------------------------------------------------------------------------

Complete Workflow Mailer Backend Daily Scripts

--------------------------------------------------------------------------------------------------------------------------
Check workflow mailer service current status --Number of running processes should be greater than 0
---------------------------------------------------------------------------------
 select running_processes
    from apps.fnd_concurrent_queues
   where concurrent_queue_name = 'WFMLRSVC';


-------------------------------------------------------------------------------------------------------------------------
Find current mailer status
---------------------------------------------------------------------------------
select component_status
    from apps.fnd_svc_components
   where component_id =
        (select component_id
           from apps.fnd_svc_components
          where component_name = 'Workflow Notification Mailer');


-------------------------------------------------------------------------------------------------------------------------
Robust script for monitoring the wf_notifications table
---------------------------------------------------------------------------------
select message_type, mail_status, count(*) from wf_notifications
where status = 'OPEN'
GROUP BY MESSAGE_TYPE, MAIL_STATUS

--messages in 'FAILED' status can be resent using the concurrent request 'resend failed workflow notificaitons'
--messages which are OPEN but where mail_status is null have a missing email address for the recipient, but the notification preference is 'send me mail'


-------------------------------------------------------------------------------------------------------------------------
Some messages like alerts don't get a record in wf_notifications table so you have to watch the WF_NOTIFICATION_OUT queue.
---------------------------------------------------------------------------------
select corr_id, retry_count, msg_state, count(*)
from applsys.aq$wf_notification_out
group by corr_id, msg_state, retry_count
order by count(*) desc;

--Messages with a high retry count have been cycling through the queue and are not passed to smtp service.Messages which are 'expired' can be rebuilt using the wfntfqup.sql


-------------------------------------------------------------------------------------------------------------------------
The following SQL to collect all the info except IMAP account password.
---------------------------------------------------------------------------------
select p.parameter_id,
p.parameter_name,
v.parameter_value value
from fnd_svc_comp_param_vals_v v,
fnd_svc_comp_params_b p,
fnd_svc_components c
where c.component_type = 'WF_MAILER'
and v.component_id = c.component_id
and v.parameter_id = p.parameter_id
and p.parameter_name in ('OUTBOUND_SERVER', 'INBOUND_SERVER',
'ACCOUNT', 'FROM', 'NODENAME', 'REPLYTO','DISCARD' ,'PROCESS','INBOX')


------------------------------------------------------------------------------------------------------------------------
Check The Workflow notification has been sent or not
---------------------------------------------------------------------------------
SELECT status, mail_status  FROM wf_notifications WHERE notification_id = 10609859;

SELECT email_address, nvl(WF_PREF.get_pref(name, ‘MAILTYPE’),notification_preference)
FROM wf_roles
WHERE name = upper(‘&recipient_role’);


-------------------------------------------------------------------------------------------------------------------------
Query to get the log file of active workflow mailer and workflow agent listener Container
---------------------------------------------------------------------------------
select fl.meaning,fcp.process_status_code, decode(fcq.concurrent_queue_name,'WFMLRSVC', 'mailer container',
'WFALSNRSVC','listener container',fcq.concurrent_queue_name),
fcp.concurrent_process_id,os_process_id, fcp.logfile_name
from fnd_concurrent_queues fcq, fnd_concurrent_processes fcp , fnd_lookups fl
where fcq.concurrent_queue_id=fcp.concurrent_queue_id and fcp.process_status_code='A'
and fl.lookup_type='CP_PROCESS_STATUS_CODE' and fl.lookup_code=fcp.process_status_code
and concurrent_queue_name in('WFMLRSVC','WFALSNRSVC')
order by fcp.logfile_name;


-------------------------------------------------------------------------------------------------------------------------
Query to Check Workflow Mailer Backlog --State=Ready implies that emails are not being sent & Waiting mailer to send emails
---------------------------------------------------------------------------------
select tab.msg_state, count(*) from applsys.aq$wf_notification_out tab group by tab.msg_state ;


-------------------------------------------------------------------------------------------------------------------------
Check any particular Alert Message email has be pending by Mailer
---------------------------------------------------------------------------------
select decode(wno.state,
0, '0 = Pending in mailer queue',
1, '1 = Pending in mailer queue',
2, '2 = Sent by mailer on '||to_char(DEQ_TIME),
3, '3 = Exception', 4,'4 = Wait', to_char(state)) State,
to_char(DEQ_TIME),
wno.user_data.TEXT_VC
from wf_notification_out wno
where corrid='APPS:ALR'
and upper(wno.user_data.TEXT_VC) like '%<Subject of Alert Email>%';


-------------------------------------------------------------------------------------------------------------------------
Check Whether workflow background Engine is working for given workflow or not in last 2 days -- Note: Workflow Deferred activities are run by workflow background engine.
---------------------------------------------------------------------------------
select a.argument1,a.phase_code, a.status_code ,a.actual_start_date,a.* from fnd_concurrent_requests a
where CONCURRENT_PROGRAM_ID =
(select concurrent_program_id from fnd_concurrent_programs where
CONCURRENT_PROGRAM_NAME='FNDWFBG')
and last_update_Date>sysdate-2 and argument1='<Workflow Item Type>'
order by last_update_date desc


--------------------------------------------------------------------------------------------------------------------------
Query to get the log file of active workflow mailer and workflow agent listener Container
---------------------------------------------------------------------------------
select fl.meaning,fcp.process_status_code, decode(fcq.concurrent_queue_name,'WFMLRSVC', 'mailer container',
'WFALSNRSVC','listener container',fcq.concurrent_queue_name),
fcp.concurrent_process_id,os_process_id, fcp.logfile_name
from fnd_concurrent_queues fcq, fnd_concurrent_processes fcp , fnd_lookups fl
where fcq.concurrent_queue_id=fcp.concurrent_queue_id and fcp.process_status_code='A'
and fl.lookup_type='CP_PROCESS_STATUS_CODE' and fl.lookup_code=fcp.process_status_code
and concurrent_queue_name in('WFMLRSVC','WFALSNRSVC')
order by fcp.logfile_name;


-------------------------------------------------------------------------------------------------------------------------
Linux Shell script Command to get outbound error in Mailer
---------------------------------------------------------------------------------
grep -i '^\[[A-Za-z].*\(in\|out\).*boundThreadGroup.*\(UNEXPECTED\|ERROR\).*exception.*' <logfilename> | tail -10 ;
--Note: All Mailer log files starts with name FNDCPGSC prefix


--------------------------------------------------------------------------------------------------------------------------
Linux Shell script Command to get inbound processing error in Mailer 
---------------------------------------------------------------------------------
grep -i '^\[[A-Za-z].*.*inboundThreadGroup.*\(UNEXPECTED\|ERROR\).*exception.*' <logfilename> | tail -10 ;
----------------------------------------------------------------------------------------------------
1) Identify the concurrent tiers node where mailer runs 
---------------------------------------------------------------------------------------------------- 
select target_node
from fnd_concurrent_queues where concurrent_queue_name like 'WFMLRSVC%';
---------------------------------------------------------------------------------------------------- 
2) Gather other parameters values necessary for the SMTP telnet test: 
---------------------------------------------------------------------------------------------------- 
SELECT b.component_name,
       c.parameter_name,
       a.parameter_value
FROM fnd_svc_comp_param_vals a,
     fnd_svc_components b,
     fnd_svc_comp_params_b c
WHERE b.component_id = a.component_id
     AND b.component_type = c.component_type
     AND c.parameter_id = a.parameter_id
     AND c.encrypted_flag = 'N'
     AND b.component_name like '%Mailer%'
     AND c.parameter_name in ('OUTBOUND_SERVER', 'REPLYTO')
ORDER BY c.parameter_name;
---------------------------------------------------------------------------------------------------- 
3) Perform the SMTP telnet test as follows: 
---------------------------------------------------------------------------------------------------- 
3.1) Log on to the node where mailer runs (to identify it, please refer to step 1)

This is mandatory. SMTP telnet test is only meaningful when it is performed from the concurrent tier where mailer runs.

3.2) From mailer node, issue the following commands one by one:
telnet hostname.domain 25
EHLO OPRDCM1
MAIL FROM: mailserver@email.com
RCPT TO: abc@xyz.com
DATA
Subject: Test message

Test message body 

quit
--------------------------------------------------------------------------------------------------------------------------

Sunday, March 17, 2019

Moving PDB in between CDB's


Step 1: Let's create PDB1 for demo
> create pluggable database pdb1
  admin user pdb1_admin identified by password
  role = (dba)
  default tablespace pdb1_ts datafile '/mnt/san_storage/oradata/orcl12c/pdb1/pdb1_ts_datafile1.dbf' size 50M autoextend on
  file_name_convert = ('/oradata/orcl12c/pdbseed','/oradata/orcl12c/pdb1');


Step 2: Open the PDB1
> alter pluggable database pdb1 open;


Step 3: For Moving PDB1, first close the PDB1
> alter pluggable database pdb1 close;


Step 4: Unplug the database
> alter pluggable database pdb1 unplug into 'mnt/san_storage/oradata/orcl12c/pdb1/pdb1.xml';


Step 5: Drop the database PDB1 keeping the datafiles;
> drop pluggable database pdb1 keep datafiles;


Step 6: Now plug PDB1 to the new CDB
> connect cdb$root of new CDB as sysdba
> create pluggable database pdb1_new using '/mnt/san_storage/oradata/orcl12c/pdb1/pdb1.xml' nocopy tempfile reuse;
Note : We can give new pdb name while using xml file or we can use the same name.


Step 7: Open newely plugged PDB1_NEW in the new CDB.
> alter pluggable database pdb1_new open;

Saturday, March 16, 2019

How to Create, Connect and Drop Pluggable database(PDB)


Some conceptual points for Create PLUGGABLE database -
  • Oracle 12c uses SEED$PDB as template to create new PDB's.
  • When you create a new PDB from SEED$PDB, you have to define admin user for that new PDB. This creates a local user inside the PDB.
  • Admin command also grants the PDB_DBA role to the ADMIN user that we created.
  • By default "CREATE PLUGGABLE DATABASE" command does not grant any privileges to the PDB_DBA role itself. So our admin user is powerless.
  • In order to let this user to control PDB, we have to explicitly grant dba role to PDB user.



Following is the command used to create new pluggable database in Oracle 12c.
> CREATE PLUGGABLE DATABASE my_new_pdb
   ADMIN USER my_pdb_admin IDENTIFIED BY password
   ROLES = (dba)
   DEFAULT TABLESPACE my_tbs 
   DATAFILE '/mnt/san_storage/oradata/orcl12c/my_new_pdb/mytbs01.dbf' SIZE 50M AUTOEXTEND ON
   FILE_NAME_CONVERT=('/oradata/orcl12c/pdbseed/','/oradata/orcl12c/my_new_pdb/');










To Check if this PDB is created run the following command
> select con_id, name from v$pdbs;









To connect the PDB, run following commands -
> alter pluggable database my_new_pdb open;
> exit;

sqlplus my_pdb_admin/password@localhost:1523/my_new_pdb
> show con_name;
> show con_id;
























Or, You can also use "alter session" command to directly connect to any container
> alter session set container = my_new_pdb;
> alter session set container = cbd$root;


To drop PDB use the following commands
> alter pluggable database my_new_pdb close;
> drop pluggable database my_new_pdb including datafiles;


















Thursday, March 14, 2019

How to check database size in Oracle


A DBA works on many aspects of database like cloning, backup, performance tuning etc. In every aspect of database administration, most of the times resolution depends upon the size of database. For example, DBA can implement DB FULL backup strategy on a very small database when compared to DB INCREMENTAL strategy on a very large database.

Use below script to check db size along with Used space and free space in database:

col "Database Size" format a20
col "Free space" format a20
col "Used space" format a20
select round(sum(used.bytes) / 1024 / 1024 / 1024 ) || ' GB' "Database Size" , round(sum(used.bytes) / 1024 / 1024 / 1024 ) - round(free.p / 1024 / 1024 / 1024) || ' GB' "Used space" , round(free.p / 1024 / 1024 / 1024) || ' GB' "Free space" from (select bytes from v$datafile union all select bytes from v$tempfile union all select bytes from v$log) used , (select sum(bytes) as p from dba_free_space) free group by free.p /


How to disable firewall in Linux 7


Firewall Status 
The below command will show you the current status “Active” in case firewall is running: 
# systemctl status firewalld

Firewall stop / start 
# service firewalld stop 
# service firewalld start 
You can start/stop Linux firewall with below commands:

Firewall Disable / Enable 
You can enable/disable firewall completely on Linux with below commands: 
# systemctl disable firewalld 
# systemctl enable firewalld

Oracle Database Administrator