Tuesday, December 15, 2015

MySQL store global persistent user variables

Install MySQL Global User Variables UDF. Global User Variables UDF is a MySQL extension which helps storing global user defined variables persistently.


Go to URL: https://www.pureftpd.org/project/mysql-udf-global-user-variables/download
























Click on appropriate method of download either HTTP or FTP.

wget https://download.pureftpd.org/mysql-udf-global-user-variables/global-user-variables-1.2.tar.gz















Extract downloaded tar file.

tar xzvf global-user-variables-1.2.tar.gz











Change directory to extracted directory and install it.

cd global-user-variables
make install











Now we can find udf_global_user_variables.so. We need to copy it to mysql plugin directory.

ll udf_global_user_variables.so
cp udf_global_user_variables.so /usr/lib64/mysql/plugin/


Now login to mysql and execute following command to create function.

mysql -u root -p

DROP FUNCTION IF EXISTS global_set;
CREATE FUNCTION global_set RETURNS INTEGER
  SONAME 'udf_global_user_variables.so';

DROP FUNCTION IF EXISTS global_get;
CREATE FUNCTION global_get RETURNS STRING
  SONAME 'udf_global_user_variables.so';

DROP FUNCTION IF EXISTS global_add;
CREATE FUNCTION global_add RETURNS INTEGER
  SONAME 'udf_global_user_variables.so';










































Now we can set variables with following command. In case below VAR_GOLD_PRICE is a variable and 48000 is the value.

do global_set("VAR_GOLD_PRICE",48000);

To read the variable value we can use following command.

select global_get("VAR_GOLD_PRICE");




































The value is readable globally and persistent till mysql server reboots.

We can set the string variables as below.

do global_set("VAR_MEMBER_NAME","Dilli Raj Maharjan");
select global_get("VAR_MEMBER_NAME");






Wednesday, December 9, 2015

Install Zabbix on Linux

Zabbix is a software for monitoring availability and performance of IT infrastructure components. It is opensource and helps gathering statistical data of system health. 


Following are its core features:
  1. A web-based interface
  2. Secure user authentication
  3. A flexible user permission schema
  4. Agents based data gathering and agent-less monitoring
  5. Polling and trapping support.
  6. Zabbix-proxy to centrally collect information and upload to zabbix server.
  7. Remote commands support
  8. True open-source software 
  9. Notification by SMS, Jabber, email, skype, IM, Voice
  10. Visualisation 

Requirement

Memory size: 128 MB 
Disk size: 256 MB 

Backend database: 

  1. MySQL 5.0.3 or later
  2. PostgreSQL 8.1 or later
  3. Oracle 10g or later
  4. SQLite 3.3.5 or later
  5. IBM DB2 9.7 or later

Zabbix frontend requires:

  1. Apache 1.3.12 or later
  2. PHP 5.3.0 or later
  3. PHP extensions: gd, bcmath, ctype , libXML, xmlreader, xmlwriter, session, sockets, mbstring, gettext, ibm_db2, mysqli, oci8, pgsql, sqlite3
  4. OS Packages: net-snmp.x86_64, snmp.x86_64, OpenIPMI-libs.x86_64, mysql-server

Install required package with yum command below.

yum -y install httpd.x86_64 php.x86_64 php-bcmath.x86_64 php-common.x86_64 php-gd.x86_64 php-mysql.x86_64 php-odbc.x86_64 php-xml.x86_64 php-mbstring

Download fping from URL below:

wget  http://pkgs.repoforge.org/fping/fping-3.1-1.el6.rf.x86_64.rpm
rpm ivh fping-3.1-1.el6.rf.x86_64.rpm







Download iksemel package

http://repo.zabbix.com/non-supported/rhel/6/x86_64/iksemel-1.4-2.el6.x86_64.rpm


Download Zabbix rpm.

  1. Go to URL http://www.zabbix.com/download.php
  2. Under Zabbix Packages heading select the version of OS and architecture.
  3. Choose desired version and download 











Package description:

zabbix-agent-*.rpm Client agent that will be installed on the Zabbix client. Need to be installed on every machine that is intended to monitor.
zabbix-web-*.rpm Web interface to configure Zabbix server. It comes with two backend database flavor.
  • zabbix-web-mysql-*.rpm
  • zabbix-web-pgsql-*.rpm
