Tuesday, November 3, 2015

Oracle Resource Management

Oracle System parameter resource_limit to true with following command

alter system set resource_limit=TRUE;



















PLAN

Resource PlanPLN_DAYPLN_NIGHT
Consumer GroupsGRP_CONNECTGRP_QUERYGRP_REPORTOthersGRP_CONNECTGRP_QUERYGRP_REPORTOthers

Following is the plan directives for PLN_DAY

GRP_CONNECT CPU_P1 => 5
SWITCH_GROUP => 'GRP_QUERY',
SWITCH_TIME => 5
switch_for_call => FALSE

Around 5% of CPU will be used for all the SQL that will complete within 5 second. If SQL goes beyond 5 seconds, it will be switch to group GRP_REPORT. 

GRP_QUERY CPU_P1 => 75 
SWITCH_GROUP => 'GRP_REPORT',
SWITCH_TIME => 60
switch_for_call => TRUE

Around 75% of CPU will be used for all the SQL that will complete execution between 6 second to 60 second. If SQL goes beyond 60 seconds, it will be switch to group GRP_REPORT.

GRP_REPORT CPU_P1 => 10

Around 10% of CPU will be used for all the SQL that will complete execution beyond 60 second. 

OTHER_GROUPS CPU_P1 => 10

For rest of all the users only 10% of CPU will be used.

Following is the plan directives for PLN_NIGHT

GRP_CONNECT CPU_P1 => 5
SWITCH_GROUP => 'GRP_QUERY',
SWITCH_TIME => 5
switch_for_call => FALSE

Around 5% of CPU will be used for all the SQL that will complete within 5 second. If SQL goes beyond 5 seconds, it will be switch to group GRP_REPORT. Switch_for_call determines the return back of to original consumer group after the SQL is completed.

GRP_QUERY CPU_P1 => 10
SWITCH_GROUP => 'GRP_REPORT',
SWITCH_TIME => 60
switch_for_call => TRUE

Around 60% of CPU will be used for all the SQL that will complete execution between 6 second to 60 second. If SQL goes beyond 60 seconds, it will be switch to group GRP_REPORT.

GRP_REPORT CPU_P1 => 75
Around 75% of CPU will be used for all the SQL that will complete execution beyond 60 second.

OTHER_GROUPS CPU_P1 => 10 For rest of all the users only 10% of CPU will be used.

Create pending area

-- Creating pending area 
begin
dbms_resource_manager.create_pending_area(); 
end;
/
-- Clearing pending area 
exec dbms_resource_manager.clear_pending_area(); 
-- Validating pending area 
exec dbms_resource_manager.validate_pending_area();












Create Plan

begin 
dbms_resource_manager.create_plan( 
PLAN => 'PLN_DAY', 
COMMENT => 'This plan will be for 8AM to 8PM' 
); 

dbms_resource_manager.create_plan( 
PLAN => 'PLN_NIGHT', 
COMMENT => 'This plan will be for 8PM to 8AM' 
); 
end; 
/
















Create consumer groups


begin 
dbms_resource_manager.create_consumer_group( 
CONSUMER_GROUP => 'GRP_CONNECT', 
COMMENT => 'This group is initial group and lasts for 5 sec.' 
); 
dbms_resource_manager.create_consumer_group( 
CONSUMER_GROUP => 'GRP_QUERY', 
COMMENT => 'The user switch to GRP_QUERY if the query stays more than 5 sec and lasts for 60 sec' 
); 
dbms_resource_manager.create_consumer_group( 
CONSUMER_GROUP => 'GRP_REPORT', 
COMMENT => 'This group is last group and user having query executed for more than 65 sec will be stays here' 
); 
end; 
/ 


Create plan directives

