-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathglobalmon.py
More file actions
225 lines (199 loc) · 8.83 KB
/
globalmon.py
File metadata and controls
225 lines (199 loc) · 8.83 KB
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
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
# implied.
# See the License for the specific language governing permissions and
# limitations under the License.
import logging
from pathlib import Path
from ruamel.yaml import YAML
import ansible_runner
class GlobalmonConfig:
def __init__(self):
self.zone = None
self.image = None
self.ansible_group_name = None
self.environment = None
self.watch_clouds = dict()
self.services = dict()
def __repr__(self):
return (
"Globalmon("
f"zone: {self.zone}; "
f"image: {self.image}; "
f"ansible_group_name: {self.ansible_group_name}; "
f"watch_clouds: {self.watch_clouds};"
")"
)
class GlobalmonManager:
log = logging.getLogger(__name__)
def __init__(self, cloudmon_config):
self.config = cloudmon_config
self.globalmon_configs = dict()
self.process_config()
def process_config(self):
# Process every plugin entry in config matrix
for matrix_entry in self.config.model.matrix:
self.log.debug("Processing %s", matrix_entry)
for plugin in matrix_entry.plugins:
self.log.debug("Processing plugin %s", plugin)
plugin_data = self.config.model.get_plugin_by_name(plugin.name)
if not plugin_data:
self.log.warn("Plugin is not known in cloudmon_plugins")
if plugin_data.type == "globalmon":
# For now we only construct global globalmon config matrix
self.process_plugin_entry(
plugin_data, matrix_entry, plugin
)
def process_plugin_entry(self, plugin_ref, matrix_entry, plugin):
env_name = matrix_entry.env
zone = matrix_entry.monitoring_zone
key = f"{zone}_{env_name}" # Unique key per env + zone
globalmon_config = self.globalmon_configs.setdefault(
key, GlobalmonConfig())
globalmon_config.environment = env_name
globalmon_config.zone = zone
ansible_group_name = plugin.globalmons_inventory_group_name
globalmon_config.ansible_group_name = ansible_group_name
globalmon_config.image = plugin_ref.image
config = None
# Read config file
# TODO: we would most likely have same config - cache?
yaml = YAML()
if self.config.config_dir is not None and Path(
self.config.config_dir, plugin.config
).exists():
with open(Path(self.config.config_dir, plugin.config), "r") as f: # noqa
config = yaml.load(f)
elif Path(plugin.config).exists():
with open(Path(plugin.config), "r") as f:
config = yaml.load(f)
else:
raise RuntimeError("Globalmon config not found. Please either use --config-dir and relative path for globalmon config in cloudmon config OR use --insecure option with full path of globalmon config in cloudmon config") # noqa
globalmon_config.services = config["services"]
globalmon_config.watch_clouds[env_name] = dict(
services=globalmon_config.services,
cloud=plugin.cloud_name,
)
def provision(self, options):
for key, globalmon_config in self.globalmon_configs.items():
self.log.info(
"Provisioning Globalmon in monitoring zone %s for env %s",
globalmon_config.zone,
globalmon_config.environment)
statsd_group_name = self.config.model.get_monitoring_zone_by_name(
globalmon_config.zone
).statsd_group_name
statsd_servers = self.config.inventory[statsd_group_name]["hosts"]
statsd_host_vars = self.config.hostvars(statsd_servers[0])
# internal_address or ansible_host or hostname
statsd_address = statsd_host_vars.get(
"internal_address",
statsd_host_vars.get("ansible_host", statsd_servers[0]),
)
# FOR MORE DETAILED CONFIG FILE USE THIS.
# globalmon_cfg = dict(
# globalmon=dict(
# clouds=[
# {k: dict(services=v["services"])}
# for (k, v) in globalmon_config.watch_clouds.items()
# ],
# socket="/tmp/globalmon.socket",
# zone=globalmon_config.zone,
# ),
# # log=dict(config="/etc/globalmon/logging.conf"),
# metrics=dict(
# statsd=dict(host=statsd_address, port=8125),
# ),
# secure="/etc/globalmon/globalmon-secure.yaml",
# )
environment = globalmon_config.environment
zone = globalmon_config.zone
globalmon_cfg = dict(
services=globalmon_config.services,
statsd=dict(
host=statsd_address,
port=8125,
path=f"globalmon.{environment}.{zone}"))
clouds_creds = []
# Construct list of cloud credentials for required environments
for env, data in globalmon_config.watch_clouds.items():
clouds_creds.append(
self.config.get_env_cloud_credentials(
env_name=env,
zone_name=globalmon_config.zone,
cloud_name=data["cloud"],
)
)
globalmon_secure_cfg = dict(clouds=clouds_creds)
extravars = dict(
globalmon_suffix=environment,
globalmon_group_name=globalmon_config.ansible_group_name,
globalmon_image=globalmon_config.image,
globalmon_config_dir="/home/ubuntu",
globalmon_secure_config_file_name="globalmon-secure.yaml",
globalmon_config=globalmon_cfg,
globalmon_secure_config=globalmon_secure_cfg,
)
r = ansible_runner.run(
private_data_dir=self.config.private_data_dir,
artifact_dir=".cloudmon_artifact",
project_dir=self.config.project_dir.as_posix(),
playbook="install_globalmon.yaml",
inventory=self.config.inventory_path,
extravars=extravars,
verbosity=3,
)
if r.rc != 0:
raise RuntimeError(
f"Error provisioning Globalmon in {zone} for {environment}") # noqa
def stop(self, options):
for _, globalmon_config in self.globalmon_configs.items():
self.log.info(
"Stopping Globalmon in monitoring zone %s",
globalmon_config.zone,
)
environment = globalmon_config.environment
extravars = dict(
globalmon_service_name=f"cloudmon-globalmon_{environment}",
globalmon_group_name=globalmon_config.ansible_group_name,
)
r = ansible_runner.run(
private_data_dir=self.config.private_data_dir,
artifact_dir=".cloudmon_artifact",
project_dir=self.config.project_dir.as_posix(),
playbook="stop_globalmon.yaml",
inventory=self.config.inventory_path,
extravars=extravars,
verbosity=1,
)
if r.rc != 0:
raise RuntimeError("Error stopping Globalmon")
def start(self, options):
for _, globalmon_config in self.globalmon_configs.items():
self.log.info(
"Starting Globalmon in monitoring zone %s",
globalmon_config.zone,
)
environment = globalmon_config.environment
extravars = dict(
globalmon_service_name=f"cloudmon-globalmon_{environment}",
globalmon_group_name=globalmon_config.ansible_group_name,
)
r = ansible_runner.run(
private_data_dir=self.config.private_data_dir,
artifact_dir=".cloudmon_artifact",
project_dir=self.config.project_dir.as_posix(),
playbook="start_globalmon.yaml",
inventory=self.config.inventory_path,
extravars=extravars,
verbosity=1,
)
if r.rc != 0:
raise RuntimeError("Error starting Globalmon")