Zabbix-proxy-*.rpm Optional package component that will centrally collect data and upload to zabbix server. It comes with three backend database.
  • zabbix-proxy-mysql-*.rpm
  • zabbix-proxy-pgsql-*.rpm
  • zabbix-proxy-sqlite3-*.rpm
Zabbix-server-*.rpm Core package the installed the zabbix server. It comes with two backend database 
  • zabbix-server-mysql-*.rpm
  • zabbix-server-pgsql-*.rpm
At least zabbix-web-*.rpm and Zabbix-server-*.rpm package is required to install on monitoring server.

Install zabbix-server package.

rpm -ivh zabbix-2.4.7-1.el6.x86_64.rpm zabbix-server-2.4.7-1.el6.x86_64.rpm zabbix-server-mysql-2.4.7-1.el6.x86_64.rpm



Install  zabbix-web packages

rpm -ivh zabbix-web-2.4.7-1.el6.noarch.rpm zabbix-web-mysql-2.4.7-1.el6.noarch.rpm





Configure backend database to store zabbix data. In my case I am using mysql as backend database.Install mysql initial database(Fresh Database)

mysql_install_db 
































Start mysql database

/etc/init.d/mysqld start


















Login as user root and change mysql root password

mysql -u root 
update mysql.user
set password=PASSWORD('*******')

where user='root';

Create mysql database zabbix with character set utf8

Create database zabbix character set utf8;

Create user zabbix

grant all on zabbix.* to zabbix@'localhost' identified by '********';
flush privileges;
















Now create required schema objects.

cd /usr/share/doc/zabbix-server-mysql-2.4.7/create/
mysql -uzabbix -p zabbix < schema.sql
mysql -uzabbix -p zabbix < images.sql
mysql -uzabbix -p zabbix < data.sql














Edit zabbix_server.conf file to set database IP, User and client. Add following lines on the file.

vi /etc/zabbix/zabbix_server.conf 
DBHost=localhost
DBName=zabbix
DBUser=zabbix
DBPassword=(your zabbix password)

Modify php.ini configuration file
Change following parameter values

max_execution_time 300
memory_limit 128M
post_max_size 16M
upload_max_filesize 2M
max_input_time 300
date.timezone Asia/Kathmandu

Start httpd and zabbix-server

/etc/init.d/httpd start
/etc/init.d/zabbix-server start








Make sure these services are startup at boot.

chkconfig httpd on
chkconfig zabbix-server on
chkconfig mysqld on








Monitor logfile with command below


tail -f /var/log/zabbix/zabbix_server.log 




































Verify all services are started properly.

Web configuration

Open following URL on browser.

http://192.168.1.250/Zabbix












Click on Next

Verify all pre-requisites are installed and click on next.


Configure database connections parameters and click on Test connection.

Click on next if Test connection returns OK

Enter hostname, name and port and click on next


Verify the Pre-Installation Summary and Click on Next to continue.


Click on Finish to complete Installation.

Login with Admin/zabbix


Friday, December 4, 2015

Shrink Oracle Virtualbox virtual machines to claim unused space

Over the time, we can notice that the size of the dynamically growing virtual disks vdi file keep growing on Oracle Virtual box. Deleting the files inside the Guest Machine does not free up any space in the Host Machine's hard drive. We need to shrink vdi file to claim unused space.
Please follow the steps below to claim unused space.

We can notice that the size of the DGMaster.vdi is 20GB











Delete unnecessary files from the Guest machine.

Power off the guest machine.

Convert vdi file to compact.

VBoxManage modifyvdi DGMaster.vdi --compact








Power On the Guest Machine and install zero free package inside it.


We can get package from URL below. Package for EL5 works perfect for EL6 too.

http://rpm.pbone.net/index.php3/stat/4/idpl/15277264/dir/redhat_el_5/com/zerofree-1.0.1-5.el5.x86_64.rpm.html
rpm -ivh zerofree-1.0.1-5.el5.x86_64.rpm 




















Now umount the partition or make it read only. In my case I am freeing space from root partition so I need to reboot my Guest Machine to single user mode and make it read only.


Boot the system to single user mode
mount -o remount,ro /dev/mapper/vg_oel1-lv_root


















Now find and fill unused, unallocated blocks with zero. 

zerofree -v /dev/mapper/vg_oel1-lv_root







Output can be interpreted as: Total number of modified blocks/Total number of free blocks/Total number of blocks


Mount back the root partition to rw mode and poweroff the guest machine.

