Skip to content

Instantly share code, notes, and snippets.

@dispera
Last active July 2, 2019 13:37
Show Gist options
  • Save dispera/243fa6c8fee3d9b41694c259f4f5faad to your computer and use it in GitHub Desktop.
Save dispera/243fa6c8fee3d9b41694c259f4f5faad to your computer and use it in GitHub Desktop.
Script to check ECS agents down on the specified regions
#!/usr/bin/python3
#
# This will output the EC2 instance id of the ECS cluster instances
# which have the ECS agent down, on the region this saltmaster runs on.
#
# Requires: sudo apt install python3-pip; pip3 install boto3
#
# You can use this with https://github.com/mheffner/awsam
# for easy AWS vars management, eg:
# aem use pingdom-prod
# aenv python3 ecs-agents-monitor.py
import boto3
import syslog
import datetime
now = datetime.datetime.now()
regions = ['eu-west-1','us-east-1']
for region in regions:
client = boto3.client('ecs',region_name=region)
ecs_clusters = client.list_clusters()
# Look for instances with the ECS agent down
# Iterate through the ECS clusters
for ecs_cluster in ecs_clusters['clusterArns']:
instances = client.list_container_instances(
cluster=ecs_cluster,
status='ACTIVE'
)
# Iterate through the ECS Instances in the cluster
for instance in instances['containerInstanceArns']:
desc_instances = client.describe_container_instances(
cluster=ecs_cluster,
containerInstances=[instance]
)
# Check if the instance has the ECS agent down
if desc_instances['containerInstances'][0]['agentConnected'] == False:
syslog.syslog('ecs-agents-monitor - Time: {}'.format(now))
syslog.syslog('ecs-agents-monitor - ECS Cluster: {}'.format(ecs_cluster.split('/')[1]))
syslog.syslog('ecs-agents-monitor - ECS Container Instance: {}'.format(instance.split('/')[1]))
syslog.syslog('ecs-agents-monitor - EC2 Instance ID: {}'.format(desc_instances['containerInstances'][0]['ec2InstanceId']))
syslog.syslog('ecs-agents-monitor - ECS Agent Version: {}'.format(desc_instances['containerInstances'][0]['versionInfo']['agentVersion']))
syslog.syslog('ecs-agents-monitor - ECS Agent Status: {}'.format(desc_instances['containerInstances'][0]['agentConnected']))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment