Pages

Monday, December 14, 2015

Script to monitor Goldengate replication


## Please test this script in your environment before you deploy.

The below script can be used to monitor your Goldengate replication and the script takes 3 input parameter values as below -

ORACLE_SID   : Database instance name that is configured for Goldengate replication.
GOLDENGATE_HOME : Directory where Goldengate is installed.
LAG_THRESHOLD   : Lag threshold in minutes

The below script will alert if any one of the following happens -
a. if any of the Goldengate process is abended or stopped.
b. if the replication latency is more than a value(in minutes) that is passed to the script as a parameter
c. if there are any errors/warnings in the goldengate log file (ggserr.log)

-- 

#!/bin/ksh
# Script      : monitoring_gg.sh
# Version     : 1.0
# Purpose     : To monitor Goldengate processes and latency
# Usage       : monitoring_gg.sh ORACLE_SID GOLDENGATE_HOME LAG_THRESHOLD
# Example     : monitoring_gg.sh DBNAME /u01/app/goldengate/11.2.1.0.17 30
#
# Parameters:
#   1. ORACLE_SID: Database Instance name. This parameter is used to set the appropriate DB environment variables. (NOTE: This is the Instance name not Database name.)
#   2. GOLDENGATE_HOME: Home directory of Goldengate installation
#   3. LAG_THRESHOLD: Allowed lag time in minutes
#
# ******************
# Functions
# ******************
# Usage function.
function show_usage {
    echo " "
    echo "Usage: $PWD/monitoring_gg.sh ORACLE_SID GOLDENGATE_HOME LAG_THRESHOLD"
    echo "   ORACLE_SID  : Name of the Database instance that is configured for Goldengate replication. "
    echo "   GOLDENGATE_HOME : Directory where Goldengate is installed. "
    echo "   LAG_THRESHOLD   : Lag threshold in minutes"
    echo "Example: $PWD/monitoring_gg.sh DBNAME /u01/app/goldengate/11.2.1.0.17 30"
    echo " "
    exit 1
}

# **************************************
# Input parameter validation
# **************************************
if [ "$1" ]
then
   ORACLE_SID=`echo $1 | tr "[a-z]" "[A-Z]" `; export ORACLE_SID
else
   show_usage
fi

if [ "$2" ]
then
   GOLDENGATE_HOME=`echo $2`; export GOLDENGATE_HOME
else
   show_usage
fi

if [ "$3" ]
then
   LAG_THRESHOLD=`echo $3`; export LAG_THRESHOLD
else
   show_usage
fi

# **************************************
# Setting up the environment
# **************************************
ORAENV_ASK=NO; export ORAENV_ASK
. /usr/local/bin/oraenv
ORAENV_ASK= ; export ORAENV_ASK

export LD_LIBRARY_PATH=$ORACLE_HOME/lib:$GOLDENGATE_HOME
export PATH=$GOLDENGATE_HOME:$PATH
export MAIL_LIST=abcd@efgh.com
" 

# **************************************
# Gather Goldengate information
# **************************************
cd $GOLDENGATE_HOME
$GOLDENGATE_HOME/ggsci -s << EOF > /tmp/monitoring_gg.log
info all
exit
EOF

