Showing posts from 2014

Script to monitor any service running or not

There are cases where we want to monitor running services in our servers and if in case they fails take a action according to that..

In the example script we are monitoring PHP, so if your monitoring any other service please adjust your at least "count"parameter accordingly..

Place the script in cron to check the required service..

#!/bin/bash #To count the no of process running count=$(ps -ef | grep -c php) #To show count no for testing echo $count #Comparing the count variable with min required by us if [ $count -lt 5 ]; then echo -e "\nThe php seems to be not running starting it..." #Auto starting the required service if not running, will differ according to your need killall php-fpm /etc/init.d/php-fpm start /etc/init.d/nginx restart #Sending a mail to desired user about the start of service with fetching hostname from AWS echo -e "The PHP was stopped somehow but now it has been restarted in `wget -q -O -…

How to take scheduled back up of AWS RDS snapshots

Amazon provides us with option of Automated backup but if in case you need to take scheduled manual back up than following script can be used..

In the script we are considering the scenario of taking monthly backup on 1st of every month from the daily automated RDS snapshots..

#!/bin/sh # The script to take monthly backup of DB snapshots # By Ravi Gadgil #Following command will find the snapshot identifier from the DB that need to be manually copied.. aws rds describe-db-snapshots --db-instance-identifier DB-server-name1 | grep `date +%Y-%m-01` | grep rds | awk '{ print $8 }' > /tmp/DB-server-name1.txt aws rds describe-db-snapshots --db-instance-identifier DB-server-name2 | grep `date +%Y-%m-01` | grep rds | awk '{ print $8 }' > /tmp/DB-server-name2.txt aws rds describe-db-snapshots --db-instance-identifier DB-server-name3 | grep `date +%Y-%m-01` | grep rds | awk '{ print $8 }' > /tmp/DB-server-name3.txt #Following command will take the snapshot …

Script to create daily AMI of AWS server and delete AMI older than n no of days..

Following Script can be used to create AMI of AWS server daily to keep the backup and delete the AMI older than n no of days including snapshots associated with it..

#!/bin/bash #Script to create AMI of server on daily basis and deleting AMI older than n no of days #By Ravi Gadgilecho -e "----------------------------------\n   `date`   \n----------------------------------"#To create a unique AMI name for this script echo "instance-`date +%d%b%y`" > /tmp/aminame.txtecho -e "Starting the Daily AMI creation: `cat /tmp/aminame.txt`\n"#To create AMI of defined instance aws ec2 create-image --instance-id i-1xxxxx34 --name "`cat /tmp/aminame.txt`" --description "This is for Daily auto AMI creation" --no-reboot | grep -i ami | awk '{print $4}' > /tmp/amiID.txt#Showing the AMI name created by AWS echo -e "AMI ID is: `cat /tmp/amiID.txt`\n"echo -e "Looking for AMI older than 3 days:\n "#Finding AMI older than 3 …

Script to know the servers running in AWS auto scaling group..

Following script can be used to know the servers which are running under AWS auto scaling group..

#!/bin/bash #Script to know thw servers running in AWS auto scaling group #By Ravi Gadgil
aws autoscaling describe-auto-scaling-groups --auto-scaling-group-name autoscaling-group | grep i- | awk '{print $6}' > /tmp/instances.txt
echo "-----------------------------------------------------------------------------------------"
for inc in `cat /tmp/instances.txt`; do aws ec2 describe-instances --instance-ids $inc | grep -ir 'publicdns\|keyname'; echo "-----------------------------------------------------------------------------------------" ;done
In following script we are just looking for hostname and respective login key but it can be further increased according to your need by increasing the parameters in grep..

Following will be the output of the script output:

[root@server auto-data]# ./ -----------------------------------------------…

How to install and Configure lsyncd

Lsyncd is a service which is used to sync data between the directories with in a server or between the servers. It is very useful tool if you want to keep redundancy between the servers. It has bit of delay while it copies data from source to target destination but its very simple to configure and light on server.

There should be password less authentication set between the servers so that they can sync data..

If your looking to get it configured it via yum following will be steps:

[root@server downloads]# yum -y install lua lua-devel pkgconfig gcc asciidoc [root@server downloads]# yum --enablerepo=dag -y install lsyncd   # install from DAG
Create the configuration file:

[root@server downloads]# cp /usr/share/doc/lsyncd/lrsync.lua /etc/lsyncd.conf[root@server downloads]# vi /etc/lsyncd.conf
Following entry needs to be done in the Configuration file:

