Tuesday, 7 October 2014

Twemproxy redis

Twemproxy is a fast and single threaded proxy. It supports multiple server pool simultaneously and maintain persistent  connections. It is lightweight and  easy to configure. 



                                                          Installation and Configuration

First I am going to install and configure two redis instances with port 6111 and 6112 then will setup them in Twemproxy.

    * Download the redis  under  /opt
       wget http://redis.googlecode.com/files/redis-2.6.10.tar.gz
        tar -xvzf redis-2.6.10.tar.gz 
        cd redis-2.6.10
        make
        make test

     * Now copy the config file into two files and change port to 6111 and 6112 in that
        cp redis.conf redis_6111.conf 
        cp redis.conf redis_6112.conf 
   
     * Start the two instances 
        cd src/
       ./redis-server /opt/redis-2.6.10/redis_6111.conf 
       ./redis-server /opt/redis-2.6.10/redis_6112.conf 
      
     *  Test the instances
        ./redis-cli -p 6111
        ./redis-cli -p 6112

You may need m4,automake,autoconf , download and install which is supported , You can get them from  the following links -
wget http://files.directadmin.com/services/custombuild/m4-1.4.15.tar.gz
wget http://files.directadmin.com/services/custombuild/automake-1.13.1.tar.gz
wget http://files.directadmin.com/services/custombuild/autoconf-2.68.tar.gz 

Now install the Twemproxy : 

$ git clone https://github.com/twitter/twemproxy.git
$ cd twemproxy
$ autoreconf -fvi
$ ./configure --enable-debug=log
$ make

Now edit the config file located in conf/nutcracker.yml and add the redis instances :
    
beta:
  listen: 127.0.0.1:22122
  hash: fnv1a_64
  hash_tag: "{}"
  distribution: ketama
  auto_eject_hosts: false
  timeout: 400
  redis: true
  servers:
   - 127.0.0.1:6111:1 server1
   - 127.0.0.1:6112:1 server2

Start the Redis servers:
    
$ ./redis-server --port 6111
$ ./redis-server --port 6112

Start twemproxy:
    
$ ./nutcracker -c ../conf/nutcracker.yml -v 11

Now connect to twemproxy with the redis client
    
$ ./redis-cli -p 22122
redis 127.0.0.1:22122> set mykey value
OK
redis 127.0.0.1:22122> get mykey
"value"



So This is a quick start installation guide how we setup Twemproxy redis.
Looking forward your comments and suggestions , Thanks  ! 

Shell Script : How To Write Nrpe plugin

Nrpe plugin to check process state of a service

Here I'm showing you guys how we can make a nrpe plugin using shell script .

This plugin checks if  'haproxy' service running in process state (ps).

Code : 

#!/bin/bash

##### Defining states 

STATE_OK=0
STATE_WARNING=1
STATE_CRITICAL=2
STATE_UNKNOWN=3

#### Checking the service name in process state

ps aux| grep -w haproxy | grep -v "grep"


##### Checking the exit status of above command

if [ $? -eq 0 ]
        then
        echo "OK - haproxy process Running"
                exit $STATE_OK

elif [ $? -eq 1 ]
        then
        echo "CRITICAL - haproxy process NOT running"
                        exit $STATE_CRITICAL

else

        echo "UNKNOWN - Information Not Available"
                                exit $STATE_UNKNOWN

fi

###########
Note : Please don't use exact 'haproxy' word as a script name :P 
Thanks :)

Shell Script : How to convert string into array in linux

In this example I am going to show you how to convert a string ( words separated with same field )  into an array.

String - aaaa|bbbb|cccc|dddd|eeee|ffff|gggg|hhhh|iiii|jjjj|kkkk

Code - 

#!/bin/bash

string="aaaa|bbbb|cccc|dddd|eeee|ffff|gggg|hhhh|iiii|jjjj|kkkk"

#### Building Array

IFS='|' read -a array <<< "$string" ### Building Array

#### Printing Array elements

for i in "${array[@]}"
do
echo "$i"
done

Output  -

# ./array.sh
 
aaaa
bbbb
cccc
dddd
eeee
ffff
gggg
hhhh
iiii
jjjj
kkkk
 


Thanks

Linux Samba Server Configuration

Samba server provides file and print service to SMB (Server Message Block) clients. It allows inter operation b/w Linux/Unix servers and Windows based client. It sets up network shares of Linux/Unix directories.
For windows users it appears as a normal windows folder accessible via the network and Linux/Unix
users can mount the shares directly as part of their file structure.

Here I am giving you step by step guide to setup samba server -
                         
                                   Server Configuration

Step1.  Login as root , install samba and samba-client packages -

# yum -y install samba samba-client 
# chkconfig smb on                                              [ Setting up the smb service 'on'  after every reboot ]  

Step 2. Now I am gonna create a directory which will be shared via samba -

# mkdir /var/sambashare                            
# chmod 777 /var/sambashare

[Create some files in it, so that we can test the configurtion ]
# cd /var/sambashare && touch file{1..10}

Step 3. Now I am creating users anna, rama and rahul (with no login shell). And will provide smb password for the users.

# useradd  -s /sbin/nologin anna 
# useradd  -s /sbin/nologin rama            
# useradd  -s /sbin/nologin rahul  

# smbpasswd -a anna
# smbpasswd -a rama
# smbpasswd -a rahul


Step 4. Edit the mail configuration file /etc/samba/smb.conf and go to the end. Now set the parameters like share name, share path ,valid users , permissions etc. 
....... 
[ sambashare ]
comment = This is for sales dept.
path = /var/sambashare
valid users = anna rama rahul
public = no
writable = yes
 printable = bo
 create mask = 0765

Save the file and exit. Start the smb service.
 # service smb start
                                      Client Side Configuration

For Linux as client -

To see the list Linux samba server in network

# findsmb

To see the shares -
# smbclient -L //<server ip>

* Connecting by mounting -
# mount //<server-ip>/sambashare /mnt -o username=rama
password:

# cd /mnt && ls                                                      [ Find the shared files ] 

* Connecting by smbclient utility -
# smbclient //<server-ip>/smabashare -U anna
password:


For Windows as client -
 Click on start button + run , type \\<server-ip>\sambashare
(provide username and password)

Start writing a shell script

I m gonna discuss about shell scripting in Linux. It’s very useful in your administrative work as it minimizes your work and save time, also does repeated things  (which you may have encountered in daily tasks) . And you can even make your own simple programs and utilities as per you requirement.
A shell script is a set of commands which is written for ‘shell’ (command line interpreter) of an operating system to perform an specific task.  
In Linux there are various types of shells - sh, bash ,ksh, csh , tcsh, fish etc .                              

To start scripting, you first need to have knowledge of some basic terms such as grep, awk, sed, cut etc.
Let's have an example of a simple bash shell script which includes printing a message and command line input for variables
and printing those values-

#!/bin/bash
echo "Please Enter two numeric values" 
read  n1 n2
echo " The value you have entered are $n1 and $n2"
Here the first line is header which indicates path to script engine, in our case it is /bin/bash. It means  bash shell will be used. 
Next we have used 'echo'. It is used to display the message written inside quotes. In third line there is 'read' which is to take input values through command line for the variables (in our case n1 and n2).
And as you have probably guessed we are printing the message and the values for those variables in fourth line ,which we do by using '$' sign. 
  
So, to run that script , just write above lines in a text file,save it (let say test.sh) give execute permission to that file n u are ready to go...
 # chmod +x test.sh
 # ./test.sh