# ********************************************
# Monitoring Godlengate processes and lag time
# ********************************************
cat /tmp/monitoring_gg.log | egrep 'MANAGER|EXTRACT|REPLICAT'| tr ":" " " | while read LINE
do
  case $LINE in
    *)
    PROCESS_TYPE=`echo $LINE | awk -F" " '{print $1}'`
    PROCESS_STATUS=`echo $LINE | awk -F" " '{print $2}'`
    if [ "$PROCESS_TYPE" == "MANAGER" ]
    then
       if [ "$PROCESS_STATUS" != "RUNNING" ]
       then
           SUBJECT="ALERT ... Goldengate process \"$PROCESS_TYPE\" is $PROCESS_STATUS on `uname -n`($ORACLE_SID)"
           mailx -s "$SUBJECT" $MAIL_LIST < $GOLDENGATE_HOME/dirrpt/MGR.rpt
           exit 1
       else
           continue
       fi
    elif [ "$PROCESS_TYPE" == "JAGENT" ]
    then
       if [ "$PROCESS_STATUS" != "RUNNING" ]
       then
           SUBJECT="WARNING ... Goldengate process \"$PROCESS_TYPE\" is $PROCESS_STATUS on `uname -n`"
           mailx -s "$SUBJECT" $MAIL_LIST < $GOLDENGATE_HOME/dirrpt/JAGENT.rpt
       fi
    else
       PROCESS_NAME=`echo $LINE | awk -F" " '{print $3}'`
       LAG_HH=`echo $LINE | awk -F" " '{print $4}'`
       LAG_MM=`echo $LINE | awk -F" " '{print $5}'`
       LAG_SS=`echo $LINE | awk -F" " '{print $6}'`
       CKPT_HH=`echo $LINE | awk -F" " '{print $7}'`
       CKPT_MM=`echo $LINE | awk -F" " '{print $8}'`
       CKPT_SS=`echo $LINE | awk -F" " '{print $9}'`

       if [ "$PROCESS_STATUS" != "RUNNING" ]
       then
           SUBJECT="ALERT ... Goldengate process \"$PROCESS_TYPE($PROCESS_NAME)\" is $PROCESS_STATUS on `uname -n`($ORACLE_SID)"
           mailx -s "$SUBJECT" $MAIL_LIST < $GOLDENGATE_HOME/dirrpt/${PROCESS_NAME}.rpt
       else
           if [ $LAG_HH -gt 00 -o $LAG_MM -ge $LAG_THRESHOLD ];
           then
              SUBJECT="ALERT ... Goldengate process \"$PROCESS_TYPE($PROCESS_NAME)\" has a lag of "$LAG_HH" hour "$LAG_MM" min on `uname -n`($ORACLE_SID)"
              mailx -s "$SUBJECT" $MAIL_LIST < $GOLDENGATE_HOME/dirrpt/${PROCESS_NAME}.rpt
           fi
 
      fi
    fi
  esac
done

# **************************************
# Monitoring Godlengate Error log
# **************************************

GG_ERROR_FILE=$GOLDENGATE_HOME/ggserr.log
GG_ERROR_MNTR=$GOLDENGATE_HOME/ggserr.monitor
GG_ERROR_DIFF=$GOLDENGATE_HOME/ggserr.diff
touch $GG_ERROR_DIFF

if [[ -r ${GG_ERROR_FILE} ]]
then
    touch $GG_ERROR_MNTR
    cp $GG_ERROR_MNTR $GG_ERROR_MNTR".old"
    egrep "ERROR|WARNING" $GG_ERROR_FILE > $GG_ERROR_MNTR

    CUR_CNT=`cat $GG_ERROR_MNTR |wc -l`
    PRV_CNT=`cat $GG_ERROR_MNTR".old" |wc -l`

    if [[ $CUR_CNT -lt $PRV_CNT ]]
    then
        # This means that ggserr.log is purged, so let's clear out the previous monitor results
        PRV_CNT=0
    fi

    if [[ $CUR_CNT -gt $PRV_CNT ]]
    then
        diff $GG_ERROR_MNTR $GG_ERROR_MNTR".old" | grep "^<" | sed -e 's/^< //' > ${GG_ERROR_DIFF}

        # send a mail..
        if test `cat ${GG_ERROR_DIFF} | wc -l` -gt 1
        then
            # multipl errors found. process only the last ten.
            tail -10 $GG_ERROR_DIFF > $GG_ERROR_DIFF".1"
        else
            # single error found.
            cp $GG_ERROR_DIFF $GG_ERROR_DIFF".1"
        fi
        SUBJECT="WARNING .. Errors encountered in Goldengate replication on `uname -n`($ORACLE_SID)"
        mailx -s "$SUBJECT" $MAIL_LIST < $GG_ERROR_DIFF".1"
    fi
    rm $GG_ERROR_DIFF*
fi

netmask

IPv4地址被分为三部分:网络部分(network)、子网部分(subnetwork,现在常被认为是网络部分的一部分——尽管它本来是“rest”部分的一部分)和主机(host)部分。共有三类IP地址,它们分别指定了各部分占多少位。
类别起始位开始结束点分十进制掩码
A00.0.0.0127.0.0.0255.0.0.0
B10128.0.0.0191.255.0.0255.255.0.0
C110192.0.0.0223.255.255.0255.255.255.0
子网的划分是一个将主机部分的若干位分配到网络部分的过程。例如,对于一个给定的A类网络:10.0.0.0,子网掩码:255.255.0.0可以将其划分为256个子网(从10.0.0.0到10.255.0.0)——第一个八位位组表示网络地址,第二个表示子网号,而最后两个表示主机部分。用子网掩码对主机地址进行位与操作,就能够提取出完整的子网地址(参见下面的例子)。
子网掩码并不局限于整数个八位位组的情况。例如,255.254.0.0(或“/15”)同样是一个有效的掩码。如果将它应用到A类地址上,就会产生128个间隔为2的子网(例如1.2.0.1~1.3.255.254,1.4.0.1~1.5.255.254等等)。這情況我們可以說是 Class A 的 network 向 host 借了 7個bits,也可以說是 host 向 Class B 的 network 借了 1個bits,後者又稱為超級線路網(英语:Supernetting)。

