blob: 157413dc8aa734aeb0b0993e587af06d58226fe3 [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(
51 {"_id": ObjectId(job["_id"]["$oid"])},
52 {"$set": {"is_active": True}}
53 )
54
bravofc973b572020-10-21 16:58:50 -030055
56def clean_up_job(prometheus_job):
garciadeblasefa4c2b2022-09-07 22:35:53 +020057 cleaned_prometheus_job = copy.deepcopy(prometheus_job)
58 # take out _id and internal keys
59 cleaned_prometheus_job.pop("_id", None)
60 cleaned_prometheus_job.pop("is_active", None)
61 cleaned_prometheus_job.pop("vnfr_id", None)
62 cleaned_prometheus_job.pop("nsr_id", None)
63 return cleaned_prometheus_job
64
bravofc973b572020-10-21 16:58:50 -030065
66def generate_prometheus_config(prometheus_jobs, config_file_path):
garciadeblas09eaa922022-09-07 22:57:53 +020067 with open(config_file_path, encoding="utf-8", mode="r") as config_file:
68 config_file_yaml = yaml.safe_load(config_file)
garciadeblasefa4c2b2022-09-07 22:35:53 +020069 if config_file_yaml is None:
70 config_file_yaml = {}
garciadeblas09eaa922022-09-07 22:57:53 +020071 if "scrape_configs" not in config_file_yaml:
garciadeblasefa4c2b2022-09-07 22:35:53 +020072 config_file_yaml["scrape_configs"] = []
garciadeblasefa4c2b2022-09-07 22:35:53 +020073
74 for prometheus_job in prometheus_jobs:
75 cleaned_up_job = clean_up_job(prometheus_job)
76 config_file_yaml["scrape_configs"].append(cleaned_up_job)
77
bravofc973b572020-10-21 16:58:50 -030078 return config_file_yaml
79
bravofc973b572020-10-21 16:58:50 -030080
garciadeblasefa4c2b2022-09-07 22:35:53 +020081async def reload_prometheus_config(prom_url):
82 async with aiohttp.ClientSession() as session:
83 async with session.post(prom_url + "/-/reload") as resp:
84 if resp.status > 204:
85 print(f"Error while updating prometheus config: {resp.text()}")
86 return False
87 await asyncio.sleep(5)
88 return True
bravofc973b572020-10-21 16:58:50 -030089
bravofc973b572020-10-21 16:58:50 -030090
91def check_configuration_equal(a_config, b_config):
garciadeblasefa4c2b2022-09-07 22:35:53 +020092 if a_config is None and b_config is None:
93 return True
94 if a_config is None or b_config is None:
95 return False
96 if "scrape_configs" not in a_config and "scrape_configs" not in b_config:
97 return True
98 if "scrape_configs" not in a_config or "scrape_configs" not in b_config:
99 return False
100 a_jobs = [j["job_name"] for j in a_config["scrape_configs"]]
101 b_jobs = [j["job_name"] for j in b_config["scrape_configs"]]
bravofc973b572020-10-21 16:58:50 -0300102
garciadeblasefa4c2b2022-09-07 22:35:53 +0200103 return a_jobs == b_jobs
bravofc973b572020-10-21 16:58:50 -0300104
garciadeblasefa4c2b2022-09-07 22:35:53 +0200105
106async def validate_configuration(prom_url, new_config):
107 async with aiohttp.ClientSession() as session:
108 # Gets the configuration from prometheus
109 # and compares with the inserted one
110 # If prometheus does not admit this configuration,
111 # the old one will remain
112 async with session.get(prom_url + "/api/v1/status/config") as resp:
113 if resp.status > 204:
114 print(f"Error while updating prometheus config: {resp.text()}")
115 return False
116 current_config = await resp.json()
117 return check_configuration_equal(
118 yaml.safe_load(current_config["data"]["yaml"]), new_config
119 )
120
bravofc973b572020-10-21 16:58:50 -0300121
122async def main_task(client):
garciadeblasefa4c2b2022-09-07 22:35:53 +0200123 stored_jobs = get_jobs(client)
124 print(f"Jobs detected : {len(stored_jobs):d}")
125 generated_prometheus_config = generate_prometheus_config(
garciadeblas09eaa922022-09-07 22:57:53 +0200126 stored_jobs, prometheus_base_config_file
garciadeblasefa4c2b2022-09-07 22:35:53 +0200127 )
128 print(f"Writing new config file to {prometheus_config_file}")
129 config_file = open(prometheus_config_file, "w")
130 config_file.truncate(0)
garciadeblas09eaa922022-09-07 22:57:53 +0200131 config_file.write(yaml.safe_dump(generated_prometheus_config))
garciadeblasefa4c2b2022-09-07 22:35:53 +0200132 config_file.close()
133 print("New config written, updating prometheus")
134 update_resp = await reload_prometheus_config(prometheus_url)
135 is_valid = await validate_configuration(
136 prometheus_url, generated_prometheus_config
137 )
138 if update_resp and is_valid:
139 print("Prometheus config update successful")
140 save_successful_jobs(client, stored_jobs)
141 else:
142 print(
143 "Error while updating prometheus config: "
144 "current config doesn't match with updated values"
145 )
146
bravofc973b572020-10-21 16:58:50 -0300147
148async def main():
garciadeblasefa4c2b2022-09-07 22:35:53 +0200149 client = pymongo.MongoClient(mongodb_url)
150 print("Connected to MongoDB!")
bravofc973b572020-10-21 16:58:50 -0300151
bravof9af7d422021-11-23 17:21:58 -0300152 try:
garciadeblasefa4c2b2022-09-07 22:35:53 +0200153 print("Refreshing prometheus config file for first time")
bravof9af7d422021-11-23 17:21:58 -0300154 await main_task(client)
bravof9af7d422021-11-23 17:21:58 -0300155 except Exception as error:
garciadeblasefa4c2b2022-09-07 22:35:53 +0200156 print("Error in first configuration attempt!")
157 print(error)
158
159 while True:
160 try:
161 # Needs mongodb in replica mode as this feature relies in OpLog
162 change_stream = client[target_database].prometheus_jobs.watch(
163 [
164 {
165 "$match": {
166 # If you want to modify a particular job,
167 # delete and insert it again
168 "operationType": {"$in": ["insert", "delete"]}
169 }
170 }
171 ]
172 )
173
174 # Single thread, no race conditions and ops are queued up in order
175 print("Listening to changes in prometheus jobs collection")
176 for change in change_stream:
177 print("Change detected, updating prometheus config")
178 await main_task(client)
179 print()
180 except Exception as error:
181 print(error)
182 print(
183 "Detected failure while listening to prometheus jobs collection, "
184 "retrying..."
185 )
186 time.sleep(5)
187
bravofc973b572020-10-21 16:58:50 -0300188
189asyncio.run(main())