Wednesday, March 28, 2018

Dynamic-EC2-Instnace-Add

 Make shell script Dynamic-EC2-Instnace-Add.sh

#!/bin/bash
#
# Author : Arun
# Version : 1.0
# Date : 1-Dec-2017
# Description : Check EC2 instances dynamically via nagios of the Project and create non available nagios host


### Set AWS KEY of IAM user which can fetch the ELB information.
export AWS_ACCESS_KEY_ID=
export AWS_SECRET_ACCESS_KEY=

### Give the AWS region name where the ELB exist
_AWS_REGION_NAME=us-east-1

### Absolute path of nagios config directory set for Autoscaling Group
_CURRENT_DIR=/usr/local/nagios/etc/autoscale

####  Absolute path of nagios Junk directory set for Autoscaling Group
_JUNK_DIR=/usr/local/nagios/libexec/autoscale

## Read all AWS account instance (name, public ip and state) ###
aws ec2 describe-instances --query 'Reservations[*].Instances[*].[Tags[?Key==`Name`].Value|[0],State.Name,PublicIpAddress]' --output text | column -t > $_JUNK_DIR/as8.txt

### Function Assigned
nagiosconfigfile () {
    echo "define host{"                                                 >> $_CURRENT_DIR/host.cfg
    echo "  host_name                 $_MY_SERVER_NAME"                 >> $_CURRENT_DIR/host.cfg
    echo "  use                       Servers-Linux"                    >> $_CURRENT_DIR/host.cfg
    echo "  alias                     $_MY_SERVER_NAME"                 >> $_CURRENT_DIR/host.cfg
    echo "  address                   $_MY_IP"                          >> $_CURRENT_DIR/host.cfg
    echo "  _HOST_ID                  101$_COUNT"                       >> $_CURRENT_DIR/host.cfg
    echo "  hostgroups                Linux-Servers,Services"   >> $_CURRENT_DIR/host.cfg
    echo "  check_command             check_host_alive_10022"           >> $_CURRENT_DIR/host.cfg
    echo "  max_check_attempts        3"                                >> $_CURRENT_DIR/host.cfg
    echo "  check_interval            1"                                >> $_CURRENT_DIR/host.cfg
    echo "  retry_interval            2"                                >> $_CURRENT_DIR/host.cfg
    echo "  check_period              24x7"                             >> $_CURRENT_DIR/host.cfg
    echo "  contact_groups           ,slackadmins"              >> $_CURRENT_DIR/host.cfg
    echo "  notification_interval     5"                                >> $_CURRENT_DIR/host.cfg
    echo "  first_notification_delay  1"                                >> $_CURRENT_DIR/host.cfg
    echo "  notification_period       24x7"                             >> $_CURRENT_DIR/host.cfg
    echo "  notification_options      d,u,r"                            >> $_CURRENT_DIR/host.cfg
    echo "  notifications_enabled     1"                                >> $_CURRENT_DIR/host.cfg
    echo "}"                                                            >> $_CURRENT_DIR/host.cfg
    echo ""                                                             >> $_CURRENT_DIR/host.cfg
}


RWR="no"

#### MK is variable ###

while read q; do
    arr=($q)
    MK1=${arr[0]}        #This will be my Server Name
    MK2=${arr[1]}        #This will show running/stoped State
    MK3=${arr[2]}        #This is Server IP Address   
   
    if grep -r "$MK3" $_CURRENT_DIR/host.cfg
    then
        # code if found
        ABC="aws"
               if [[ $MK2 != "running" ]]; then
                RWR="yes"
                fi
    else   
        if [[ $MK2 == "running" ]]; then         
        RWR="yes"
        fi
    fi
done <$_JUNK_DIR/as8.txt

F1=$(sed -n '$=' $_JUNK_DIR/as10.txt)
F2=$(sed -n '$=' $_JUNK_DIR/as8.txt)

echo $F1
echo $F2

if [[ $F1 != $F2 ]]
then
    RWR="yes"
fi