Linux VIP


1. Introduction

Did you know that you can assign more that one IP address to a single physical network interface? This technique is quite useful, for example when working with Apache and virtual hosts, as it allows you to access same Apache server by using two different IP addresses.

2. Temporary virtual network interface

The process of creating a virtual network interface in Linux is a quite simple matter. It involves a single execution of the ifconfig command.
ifconfig eth0:0 123.123.22.22
The above command will create a new virtual network interface based on original eth0 physical interface. The only most important condition for creating the virtual network interface is the physical network interface, as in our case eth0 must exists. The whole example is shown below:
# ifconfig eth0 
eth0 Link encap:Ethernet  HWaddr 3c:97:0e:02:98:c8  
inet addr:192.168.100.23  Bcast:192.168.100.255  
Mask:255.255.255.0
# ping 192.168.100.23
PING 192.168.100.23  bytes of data.
64 bytes from 192.168.100.23 time=0.023 ms
64 bytes from 192.168.100.23 time=0.059 m
At this point we can configure a new virtual interface based on eth0. Once you execute the ifconfig command the new virtual interface will be immediately ready to use.
# ifconfig eth0:0
eth0:0    Link encap:Ethernet  HWaddr 3c:97:0e:02:98:c8  
          UP BROADCAST MULTICAST  MTU:1500  Metric:1
          Interrupt:20 Memory:f1600000-f1620000 

# ifconfig eth0:0 123.123.22.22
# ifconfig eth0:0
eth0:0    Link encap:Ethernet  HWaddr 3c:97:0e:02:98:c8  
          inet addr:123.123.22.22  Bcast:123.255.255.255  Mask:255.0.0.0
# ping 123.123.22.22
PING 123.123.22.22 (123.123.22.22) 56(84) bytes of data.
64 bytes from 123.123.22.22: icmp_req=1 ttl=64 time=0.060 ms
64 bytes from 123.123.22.22: icmp_req=2 ttl=64 time=0.057 ms

2.1.  Disable virtual network interface

To disable our temporary network interface created earlier we can use the ifconfig command again but with a down flag.
# ifconfig eth0:0 down

3. Virtual address permanent assignment

The above configuration would not last after reboot. If you wish the virtual network interface to be permanent you would need to modify config files relevant to your particular Linux distribution. Below, you can find information describing this procedure for the major Linux distributions:

3.1. Debian / Ubuntu

3.1.1. Static

For Debian or Ubuntu Linux you need to edit /etc/network/interfaces file with your favorite text editor and add the following lines:
iface eth0:0 inet static
address 123.123.22.22
netmask 255.0.0.0
broadcast 123.255.255.255

3.1.2. Dhcp

It is also possible to use virtual network interface with DHCP. In that case, you will need to add to your /etc/network/interfaces the following line.
iface eth0:0 inet dhcp
To apply your change restart the network with:
 # /etc/init.d/networking restart

3.2. Redhat / Fedora / CentOS

3.2.1. Static

For Redhat, Fedora or CentOS Linux the directory responsible for permanent IP address assignment is /etc/sysconfig/network-scripts. In this directory you need to create a file corresponding to your new virtual interface. In our case, that will be a file called: ifcfg-eth0:0 . Create this new file and insert following lines to make your virtual address assignment permanent after reboot:
DEVICE=eth0:0
IPADDR=123.123.22.22
NETMASK=255.0.0.0
NETWORK=123.0.0.0
BROADCAST=123.255.255.255
ONBOOT=yes

3.2.2. Dhcp

DEVICE=eth0:0
BOOTPROTO=dhcp
ONBOOT=yes
Once ready restart your interfaces with:
# service network restart

4. Conclusion

