Tuesday 29 September 2015

Backup MariaDB using xtrabackup on CentOS7

Percona XtraBackup is an open-source hot backup utility for MariaDB/MySQL-based servers that doesn’t lock your database during the backup. It can back up data from InnoDB, XtraDB, and MyISAM tables on MariaDB/MySQL servers.
This guide looks into how to setup xtrabackup backup system for MariaDB on CentOS7, and how to full backup an MariaDB, and then restore the DB.

First thing, ensure the minimum setup for database config file:
[root@centos7 ~]# vi /etc/my.cnf
#
# This group is read both both by the client and the server
# use it for options that affect everything
#
[client-server]

#
# This group is read by the server
#
[mysqld]
# Disabling symbolic-links is recommended to prevent assorted security risks
symbolic-links=0

#
# include all files from the config directory
#
!includedir /etc/my.cnf.d
[root@centos7 ~]# vi /etc/my.cnf.d/server.cnf
[mysqld]
datadir=/var/lib/mysql
socket=/var/lib/mysql/mysql.sock
log-error=/var/log/mariadb/mariadb.log
pid-file=/var/run/mariadb/mariadb.pid
Start MariaDB/Mysql
[root@centos7 ~]# service mysql start
Install percona repository, and install xtrabackup
[root@centos7 ~]# yum install https://www.percona.com/redir/downloads/percona-release/redhat/latest/percona-release-0.1-3.noarch.rpm
[root@centos7 ~]# yum list|grep percona
[root@centos7 ~]# yum install percona-xtrabackup
We will create a backup directory
[root@centos7 ~]# mkdir /root/backup
Installation done. To proceed with Full backup,we need to issue the innobackupex, with the DB admin user and password, and also the backup directory.
[root@centos7 ~]# innobackupex --user=root --password=PASSWORD /root/backup
To get the backup information for a particular backup, we can check the xtrabackup_checkpoints file.
[root@centos7 backup]# cat /root/backup/2015-09-29_02-01-17/xtrabackup_checkpoints
We have done the Full backup. To restore a Full backup, we have to stop the MariaDB/Mysql server first.
[root@centos7 ~]# service mysql stop
Move the current mysql data directory to somewhere.
[root@centos7 ~]# rm -fr /var/lib/mysql_old
[root@centos7 ~]# mv /var/lib/mysql/ /var/lib/mysql_old
Issue the following command to restore.
[root@centos7 ~]# innobackupex --copy-back /root/backup/2015-09-29_02-01-17/
Change back the ownership of the mysql data directory.
[root@centos7 ~]# chown mysql:mysql /var/lib/mysql -R
And lastly, start the MariaDB/Mysql service.
[root@centos7 ~]# service mysql start

Wednesday 23 September 2015

Setup MariaDB Galera Cluster 10.0 On CentOS7

This guide looks into how to setup MariaDB Galera Cluster on CentOS7. MariaDB Galera Cluster is a synchronous multi-master cluster for MariaDB. It is is an easy-to-use, high-availability solution, which provides high system uptime, no data loss and scalability for future growth. It only supports the XtraDB/InnoDB storage engines

Features:
Synchronous replication
Active-active multi-master topology
Read and write to any cluster node
Automatic membership control, failed nodes drop from the cluster
Automatic node joining
True parallel replication, on row level
Direct client connections, native MySQL look & feel

Benefits:
The above features yield several benefits for a DBMS clustering solution, including:
No slave lag
No lost transactions
Both read and write scalability
Smaller client latencies

For this setup, we have 2 nodes.(ie 192.168.1.11,192.168.1.12.) MariaDB will first setup in (.11) and start in bootstrap mode. After (.11) has started, (.12) will setup next. Once the MariaDB service on (.12) started, Galera process will sync database data and transactions from (.11) to (.12), and after in sync, MariaDB in (.12) will turn on. Both servers will be multi masters and in sync constantly.

To begin with the setup, for simplicity, we will switch off selinux and firewall.

[root@centos7-11 ~]#systemctl stop firewalld
[root@centos7-11 ~]#systemctl disable firewalld
[root@centos7-11 ~]#vi /etc/selinux/config
disabled
[root@centos7-11 ~]#reboot
We will add a repository from MariaDB.

[root@centos7-11 ~]# vi /etc/yum.repos.d/mariadb.repo