settings = { statusFile = "/tmp/lsyncd.stat", statusInterval = 1, }sync{ default.rsync, # source directory source="/var/www/ht…

How to install and configure Google Page Speed Module for Nginx

Google Page speed module is very good tool to optimize sites performance by arranging CSS, images, java scripts etc in a better way which results in better performance from site..

Download the Page Speed Module and unzip it

[root@server downloads]# wget
[root@server downloads]# unzip release-
[root@server downloads]# cd ngx_pagespeed-release-
We also need posl to make module work so download and untar it in page speed module directory..

[root@server downloads]# wget
[root@server downloads]# tar -xzvf
Now compile the nginx with page speed module enable in it. Go in to the directory where you have your nginx setup available and configure it with including the page speed module.

[root@theteamie nginx-1.5.7]# ./configure --prefix=/etc/nginx --conf-path=/etc/nginx/nginx.conf --sbin-path=/usr/sbin/nginx --pid-path=/tmp/ng…

Script to automatically create an AMI of server than add it to Auto Scaling Configuration and Group

Following script can help you to create an AMI of running server, than automatically create a auto scaling configuration from newly created AMI and updating the auto scaling group with new auto scaling configuration.

Note: To run this script AWS cli needs to be configured correctly and this script should be run in the server of whom you want to create AMI.

If in case your AWS cli is not configured follow the following steps..


#!/bin/bash #Script to create ami of running instance, make launch Conf from it and than add it to Auto Scaling group #By Ravi Gadgil#To get the current Instance ID wget -q -O - > /tmp/instanceID.txt#To get the current Instance Type wget -q -O - > /tmp/instanceType.txt#To create a unique AMI name for this script echo "test-ami-`date +%d%b%y%H%M`" > /tmp/aminame.txt#To create a unique Launch Conf name for this script echo "test-…

How to install and Configure Amazon Web Services(AWS) cli

Amazon Web Services(AWS) has its own set of command line interface tool which has all the necessary tools to run all AWS commands and is managed by them so updated frequently..

Download the zip folder containing all the required packages:

[root@server downloads]# wget --2014-04-03 19:39:18-- Resolving Connecting to||:443... connected. HTTP request sent, awaiting response... 200 OK Length: 6063815 (5.8M) [application/zip] Saving to: “”
100%[=============================================================>] 6,063,815   1.52M/s   in 8.3s   
2014-04-03 19:39:28 (717 KB/s) - “” saved [6063815/6063815]
Unzip the folder downloaded:

[root@server downloads]# unzip Archive:   inflating: awscli-bundle/install     inflating: awscli-bundle/packages/argparse-1.…

How to remove GlusterFS Volumes

To remove the GlusterFS Volume from the servers following steps need to be followed:

These Steps need to be followed on all the servers which are part of gluster Cluster:

[root@ip-10-138-150-225 ~]# setfattr -x trusted.glusterfs.volume-id /data/share [root@ip-10-138-150-225 ~]# setfattr -x trusted.gfid /data/share [root@ip-10-138-150-225 ~]# service glusterd stop [  OK  ]
[root@ip-10-138-150-225 ~]# cd /data/share/                [root@ip-10-138-150-225 share]# ls -a .  ..  a1  a2  a3  b1  b2  b3  c1  c2  c3  c4  d1  d2  d3  .glusterfs [root@ip-10-138-150-225 share]# rm -rf .glusterfs [root@ip-10-138-150-225 share]# ls a1  a2  a3  b1  b2  b3  c1  c2  c3  c4  d1  d2  d3 [root@ip-10-138-150-225 share]# cd /var/lib/glusterd/ [root@ip-10-138-150-225 glusterd]# ls  glustershd  groups  hooks  nfs  options  peers  vols [root@ip-10-138-150-225 glusterd]# rm -rf * [root@ip-10-138-150-225 glusterd]# service glusterd start Starting glusterd:                                …

How to configure raid 0 in AWS with glusterFS to have high availability

Here is the procedure to configure Raid 0 on AWS's EBS to have high performance and GlusterFS to get High Availability..

This process can be used to get Central Storage in AWS as well as physical servers, as some of the Application needs central storage..

We are using 2 amazon instances with 4 EBS attached in each to configure Raid 0 on them to have good throughput..

Server 1 : ip-10-128-50-246
Server 2 : ip-10-138-150-225

Check the Attached EBS in each of the server:

[ec2-user@ip-10-128-50-246 ~]$ hostname ip-10-128-50-246 [ec2-user@ip-10-128-50-246 ~]$ lsblk NAME  MAJ:MIN RM SIZE RO TYPE MOUNTPOINT xvdb  202:16   0   5G  0 disk xvdc  202:32   0   5G  0 disk xvdd  202:48   0   5G  0 disk xvde  202:64   0   5G  0 disk xvda1 202:1    0   8G  0 disk /
[root@ip-10-138-150-225 ec2-user]# hostname ip-10-138-150-225 [root@ip-10-138-150-225 ec2-user]# lsblk NAME  MAJ:MIN RM SIZE RO TYPE MOUNTPOINT xvdb  202:16   0   5G  0 disk xvdc  202:32   0   5G  0 disk xvdd  202:48   0   5G  …