In the old days one physical server served a single website. This is no longer a viable option for hosting websites today so ability of an operating system to create virtual network interface is really a must.


In centos:

> vi ifcfg-eth2:0

TYPE=Ethernet
BOOTPROTO=none
IPADDR=192.168.137.163
PREFIX=24
GATEWAY=192.168.137.1
DNS1=8.8.8.8
DEFROUTE=yes
IPV4_FAILURE_FATAL=yes
IPV6INIT=no
DEVICE=eth2:0
HWADDR=00:0C:29:BC:DF:41
ONBOOT=yes

> ifup eth2:0


Thursday, December 10, 2015

Edition-Based Redefinition

http://blog.itpub.net/785478/viewspace-1028200/


在11gR1之前,当业务系统需要升级的时候,我们常常需要停下,从而保证版本的一直性和访问的连续性,对于一些APP变更频繁但又不允许经常停应用的IT系统,
    这实在是一个很头大的问题,那么现在好了,从11gR2开始,Oracle引入了一个edition(版本)的概念,借助这个特性,我们可以实现app的online upgrade.同时该
    特性允许pre-upgrade application and the post-upgrade application并存,等我们确认post-upgrade app没问题的时候再把pre-upgrade app切换下来,在
    这期间整个APP的访问是不受影响的.从而可以最大程度的减少application down time.


     实际上这个特性,在11gR1中已经提供了,只不过需要通过一个隐藏参数来控制
     _edition_based_redefinition 11gR1中该参数默认是false.
     11gR2中,该参数已经被废弃!
   

    关于的edition-based redefinition的一些特性

    1)一个database至少有一个缺省的edition
       SQL> SELECT * FROM DATABASE_PROPERTIES WHERE PROPERTY_NAME = 'DEFAULT_EDITION';
       PROPERTY_NAME     PROPERTY_VALUE   DESCRIPTION
       ----------------- ---------------- ------------------------------------
       DEFAULT_EDITION   ORA$BASE          Name of the database default edition
       SQL> 

    2)设置数据库的缺省edition
       alter database DEFAULT EDITION = edition_name;

    3)与edition相关的系统权限有
       SQL> select name from system_privilege_map where name like '%EDITION%';
       NAME
       ------------------
       ALTER ANY EDITION
       DROP ANY EDITION
       CREATE ANY EDITION
       SQL>

       这些系统权限都默认授予了DBA角色,也就是说任何具有DBA role的用户都可以执行与Edition-Based Redefinition相关的任务。
       这些任务包括
       ▪Grant or revoke privileges to create, alter, and drop editions
       ▪Enable editions for a schema 
       ▪Set the database default edition

    4)与edition相关的数据字典
       *_editions    -->列出了当前数据库中的所有的EDITIONS,缺省情况下ORA$BASE
       SQL> select * from dba_editions;
       EDITION_NAME   PARENT_EDITION_NAME  USABLE
       -------------- -------------------- ------
       ORA$BASE                            YES
       *_objects      -->描述当前版本下对应的objects的可见性(是实际对象还是继承对象)
       SQL> select owner,object_name,edition_name from dba_objects;
       *_objects_ae    -->描述当前数据库中每个真实的objects(所有版本)

       SQL> select owner,object_name,edition_name from dba_objects_ae; 

    5)通过不同的edition连接数据库
       从11gR2开始,SQL*Plus这样的工具也有对edition的连接支持        
                   [AS {SYSDBA | SYSOPER | SYSASM}] [EDITION=value]

       默认情况下EDTION=DEFAULT_EDITION,也就是对应当前数据库的缺省版本

    6)11gR2中editions支持的objects type有
       ■ Synonym
       ■ View
       ■ Function
       ■ Procedure
       ■ Package (specification and body)
       ■ Type (specification and body)
       ■ Library
       ■ Trigger

    7)用户要想使用edition,必须先Enable editions for a schema。
       注意:这是一个不可逆的动作,一旦enable了就不能disable了
       比如
       conn /as sysdba
       alter user study enable editions;

       通过如下语句可以查看某个schema是否enable editions.
       select username,editions_enabled from dba_users;       
       注:这里有一个隐藏参数来控制是否对所有的用户enable editions,默认是false,需要按照上面的方法去enable.
           _enable_editions_for_users FALSE enable editions for all users