[mariadb]
name = MariaDB
baseurl = http://yum.mariadb.org/10.0/centos6-amd64
gpgkey=https://yum.mariadb.org/RPM-GPG-KEY-MariaDB
gpgcheck=1
Install the package using YUM.
[root@centos7-11 ~]# yum clean all
[root@centos7-11 ~]# yum install MariaDB-Galera-server
Setup MariaDB.(At this point,the service is still call mysql.)
[root@centos7-11 ~]# service mysql start
[root@centos7-11 ~]# mysql_secure_installation
Stop the mysql service to adjust the MariaDB config file.
[root@centos7-11 ~]# service mysql stop
[root@centos7-11 ~]# vi /etc/my.cnf
!includedir /etc/my.cnf.d
Add replication parameters to server.cnf
[root@centos7-11 ~]# vi /etc/my.cnf.d/server.cnf 
[galera]
# Mandatory settings
wsrep_provider=/usr/lib64/galera/libgalera_smm.so
wsrep_cluster_address=gcomm://
binlog_format=row
default_storage_engine=InnoDB
innodb_autoinc_lock_mode=2
bind-address=192.168.1.11
#
# Optional setting
#wsrep_slave_threads=1
#innodb_flush_log_at_trx_commit=0
We can start the MariaDB on first node.(.11)
[root@centos7-11 ~]# service mysql start
By now, this node has setup to be the first master database. We shell proceed to setup the second master database.

Similarly, we will switch off selinux and firewall.

[root@centos7-12 ~]#systemctl stop firewalld
[root@centos7-12 ~]systemctl disable firewalld
[root@centos7-12 ~]vi /etc/selinux/config
disabled
[root@centos7-12 ~]reboot
We will add a repository from MariaDB.

[root@centos7-12 ~]# vi /etc/yum.repos.d/mariadb.repo

[mariadb]
name = MariaDB
baseurl = http://yum.mariadb.org/10.0/centos6-amd64
gpgkey=https://yum.mariadb.org/RPM-GPG-KEY-MariaDB
gpgcheck=1
Install the package using YUM.
[root@centos7-12 ~]# yum clean all
[root@centos7-12 ~]# yum install MariaDB-Galera-server
At this point, we just need to configure the MariaDB config files
[root@centos7-12 ~]# vi /etc/my.cnf
!includedir /etc/my.cnf.d
Add replication parameters to server.cnf
[root@centos7-12 ~]# vi /etc/my.cnf.d/server.cnf
[galera]
# Mandatory settings
wsrep_provider=/usr/lib64/galera/libgalera_smm.so
wsrep_cluster_address=gcomm://192.168.1.11
binlog_format=row
default_storage_engine=InnoDB
innodb_autoinc_lock_mode=2
bind-address=192.168.1.12

#
# Optional setting
#wsrep_slave_threads=1
#innodb_flush_log_at_trx_commit=0
We can start the MariaDB on second node.(.12)
[root@centos7-12 ~]# service mysql start
And thats it, 2 node multi masters MariaDB clusters.

Monday 30 March 2015

Automatic WAN failover (Multiple WAN) using ClearOS

In this tutorial, we look into how to configure Automatic Failover of 2 WAN Network using ClearOS.

Setup:
2 Internet Links (WAN)
1 LAN Link

Note: For ClearOS, you will need to have at least 1GB of ram.

Insert the DVD rom and lets start:

Install or upgrade an existing system
Install with Basic Storage Device.
Enter your root password twice.
It will take a while to copy the system files.
After it is done, let it reboot.
Login using the root credentials setup earlier.
At install wizard,click on next,and select Gateway Mode,click next.

eth0: External,DHCP,isp1.domain.lan
eth1: External,DHCP,isp2.domain.lan
eth2: LAN,Static,192.168.4.1,255.255.255.0,Enable DHCP Server

DNS Servers: Next,configure your DNS.
Select Support Edition: You can choose Community Edition or Professional Edition.
Software updates: Let it update its latest software.
System Registration: If you have an account, enter the login details. If you do not have, you can create an account.

Internet domain: Enter your domain.
Hostname: Enter your hostname.
Date and Time: Sync your time
Getting Started: Click Next
App Selection: Click Next
App Review: Click Next
Down and Install: Click Next



After above setup, the LAN link will act as a gateway, routing thru active link 1 or 2.


