-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathplaybook_summary_to_dd.py
71 lines (59 loc) · 2.17 KB
/
playbook_summary_to_dd.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
#!/usr/bin/env python3
"""Reads ansible playbook summary and sends them to DataDog
Run with playbook name e.g.
python3 ./playbook_stats_to_dd.py --playbook-file my-playbook-file.yml
"""
import argparse
import os
from datadog import initialize, api
DD_API_KEY = os.environ["DATADOG_API_KEY"]
DD_APP_KEY = os.environ["DATADOG_APP_KEY"]
def parse_arguments():
"""Get parameters passed at runtime"""
parser = argparse.ArgumentParser()
parser.add_argument("--playbook-file",
dest="playbook_file",
default=None,
type=str)
parser.add_argument("--env",
dest="env",
default="dev",
type=str)
if not parser.parse_args().playbook_file:
parser.error(
"No playbook file specified: e.g. --playbook-file my-playbook-file.yml"
)
return parser.parse_args()
def main(): # pylint: disable=W0613
"""Main starting point"""
args = parse_arguments()
options = {"api_key": DD_API_KEY, "app_key": DD_APP_KEY}
print("Initializing DataDog Client...\n")
initialize(**options)
# Open the summary of the playbook run
ansible_log = open("playbook_summary.txt", "r")
metrics = []
while ansible_line := ansible_log.readline():
# someservername-10-100-1-2 : ok=145
# changed=0 unreachable=0 failed=0
# skipped=96 rescued=0 ignored=0
host_name = ansible_line.split()[0]
stats_string = ansible_line.split(":")[1]
stats_dict = dict(x.split("=") for x in stats_string.split())
for key, value in stats_dict.items():
metrics.append(
{
"type": "metric",
"metric": "ansible.tasks." + key,
"points": int(value),
"host": host_name,
"tags": [
f"playbook:{args.playbook_file.split('.')[0]}",
],
}
)
print(metrics)
response = api.Metric.send(metrics)
print(response)
if __name__ == "__main__": # pylint: disable=W0621, C0103, W0613
main()