begin 
dbms_resource_manager.create_plan_directive( 
PLAN => 'PLN_DAY', 
GROUP_OR_SUBPLAN => 'GRP_CONNECT', 
COMMENT => 'This is plan directive for user in GRP_CONNECT group', 
CPU_P1 => 5, 
SWITCH_GROUP => 'GRP_QUERY', 
SWITCH_TIME => 5, 
switch_for_call => FALSE 
); 
dbms_resource_manager.create_plan_directive( 
PLAN => 'PLN_DAY', 
GROUP_OR_SUBPLAN => 'GRP_QUERY', 
COMMENT => 'This is plan directive for user in GRP_QUERY group', 
CPU_P1 => 75, 
SWITCH_GROUP => 'GRP_REPORT', 
SWITCH_TIME => 60, 
switch_for_call => TRUE 
); 
dbms_resource_manager.create_plan_directive( 
PLAN => 'PLN_DAY', 
GROUP_OR_SUBPLAN => 'GRP_REPORT', 
COMMENT => 'This is plan directive for user in GRP_REPORT group', 
CPU_P1 => 10 
); 
dbms_resource_manager.create_plan_directive( 
PLAN => 'PLN_DAY', 
GROUP_OR_SUBPLAN => 'OTHER_GROUPS', 
COMMENT => 'This is plan directive for user in GRP_REPORT group', 
CPU_P1 => 10 
); 
dbms_resource_manager.create_plan_directive( 
PLAN => 'PLN_NIGHT', 
GROUP_OR_SUBPLAN => 'GRP_CONNECT', 
COMMENT => 'This is plan directive for user in GRP_CONNECT group', 
CPU_P1 => 5, 
SWITCH_GROUP => 'GRP_QUERY', 
SWITCH_TIME => 5, 
switch_for_call => FALSE 
); 
dbms_resource_manager.create_plan_directive( 
PLAN => 'PLN_NIGHT', 
GROUP_OR_SUBPLAN => 'GRP_QUERY', 
COMMENT => 'This is plan directive for user in GRP_QUERY group', 
CPU_P1 => 10, 
SWITCH_GROUP => 'GRP_REPORT', 
SWITCH_TIME => 60, 
switch_for_call => TRUE 
); 
dbms_resource_manager.create_plan_directive( 
PLAN => 'PLN_NIGHT', 
GROUP_OR_SUBPLAN => 'GRP_REPORT', 
COMMENT => 'This is plan directive for user in GRP_REPORT group', 
CPU_P1 => 75 
); 
dbms_resource_manager.create_plan_directive( PLAN => 'PLN_NIGHT', 
GROUP_OR_SUBPLAN => 'OTHER_GROUPS', 
COMMENT => 'This is plan directive for user in GRP_REPORT group', 
CPU_P1 => 10 
); 
end; 
/





































Validate and submit the pending area

begin 
DBMS_RESOURCE_MANAGER.VALIDATE_PENDING_AREA(); 
DBMS_RESOURCE_MANAGER.SUBMIT_PENDING_AREA(); 
end; 
/










Grant user to switch to consumer_group

begin
dbms_resource_manager_privs.grant_switch_consumer_group(
grantee_name => 'dilli',
consumer_group => 'GRP_CONNECT',
grant_option => FALSE);
end;
/














Mapping user to group created

begin
dbms_resource_manager.create_pending_area(); 
end;
/

begin
dbms_resource_manager.set_consumer_group_mapping(
attribute => DBMS_RESOURCE_MANAGER.oracle_user,
value => 'dilli',
consumer_group => 'GRP_CONNECT' 
);
end;
/

begin 
DBMS_RESOURCE_MANAGER.VALIDATE_PENDING_AREA(); 
DBMS_RESOURCE_MANAGER.SUBMIT_PENDING_AREA(); 
end; 
/

























Activating the resource plan

alter system set resource_manager_plan='FORCE:PLN_DAY';







Setting session to group

begin
dbms_resource_manager.switch_consumer_group_for_sess(
session_id => 139,
session_serial => 256,
consumer_group => 'GRP_REPORT'
);
end;
/





Saturday, October 31, 2015

Remove journal from ext4 for DISK IO enhancement

Journaling Filesystem

A journaling filesystem is a filesystem that maintains a special file called a journal that is used to repair any inconsistencies that occur as the result of an improper shutdown of a computer. Such shutdowns are usually due to an interruption of the power supply or to a software problem that cannot be resolved without a rebooting.


To enhance Disk IO performance we may need to disable file system journaling. Ext4 with disabled journal is faster in comparison to ext4 with journal option and is appropriate for less critical system as MySQL replication slave database. In my case I have multiple MySQL replication slaves and I have used ext4 partition with journal disabled on data partition where MySQL datadir resides. Follow step below to disable journaling from ext4 filesystem.


Unmount the partition on which we wish to disable the journaling.

Here we are about to disable journal from the partition /data with device file /dev/sdb1 where MySQL database resides.

umount /data













Verify journaling option exists on the partition. While execution command below we can notice has_journal on Filesystem features: at 6th row.