Have a try and let me know if it works for you.

Monday 9 March 2015

Setup HAProxy (loadbalancing) on CentOS7

This guide looks into how to setup HAProxy on CentOS7. HAProxy offers load balanced services to HTTP and TCP-based services, such as internet-connected services and web-based applications.

In Apr 2013, we looked into how to Setup Load-Balancing Cluster with LVS and Piranha on Centos 6. This time round for CentOS7, we will setup HAProxy Loadbalancing Cluster as this is shipped with CentOS7.

Setup:
1. HAProxy Server at 192.168.1.3
2. LB Virtual IP to use 192.168.1.3
3. Web1 at 192.168.1.9
4. Web2 at 192.168.1.10

For all the servers, lets (temporary) remove the firewall and selinux.
systemctl stop firewalld
systemctl disable firewalld
vi /etc/selinux/config
disabled
reboot
Lets start, login to HAProxy Server:
yum install haproxy
vi /etc/haproxy/haproxy.cfg
#---------------------------------------------------------------------
# Global settings
#---------------------------------------------------------------------
global
    # to have these messages end up in /var/log/haproxy.log you will
    # need to:
    #
    # 1) configure syslog to accept network log events.  This is done
    #    by adding the '-r' option to the SYSLOGD_OPTIONS in
    #    /etc/sysconfig/syslog
    #
    # 2) configure local2 events to go to the /var/log/haproxy.log
    #   file. A line like the following can be added to
    #   /etc/sysconfig/syslog
    #
    #    local2.*                       /var/log/haproxy.log
    #
    log         127.0.0.1 local2

    chroot      /var/lib/haproxy
    pidfile     /var/run/haproxy.pid
    maxconn     4000
    user        haproxy
    group       haproxy
    daemon

    # turn on stats unix socket
    stats socket /var/lib/haproxy/stats

#---------------------------------------------------------------------
# common defaults that all the 'listen' and 'backend' sections will
# use if not designated in their block
#---------------------------------------------------------------------
defaults
    mode                    http
    log                     global
    option                  httplog
    option                  dontlognull
    option http-server-close
    option forwardfor       except 127.0.0.0/8
    option                  redispatch
    retries                 3
    timeout http-request    10s
    timeout queue           1m
    timeout connect         10s
    timeout client          1m
    timeout server          1m
    timeout http-keep-alive 10s
    timeout check           10s
    maxconn                 3000

#---------------------------------------------------------------------
# main frontend which proxys to the backends
#---------------------------------------------------------------------
frontend LB
   bind 192.168.1.3:80
   reqadd X-Forwarded-Proto:\ http
   default_backend LB

#---------------------------------------------------------------------
# round robin balancing between the various backends
#---------------------------------------------------------------------
backend LB 192.168.1.3:80
   mode http
   stats enable
   stats hide-version
   stats uri /stats
   stats realm Haproxy\ Statistics
   stats auth admin:pass4325    # HAProxy Statistic username/password
   balance roundrobin           # Load balancing to use round-robin
   option httpchk
   option httpclose
   option forwardfor
   cookie LB insert
   server web1 192.168.1.9:80  check  # backend server.
   server web2 192.168.1.10:80 check  # backend server.
We shell enable the service and start the service:
systemctl enable haproxy
systemctl start haproxy
At Webserver create a test page.
vi /var/www/html/p.html
192.168.1.9 (To display Webserver IP address)
systemctl restart httpd
To access the statistic page, navigate to the HAProxy ip/stats,login with the username and pass as states in haproxy.cfg.

Friday 27 February 2015

Install MongoDB with PHP on Fedora

MongoDB is an open-source database used by companies of all sizes, across all industries and for a wide variety of applications. It is an agile database that allows schemas to change quickly as applications evolve, while still providing the functionality developers expect from traditional databases, such as secondary indexes, a full query language and strict consistency.

This tutorial looks into how to install latest MongoDB with PHP on Fedora.