创建Editions

    要创建一个edition,用户必须具有create any editions的权限.
    Create edtion的语法如下
        CREATE EDITION edition [AS CHILD OF parent_edition];

    说明:1)This statement creates a new edition as a child of an existing edition. An edition makes it possible to have two or more 
    versions of the same editionable objects in the database. When you create an edition, it immediately inherits all of the editionable 
    objects of its parent edition。
         2)用户定义的第一个edtion对应的parent edition 一定是ORA$BASE.
         3)在11gR2中,一个edition 只能有一个child edition.否则你可能会遇到类似如下的错误
           ORA-38807: Implementation restriction: an edition can have only one child
           这个时候你就到dba_editions视图中去查看了。

    示例
    --查看当前数据库的缺省DEFAULT_EDITION
    SQL> SELECT * FROM DATABASE_PROPERTIES WHERE PROPERTY_NAME = 'DEFAULT_EDITION';
    PROPERTY_NAME      PROPERTY_VALUE    DESCRIPTION
    ------------------ ----------------- ------------------------------------
    DEFAULT_EDITION    ORA$BASE          Name of the database default edition
    SQL> 
    --创建一个edition
    SQL> create edition app_version_01;
    Edition created.
    SQL> 

    SQL> select * from dba_editions;
    EDITION_NAME      PARENT_EDITION_NAME   USABLE
    ----------------- --------------------- ------
    ORA$BASE                                YES
    APP_VERSION_01    ORA$BASE              YES
    SQL> 

    默认情况下,如果不指定as child of parent_edition字句的话,对应的partent_edition 一定是DEFAULT_EDITION.

    这个时候如果再默认创建一个app_version_02,那么就会遇到错误

    SQL> create edition app_version_02;
    create edition app_version_02
    *
    ERROR at line 1:
    ORA-38807: Implementation restriction: an edition can have only one child

    SQL> 

    出现这个错误的原因就是一个edition只能有一个child edition.而当前系统中默认的DEFAULT_EDITION已经有一个child edition:app_version_01了,改成如下就可以了,当然了
    这样创建的意义不是很大,我只是为了说明问题而已。

    SQL> create edition app_version_02 as child of app_version_01;
    Edition created.
    SQL> 

Tuesday, December 8, 2015

GG velocity methods

$!col.getIndex()  92
$!col.getAfter()  14.125999998999999
$!col.getDataType()  DOUBLE
$!col.getData()   "14.125999998999999"


$col.getAfter():  14.125999998999999
Type:    com.goldengate.atg.datasource.DsColumnAfterValue
$col.toString():  "14.125999998999999"
Type:    java.lang.String
c$ol.AfterValue:  14.125999998999999
Type       :  java.lang.String


Code:
col.getAfter():         $col.getAfter()
Type:                   $col.getAfter().getClass().getName()
col.toString():         $col.toString()
Type:                   $col.toString().getClass().getName()
col.AfterValue:         $col.AfterValue
Type          :         $col.AfterValue.getClass().getName()


SSH chmod

after adding authorized_keys,


chmod 700 .ssh
chmod 600 authorized_keys

Friday, November 27, 2015

sql command files to disable and enable all constraints

Here sql command files to disable and enable all constraints: 

Disable: 

set feedback off 
set verify off 
set echo off 
prompt Finding constraints to disable... 
set termout off 
set pages 80 
set heading off 
set linesize 120 
spool tmp_disable.sql 
select 'spool igen_disable.log;' from dual; 
select 'ALTER TABLE '||substr(c.table_name,1,35)|| 
' DISABLE CONSTRAINT '||constraint_name||' ;' 
from user_constraints c, user_tables u 
where c.table_name = u.table_name; 
select 'exit;' from dual; 
set termout on 
prompt Disabling constraints now... 
set termout off 
@tmp_disable.sql; 
exit 
/ 


Enable: 

set feedback off 
set verify off 
set wrap off 
set echo off 
prompt Finding constraints to enable... 
set termout off 
set lines 120 
set heading off 
spool tmp_enable.sql 
select 'spool igen_enable.log;' from dual; 
select 'ALTER TABLE '||substr(c.table_name,1,35)|| 
' ENABLE CONSTRAINT '||constraint_name||' ;' 
from user_constraints c, user_tables u 
where c.table_name = u.table_name; 
/ 
select 'exit;' from dual; 
set termout on 
prompt Enabling constraints now... 
set termout off 
@tmp_enable; 
!rm -i tmp_enable.sql; 
exit 
/