# frozen_string_literal: true
source 'https://supermarket.chef.io'
metadata
Attributes
#
# Cookbook Name:: checkagentconnect
# Attributes:: default
default['CLUSTER']['ENV'] = 'Dev'
Metadata.rb
name 'checkagentconnect'
maintainer 'The Authors'
maintainer_email 'kaushikvhm@gmail.com'
license 'All Rights Reserved'
description 'Installs/Configures checkagentconnect'
long_description 'Installs/Configures checkagentconnect'
version '0.1.0'
chef_version '>= 12.1' if respond_to?(:chef_version)
recipes
#
# Cookbook:: checkagentconnect
# Recipe:: default
#
# Copyright:: 2017, The Authors, All Rights Reserved.
template '/tmp/get-pip.py' do
source 'tmp/get-pip.py.erb'
mode '0755'
end
bash 'Install python pip and awscli' do
code <<-EOH
python /tmp/get-pip.py
pip install awscli
EOH
not_if { ::File.exist?('/tmp/get-pip.py') }
end
template '/tmp/AgentStatusWithInstance.sh' do
source 'tmp/AgentStatusWithInstance.sh.erb'
mode '0755'
end
cron 'AgentNotification' do
hour '*'
minute '*/1'
command "/tmp/AgentStatusWithInstance.sh"
#user centos
end
======================================================================
Templates
#!/bin/bash
getLocalIpaddress=`hostname -I`
arrayIpaddress=($getLocalIpaddress)
validIpaddress=${arrayIpaddress[0]}
#echo $validIpaddress
aws ec2 describe-instances >> /tmp/jsontxt
var=$(jq --arg validIpaddress "$validIpaddress" '.Reservations[].Instances[]|select(.PrivateIpAddress==$validIpaddress)|.InstanceId' jsontxt)
var2=$(echo $var|tr -d '"')
#echo $var2
ECS_CLUSTER=<%= node['CLUSTER']['ENV']%>
CONTAINERS_ID=$(aws ecs list-container-instances --cluster $ECS_CLUSTER --output text --query 'containerInstanceArns')
DATE=$(date +%Y-%m-%d-%H:%M)
for container in $CONTAINERS_ID
do
STATUS=$(aws ecs describe-container-instances --container-instances $container --cluster $ECS_CLUSTER --output json --query 'containerInstances[0].agentConnected')
CHECK_INSTANCE_ID=$(aws ecs describe-container-instances --container-instances $container --cluster $ECS_CLUSTER --output text --query 'containerInstances[0].ec2InstanceId')
#echo "check instance $CHECK_INSTANCE_ID"
if [ "$var2" = "$CHECK_INSTANCE_ID" ]
then
if [ $STATUS == "true" ]
then
#echo $validIpaddress
#echo $var2
Agentversion=$(aws ecs describe-container-instances --container-instances $container --cluster $ECS_CLUSTER --output text --query 'containerInstances[0].versionInfo.agentVersion')
echo $Agentversion
dockerversion=$(aws ecs describe-container-instances --container-instances $container --cluster $ECS_CLUSTER --output text --query 'containerInstances[0].versionInfo.dockerVersion')
#echo $dockerversion
tags=$(jq --arg instanceid "$var2" '.Reservations[].Instances[]|select(.InstanceId==$instanceid)|.Tags[]|select(.Key=="Name")|.Value' /tmp/jsontxt)
#echo $tags
echo -e "Ipaddress=$validIpaddress\n Agentversion=$Agentversion\n dockerversion=$dockerversion\n tags=$tags\n" | mailx -s "ECS-Agent is not connected with the following host" xxxxxxxxxxxx@mail.com
fi
=====================================================================
No comments:
Post a Comment