forked from amazon-connect/amazon-connect-snippets
-
Notifications
You must be signed in to change notification settings - Fork 0
/
multi_account_metrics.py
100 lines (86 loc) · 3.29 KB
/
multi_account_metrics.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
import datetime
from datetime.tz import tzlocal
import boto3
import botocore
# You could use this code to load the instances from SSM parameter store.
# import json
# import os
# ssm = boto3.client("SSM")
# ROLE_INSTANCES_MAP = json.loads(
# ssm.get_parameter(
# Name=os.getenv('PARAMETER_NAME')
# )['Parameter']['Value']
# )
# Mapping of roles (accounts) to connect instances
ROLE_INSTANCES_MAP = {
"arn:aws:iam::<ACCOUNT>:role/<METRICS-ROLE>": [
"arn:aws:connect:<REGION>:<ACCOUNT>:instance/<ID>"
],
"arn:aws:iam::ACCOUNT:role/<METRICS-ROLE>": [
"arn:aws:connect:<REGION>:<ACCOUNT>:instance/<ID>"
],
}
METRICS = [
{"Name": "AGENTS_AFTER_CONTACT_WORK", "Unit": "COUNT"},
{"Name": "AGENTS_AVAILABLE", "Unit": "COUNT"},
{"Name": "AGENTS_ERROR", "Unit": "COUNT"},
{"Name": "AGENTS_NON_PRODUCTIVE", "Unit": "COUNT"},
{"Name": "AGENTS_ON_CALL", "Unit": "COUNT"},
{"Name": "AGENTS_ON_CONTACT", "Unit": "COUNT"},
{"Name": "AGENTS_ONLINE", "Unit": "COUNT"},
{"Name": "AGENTS_STAFFED", "Unit": "COUNT"},
{"Name": "CONTACTS_IN_QUEUE", "Unit": "COUNT"},
{"Name": "CONTACTS_SCHEDULED", "Unit": "COUNT"},
{"Name": "OLDEST_CONTACT_AGE", "Unit": "SECONDS"},
{"Name": "SLOTS_ACTIVE", "Unit": "COUNT"},
{"Name": "SLOTS_AVAILABLE", "Unit": "COUNT"},
]
# could also be CHAT, only one channel at a time per:
# https://docs.aws.amazon.com/connect/latest/APIReference/API_Filters.html#connect-Type-Filters-Channels
CHANNEL = ["VOICE"]
# this is just a helper function for assuming x-account roles
def assumed_role_session(role_arn: str, base_session: botocore.session.Session=None):
base_session = base_session or boto3.session.Session()._session
fetcher = botocore.credentials.AssumeRoleCredentialFetcher(
client_creator=base_session.create_client,
source_credentials=base_session.get_credentials(),
role_arn=role_arn,
extra_args={
# feel free to change this name
"RoleSessionName": "MetricsCollector"
},
)
creds = botocore.credentials.DeferredRefreshableCredentials(
method="assume-role",
refresh_using=fetcher.fetch_credentials,
time_fetcher=lambda: datetime.datetime.now(tzlocal()),
)
botocore_session = botocore.session.Session()
botocore_session._credentials = creds
return boto3.Session(botocore_session=botocore_session)
# collect metrics on all standard queues
def collect_metrics(connect, instance):
queues = [
queue["Arn"]
for queue in connect.list_queues(
InstanceId=instance, QueueTypes=["STANDARD"]
).get("QueueSummaryList", [])
]
if not queues:
print(f"No valid queues found for {instance}")
return []
resp = connect.get_current_metric_data(
InstanceId=instance,
Filters={"Queues": queues, "Channels": CHANNEL},
Groupings=["Queue"],
CurrentMetrics=METRICS,
)
return resp["MetricResults"]
def lambda_handler(event, context):
for role, instances in ROLE_INSTANCES_MAP.items():
session = assumed_role_session(role)
client = session.client("connect")
for instance in instances:
metrics = collect_metrics(client, instance)
# do something with the metrics here
print(metrics)