mount -o remount,rw /dev/mapper/vg_oel1-lv_root
poweroff


Now execute VBoxManage compact command from Host Machine to reclaim unused space. After modifyvdi completes you may notice the file size change.

VBoxManage modifyvdi DGMaster.vdi --compact






















We can notice that Size of DGMaster.vdi has been decreased from 20GB to 14GB.

Tuesday, December 1, 2015

Linux Network Interface Card Bonding


Channel Bonding helps two or more NIC card act as single. It increase the bandwidth and provides redundancy. In case on one physical NIC is down network traffic moves to another NIC card providing High Availability. We use bond driver to achieve it.

The following instructions are tested on:
OS: CentOS 6.7 installed on Virtual box Version 5.0.10 r104061
VM adapter settings: Bridged Adapter, Paravirtualized Network (virtio-net)
2 Ethernet Adapters eth0, eth1

Create bonding.conf file inside directory /etc/modprobe.d and add following linesalias bond0 bonding








Add following lines on the file bond0 interface configuration file /etc/sysconfig/network-scripts/ifcfg-bond0

DEVICE=bond0
IPADDR=192.168.1.254
NETWORK=192.168.1.0
NETMASK=255.255.255.0
USERCTL=no
BOOTPROTO=none
BONDING_OPTS="miimon=100 mode=1 fail_over_mac=1"
ONBOOT=yes
NM_CONTROLLED=no













Add following lines on file eth0 and eth1 configuration files
/etc/sysconfig/network-scripts/ifcfg-eth0

DEVICE=eth0
USERCTL=no
ONBOOT=yes
MASTER=bond0
SLAVE=yes
BOOTPROTO=none
NM_CONTROLLED=no











/etc/sysconfig/network-scripts/ifcfg-eth1

DEVICE=eth1
USERCTL=no
ONBOOT=yes
MASTER=bond0
SLAVE=yes
BOOTPROTO=none
NM_CONTROLLED=no











Now restart network service

/etc/init.d/network restart









Verify failover

Ping IP 192.168.1.254 from another host.

ping 192.168.1.254


Keep watching the /var/log/message log file for interface down.


tail -f /var/log/message

Plug OFF cable from eth0. Message on the log file will be displayed as below
Dec  1 07:53:45 svn NetworkManager[1581]: <info> (eth0): carrier now OFF (device state 1)
Dec  1 07:53:45 svn kernel: bonding: bond0: link status definitely down for interface eth0, disabling it
Dec  1 07:53:45 svn kernel: bonding: bond0: making interface eth1 the new active one.

Plug ON cable on eth0
Dec  1 07:53:55 svn NetworkManager[1581]: <info> (eth0): carrier now ON (device state 1)
Dec  1 07:53:56 svn kernel: bonding: bond0: link status definitely up for interface eth0, 4294967295 Mbps full duplex.

Plug OFF cable on eth1
Dec  1 07:54:00 svn NetworkManager[1581]: <info> (eth1): carrier now OFF (device state 1)
Dec  1 07:54:00 svn kernel: bonding: bond0: link status definitely down for interface eth1, disabling it
Dec  1 07:54:00 svn kernel: bonding: bond0: making interface eth0 the new active one.

Plug ON cable on eth1
Dec  1 07:54:04 svn NetworkManager[1581]: <info> (eth1): carrier now ON (device state 1)
Dec  1 07:54:04 svn kernel: bonding: bond0: link status definitely up for interface eth1, 4294967295 Mbps full duplex.
[root@svn ~]# 


During cable Plug on and OFF, We can notice there is 0% Packet loss output from Ping result

--- 192.168.1.254 ping statistics ---
26 packets transmitted, 26 packets received, 0.0% packet loss
round-trip min/avg/max/stddev = 0.377/0.496/1.085/0.188 ms

Friday, November 27, 2015

Auto Start/Shutdown Oracle 11g on CEntOS

Modify /etc/oratab and Replace N with Y for enabling Oracle Startup at Boot.

TESTDB:/u01/app/oracle/product/11.2.0/db_1:N
to 
TESTDB:/u01/app/oracle/product/11.2.0/db_1:Y








Create /etc/init.d/oracle file with following codes vi /etc/init.d/oracle. Must change ORACLE_HOME, ORACLE_SID

#!/bin/sh
#
# /etc/rc.d/init.d/oracle
# Description: Starts and stops the Oracle database, listeners and Enterprise Manager
# See how we were called.