dumpe2fs /dev/sdb1 |more


Disable journaling with the command below.

tune2fs -O ^has_journal /dev/sdb1





Check the file system status with the command. The command below will scan the file system for any errors.


fsck.ext4 -f /dev/sdb1









Check for file system journaling option. We can noticed has_journal has been disappeared from Filesystem features. 

dumpe2fs /dev/sdb1 |more


For more performance improvement add following options on /etc/fstab.

/dev/sdb1 /data ext4 defaults,noatime,nodiratime 0 0 


Now mount the partition with following option.

mount /data














Finally start mysql services.

/etc/init.d/mysql start





Wednesday, October 21, 2015

Parallel compression on Linux using pbzip2

Install pbzip2 in centos

Search and install pbzip2 package 

# yum search pbzip2
# yum -y install pbzip2


































Now select the file to compress.




Start compression with normal tar command. 

# time tar cjvf host01.vdi.tar.bz2 host01.vdi





We can notice only Single CPU is used and load never goes beyond 1.06














Finally it is completed with following statistics.









Now compress using same file with pbzip2 with DOP 4 command

time pbzip2 -p4  host01.vdi





It is noticeable that multiple CPU is used and CPU load reached around 4.0














Compression completed with following statistics








Compression is completed around 4 times faster than normal compression and compressed size is same.






Decompress compressed file with command below

time pbzip2 -d -p4 host01.vdi.bz2






Tuesday, October 13, 2015

MySQL multi master replication

Environment:

Masters:

192.168.1.201 master1.localdomain master1
192.168.1.202 master2.localdomain master2

Slave:

192.168.1.203 slave.localdomain slave

Create MySQL user in master servers that will be used as mysql replication slave. Execute following commands on master server.

create user 'replicator'@'%' identified by 'password'; 
grant replication slave on *.* to 'replicator'@'%';











Setup mysql master on hosts master1 and master2 two node circular replication. Add following parameters on my.cnf of master1 and master2


Master1:
server-id = 1
log-bin = master1-bin

log-slave-updates
log-bin-index = /var/lib/mysql/log-bin.index
log-error = /var/lib/mysql/error.log

relay-log = /var/lib/mysql/relay.log
relay-log-info-file = /var/lib/mysql/relay-log.info
relay-log-index = /var/lib/mysql/relay-log.index

auto_increment_increment = 2
auto_increment_offset = 1



















Master2:
server-id = 2
log-bin = master2-bin

log-slave-updates
log-bin-index = /var/lib/mysql/log-bin.index
log-error = /var/lib/mysql/error.log

relay-log = /var/lib/mysql/relay.log
relay-log-info-file = /var/lib/mysql/relay-log.info
relay-log-index = /var/lib/mysql/relay-log.index


auto_increment_increment = 2
auto_increment_offset = 2




















Restart mysql services on both master servers. Login to masters as user root and execute following command.

Master1:
CHANGE MASTER TO MASTER_HOST='192.168.1.202',MASTER_USER='replicator', MASTER_PASSWORD='password', MASTER_LOG_FILE='master2-bin.000001', MASTER_LOG_POS=120;
start slave;
show slave status\G





Master2:
CHANGE MASTER TO MASTER_HOST='192.168.1.201',MASTER_USER='replicator', MASTER_PASSWORD='password', MASTER_LOG_FILE='master1-bin.000001', MASTER_LOG_POS=120;
start slave;
show slave status\G






Verify two node circular replication.

From master1 create database db1 and verify database exists on master2.
Create database db2 on master2 and verify database exists on master1.

Master1:
CREATE TABLE db1.animals (
     id MEDIUMINT NOT NULL AUTO_INCREMENT,
     name CHAR(30) NOT NULL,
     PRIMARY KEY (id)
) ;
INSERT INTO db1.animals (name) VALUES
    ('dog1'),('cat1'),('penguin1'),
    ('lax1'),('whale1'),('ostrich1');

















Master2:
INSERT INTO db1.animals (name) VALUES
    ('dog'),('cat'),('penguin'),
    ('lax'),('whale'),('ostrich');











Execute following command on both master1 and master2 and verify the output.

select * from db1.animals;























Configure Slave.

Add following configuration parameters on my.cnf of slave.

server-id=3

log-bin-index = /var/lib/mysql/log-bin.index
log-error = /var/lib/mysql/error.log

