Sunday, February 9, 2014

Two node PBX In A Flash (PIAF) Cluster using heartbeat and drbd

Install elrepo and epel on pbxin a flash both nodes.

# wget http://epel.mirror.freedomvoice.com/6/i386/epel-release-6-8.noarch.rpm
# yum install epel-release-6-8.noarch.rpm

# wget http://www.elrepo.org/elrepo-release-6-5.el6.elrepo.noarch.rpm
# yum install elrepo-release-6-5.el6.elrepo.noarch.rpm

Install DRBD

# yum install kmod-drbd83 drbd83-utils

Install heartbeat

# yum install heartbeat

Assuming the disk /dev/sdb is the one on both nodes to be mirrored.

create the file /etc/drbd.d/disk1.res with the following contents

resource disk1
{
startup {
wfc-timeout 30;
outdated-wfc-timeout 20;
degr-wfc-timeout 30;
}
net {
cram-hmac-alg sha1;
shared-secret sync_disk;
}
syncer {
rate 100M;
verify-alg sha1;
}
on node1 {
device /dev/drbd0;
disk /dev/sdb;
address ip-of-node1:7789;
meta-disk internal;
}
on node2 {
device /dev/drbd0;
disk /dev/sdb;
address ip-of-node2:7789;
meta-disk internal;
}
}



Create meta disk

#drbdadm create-md disk1


#service drbd start

On Node you want to make primary in this case node1
drbdadm -- --overwrite-data-of-peer primary disk1
To monitor the sync you can use
# watch "cat /proc/drbd"

Once the disk are synced  you should see something like this in
/proc/drbd

version: 8.3.16 (api:88/proto:86-97)
GIT-hash: a798fa7e274428a357657fb52f0ecf40192c1985 build by phil@Build32R6, 2013-09-27 15:59:12
 0: cs:Connected ro:Primary/Secondary ds:UpToDate/UpToDate C r-----
    ns:144600 nr:428 dw:145028 dr:32986 al:47 bm:0 lo:0 pe:0 ua:0 ap:0 ep:1 wo:f oos:0


Now make filesystem
# mkfs.ext4 /dev/drbd0

For some reason heartbeat does not work on ucast so I used mcast. Create your heartbeat configuration in /etc/ha.d/ha.cf as follows
debugfile /var/log/ha-debug
logfile /var/log/ha-log
logfacility local0
keepalive 300ms
deadtime 4
warntime 2
initdead 10
udpport 694
mcast eth0 239.1.2.3 694 1 0
#bcast eth0
auto_failback on
node node1 node2





Create haresource file
# cat /etc/ha.d/haresources
node1 drbddisk::disk1 Filesystem::/dev/drbd0::/disk1::ext4 mysqld
node1 cluster-ip/24/eth0/cluster-ip-broadcast-ip IPsrcaddr::cluster-ip asterisk httpd


Explanation
The first line is to make primary the drbd disk and mount it on the primary node and start MySQL
The 2nd line is to create a cluster-IP and send all outbound traffic through it and start asterisk (using amportal) and httpd.

Create authkeys file with permission 600 on both nodes
# dd if=/dev/urandom count=4 2>/dev/null | md5sum | cut -c1-32

# cat > /etc/ha.d/authkeys
auth 1
1 sha1

Create symlinks to mysqld and asterisk in /etc/ha.d/resources.d
# cd /etc/ha.d/resources.d
# ln -s /etc/init.d/mysqld
# ln -s /usr/local/sbin/amportal asterisk


Create a folder /disk1 so that when you start heartbeat the drbd disk is mounted
# mkdir /disk1

# service heartbeat start 


The above command will start mysqld asterisk and mount the drbd disk on /disk1


Stop mysqld and asterisk
Move /var/lib/mysql to /disk1/var/mysql
Move /var/lib/asterisk to /disk1/var/asterisk
Move /etc/asterisk to /disk1/etc/asterisk
Move /usr/lib/asterisk to /disk1/usr/asterisk
Move /tftpboot to /disk1/tftpboot
Move /var/spool/asterisk to /disk1/var/spool


on Node2 stop mysqld and asterisk
delete /var/lib/mysql /var/lib/asterisk /etc/asterisk /usr/lib/asterisk /tftpboot /var/spool/asterisk


Now create symlinks to the corresponding folders in disk1
example:
# cd /var/lib
# ln -s /disk1/var/mysql
# ln -s /disk1/var/asterisk
# cd /usr/lib
# ln -s /disk1/usr/asterisk
# cd /etc
# ln -s /disk1/etc/asterisk
# cd /
# ln -s /disk1/tftpboot
# cd /var/spool
# ln -s /disk1/var/spool/asterisk






