-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrun_servicemon.py
executable file
·152 lines (99 loc) · 3.88 KB
/
run_servicemon.py
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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
#!/bin/env python
import sys
import time
import pprint
import boto3
from datetime import datetime
from botocore.exceptions import ClientError
class run_servicemon():
def __init__(self, region):
# This code starts a run of "servicemon on a "NAVO" EC2 instance.
pp = pprint.PrettyPrinter(indent=4)
# Connect to Region
region_client = boto3.client('ec2', region_name=region)
ssm = boto3.client('ssm', region_name=region)
# Find an instance that can run "servicemon"
starttime = datetime.now()
print('\n' + region + ': Start time: ' + str(starttime) + ' [run_servicemon]')
try:
response = region_client.describe_instances(
Filters = [
{
'Name': 'tag:Name',
'Values': ['NAVO']
}
])
except:
print(region + ': EC2 describe_instances() failed.')
sys.exit(0)
instance_id = -1
reservations = response['Reservations']
for reservation in reservations:
instances = reservation['Instances']
for instance in instances:
instance_id = instance['InstanceId']
state = instance['State']['Name']
if state == 'terminated':
continue
if instance_id == -1:
print(region + ': No "NAVO" instances. [run_servicemon]')
sys.exit(0)
else:
print(region + ': Instance ID: ' + instance_id)
# Send a command to (in this case) one EC2 instance
command = 'cd /home/ec2-user/ServiceMon; ./aws.sh'
timeout = 1800
start_time = time.time()
try:
response = ssm.send_command(
InstanceIds=[instance_id],
DocumentName="AWS-RunShellScript",
Parameters={
'commands': [command],
'executionTimeout': [str(timeout)]
} )
print(region + ': Command "' + command + '" sent to instance ' + instance_id)
command_id = response['Command']['CommandId']
except ClientError as e:
print(region + ': SSM send_command() failed:')
pp.pprint(region + ": " + str(e))
sys.exit(0)
# Poll until command finishes. Servicemon fires up
# a bunch of scripts and these will continue running
# for several minutes.
total = 0
while True:
time.sleep(30)
total = total + 30
try:
list = ssm.list_commands(CommandId=command_id)
except:
print(region + ': SSM list_commands() failed.')
sys.exit(0)
status = list['Commands'][0]['Status']
print(region + ': ' + status + ' [' + str(total) + ']')
if status == 'Pending' or status == 'InProgress':
continue
elif status == 'Failed':
print(region + ': Failed (or timeout)\n')
break
elif status == 'Success':
elapsed_time = time.time() - start_time
print(region + ': Success. Elapsed time: ' + str(elapsed_time))
break
else:
print(region + ': Final status: ' + status)
elapsed_time = time.time() - start_time
print(region + ': Elapsed time: ' + str(elapsed_time))
break
endtime = datetime.now()
print(region + ': End time: ' + str(endtime) + ' [start_region]\n')
def main():
if len(sys.argv) < 2:
print('Usage: run_servicemon.py <region_name>\n')
sys.exit(0)
else:
run_servicemon(sys.argv[1])
sys.exit(0)
if __name__ == "__main__":
main()