Tuesday, September 22, 2015

Nagios Port moniter as service templete

define service{
        use                             local-service         ; Name of service template to use
        host_name                       hostname
        service_description             Nodejs
        check_command                   check_tcp!3000
        notifications_enabled           0
        }

define service{
        use                             local-service         ; Name of service template to use
        host_name                       hostname
        service_description             kafka
        check_command                   check_tcp!9092
        notifications_enabled           0
        }


yum install httpd php php-cli gcc glibc glibc-common gd gd-devel net-snmp
wget http://epel.mirror.net.in/epel/6/i386/epel-release-6-8.noarch.rpm
rpm -Uvh epel-release-6-8.noarch.rpm
yum install nagios*

vi /etc/sysconfig/iptables
-A INPUT -m state --state NEW -m tcp -p tcp --dport 80 -j ACCEPT
service iptables restart

vi /etc/nagios/objects/contacts.cfg
email                         user@example.com ;

vi /etc/httpd/conf.d/nagios.conf
## Comment Lines 15 & 16 ##
#   Order allow,deny
#   Allow from all

## Uncomment and Change lines 17,18 & 19 as shown below ##
Order deny,allow
Deny from all
Allow from 127.0.0.1 10.50.249.0/24

htpasswd /etc/nagios/passwd nagiosadmin
New password:
Re-type new password:
Updating password for user nagiosadmin

service nagios start
# service httpd start
# chkconfig nagios on
# chkconfig httpd on

http://nagios-server-ip/nagios

vi /etc/nagios/nagios.cfg

cfg_dir=/etc/nagios/servers

mkdir /etc/nagios/servers

Put
Client End

yum install nrpe nagios-plugins-all openssl

vi /etc/nagios/nrpe.cfg
## Line 81 - Add the Nagios server IP ##
allowed_hosts=127.0.0.1 10.50.249.0/24


service nrpe start
chkconfig nrpe on


Sunday, September 13, 2015

Create User for Ec2 Instance

For CentoS

From root access user press below command

adduser username
passwd username
visudo
cd /home/username/

su username

ssh-keygen -b 1024 -f username -t dsa
mkdir .ssh
chmod -Rv 700 .ssh/
cat username.pub > .ssh/authorized_keys
chmod -Rv 600 .ssh/authorized_keys
chown username:ec2-user .ssh

Now pass pem file to user.

For Ubuntu

Managing User Accounts on Your Linux Instance

[ec2-user ~]$ sudo adduser newuser

[ec2-user ~]$ sudo su - newuser

[newuser ~]$ mkdir .ssh

[newuser ~]$ chmod 700 .ssh

[newuser ~]$ touch .ssh/authorized_keys

[newuser ~]$ chmod 600 .ssh/authorized_keys