If you have any other folders that need to be synced between two nodes just move them to the /disk1 and create symlinks on both nodes to point to it.

Disable mysqld and asterisk startup on boot using
# chkconfig mysqld off
# chkconfig asterisk off

Enable drbd to be started on boot on both nodes
# chkconfig drbd on

PIAF uses /etc/rc.local file to start asterisk so comment out the line  
/usr/local/sbin/amportal

Start heartbeat on second node. You have now a two node pbx in a flash cluster which works on the same database and same asterisk configuration. All you have to do is to use the cluster IP to manage the instance.

Thursday, January 16, 2014

Counting the total allocated disk space on a remote or local server.

If you are trying to count the total disk space allocated to a server local or remote the following would be useful

ssh user@server df -k | grep -v Filesystem|  awk '{print $2}' | paste -sd+ | bc 

the output will be total K of your disks.
grep -v Filesystem is to remove your header field for df -k command.

If you are looking for say a particular volume you could grep for it before awk.
For example you want to find the total allocated corresponding to a particular logical volume let say logVol1 you could use the following

ssh user@server df -k | grep logVol1| awk '{print $2}' | paste -sd+ | bc

If you want to do it on local server drop the ssh.

Thursday, October 17, 2013

Connecting to MS SQLServer from Linux using windows authentication , java and jtds

Download jtds from http://jtds.sourceforge.net

The following example connects to a SQLServer using windows authentication.
Source modified from "Here"

To compile the code use
$ javac testConnection.java

To run the code

$ java -cp ./jtds/jtds.jar:. testConnection

This assumes you have downloaded the jtds into a subfolder called jtds. Also in the code 1433 is the port of SQLServer.


 import java.sql.*;  
 public class testConnection  
 {  
   public static void main(String[] args)  
   {  
     DB db = new DB();   
     db.dbConnect("jdbc:jtds:sqlserver://<your SQL Server>:1433/<DatabaseName>;domain=<your Domain>","UserName","Password");  
   }  
 }  
 class DB  
 {  
   public DB() {}  
   public void dbConnect(String db_connect_string,  
  String db_userid, String db_password)  
   {  
     try  
     {  
       Class.forName("net.sourceforge.jtds.jdbc.Driver");  
       Connection conn = DriverManager.getConnection(  
   db_connect_string, db_userid, db_password);  
       System.out.println("connected");  
     }  
     catch (Exception e)  
     {  
       e.printStackTrace();  
     }  
   }  
 };  

Monday, July 15, 2013

My Journey in building a $400 Storage Server Part 1 (Hardware)

I have started thinking about making a $400 NAS server a very long back before the floods in Thaiwan and I planned to acquire the following items to build the server.

1. An ATX Cabinet
2. Some AMD processor with 2 cores
3. 3x 2TB drives
4. A Motherboard
5. 4GB of RAM
6. DVD Drive
7. Power Supply for the Cabinet.

I wanted to build every thing under $400 but with floods the cost of Hard Drives sky-rocketed and my project delayed. After thinking very hard I have decided that instead of sticking to just a storage server if I upgrade my desire to build a home all in one server which I can use as a Storage Server, Web Server, Email Server and File Share server then I can justify spending more for this project.

This is a list of hardware I purchased to build the server. In this part 1 I am going to document the building of the physical server and next parts I will write about what software / services I have installed on this server.

1. 3x 3TB Hard Drives from Amazon for $330

2. ATX Shinobi Cabinet from microcenter for $40

3. MSI Motherboard clearance from microcenter for $16
4. AMD FX 8320 CPU from microenter for $150

5. 500W Power supply from microcenter for $28

6.8GB Crucial memory from microcenter for $72
7. Some SATA III cables from microcenter for $10
8. Video card NVidia 8400 from microcenter for $25

Total Server cost $687 which is $287 above my planned budget. I hope this will turn into a good investment.

Here are some pictures of the build process.








Thursday, June 13, 2013

iptables Quick Reference

List current iptables

# iptables -L

To list in numeric form

# iptables -L -n

To include interface information too use verbose output

# iptables -L -n -v 

To append to a chain follow the example below

# iptables -A INPUT -s 10.1.10.1 -p tcp --dport 80 -j ACCEPT

To insert at say 7th position instead of Append

# iptables -I INPUT 7 -s 10.1.10.1 -p tcp --dport 80 -j ACCEPT

To save the iptables so that they survive reboots

# service iptables save

Saves every thing to /etc/sysconfig/iptables

