in source/msam/chalicelib/periodic.py [0:0]
def process_ssm_run_command(event):
"""
Processes the results from running an SSM command on a managed instance.
"""
event_dict = event.to_dict()
instance_id = event_dict['detail']['instance-id']
command_name = event_dict['detail']['document-name']
command_status = event_dict['detail']['status']
cw_client = boto3.client('cloudwatch', config=MSAM_BOTO3_CONFIG)
log_client = boto3.client('logs', config=MSAM_BOTO3_CONFIG)
dimension_name = "Instance ID"
metric_name = command_name
status = 0
try:
# test to make sure stream names are always of this format, esp if you create your own SSM document
log_stream_name = event_dict['detail']['command-id'] + "/" + instance_id + "/aws-runShellScript/stdout"
response = log_client.get_log_events(
logGroupName=SSM_LOG_GROUP_NAME,
logStreamName=log_stream_name,
)
#print(response)
if command_status == "Success":
# process document name (command)
if "MSAMElementalLiveStatus" in command_name:
metric_name = "MSAMElementalLiveStatus"
for log_event in response['events']:
if "running" in log_event['message']:
status = 1
break
elif "MSAMSsmSystemStatus" in command_name:
metric_name = "MSAMSsmSystemStatus"
status = 1
elif "MSAMElementalLiveActiveAlerts" in command_name:
metric_name = "MSAMElementalLiveActiveAlerts"
root = ET.fromstring(response['events'][0]['message'])
status = len(list(root))
if status == 1 and root[0].tag == "empty":
status = 0
else:
if "MSAMElementalLiveCompletedEvents" in command_name:
metric_name = "MSAMElementalLiveCompletedEvents"
elif "MSAMElementalLiveErroredEvents" in command_name:
metric_name = "MSAMElementalLiveErroredEvents"
elif "MSAMElementalLiveRunningEvents" in command_name:
metric_name = "MSAMElementalLiveRunningEvents"
root = ET.fromstring(response['events'][0]['message'])
status = len(root.findall("./live_event"))
else:
# for the elemental live status, the command itself returns a failure if process is not running at all
# which is different than when a command fails to execute altogether
if command_status == "Failed" and "MSAMElementalLiveStatus" in command_name:
for log_event in response['events']:
if "Not Running" in log_event['message'] or "Active: failed" in log_event['message']:
metric_name = "MSAMElementalLiveStatus"
break
else:
# log if command has timed out or failed
print("SSM Command Status: Command %s sent to instance %s has %s" % (command_name, instance_id, command_status))
# create a metric for it
status = 1
metric_name = "MSAMSsmCommand"+command_status
cw_client.put_metric_data(
Namespace = SSM_LOG_GROUP_NAME,
MetricData = [
{
'MetricName': metric_name,
'Dimensions': [
{
'Name' : dimension_name,
'Value' : instance_id
},
],
"Value": status,
"Unit": "Count"
}
]
)
except ClientError as error:
print(error)
print("SSM Command Status: Command %s sent to instance %s has status %s" % (command_name, instance_id, command_status))
print("Log stream name is %s" % (log_stream_name))