Redirect to www in php

Here is a simple way to redirect any hit to www using php .
and also keep the protocol ( http/https )

if($_SERVER[HTTPS])
{
	header('Location:' . "http://www.$_SERVER[HTTP_HOST]$_SERVER[REQUEST_URI]" ,true, 301);
}
else
{
	header('Location:' . "https://www.$_SERVER[HTTP_HOST]$_SERVER[REQUEST_URI]" ,true, 301);
}

install cx_Oracle on CentOS 7

Here is a simple way to install cx_Oracle on CentOS 7
in order to install , we will use Oracle instant client , and python pip

1. Download and install oracle client from Oracle

yum install libaio
rpm -Uvh oracle-instantclient12.1-basic-12.1.0.2.0-1.x86_64.rpm
rpm -Uvh oracle-instantclient12.1-devel-12.1.0.2.0-1.x86_64.rpm
rpm -Uvh oracle-instantclient12.1-sqlplus-12.1.0.2.0-1.x86_64.rpm

2. Add Oracle client to library path

echo "/usr/lib/oracle/12.1/client64/lib" >/etc/ld.so.conf.d/oracle.conf
ldconfig

3. install epel repo via yum extra

yum install epel-release

4. Install additional

yum install gcc python-pip.noarch python-devel

5. Install  cx_Oracle via pip

pip install cx_Oracle

 

symetric gpg encryption

Symetric encryption is an encryption that uses same key to decrypt.
here is a way to encrypt file with symetric key without user interact ( batch )

gpg --batch -q --passphrase '<password>' --cipher-algo AES256 -c <file>

Create TAP/TUN device CentOS 7

Here is how to do it on CentOS 7 / RedHat 7 where the command  tunctl is missing
and when you try to run ifconfig you get an error

~# ifconfig tap0 10.0.0.1/30
SIOCSIFADDR: No such device
tap0: ERROR while getting interface flags: No such device
SIOCSIFNETMASK: No such device

to solve this you may run this commands
1. first we must create the device using ip command

# ip tuntap add tap0 mode tap

2. run ifconfig

~# ifconfig tap0 10.0.0.1/30
~# ifconfig tap0
tap0: flags=4099<UP,BROADCAST,MULTICAST> mtu 1500
inet 10.0.0.1 netmask 255.255.255.252 broadcast 10.0.0.3
ether 32:f5:3a:44:a5:64 txqueuelen 500 (Ethernet)
RX packets 0 bytes 0 (0.0 B)
RX errors 0 dropped 0 overruns 0 frame 0
TX packets 0 bytes 0 (0.0 B)
TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0

Bash Special Variables

Bash shell has some built in variables that can be use in scripts
for example if you want to know the process id of the current command , you may use $$
here is a list of some special variables

$# Number of command-line arguments.
$_ The underscore variable is set at shell startup and contains the absolute file name of the shell or script being executed as passed in the argument list. Subsequently, it expands to the last argument to the previous command, after expansion. It is also set to the full pathname of each command executed and placed in the environment exported to that command. When checking mail, this parameter holds the name of the mail file.
$- A hyphen expands to the current option flags as specified upon invocation, by the set built-in command, or those set by the shell itself (such as the -i).
$? Exit value of last executed command.
$ Process number of the shell.
$! Process number of last background command.
$0 First word; that is, the command name. This will have the full pathname if it was found via a PATH search.
$n Individual arguments on command line (positional parameters). The Bourne shell allows only nine parameters to be referenced directly (n = 1–9); Bash allows n to be greater than 9 if specified as ${n}.
$*, [email protected] All arguments on command line ($1 $2 …).
“$*” All arguments on command line as one string (“$1 $2…”). The values are separated by the first character in $IFS.
[email protected] All arguments on command line, individually quoted (“$1” “$2” …).

simple install sun java

a quick and eazy way to install java under linux
1. download jre or jdk from www.java.com
2. create a directory to hold the versions

# mkdir /usr/java

3. extract the tar file under the directory

# tar -xzvf jre-7u45-linux-x64.tar.gz -C /usr/java
# ln -s /usr/java/jre1.7.0_45 /usr/java/jre-latest

4. the last part is to create links in the system in order to
make sure it uses this new java version we just extracted .
* global java command
debian linux based version:

# update-alternatives --install /usr/bin/java java /usr/java/jre-latest/bin/java 90
# update-alternatives --config java