relay-log = /var/lib/mysql/relay.log
relay-log-info-file = /var/lib/mysql/relay-log.info
relay-log-index = /var/lib/mysql/relay-log.index















Execute following command to configure master and you may point to any one of the master.


CHANGE MASTER TO MASTER_HOST='192.168.1.202',MASTER_USER='replicator', MASTER_PASSWORD='password', MASTER_LOG_FILE='master2-bin.000001', MASTER_LOG_POS=120;
start slave;





Execute following command on slave and verify the output.

select * from db1.animals;


Friday, October 2, 2015

Log all queries in MySQL

Create logging tables on mysql database that will hold the log.


Create your log tables on the mysql database

  CREATE TABLE `slow_log` (
   `start_time` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP
                          ON UPDATE CURRENT_TIMESTAMP,
   `user_host` mediumtext NOT NULL,
   `query_time` time NOT NULL,
   `lock_time` time NOT NULL,
   `rows_sent` int(11) NOT NULL,
   `rows_examined` int(11) NOT NULL,
   `db` varchar(512) NOT NULL,
   `last_insert_id` int(11) NOT NULL,
   `insert_id` int(11) NOT NULL,
   `server_id` int(10) unsigned NOT NULL,
   `sql_text` mediumtext NOT NULL,
   `thread_id` bigint(21) unsigned NOT NULL
  ) ENGINE=CSV DEFAULT CHARSET=utf8 COMMENT='Slow log';

  CREATE TABLE `general_log` (
   `event_time` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP
                          ON UPDATE CURRENT_TIMESTAMP,
   `user_host` mediumtext NOT NULL,
   `thread_id` bigint(21) unsigned NOT NULL,
   `server_id` int(10) unsigned NOT NULL,
   `command_type` varchar(64) NOT NULL,
   `argument` mediumtext NOT NULL
  ) ENGINE=CSV DEFAULT CHARSET=utf8 COMMENT='General log';


Enable Query logging on the database with parameter below

SET global general_log = 1;
SET global log_output = 'table';

View the log

select * from mysql.general_log;

Disable Query logging on the database

SET global general_log = 0;

Sample Report SQL

select argument,count(*) from general_log 
where lower(argument) like 'select%'
and user_host like '%192.168.1.1%'
group by argument
order by 2 desc;

Tuesday, September 29, 2015

Securing Linux with denyhosts

Introduction

DenyHosts is a script intended to be run by Linux system administrators to help thwart SSH server attacks (also known as dictionary based attacks and brute force attacks). 

Installation and Configuration

Download and install denyhosts

Go to http://pkgs.repoforge.org/denyhosts/ and download http://pkgs.repoforge.org/denyhosts/denyhosts-2.6-5.el6.rf.noarch.rpm
wget http://pkgs.repoforge.org/denyhosts/denyhosts-2.6-5.el6.rf.noarch.rpm






Install package with following command.

rpm -ivh denyhosts-2.6-5.el6.rf.noarch.rpm







Configure known hosts or the network that do not rely on deny hosts. This means all the host on 192.168.1.0/24 boycott the hostdenys. If any host on 192.168.1.0/24 network types wrong password that host won't get blocked.


echo "SSHD: 192.168.1." >> /etc/hosts.allow


Restart the services with command below

/etc/init.d/denyhosts restart

Verify the denyhosts is running

ps ax | grep denyhost


Settings can be changed on /etc/denyhosts/denyhosts.cfg

PURGE_DENY = defines the deny_hosts entries to be purged after certain time.

# PURGE_DENY: removed HOSTS_DENY entries that are older than this time
#             when DenyHosts is invoked with the --purge flag
#
#      format is: i[dhwmy]
#      Where 'i' is an integer (eg. 7)
#            'm' = minutes
#            'h' = hours
#            'd' = days
#            'w' = weeks
#            'y' = years
#
# never purge:

DENY_THRESHOLD_INVALID = 5
# DENY_THRESHOLD_INVALID: block each host after the number of failed login
# attempts has exceeded this value.  This value applies to invalid
# user login attempts (eg. non-existent user accounts)

ADMIN_EMAIL=
SMTP_HOST = localhost
SMTP_PORT = 25
SMTP_FROM = DenyHosts <nobody@localhost>
SMTP_SUBJECT = DenyHosts Report


Verify current hosts.deny list

cat /etc/hosts.deny

















Try ssh login with wrong password for 5 times
Now host entry can be seen on hosts.deny list



















SSH to that server is blocked now.