export ORACLE_HOME=/u01/app/oracle/product/11.2.0/db_1
export ORACLE_SID=TESTDB

case "$1" in
start)
echo "Starting Oracle"
echo "—————————————————-" >> /var/log/oracle
date +"! %T %a %D : Starting Oracle Databases as part of system up." >> /var/log/oracle
echo "—————————————————-" >> /var/log/oracle
echo -n "Starting Oracle Databases: "
su - oracle -c ${ORACLE_HOME}/bin/dbstart >> /var/log/oracle
echo "Done."
echo -n "Starting Oracle Listeners: "
su - oracle -c "${ORACLE_HOME}/bin/lsnrctl start" >> /var/log/oracle
echo "Done."
echo -n "Starting Oracle Enterprise Manager: "
su - oracle -c "${ORACLE_HOME}/bin/emctl start dbconsole" >> /var/log/oracle
echo "Done."
echo ""
echo "—————————————————-" >> /var/log/oracle
date +"! %T %a %D : Finished." >> /var/log/oracle
echo "—————————————————-" >> /var/log/oracle
touch /var/lock/subsys/oracle
;;
stop)
echo "Shutting Down Oracle"
echo "—————————————————-" >> /var/log/oracle
date +"! %T %a %D : Shutting Down Oracle Databases as part of system down." >> /var/log/oracle
echo "—————————————————-" >> /var/log/oracle
echo -n "Shutting Down Oracle Enterprise Manager: "
su - oracle -c "${ORACLE_HOME}/bin/emctl stop dbconsole" >> /var/log/oracle
echo "Done."
echo -n "Shutting Down Oracle Listeners: "
su - oracle -c "${ORACLE_HOME}/bin/lsnrctl stop" >> /var/log/oracle
echo "Done."
rm -f /var/lock/subsys/oracle
echo -n "Shutting Down Oracle Databases: "
su - oracle -c ${ORACLE_HOME}/bin/dbshut >> /var/log/oracle
echo "Done."
echo ""
echo "—————————————————-" >> /var/log/oracle
date +"! %T %a %D : Finished." >> /var/log/oracle
echo "—————————————————-" >> /var/log/oracle
;;
restart)
echo "Restarting Oracle"
echo "—————————————————-" >> /var/log/oracle
date +"! %T %a %D : Restarting Oracle Databases as part of system up." >> /var/log/oracle
echo "—————————————————-" >> /var/log/oracle
echo -n "Restarting Oracle Databases: "
su - oracle -c ${ORACLE_HOME}/bin/dbshut >> /var/log/oracle
su - oracle -c ${ORACLE_HOME}/bin/dbstart >> /var/log/oracle
echo "Done."
echo -n "Restarting Oracle Listeners: "
su - oracle -c "${ORACLE_HOME}/bin/lsnrctl stop" >> /var/log/oracle
su - oracle -c "${ORACLE_HOME}/bin/lsnrctl start" >> /var/log/oracle
echo "Done."
echo -n "Restarting Oracle Enterprise Manager: "
su - oracle -c "${ORACLE_HOME}/bin/emctl stop dbconsole" >> /var/log/oracle
su - oracle -c "${ORACLE_HOME}/bin/emctl start dbconsole" >> /var/log/oracle
echo "Done."
echo ""
echo "—————————————————-" >> /var/log/oracle
date +"! %T %a %D : Finished." >> /var/log/oracle
echo "—————————————————-" >> /var/log/oracle
touch /var/lock/subsys/oracle
;;
*)
echo "Usage: oracle {start|stop|restart}"
exit 1
esac


Make the script file executable. Execute following command to change permission.

chmod 755 /etc/init.d/oracle






The following commands will ensure that the databases will come up in runlevels 2,3,4 and 5

ln -s /etc/rc.d/init.d/oracle /etc/rc.d/rc2.d/S99oracle
ln -s /etc/rc.d/init.d/oracle /etc/rc.d/rc3.d/S99oracle
ln -s /etc/rc.d/init.d/oracle /etc/rc.d/rc4.d/S99oracle
ln -s /etc/rc.d/init.d/oracle /etc/rc.d/rc5.d/S99oracle








To stop the databases on reboot or restart we need the following links

ln -s /etc/rc.d/init.d/oracle /etc/rc.d/rc0.d/K01oracle
ln -s /etc/rc.d/init.d/oracle /etc/rc.d/rc6.d/K01oracle