RedHat linux based version :

# alternatives --install /usr/bin/java java /usr/java/jre-latest/bin/java 90
alternatives --config java

browsers links

ln -s /usr/java/jre-latest/lib/amd64/libnpjp2.so <path to install dir>/plugins/

bash history security

Bash history can reviel what a person commands where .
but what if the person wants to hide his tracks , he may use the

unset HISTFILE

will couse bash to delete the history upon logout .
there is how ever a harden attribute you may set to overcome this problem
when a user login , there are set of files/scripts the profile runs .
one place you may put such script would be under /etc/profile.d/ , with file extension sh
as it is being called by /etc/profile .
just place this small line in there :

readonly HISTFILE

now when a person wants to unset this parameter , bash will scream with this error

-su: unset: HISTFILE: cannot unset: readonly variable

Move ASM disk across storage online

This is a way you can use to copy/move your ASM disks to new location
without the need to stop your instance. it include command to be run
by root and by asm admin
1. creating the new disks(LUN) as ASM devices (run as root)

[[email protected] ~]# /etc/init.d/oracleasm listdisks
DATA1
OCR1
[[email protected] ~]# /etc/init.d/oracleasm createdisk DATA2 /dev/sdb8
Marking disk "DATA2" as an ASM disk:
[ OK ]
[[email protected] ~]#
[[email protected] ~]# /etc/init.d/oracleasm listdisks
DATA1
DATA2
OCR1

2. List disk in disk-group (run as asmadmin)

SQL> select GROUP_NUMBER,NAME,STATE,TOTAL_MB from v$asm_diskgroup ;
GROUP_NUMBER NAME
STATE TOTAL_MB
------------ ------------------------------ ----------- ----------
1 OCR
MOUNTED
964
2 DATA
MOUNTED
4777
SQL> select NAME,GROUP_NUMBER,REDUNDANCY from v$asm_disk ;
NAME
GROUP_NUMBER REDUNDA
------------------------------ ------------ -------
0 UNKNOWN
DATA1
2 UNKNOWN
OCR1
1 UNKNOWN

3. Adding disk to DATA disk-group (run as asdmadmin)

SQL> ALTER DISKGROUP DATA REBALANCE POWER 0 ;
Diskgroup altered.
SQL> ALTER DISKGROUP DATA ADD DISK 'ORCL:DATA2' NAME DATA2 ;
Diskgroup altered.
SQL> ALTER DISKGROUP DATA DROP DISK DATA1 ;
Diskgroup altered.
SQL> ALTER DISKGROUP data REBALANCE POWER 1;
Diskgroup altered.
SQL> select NAME,GROUP_NUMBER,REDUNDANCY from v$asm_disk ;
NAME
GROUP_NUMBER REDUNDA
------------------------------ ------------ -------
0 UNKNOWN
DATA1
1 UNKNOWN
DATA2
1 UNKNOWN
OCR1

Note that DATA1 was marked as DROP , it will stop showing once all data sync to DATA2
the process can be monitored by this query (run as asmadmin)

SELECT g.name "Diskgroup",
100*(max((d.total_mb-d.free_mb)/d.total_mb)-min((d.total_mb-d.free_mb)/d.total_mb))/max((d.total_mb-d.free_mb)/d.total_mb) "Imbalance",
100*(max(d.total_mb)-min(d.total_mb))/max(d.total_mb) "Variance",
100*(min(d.free_mb/d.total_mb)) "MinFree",
count(*) "DiskCnt",
g.type "Type"
FROM v$asm_disk d, v$asm_diskgroup g
WHERE d.group_number = g.group_number and
--d.group_number = 1 and
d.state = 'NORMAL' and
d.mount_status = 'CACHED'
12 GROUP BY g.name, g.type;
Diskgroup
Imbalance Variance MinFree
------------------------------ ---------- ---------- ---------- ----------
Type
------
OCR
EXTERN
DATA
EXTERN
•
DiskCnt
0 63.3817427 1
0 59.5143395 1

Distributed RAID over ISCSI

