forked from Gusto/aptible_datadog_postgres_monitor
-
Notifications
You must be signed in to change notification settings - Fork 0
/
entrypoint.py
61 lines (49 loc) · 1.7 KB
/
entrypoint.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
import os
from urllib.parse import urlparse
from ruamel import yaml
def get_db_instance_dict(db_url, tags):
url_components = urlparse(db_url)
host = url_components.hostname
port = url_components.port
username = url_components.username
password = url_components.password
database_instance = """\
- server: {host}
user: {username}
pass: '{password}'
port: {port}
tags: {tags}
options:
replication: true
galera_cluster: true
extra_status_metrics: true
extra_innodb_metrics: true
extra_performance_metrics: true
schema_size_metrics: false
disable_innodb_metrics: false
""".format(host=host, username=username, password=password, port=port, tags=tags)
return database_instance
def get_database_url_with_index(index):
return os.environ.get('DB_URL_{}'.format(index), None)
def get_database_tags_with_index(index):
tag_environment_key = 'DB_TAGS_{}'.format(index)
return os.environ[tag_environment_key].split(',')
def get_database_instances():
instances = []
database_number = 1
while get_database_url_with_index(database_number):
db = get_database_url_with_index(database_number)
tags = get_database_tags_with_index(database_number)
instances += [get_db_instance_dict(db, tags)]
database_number += 1
return instances
if __name__ == "__main__":
instances = "\n".join(get_database_instances())
yaml_dict = """\
init_config:
instances:
{instances}
""".format(instances=instances)
with open('/etc/datadog-agent/conf.d/mysql.d/conf.yaml', 'w') as outfile:
data = yaml.round_trip_load(yaml_dict, preserve_quotes=True)
yaml.round_trip_dump(data, outfile)