blob: b06f448684eae8a6277f83d5f01d50873a75797c [file] [log] [blame]
bravofc973b572020-10-21 16:58:50 -03001#!/usr/bin/env python
2
3# Copyright 2021 Whitestack, LLC
4# *************************************************************
5
6# This file is part of OSM Monitoring module
7# All Rights Reserved to Whitestack, LLC
8
9# Licensed under the Apache License, Version 2.0 (the "License"); you may
10# not use this file except in compliance with the License. You may obtain
11# a copy of the License at
12
13# http://www.apache.org/licenses/LICENSE-2.0
14
15# Unless required by applicable law or agreed to in writing, software
16# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
17# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
18# License for the specific language governing permissions and limitations
19# under the License.
20
21# For those usages not covered by the Apache License, Version 2.0 please
22# contact: fbravo@whitestack.com
23##
24
25import os
26import pymongo
27import yaml
28import aiohttp
29import asyncio
30import copy
31import json
bravof9af7d422021-11-23 17:21:58 -030032import time
bravofc973b572020-10-21 16:58:50 -030033from bson.json_util import dumps
34from bson import ObjectId
35
garciadeblasefa4c2b2022-09-07 22:35:53 +020036# Env variables
37mongodb_url = os.environ["MONGODB_URL"]
38target_database = os.environ["TARGET_DATABASE"]
39prometheus_config_file = os.environ["PROMETHEUS_CONFIG_FILE"]
garciadeblas09eaa922022-09-07 22:57:53 +020040prometheus_base_config_file = os.environ["PROMETHEUS_BASE_CONFIG_FILE"]
garciadeblasefa4c2b2022-09-07 22:35:53 +020041prometheus_url = os.environ["PROMETHEUS_URL"]
42
bravofc973b572020-10-21 16:58:50 -030043
44def get_jobs(client):
garciadeblasefa4c2b2022-09-07 22:35:53 +020045 return json.loads(dumps(client[target_database].prometheus_jobs.find({})))
46
bravofc973b572020-10-21 16:58:50 -030047
48def save_successful_jobs(client, jobs):
garciadeblasefa4c2b2022-09-07 22:35:53 +020049 for job in jobs:
50 client[target_database].prometheus_jobs.update_one(
garciadeblas84fe31f2022-11-15 14:12:48 +010051 {"_id": ObjectId(job["_id"]["$oid"])}, {"$set": {"is_active": True}}
garciadeblasefa4c2b2022-09-07 22:35:53 +020052 )
53
bravofc973b572020-10-21 16:58:50 -030054
55def clean_up_job(prometheus_job):
garciadeblasefa4c2b2022-09-07 22:35:53 +020056 cleaned_prometheus_job = copy.deepcopy(prometheus_job)
57 # take out _id and internal keys
58 cleaned_prometheus_job.pop("_id", None)
59 cleaned_prometheus_job.pop("is_active", None)
60 cleaned_prometheus_job.pop("vnfr_id", None)
61 cleaned_prometheus_job.pop("nsr_id", None)
62 return cleaned_prometheus_job
63
bravofc973b572020-10-21 16:58:50 -030064
65def generate_prometheus_config(prometheus_jobs, config_file_path):
garciadeblas09eaa922022-09-07 22:57:53 +020066 with open(config_file_path, encoding="utf-8", mode="r") as config_file:
67 config_file_yaml = yaml.safe_load(config_file)
garciadeblasefa4c2b2022-09-07 22:35:53 +020068 if config_file_yaml is None:
69 config_file_yaml = {}
garciadeblas09eaa922022-09-07 22:57:53 +020070 if "scrape_configs" not in config_file_yaml:
garciadeblasefa4c2b2022-09-07 22:35:53 +020071 config_file_yaml["scrape_configs"] = []
garciadeblasefa4c2b2022-09-07 22:35:53 +020072
garciadeblasbef284e2022-11-18 00:55:44 +010073 prometheus_jobs_to_be_added = []
74
garciadeblasefa4c2b2022-09-07 22:35:53 +020075 for prometheus_job in prometheus_jobs:
76 cleaned_up_job = clean_up_job(prometheus_job)
garciadeblasbef284e2022-11-18 00:55:44 +010077 job_to_be_added = True
78 for sc in config_file_yaml["scrape_configs"]:
79 if sc.get("job_name") == cleaned_up_job.get("job_name"):
80 job_to_be_added = False
81 break
82 if job_to_be_added:
83 prometheus_jobs_to_be_added.append(cleaned_up_job)
84
85 for job in prometheus_jobs_to_be_added:
86 config_file_yaml["scrape_configs"].append(job)
garciadeblasefa4c2b2022-09-07 22:35:53 +020087
bravofc973b572020-10-21 16:58:50 -030088 return config_file_yaml
89
bravofc973b572020-10-21 16:58:50 -030090
garciadeblasefa4c2b2022-09-07 22:35:53 +020091async def reload_prometheus_config(prom_url):
92 async with aiohttp.ClientSession() as session:
93 async with session.post(prom_url + "/-/reload") as resp:
94 if resp.status > 204:
95 print(f"Error while updating prometheus config: {resp.text()}")
96 return False
97 await asyncio.sleep(5)
98 return True
bravofc973b572020-10-21 16:58:50 -030099
bravofc973b572020-10-21 16:58:50 -0300100
101def check_configuration_equal(a_config, b_config):
garciadeblasefa4c2b2022-09-07 22:35:53 +0200102 if a_config is None and b_config is None:
103 return True
104 if a_config is None or b_config is None:
105 return False
106 if "scrape_configs" not in a_config and "scrape_configs" not in b_config:
107 return True
108 if "scrape_configs" not in a_config or "scrape_configs" not in b_config:
109 return False
110 a_jobs = [j["job_name"] for j in a_config["scrape_configs"]]
111 b_jobs = [j["job_name"] for j in b_config["scrape_configs"]]
bravofc973b572020-10-21 16:58:50 -0300112
garciadeblasefa4c2b2022-09-07 22:35:53 +0200113 return a_jobs == b_jobs
bravofc973b572020-10-21 16:58:50 -0300114
garciadeblasefa4c2b2022-09-07 22:35:53 +0200115
116async def validate_configuration(prom_url, new_config):
117 async with aiohttp.ClientSession() as session:
118 # Gets the configuration from prometheus
119 # and compares with the inserted one
120 # If prometheus does not admit this configuration,
121 # the old one will remain
122 async with session.get(prom_url + "/api/v1/status/config") as resp:
123 if resp.status > 204:
124 print(f"Error while updating prometheus config: {resp.text()}")
125 return False
126 current_config = await resp.json()
127 return check_configuration_equal(
128 yaml.safe_load(current_config["data"]["yaml"]), new_config
129 )
130
bravofc973b572020-10-21 16:58:50 -0300131
132async def main_task(client):
garciadeblasefa4c2b2022-09-07 22:35:53 +0200133 stored_jobs = get_jobs(client)
134 print(f"Jobs detected : {len(stored_jobs):d}")
135 generated_prometheus_config = generate_prometheus_config(
garciadeblas09eaa922022-09-07 22:57:53 +0200136 stored_jobs, prometheus_base_config_file
garciadeblasefa4c2b2022-09-07 22:35:53 +0200137 )
138 print(f"Writing new config file to {prometheus_config_file}")
139 config_file = open(prometheus_config_file, "w")
140 config_file.truncate(0)
garciadeblas7688da42022-11-17 17:44:23 +0100141 print(yaml.safe_dump(generated_prometheus_config))
garciadeblas09eaa922022-09-07 22:57:53 +0200142 config_file.write(yaml.safe_dump(generated_prometheus_config))
garciadeblasefa4c2b2022-09-07 22:35:53 +0200143 config_file.close()
144 print("New config written, updating prometheus")
145 update_resp = await reload_prometheus_config(prometheus_url)
garciadeblas84fe31f2022-11-15 14:12:48 +0100146 is_valid = await validate_configuration(prometheus_url, generated_prometheus_config)
garciadeblasefa4c2b2022-09-07 22:35:53 +0200147 if update_resp and is_valid:
148 print("Prometheus config update successful")
149 save_successful_jobs(client, stored_jobs)
150 else:
151 print(
152 "Error while updating prometheus config: "
153 "current config doesn't match with updated values"
154 )
155
bravofc973b572020-10-21 16:58:50 -0300156
157async def main():
garciadeblasefa4c2b2022-09-07 22:35:53 +0200158 client = pymongo.MongoClient(mongodb_url)
garciadeblas84fe31f2022-11-15 14:12:48 +0100159 print("Created MongoClient to connect to MongoDB!")
bravofc973b572020-10-21 16:58:50 -0300160
garciadeblas84fe31f2022-11-15 14:12:48 +0100161 # Initial loop. First refresh of prometheus config file
162 first_refresh_completed = False
163 tries = 1
164 while tries <= 3:
165 try:
166 print("Refreshing prometheus config file for first time")
167 await main_task(client)
168 first_refresh_completed = True
169 except Exception as error:
170 print(f"Error in configuration attempt! Number of tries: {tries}/3")
171 print(error)
172 time.sleep(5)
173 tries += 1
174 if not first_refresh_completed:
175 print("Not possible to refresh prometheus config file for first time")
176 return
garciadeblasefa4c2b2022-09-07 22:35:53 +0200177
garciadeblas84fe31f2022-11-15 14:12:48 +0100178 # Main loop
garciadeblasefa4c2b2022-09-07 22:35:53 +0200179 while True:
180 try:
181 # Needs mongodb in replica mode as this feature relies in OpLog
182 change_stream = client[target_database].prometheus_jobs.watch(
183 [
184 {
185 "$match": {
186 # If you want to modify a particular job,
187 # delete and insert it again
188 "operationType": {"$in": ["insert", "delete"]}
189 }
190 }
191 ]
192 )
193
194 # Single thread, no race conditions and ops are queued up in order
195 print("Listening to changes in prometheus jobs collection")
196 for change in change_stream:
197 print("Change detected, updating prometheus config")
garciadeblas84fe31f2022-11-15 14:12:48 +0100198 print(f"{change}")
garciadeblasefa4c2b2022-09-07 22:35:53 +0200199 await main_task(client)
200 print()
201 except Exception as error:
202 print(error)
203 print(
204 "Detected failure while listening to prometheus jobs collection, "
205 "retrying..."
206 )
207 time.sleep(5)
208
bravofc973b572020-10-21 16:58:50 -0300209
210asyncio.run(main())