Wednesday, November 25, 2015

Install svn on CEntOS 6

svn is a software versioning and revision control system distributed as free software under the Apache License. Software developers useSubversion to maintain current and historical versions of files such as source code, web pages, and documentation.

go to url http://opensource.wandisco.com/centos/6/svn-1.8/RPMS/<arch> in my case I am using x86_64 architecture so using following URL to download required packages.
http://opensource.wandisco.com/centos/6/svn-1.8/RPMS/x86_64/


Download following packages:



  1. mod_dav_svn-1.8.14-1.x86_64.rpm
  2. serf-1.3.7-1.x86_64.rpm
  3. subversion-1.8.14-1.x86_64.rpm


Optionally following packages can be downloaded:


  1. serf-debuginfo-1.3.7-1.x86_64.rpm
  2. serf-devel-1.3.7-1.x86_64.rpm
  3. subversion-devel-1.8.14-1.x86_64.rpm
  4. subversion-gnome-1.8.14-1.x86_64.rpm
  5. subversion-javahl-1.8.14-1.x86_64.rpm
  6. subversion-perl-1.8.14-1.x86_64.rpm
  7. subversion-python-1.8.14-1.x86_64.rpm
  8. subversion-ruby-1.8.14-1.x86_64.rpm
  9. subversion-tools-1.8.14-1.x86_64.rpm

Install mod_dav_svn, serf and subversion



rpm -ivh mod_dav_svn-1.8.14-1.x86_64.rpm serf-1.3.7-1.x86_64.rpm subversion-1.8.14-1.x86_64.rpm 









Modify subversion.conf file on conf.d of httpd.

vi /etc/httpd/conf.d/subversion.conf

Modify following settings 

SVNPath /opt/repo/ to SVNParentPath /opt/repo/
AuthName "SVN Repo" to AuthName "Authorization Realm"
AuthUserFile /opt/WANdisco/apache22/conf/svn.passwd AuthUserFile /etc/httpd/conf/.htpasswd

Add following settings

AuthzSVNAccessFile /etc/svnusers















Create repo

mkdir -p /opt/repo/
cd /opt/repo/
svnadmin create rapti_rp
svnadmin create bheri_rp
svnadmin create karnali_rp
chown -R apache:apache /opt/repo

















Create user and group logins

htpasswd -c -m /etc/httpd/conf/.htpasswd admin_rapti
htpasswd -m /etc/httpd/conf/.htpasswd admin_bheri
htpasswd -m /etc/httpd/conf/.htpasswd admin_karnali
htpasswd -m /etc/httpd/conf/.htpasswd user_rapti
htpasswd -m /etc/httpd/conf/.htpasswd user_bheri
htpasswd -m /etc/httpd/conf/.htpasswd user_karnali
































Create groups

cat > /etc/svnusers
[groups]
rapti_admin = admin_rapti
bheri_admin = admin_bheri
karnali_admin = admin_karnali
rapti_user = user_rapti
bheri_user = user_bheri
karnali_user =  user_karnali

[rapti_rp:/]
@rapti_admin = rw
@rapti_user = r

[bheri_rp:/]
@bheri_admin = rw
@bheri_user = r

[karnali_rp:/]
@karnali_admin = rw
@karnali_user = r




















Restart httpd service

/etc/init.d/httpd start








We can browse SVN with the url below:

http://<IPADDRESS>/svn/<repo_name>
http://192.168.1.251/svn/rapti_rp












Now we can create trunk, tags and branches with command below

svn mkdir file:///opt/repo/bheri_rp/tags -m "adding tag directory"
svn mkdir file:///opt/repo/bheri_rp/branches -m "adding branch directory"
svn mkdir file:///opt/repo/bheri_rp/trunk -m "adding trunk directory"














You can find the directories while accessing the svn url

http://192.168.1.251/svn/bheri_rp/

















Importing existing project into repo.

svn import /home/dilli/proj_bheri http://192.168.1.251/svn/bheri_rp/ -m "Initial import"


Checkout svn to current directory

svn co http://192.168.1.251/svn/bheri_rp/ .

Create hook file so that whenever commit occurs some svn command get executed.

cat > post-commit
#!/bin/sh

cd /var/www/web/bheri && /usr/bin/svn update --username username --password password --no-auth-cache >> /tmp/svn.out


Change ownership and mode of the hook file.

chmod 755 post-commit
chown apache:apache post-commit