Wednesday, April 3, 2013

Aligning Linux VM LVM Disks to NetApp NFS recommendation

Aligning disks of virtual machines with the underlying storage system is very important as it helps in the I/O performance. Lets examine aligning an existing misaligned disk on a Linux (Redhat/ CentOS). For NetApp NFS we have to change the starting sector on linux partition to 64 (63 by default). For more information about alignment and recommendations look up NetApp documentation.
Scenario
We have a Logical volume VolGroup01 with 1 disk /dev/sdb which is not aligned with the underlying storage system. 
Plan to fix this.
We will add a disk of same size to the VM let say its /dev/sdc.
We partition it with the proper alignment
Add this to the VolGroup01 
Move data from /dev/sdb to this new disk 
Remove /dev/sdb from the VolGroup01
Remove the disk from the VM.

The process.





Checking the existing disk which which is misaligned.
# sfdisk -uS -l /dev/sdb

Disk /dev/sdb: 13054 cylinders, 255 heads, 63 sectors/track
Units = sectors of 512 bytes, counting from 0

   Device Boot    Start       End   #sectors  Id  System
/dev/sdb1            63 209712509  209712447  8e  Linux LVM
/dev/sdb2             0         -          0   0  Empty
/dev/sdb3             0         -          0   0  Empty
/dev/sdb4             0         -          0   0  Empty


Formating the New Disk
 
# fdisk /dev/sdc

Command (m for help): u
Changing display/entry units to sectors

Command (m for help): n
Command action
   e   extended
   p   primary partition (1-4)
p
Partition number (1-4): 1
First sector (63-209715199, default 63): 64
Last sector, +sectors or +size{K,M,G} (64-209715199, default 209715199):
Using default value 209715199

Command (m for help): t
Selected partition 1
Hex code (type L to list codes): 8e
Changed system type of partition 1 to 8e (Linux LVM)

Command (m for help): w
The partition table has been altered!

Calling ioctl() to re-read partition table.
Syncing disks.


Checking New Disk partition
 

# sfdisk -uS -l /dev/sdc

Disk /dev/sdc: 13054 cylinders, 255 heads, 63 sectors/track
Units = sectors of 512 bytes, counting from 0

   Device Boot    Start       End   #sectors  Id  System
/dev/sdc1            64 209715199  209715136  8e  Linux LVM
/dev/sdc2             0         -          0   0  Empty
/dev/sdc3             0         -          0   0  Empty
/dev/sdc4             0         -          0   0  Empty

# pvcreate /dev/sdc1
  Writing physical volume data to disk "/dev/sdc1"
  Physical volume "/dev/sdc1" successfully created

# vgextend VolGroup01 /dev/sdc1
  Volume group "VolGroup01" successfully extended
#pvmove /dev/sdb1

This will move all data off of the old disk to the new disk. This process will take time depending on the size of the disk.
#vgreduce VolGroup01 /dev/sdb1
  Removed "/dev/sdb1" from volume group "VolGroup01"




Remove the other non-aligned disk from the VM after shutting down the VM.
Now the disks are aligned.
In VMware after reboot the disk label changes so checking the alignment on the renamed disk /dev/sdc is re-labeled as /dev/sdb

#sfdisk -uS -l /dev/sdb

Disk /dev/sdb: 13054 cylinders, 255 heads, 63 sectors/track
Units = sectors of 512 bytes, counting from 0

   Device Boot    Start       End   #sectors  Id  System
/dev/sdb1            64 209715199  209715136  8e  Linux LVM
/dev/sdb2             0         -          0   0  Empty
/dev/sdb3             0         -          0   0  Empty
/dev/sdb4             0         -          0   0  Empty

Tuesday, January 22, 2013

Combine multiple PDF files to one

You can use gs (ghostscript) to combine multiple pdf files to one as follows


gs -dBATCH -dNOPAUSE -q -sDEVICE=pdfwrite -sOutputFile=combined.pdf file1.pdf file2.pdf


  • gs -- starts the Ghostscript program
  • -dBATCH -- once Ghostscript processes the PDF files, it should exit. If you don't include this option, Ghostscript will just keep running
  • -dNOPAUSE -- forces Ghostscript to process each page without pausing for user interaction
  • -q -- stops Ghostscript from displaying messages while it works
  • -sDEVICE=pdfwrite -- tells Ghostscript to use its built-in PDF writer to process the files
  • -sOutputFile=combined.pdf -- tells Ghostscript to save the combined PDF file with the name that you specified

--
This is exact copy from "link" made a note on this blog for reference in case of non-availability of the original source.