-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcheck_instance_status.py
More file actions
executable file
·66 lines (51 loc) · 2.43 KB
/
check_instance_status.py
File metadata and controls
executable file
·66 lines (51 loc) · 2.43 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
#!/usr/bin/python3
# check_instance_status.py
# Displays EC2 instance state and health check details for a region.
import boto3
from colorama import Fore, Style, init
init(autoreset=True)
def get_instance_name(tags):
# Loop through the tags and find the 'Name' tag
for tag in tags:
if tag['Key'] == 'Name':
return tag['Value']
return None
def check_instance_status(region):
# Create an EC2 client
ec2_client = boto3.client('ec2', region_name=region)
# Describe instance status
response = ec2_client.describe_instance_status(IncludeAllInstances=True)
instance_statuses = response['InstanceStatuses']
if not instance_statuses:
print(f"{Fore.RED}No instances found.")
return
for instance_status in instance_statuses:
instance_id = instance_status['InstanceId']
instance_state = instance_status['InstanceState']['Name']
system_status = instance_status['SystemStatus']['Status']
instance_status_check = instance_status['InstanceStatus']['Status']
status_details = instance_status['InstanceStatus']['Details']
# Describe instances to get the tags and public IP address
instance_details = ec2_client.describe_instances(InstanceIds=[instance_id])
instance_name = None
public_ip = None
if instance_details['Reservations']:
instance = instance_details['Reservations'][0]['Instances'][0]
instance_name = get_instance_name(instance.get('Tags', []))
public_ip = instance.get('PublicIpAddress')
print(f"{Fore.YELLOW}Instance ID: {Fore.CYAN}{instance_id}")
print(f"{Fore.YELLOW}Instance Name: {Fore.CYAN}{instance_name if instance_name else 'N/A'}")
print(f"{Fore.YELLOW}Instance State: {Fore.CYAN}{instance_state}")
print(f"{Fore.YELLOW}Public IP: {Fore.CYAN}{public_ip if public_ip else 'N/A'}")
print(f"{Fore.YELLOW}System Status: {Fore.CYAN}{system_status}")
print(f"{Fore.YELLOW}Instance Status: {Fore.CYAN}{instance_status_check}")
print(f"{Fore.YELLOW}Status Checks:")
for detail in status_details:
print(f" - {detail['Name']}: {Fore.CYAN}{detail['Status']}")
print(f"{Fore.YELLOW}{'-' * 30}")
def main():
# Specify the AWS region
region = 'us-east-2' # Change this to your desired region
check_instance_status(region)
if __name__ == "__main__":
main()