Create a /etc/yum.repos.d/mongodb.repo file:
vi /etc/yum.repos.d/mongodb.repo
If you are running a 64-bit system, use the following configuration:
[mongodb]
name=MongoDB Repository
baseurl=http://downloads-distro.mongodb.org/repo/redhat/os/x86_64/
gpgcheck=0
enabled=1
else for 32-bit system, use:
[mongodb]
name=MongoDB Repository
baseurl=http://downloads-distro.mongodb.org/repo/redhat/os/i686/
gpgcheck=0
enabled=1
To install the latest stable version of MongoDB, issue the following command:
yum install -y mongodb-org
Install php,mongo driver:
pecl install mongo
create a mongo (php) ini
vi /etc/php.d/mongo.ini
And include the driver:
extension=mongo.so
Enable mongod on startup
systemctl enable mongod
start mongod:
service mongod start
Open mongo client to test.
mongo
create database test:
use test
Lets do an insert:
db.collection1.insert({"name":"desc"})
And lets do an query:
db.collection1.find();
Lets proceed with using mongodb in php. Restart php-fpm:
systemctl restart php-fpm.service
We shell create a php script:
<?php
$connection = new Mongo();
$dbname = $connection->selectDB('test');
$collection = $dbname->collection1;
$arr = array(
        'name' => 'MongoDB',
        'desc' => 'MongoDB is a document database.'
);
$collection->insert($arr);
$result = $collection->find();
foreach ($result as $document) {
        echo $document["name"]."\n";
        echo $document["desc"]."\n";
}
?>
You can run above script in web server or just run:
php test_mongo.php

Tuesday 17 February 2015

Benchmark redis 2.8.19 with memcache 1.4.22

Having read a few articles on comparing redis with memcache. I decided to benchmark myself.

My setup:
Fedora 21,Nginx 1.6.2,redis 2.8.19,memcache 1.4.22,php-fpm 5.6.4.

My Benchmark script(php)
<?php
    ################# redis ###################################################
    $k=0;
    $t_time=0;
    echo "Benchmark redis : 
"; $redis = new Redis(); $redis->connect('127.0.0.1', 6379); $original_string = 'abcdefghijklmnopqrstuvwxyz'; for ($i=0;$i<=10000;$i++){ $random_string = get_random_string($original_string, 10); $random_string2 = get_random_string($original_string, 10); $a1[$i]=$random_string; $a2[$random_string]=$random_string2; } $tstart=timer_start(); for ($i=0;$i<=10000;$i++){ $redis -> set($a1[$i],$a2[$a1[$i]]); } $t_time=timer_end($tstart); echo "Total Set time: ".$t_time."
"; for ($j=0;$j<10;$j++){ $tstart=timer_start(); for ($i=0;$i<=10000;$i++){ $value=$redis -> get($a1[$i]); } $t_time=timer_end($tstart); echo "Loop $j (Get) time: ".$t_time."
"; $k=$k+$t_time; } echo "Total 10 loop Get time $k
"; ################# memcache ################################################### $k=0; $t_time=0; echo "
Benchmark memcache :
"; $memcache = new Memcache; $memcache->connect('127.0.0.1', 11211) or die ("Cannot connect"); $original_string = 'abcdefghijklmnopqrstuvwxyz'; for ($i=0;$i<=10000;$i++){ $random_string = get_random_string($original_string, 10); $random_string2 = get_random_string($original_string, 10); $a1[$i]=$random_string; $a2[$random_string]=$random_string2; } $tstart=timer_start(); for ($i=0;$i<=10000;$i++){ $memcache->set($a1[$i], $a2[$a1[$i]], false, 20) or die ("Cannot save data"); } $t_time=timer_end($tstart); echo "Total Set time: ".$t_time."
"; for ($j=0;$j<10;$j++){ $tstart=timer_start(); for ($i=0;$i<=10000;$i++){ $value = $memcache->get($a1[$i]); } $t_time=timer_end($tstart); echo "Loop $j (Get) time: ".$t_time."
"; $k=$k+$t_time; } echo "Total 10 loop Get time $k
"; function timer_start() { $time = microtime(); $time = explode(" ", $time); $time = $time[1] + $time[0]; return $time; } function timer_end($start) { $time = microtime(); $time = explode(" ", $time); $time = $time[1] + $time[0]; return $time-$start; } function get_random_string($valid_chars, $length) { $random_string = ""; $num_valid_chars = strlen($valid_chars); for ($i = 0; $i < $length; $i++) { $random_pick = mt_rand(1, $num_valid_chars); $random_char = $valid_chars[$random_pick-1]; $random_string .= $random_char; } return $random_string; } ?>
Benchmark redis : 
Total Set time: 0.12764096260071
Loop 0 (Get) time: 0.175616979599
Loop 1 (Get) time: 0.18264698982239
Loop 2 (Get) time: 0.13450312614441
Loop 3 (Get) time: 0.1352870464325
Loop 4 (Get) time: 0.13448214530945
Loop 5 (Get) time: 0.13312005996704
Loop 6 (Get) time: 0.15526604652405
Loop 7 (Get) time: 0.40630602836609
Loop 8 (Get) time: 0.13408994674683
Loop 9 (Get) time: 0.13564801216125
Total 10 loop Get time 1.726966381073 

Benchmark memcache : 
Total Set time: 0.14455795288086
Loop 0 (Get) time: 0.12623596191406
Loop 1 (Get) time: 0.13038897514343
Loop 2 (Get) time: 0.1564610004425
Loop 3 (Get) time: 0.11394095420837
Loop 4 (Get) time: 0.11402487754822
Loop 5 (Get) time: 0.11300802230835
Loop 6 (Get) time: 0.11119389533997
Loop 7 (Get) time: 0.1115710735321
Loop 8 (Get) time: 0.11155104637146
Loop 9 (Get) time: 0.19941091537476
Total 10 loop Get time 1.28778672218322
The results shows for simple Set and Get, redis is faster. For SET, redis is faster by 12% For GET, memcache is faster by 25%

For other functions,more extensive tests/benchmarks have to perform on both.

Monday 16 February 2015

Install phpredis

After installing redis, let further install the php package for apache or nginx.
To download and compile phpredis:
git clone https://github.com/phpredis/phpredis
cd phpredis
phpize
./configure
make && make install
Include the compiled extension to php.ini:
touch /etc/php.d/redis.ini
echo extension=redis.so > /etc/php.d/redis.ini
If you use Apache (httpd), restart by:
systemctl restart httpd.service

If you use nginx, restart it by:
systemctl restart php-fpm.service
systemctl restart nginx.service

Install latest Redis cache

Redis is an open source advanced key-value cache. It is high performance and often compared to memcached.

This post we look into how to install latest Redis cache onto (say) Fedora 21. It is quite simple, and lets start:

Goto http://redis.io/download, and download latest version using wget.
wget http://download.redis.io/releases/redis-2.8.19.tar.gz
untar/unzip the package
tar -xzvf redis-2.8.19.tar.gz
compile the bundle
cd redis-2.8.19
make
It is good to do a "make test" to test the functionality on your system. For this test you need tcl.

If you don't have tcl, install it by:
yum install tcl
Run make test:
make test
If functionality test passed it will show:
\o/ All tests passed without errors!
Lets install the excutables into /usr/local/bin/.
make install
For Fedora systemctl system, lets create a redis service script.
vi /usr/lib/systemd/system/redis.service

[Unit]
Description=Redis persistent key-value database
After=network.target

[Service]
ExecStart=/usr/local/bin/redis-server /etc/redis.conf --daemonize no
ExecStop=/usr/local/bin/redis-shutdown
User=redis
Group=redis

[Install]
WantedBy=multi-user.target
Move sample redis.conf to /etc.
cp /root/download/redis-2.8.19/redis.conf /etc/redis.conf
Create redis-shutdown script.
vi /usr/local/bin/redis-shutdown
#!/bin/bash
#
# Wrapper to close properly redis and sentinel
test x"$REDIS_DEBUG" != x && set -x

REDIS_CLI=/usr/local/bin/redis-cli

# Retrieve service name
SERVICE_NAME="$1"
if [ -z "$SERVICE_NAME" ]; then
   SERVICE_NAME=redis
fi

# Get the proper config file based on service name
CONFIG_FILE="/etc/$SERVICE_NAME.conf"

# Use awk to retrieve port from config file
PORT=`awk '/^[[:blank:]]*port/ { print $2 }' $CONFIG_FILE`

# Just in case, use default port
if [ "$SERVICE_NAME" = redis ]; then
    PORT=${PORT:-6379}
else
    PORT=${PORT:-26739}
fi

# shutdown the service properly
$REDIS_CLI -p $PORT SHUTDOWN NOSAVE
Enable and start redis service.
systemctl enable redis.service
systemctl start redis.service
Lets try the redis-cli. Set a key and get a key:
[root@fc21 temp]# redis-cli
127.0.0.1:6379> get key1
(nil)
127.0.0.1:6379> set key1 value1
OK
127.0.0.1:6379> get key1
"value1"