Edit the authorized_keys file with your favorite text editor and paste the public key for your key pair (pem file open in puttygen and pase ppk file into the file

[newuser ~]$ sudo su

[ec2-user ~]$  vi /etc/sudoers.d/90-cloudimg-ubuntu

newuser ALL=(ALL) NOPASSWD:ALL

[ec2-user ~]$ sudo userdel -r olduser

Install PHP 5.3 or higher

git clone git://github.com/amazonwebservices/aws-sdk-for-php.git AWSSDKforPHP

cd AWSSDKForPHP

cp config.inc-sample.php  config.inc.php

vi config.inc.php

'key' => 'development-key',
'secret' => 'development-secret',

above two variable mention AWS managment conclose login key and password in encrypted format which information you get from securites IAM user.

Download phpmailer also

Now make one backup script  like below i got from google search



/**************************************************************************
|
|   Script to Automate EBS Backups
|   Run this script with CRON or whatever every X period of time to take
|   automatic snapshots of your EBS Volumes.  Script will delete old
|   snapshot after Y period of time
|
|   Version 1.01 updated 2012-08-02
|
|   Copyright 2012 Caleb Lloyd
|   http://www.caleblloyd.com/
|
|   I offer no warrant or guarentee on this code - use at your own risk
|   You are free to modify and redistribute this code as you please
|
|   Requires AWS PHP SDK be configured for your AWS Account:
|       http://aws.amazon.com/sdkforphp/
|
|   Optional PHPMailer Support to email results to yourself
|       http://phpmailer.worxware.com/
|
|   Stores snapshot information in "./snapshot_information.json"
|       Make sure PHP can write this file
|
**************************************************************************/


/**************************************************************************
|   Begin Configuration
**************************************************************************/

//Declare the volumes that you want to backup
//The Volume ID's are the keys of the array, you can store any custom information you
//want in value array, or just keep it blank.  Make sure you keep it as a blank array
//because the script will fillthis up with values...
$volumes=array( '111111111'=>array(),
//                'vol-22222222'=>array()
);

//Do not take a snapshot more than every X hours/minutes/days, etc. (uses strtotime)
//This prevents the script from running out of control and producing tons of snapshots
$snapshot_limit = '23 hours';

//Keep snapshots for this amount of time (also uses strtotime)
$keep_snapshots = '7 days 12 hours';

//Your path to the Amazon AWS PHP SDK
require_once 'path of  aws sdk/sdk.class.php';
//EC2 Region, view path of  aws sdk/services/services/ec2.class.php for definitions
$region='region which get in above file';

//Your path to PHP Mailer (if you don't want to eamil yourself the results, you can get rid of this)
require_once('php mailer path/class.phpmailer.php');
//Go to bottom of script to configure PHP Mailer settings


/**************************************************************************
|   End Configuration
**************************************************************************/

function snapshot_info($s)
{
    $info='';
    $info.='Volume: '.$s['volume'].'
';
    $info.=(!empty($s['volume_name'])?'Volume Name: '.$s['volume_name'].'
':'');
    $info.=(!empty($s['snapshot'])?'Snapshot: '.$s['snapshot'].'
':'');
    $info.=(!empty($s['instance'])?'EC2 Instance: '.$s['instance'].'
':'');
    $info.=(!empty($s['device'])?'Device: '.$s['device'].'
':'');
    $info.=(!empty($s['error'])?'Error: '.$s['error'].'
':'');
    $info.=(!empty($s['datetime'])?'Date/Time: '.$s['datetime'].'
':'');
    $info.='
';
    return $info;
}

$success=array();
$failure=array();
$preserve=array();
$success_delte=array();
$failure_delete=array();

$ec2 = new AmazonEC2();
$ec2 = $ec2->set_region($region);

$latest_snapshot=array();

if (file_exists('snapshot_information.json'))
    $json=file_get_contents('snapshot_information.json');
else
    $json='[]';
$snapshots=json_decode($json,TRUE);
foreach ($snapshots as $s)
{
    if (!empty($lastest_snapshot[$s['volume']]))
    {
        if ($s['timestamp']>$lastest_snapshot[$s['volume']]['timestamp'])
        {
            $lastest_snapshot[$s['volume']]=$s;
        }
    }
    else
    {
        $lastest_snapshot[$s['volume']]=$s;
    }
}

foreach ($volumes as $volume => $v)
{
    $v['volume']=$volume;
    $v['instance']='Not Attached to an Instance';

    $volume_information = $ec2->describe_volumes(array('VolumeId' => $volume));
    $v['volume_name'] = '(volume has no tags)';
    if (!empty($volume_information->body->volumeSet->item->tagSet->item->value))
    {
        $v['volume_name'] = (string)$volume_information->body->volumeSet->item->tagSet->item->value;
    }
    $description = 'Volume '.$volume.(empty($v['volume_name'])?'':' ('.$v['volume_name'].')');
   
    if (!empty($volume_information->body->volumeSet->item->attachmentSet->item->status))
    {
        if ($volume_information->body->volumeSet->item->attachmentSet->item->status == "attached")
        {
            $v['device'] = (string)$volume_information->body->volumeSet->item->attachmentSet->item->device;
            $v['instance'] = (string)$volume_information->body->volumeSet->item->attachmentSet->item->instanceId;
            $description.=' attached to '.$v['instance'].' as '.$v['device'];
        }
    }
    else
    {
        $description.= ' ('.$v['instance'].')';
    }
   
    if ((!empty($lastest_snapshot[$volume]))&&($lastest_snapshot[$volume]['timestamp']>strtotime('-'.$snapshot_limit)))
    {
        $error=TRUE;
        $v['datetime']=date('Y-m-d H:i:s');
        $v['timestamp']=time();
        $v['error']='An Automatic Snapshot Already Exists for that volume in the past '.$snapshot_limit;
        $failure[]=$v;
    }
    else
    {
        $response = $ec2->create_snapshot($volume, array('Description'=>$description));
        if ($response->isOK())
        {
            $v['datetime']=date('Y-m-d H:i:s');
            $v['timestamp']=time();
            $v['snapshot']=(string)$response->body->snapshotId;
            $success[$v['snapshot']]=$v;
 // adding the tag :
            $responseTag = $ec2->create_tags ( $v['snapshot'], array ( 'Key'=>'Name', 'Value'=>$v['volume_name'] ) );
        }
        else
        {
            $error=TRUE;
            $v['datetime']=date('Y-m-d H:i:s');
            $v['timestamp']=time();
            $v['error']=(string)$response->body->Errors->Error->Message;
            $failure[]=$v;
        }
    }
}

if (!empty($snapshots))
{
    foreach ($snapshots as $snapshot => $s)
    {
        $s['snapshot']=$snapshot;
        if ($s['timestamp']        {
            $response = $ec2->delete_snapshot($snapshot);
            if ($response->isOK())
            {
                $success_delete[$snapshot]=$s;
            }
            else
            {
                $error=TRUE;
                $s['error']=(string)$response->body->Errors->Error->Message;
                $failure_delete[$snapshot]=$s;
            }
        }
        else
        {
            $preserve[$snapshot]=$s;
        }
    }
    $snapshots_json=json_encode(array_merge($success,$preserve));
}
else
{
    $snapshots_json=json_encode($success);
}
file_put_contents('snapshot_information.json',$snapshots_json);

$message='';

if (!empty($success))
{
    $message.='The following Snapshots Succeeded:
';
    foreach ($success as $v)
    {
        $message.=snapshot_info($v);
    }
}

if (!empty($failure))
{
    $message.='The following Snapshots Failed and had Errors:
';
    foreach ($failure as $v)
    {
        $message.=snapshot_info($v);
    }
}

if (!empty($success_delete))
{
    $message.='The following old Snapshots were removed:
';
    foreach ($success_delete as $v)
    {
        $message.=snapshot_info($v);
    }
}

if (!empty($failure_delete))
{
    $message.='The following old Snapshots had errors while trying to remove:
';
    foreach ($failure_delete as $v)
    {
        $message.=snapshot_info($v);
    }
}

if (!empty($preserve))
{
    $message.='The following Snapshots were preserved:
';
    foreach ($preserve as $v)
    {
        $message.=snapshot_info($v);
    }
}
?>



Tuesday, September 8, 2015

how to configure reverse proxy in iis

Install URL_Rewrite model in IIS 

Create domain in iis which name you have to show in browser.  Go to that domain and in feature view open URL_Rewrite.  click on Revers proxy  it will ask for Application request routing too be enable press ok   it will open Add Revers proxy rule box.  Put tomcat url or other url where domain should point. example localhost:8080

And outbond rules put domain name. it should work means some domain request come to www.example.com it will search from local tomcat server.

 After disable outboundRules, application links redirect to internal web app url.

First take volume snapshot of server. then launch new instance.  login instance and shutdown same

copy paste  Root device Name e.g "/dev/xvda" . Now remove volume of that new instance. and make volume from snapshot which want to recover and attached that new created volume to instance with name of " Root device Name e.g "/dev/xvda"   that its.

First login account  select instance then go to Action tab --- image-- create image go to -- AMIs select recent created snapshot Action --- Modify Image Permission and --- Add (second) AWS Account Number and --save.

Now go to second account Launch Instance ---- My AIMs -- you will get that instance name just select same and continue wizard.

If worst case you face any issue just make snapshot modify image as public.