In an early post building-raid-over-network-share I had
show how to build distributed RAID over network using CIFS (samba or windows share)
now i wanted to show you a similar method by using ISCSI .
ISCSI is a protocol supported by all OS versions Windows,Linux,BSD,Solaris,AIX,OSX and other OS out there .
In this how to , i am going to use CentOS 6 . we will have 3 node servers as iscsi target and one server as
initiator that will be managing the RAID and will also act as target for the created RAID device .
The explain is very brief and will not cover all ISCSI settings , for better performance
I do suggest reading documentation for each individual distribution OS . another suggestion it is highly recommend
using a spare network interface with dedicated VLAN for ISCSI as it may load the network with high traffic .
node settings ( do this step at all nodes )
1. install scsi-target-utils

# yum -y install scsi-target-utils

2. create a device file

# dd if=/dev/zero of=/mydrive.file bs=1M count=1024

3. edit /etc/tgt/targets.conf , and add a new target lun .
make sure each server uses a uniq iqn id

<target iqn.2013-05:node1.0>
backing-store /mydrive.file
</target>

4. start the tgtd service

# service tgtd start

5. on the initiator server ( our mdadm raid master ) search for the iscsi devices

# iscsiadm -m discovery -t sendtargets -p 192.168.122.11
192.168.122.11:3260,1 iqn.2013-05:node1.0
# iscsiadm -m discovery -t sendtargets -p 192.168.122.12
192.168.122.12:3260,1 iqn.2013-05:node2.0
# iscsiadm -m discovery -t sendtargets -p 192.168.122.13
192.168.122.13:3260,1 iqn.2013-05:node3.0

6. start iscasi service and get toy know the new devices

# service iscsi start

* the command “fdisk -l” may now show new scsi devices , in my case its sda,sdb,sdc
—- time to build the soft RAID —–
1. install mdadm

# yum search mdadm

2. create a new RAID device

# mdadm --create --verbose /dev/md0 --level=5 --raid-devices=3 /dev/sda /dev/sdb /dev/sdc 

from now on you may have a new RAID device called /dev/md0 you can use ,
* update
its a good idea to add a spare device to the raid , and even better to use the initiator itself ( the host that run the mdadm )
so I create a new device using dd command , then create a loop device then add it to the raid

dd if=/dev/zero of=/myfile.img bs=1M count=100
losetup -v /dev/loop1 /myfile.img
mdadm --add /dev/md0 /dev/loop1

So now the RAID may look like this :

 Number Major Minor RaidDevice State
0 8 0 0 active sync /dev/sda
1 8 16 1 active sync /dev/sdb
3 8 32 2 active sync /dev/sdc
4 7 1 - spare /dev/loop1

Manage partitions on a raw device file

Every once in a while there comes a time when you need to use a file as raw device
for example when using qemu qcow file or if you want to have your home directory portable and encrypted
in this example i am using one file named my-hd-image.file and 2 Linux partitions inside it . lets start
Creating the raw device file is simply by using dd ( can also be use via qemu-img )

dd if=/dev/zero of=my-hd-image.file bs=1M count=1024

Create partitions inside the new disk using fdisk , in my file
I had created 2 partitions of 512M

Device Boot Start End Blocks Id System
my-hd-image.file1 2048 1050623 524288 83 Linux
my-hd-image.file2 1050624 2097151 523264 83 Linux

creating the file system using loop devices to be linked to specific offset inside the file
so that each partition will have its own loop device . the offset calculation is in bits
and i am using bc to calculate it fast for me . the offset of each partition is its start block times 512
witch is the block size ( unless you have set a different one upon creation )
Partition my-hd-image.file1 starts at offset 2048

echo "2048 * 512" | bc
1048576

Partition my-hd-image.file2 starts at offset 1050624

echo "1050624 * 512" | bc
537919488

use the calculation offset to map to the loop device

losetup -o 1048576 /dev/loop1 my-hd-image.file
losetup -o 537919488 /dev/loop2 my-hd-image.file

Now we can format the partition

mkfs.ext4 /dev/loop1
mkfs.ext4 /dev/loop2

all is left to do is mount the partitions

[email protected]# mkdir /mnt/my-hd-partition-1
[email protected]# mkdir /mnt/my-hd-partition-2
[email protected]# mount /dev/loop1 /mnt/my-hd-partition-1
[email protected]# mount /dev/loop2 /mnt/my-hd-partition-2

To remove the device just use unmount and to release the loop device
you will need to use losetup

umount /mnt/my-hd-partition-2
umount /mnt/my-hd-partition-1
losetup -d /dev/loop1
losetup -d /dev/loop2