Thursday 29 January 2015

Install Nginx (and PHP) server on Fedora

Nginx [engine x] is a free high-perfomance HTTP and reverse proxy server, as well as a mail proxy server, written by Igor Sysoev.

Lets start the installation process:
Stop and disable existing apache.

# systemctl stop httpd.service
# systemctl disable httpd.service 

Install php-fpm

# yum install php-fpm -y

Enable and start php-fpm service:

# systemctl enable php-fpm.service
# systemctl start php-fpm.service

To install Nginx

# yum install nginx -y

Enable and start Nginx service

# systemctl enable nginx.service
# systemctl start nginx.service

Test Nginx

Open up your web browser and navigate to http://localhost/ (example). You will see the default nginx welcome screen.

Configure Nginx

Open the file /etc/nginx/nginx.conf in vi

# vi /etc/nginx/nginx.conf

Set the worker_processes (No. Of CPU’s in your system you want to utilise). To see the no. Of CPU’s, you can use the command “lscpu”. For default you can set to 1.
worker_processes 1;

Example of working nginx.conf:

# For more information on configuration, see:
#   * Official English Documentation: http://nginx.org/en/docs/
#   * Official Russian Documentation: http://nginx.org/ru/docs/

user  nginx;
worker_processes  1;

error_log  /var/log/nginx/error.log;
#error_log  /var/log/nginx/error.log  notice;
#error_log  /var/log/nginx/error.log  info;

pid        /run/nginx.pid;


events {
    worker_connections  1024;
}


