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 ##
#!/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 ##
1 comments:
Thanks for providing, Keep share and update more thing with us AWS Online Training
Post a Comment