if [[ $RWR == "yes" ]]; then
    cat /dev/null > $_CURRENT_DIR/host.cfg
    cat /dev/null > $_JUNK_DIR/as10.txt
    counter=0
### AS is veriable ##

    while read s; do
        arr=($s)
        AS1=${arr[0]}
        AS2=${arr[1]}
        AS3=${arr[2]}
   
        if [[ $AS2 == "running" ]]; then     
            let counter++
            _MY_IP=$AS3
            _MY_SERVER_NAME=$AS1
            _COUNT=$counter

            ## echo "My IP $q">>host.cfg
            echo $_MY_IP >> $_JUNK_DIR/as10.txt
            nagiosconfigfile;
        fi       

    done <$_JUNK_DIR/as8.txt

    #systemctl reload nagios
    #service nagios restart
#then
fi

### Unset the variable value AWS_ACCESS_KEY_ID and AWS_SECRET_ACCESS_KEY
unset AWS_ACCESS_KEY_ID
unset AWS_SECRET_ACCESS_KEY

## End Of Line ##

Download pflogsumm
  •  wget http://jimsun.linxnet.com/downloads/pflogsumm-1.1.1.tar.gz
  •  tar -zxf pflogsumm-1.1.1.tar.gz
  • cd pflogsumm-1.1.1

Generate the statistics  :-
#  cat /var/log/maillog | ./pflogsumm.pl

 Grand Totals
------------
messages

   8596   received
   8606   delivered
      0   forwarded
      0   deferred
      0   bounced
      0   rejected (0%)
      0   reject warnings
      0   held
      0   discarded (0%)

  77044k  bytes received
  77324k  bytes delivered
      7   senders
      2   sending hosts/domains
     13   recipients
      4   recipient hosts/domains


Per-Day Traffic Summary
    date          received  delivered   deferred    bounced     rejected
    --------------------------------------------------------------------
    Mar 24 2018         3          3
    Mar 25 2018       215        208
    Mar 26 2018       302        298
    Mar 27 2018       289        307
    Mar 28 2018      7787       7790
For server configuration
 
wget https://www.stunnel.org/downloads/stunnel-5.44.tar.gz
tar -zxvf stunnel-5.44.tar.gz
cd stunnel-5.44
yum install gcc
yum install openssl openssl-devel gcc
./configure
make
make install
cd /usr/bin/
ln -s /usr/local/bin/stunnel stunnel
cd /etc/
ln -s /usr/local/etc/stunnel stunnel

cd stunnel-5.44/tools
./makecert.sh

cd /usr/local/etc/stunnel
cp stunnel-5.44/tools/stunnel.pem .

vi /etc/hosts
server ip  localhost localhost.localdomain localhost4 localhost4.localdomain4

start at server
vi /usr/local/etc/stunnel/stunnel.conf
debug = 7
output = stunnel.log
cert = stunnel.pem
[mysql]
accept = localhost:10001
connect = 127.0.0.1:3306

[apache]
accept = localhost:8080
connect = 127.0.0.1:80

/usr/bin/stunnel /usr/local/etc/stunnel/stunnel.conf

Open port 10001 & 8080 in firewall


For client configuration

wget https://www.stunnel.org/downloads/stunnel-5.44.tar.gz
tar -zxvf stunnel-5.44.tar.gz
cd stunnel-5.44
yum install gcc
yum install openssl openssl-devel gcc
./configure
make
make install
cd /usr/bin/
ln -s /usr/local/bin/stunnel stunnel
cd /etc/
ln -s /usr/local/etc/stunnel stunnel
copy paste stunnel.pem file from server
start at client end
vi /usr/local/etc/stunnel/client.conf

debug = 7
output = stunnel-server.log
cert = stunnel.pem
client = yes
[mysql]
accept = 127.0.0.1:6004
connect = EIP server ip :10001

[apache]
accept = 127.0.0.1:80
connect = EIP server ip:8080

/usr/bin/stunnel  /usr/local/etc/stunnel/client.conf

For testing
mysql -u root -p -h 127.0.0.1 -P 6004

elinks : http://127.0.0.1