http {
    include       /etc/nginx/mime.types;
    default_type  application/octet-stream;

    log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
                      '$status $body_bytes_sent "$http_referer" '
                      '"$http_user_agent" "$http_x_forwarded_for"';

    access_log  /var/log/nginx/access.log  main;

    sendfile        on;
    #tcp_nopush     on;

    #keepalive_timeout  0;
    keepalive_timeout  65;

    #gzip  on;

    index   index.html index.htm;

    # Load modular configuration files from the /etc/nginx/conf.d directory.
    # See http://nginx.org/en/docs/ngx_core_module.html#include
    # for more information.
    include /etc/nginx/conf.d/*.conf;

    #Change below value to suit your need:
    fastcgi_read_timeout 6000s;

    server {
        listen       80 default_server;
        server_name  localhost;
        root         /usr/share/nginx/html;

        #charset koi8-r;

        #access_log  /var/log/nginx/host.access.log  main;

        # Load configuration files for the default server block.
        include /etc/nginx/default.d/*.conf;

        location / {
        }

        # redirect server error pages to the static page /40x.html
        #
        error_page  404              /404.html;
        location = /40x.html {
        }

        # redirect server error pages to the static page /50x.html
        #
        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
        }

        location ~ [^/]\.php(/|$) {
           fastcgi_split_path_info ^(.+?\.php)(/.*)$;
           if (!-f $document_root$fastcgi_script_name) {
              return 404;
           }

           fastcgi_pass 127.0.0.1:9000;
           fastcgi_index index.php;
           include fastcgi_params;           
        }
    }
}

Save and close the file. Restart Nginx service:

# systemctl restart nginx.service

Test PHP

Create a sample “phpinfo.php” file in the nginx document root folder:

# vi /usr/share/nginx/html/phpinfo.php

Append the lines as shown below:



Save and close the file. Restart Nginx service:

# systemctl restart nginx.service

Navigate to http://localhost/testphp.php (example). It will display all the details about PHP,build,setups.

Monday 19 January 2015

Monitoring page faults (page out)

Page-outs can be a sign of trouble. When the kernel detects that memory is running low, it attempts to free up memory by paging out. Though this may happen briefly from time to time, if page-outs are plentiful and constant, the kernel can reach a point where it's actually spending more time managing paging activity than running the applications, and system performance suffers. To monitor page-outs in the system, we can use vmstat. Below script display the amount of free memory for page-out. Ideally it should be zero.
#!/bin/bash
/usr/bin/vmstat | head -3 | tail -n +3 | awk {'print $8'}

[root@fc21 ~]# pageout
0

Monitoring swap space

To monitor the swap space available in the system, we can use free command. Below script display the swap space available in the system.
[root@fc21 ~]# cat /usr/local/sbin/swap
#!/bin/bash
/usr/bin/free | head -3 | tail -n +3 | awk {'print $3'}

[root@fc21 ~]# swap
1648

Monitoring ram

To monitor the free ram available in the system, we can use free command. Below script display the available ram.
#!/bin/bash
/usr/bin/free | head -2 | tail -n +2 | awk {'print $4'}

[root@fc21 ~]# ram
135720

Monitoring active tcp connections

Active tcp connections number brings us light on whether the system can serve the request fast. A high number indicate queuing/processing of requests. This number can be retrieved from netstat.
#!/bin/bash
tcp=$(/bin/netstat -nt | tail -n +3 | grep ESTABLISHED | wc -l)
printf "%s \n" "$tcp"

[root@fc21 ~]# tcp
5

Monitoring diskspace

To monitor the amount of hdd space used in the system, we can use df. Below script display the percentage the disk is used.
[root@fc21 ~]# cat /usr/local/sbin/disk
#!/bin/bash
/bin/df -t ext4 -m | awk {'print $5'} | tail -1

[root@fc21 ~]# disk
10%

Monitoring system load

System load can be obtained from Uptime command. Below script is used to display the last min system load number.
[root@fc21 ~]# cat load
#!/bin/bash
uptime | awk '{print $10}' | awk -F, '{print $1}'

[root@fc21 ~]# load
1.43

Friday 9 January 2015

httpd: segmentation fault (GDB)

During one of my php programming, I encountered a httpd segmentation fault. The error was shown in error_log.
[root@fc21 ~]# tail -f /var/log/httpd/error_log

[Fri Jan 09 08:31:50.859976 2015] [core:notice] [pid 6757] AH00052: child pid 6775 exit signal Segmentation fault (11)
I decided to use GDB to see where the segmentation fault occur.
[root@fc21 ~]# systemctl stop httpd
[root@fc21 ~]# systemctl status httpd
To start GDB.
[root@fc21 ~]# gdb httpd
(gdb) r -DONE_PROCESS
Program received signal SIGSEGV, Segmentation fault.
0x00007fffe7f431e8 in _zend_mm_free_int () from /etc/httpd/modules/libphp5.so
And it displayed the fault occured in zend_mm_free_int. Proceed with a "where".
(gdb) where
#0  0x00007fffe7f431e8 in _zend_mm_free_int ()
   from /etc/httpd/modules/libphp5.so
#1  0x00007fffe7f629ce in convert_to_double ()
   from /etc/httpd/modules/libphp5.so
#2  0x00007fffe0d20ea1 in zif_trader_stoch (ht=,
    return_value=0x7ffff7f257f8, return_value_ptr=,
    this_ptr=, return_value_used=)
    at /root/trader-0.3.0/functions/trader_stoch.c:67
#3  0x00007fffe7f5bf2a in dtrace_execute_internal ()
   from /etc/httpd/modules/libphp5.so
#4  0x00007fffe8019800 in zend_do_fcall_common_helper_SPEC ()
   from /etc/httpd/modules/libphp5.so
#5  0x00007fffe7fa9dd0 in execute_ex () from /etc/httpd/modules/libphp5.so
#6  0x00007fffe7f5bdc8 in dtrace_execute_ex ()
   from /etc/httpd/modules/libphp5.so
#7  0x00007fffe7f6e7c0 in zend_execute_scripts ()
   from /etc/httpd/modules/libphp5.so
#8  0x00007fffe7f0a6cb in php_execute_script ()
   from /etc/httpd/modules/libphp5.so
#9  0x00007fffe801b3c2 in php_handler () from /etc/httpd/modules/libphp5.so
#10 0x00005555555942f0 in ap_run_handler ()
#11 0x0000555555594839 in ap_invoke_handler ()
#12 0x00005555555a966a in ap_process_async_request ()
To continue, enter "cont".
(gdb) cont
Continuing.

Program terminated with signal SIGSEGV, Segmentation fault.
GDB is a useful tool to identify where fault lies. Maybe you could be one to raise a bug in php/httpd/linux with this. And for this problem, I actually found out that I need to cast a decimal in a php variable.
array_push($high,number_format($obj["high"],2,'.',''));

search iomeweekly