| bravof | c973b57 | 2020-10-21 16:58:50 -0300 | [diff] [blame] | 1 | #!/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 | |
| 25 | import os |
| 26 | import pymongo |
| 27 | import yaml |
| 28 | import aiohttp |
| 29 | import asyncio |
| 30 | import copy |
| 31 | import json |
| bravof | 9af7d42 | 2021-11-23 17:21:58 -0300 | [diff] [blame] | 32 | import time |
| bravof | c973b57 | 2020-10-21 16:58:50 -0300 | [diff] [blame] | 33 | from bson.json_util import dumps |
| 34 | from bson import ObjectId |
| 35 | |
| garciadeblas | efa4c2b | 2022-09-07 22:35:53 +0200 | [diff] [blame^] | 36 | # Env variables |
| 37 | mongodb_url = os.environ["MONGODB_URL"] |
| 38 | target_database = os.environ["TARGET_DATABASE"] |
| 39 | prometheus_config_file = os.environ["PROMETHEUS_CONFIG_FILE"] |
| 40 | prometheus_url = os.environ["PROMETHEUS_URL"] |
| 41 | |
| bravof | c973b57 | 2020-10-21 16:58:50 -0300 | [diff] [blame] | 42 | |
| 43 | def get_jobs(client): |
| garciadeblas | efa4c2b | 2022-09-07 22:35:53 +0200 | [diff] [blame^] | 44 | return json.loads(dumps(client[target_database].prometheus_jobs.find({}))) |
| 45 | |
| bravof | c973b57 | 2020-10-21 16:58:50 -0300 | [diff] [blame] | 46 | |
| 47 | def save_successful_jobs(client, jobs): |
| garciadeblas | efa4c2b | 2022-09-07 22:35:53 +0200 | [diff] [blame^] | 48 | for job in jobs: |
| 49 | client[target_database].prometheus_jobs.update_one( |
| 50 | {"_id": ObjectId(job["_id"]["$oid"])}, |
| 51 | {"$set": {"is_active": True}} |
| 52 | ) |
| 53 | |
| bravof | c973b57 | 2020-10-21 16:58:50 -0300 | [diff] [blame] | 54 | |
| 55 | def clean_up_job(prometheus_job): |
| garciadeblas | efa4c2b | 2022-09-07 22:35:53 +0200 | [diff] [blame^] | 56 | 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 | |
| bravof | c973b57 | 2020-10-21 16:58:50 -0300 | [diff] [blame] | 64 | |
| 65 | def generate_prometheus_config(prometheus_jobs, config_file_path): |
| garciadeblas | efa4c2b | 2022-09-07 22:35:53 +0200 | [diff] [blame^] | 66 | config_file = open(config_file_path, encoding="utf-8", mode="r") |
| 67 | config_file_contents = config_file.read() |
| 68 | config_file.close() |
| bravof | c973b57 | 2020-10-21 16:58:50 -0300 | [diff] [blame] | 69 | |
| garciadeblas | efa4c2b | 2022-09-07 22:35:53 +0200 | [diff] [blame^] | 70 | config_file_yaml = yaml.load(config_file_contents, yaml.FullLoader) |
| 71 | if config_file_yaml is None: |
| 72 | config_file_yaml = {} |
| bravof | c973b57 | 2020-10-21 16:58:50 -0300 | [diff] [blame] | 73 | |
| garciadeblas | efa4c2b | 2022-09-07 22:35:53 +0200 | [diff] [blame^] | 74 | if len(prometheus_jobs) == 0: |
| 75 | config_file_yaml["scrape_configs"] = [] |
| 76 | return config_file_yaml |
| 77 | |
| 78 | config_file_yaml["scrape_configs"] = [] |
| 79 | |
| 80 | for prometheus_job in prometheus_jobs: |
| 81 | cleaned_up_job = clean_up_job(prometheus_job) |
| 82 | config_file_yaml["scrape_configs"].append(cleaned_up_job) |
| 83 | |
| bravof | c973b57 | 2020-10-21 16:58:50 -0300 | [diff] [blame] | 84 | return config_file_yaml |
| 85 | |
| bravof | c973b57 | 2020-10-21 16:58:50 -0300 | [diff] [blame] | 86 | |
| garciadeblas | efa4c2b | 2022-09-07 22:35:53 +0200 | [diff] [blame^] | 87 | async def reload_prometheus_config(prom_url): |
| 88 | async with aiohttp.ClientSession() as session: |
| 89 | async with session.post(prom_url + "/-/reload") as resp: |
| 90 | if resp.status > 204: |
| 91 | print(f"Error while updating prometheus config: {resp.text()}") |
| 92 | return False |
| 93 | await asyncio.sleep(5) |
| 94 | return True |
| bravof | c973b57 | 2020-10-21 16:58:50 -0300 | [diff] [blame] | 95 | |
| bravof | c973b57 | 2020-10-21 16:58:50 -0300 | [diff] [blame] | 96 | |
| 97 | def check_configuration_equal(a_config, b_config): |
| garciadeblas | efa4c2b | 2022-09-07 22:35:53 +0200 | [diff] [blame^] | 98 | if a_config is None and b_config is None: |
| 99 | return True |
| 100 | if a_config is None or b_config is None: |
| 101 | return False |
| 102 | if "scrape_configs" not in a_config and "scrape_configs" not in b_config: |
| 103 | return True |
| 104 | if "scrape_configs" not in a_config or "scrape_configs" not in b_config: |
| 105 | return False |
| 106 | a_jobs = [j["job_name"] for j in a_config["scrape_configs"]] |
| 107 | b_jobs = [j["job_name"] for j in b_config["scrape_configs"]] |
| bravof | c973b57 | 2020-10-21 16:58:50 -0300 | [diff] [blame] | 108 | |
| garciadeblas | efa4c2b | 2022-09-07 22:35:53 +0200 | [diff] [blame^] | 109 | return a_jobs == b_jobs |
| bravof | c973b57 | 2020-10-21 16:58:50 -0300 | [diff] [blame] | 110 | |
| garciadeblas | efa4c2b | 2022-09-07 22:35:53 +0200 | [diff] [blame^] | 111 | |
| 112 | async def validate_configuration(prom_url, new_config): |
| 113 | async with aiohttp.ClientSession() as session: |
| 114 | # Gets the configuration from prometheus |
| 115 | # and compares with the inserted one |
| 116 | # If prometheus does not admit this configuration, |
| 117 | # the old one will remain |
| 118 | async with session.get(prom_url + "/api/v1/status/config") as resp: |
| 119 | if resp.status > 204: |
| 120 | print(f"Error while updating prometheus config: {resp.text()}") |
| 121 | return False |
| 122 | current_config = await resp.json() |
| 123 | return check_configuration_equal( |
| 124 | yaml.safe_load(current_config["data"]["yaml"]), new_config |
| 125 | ) |
| 126 | |
| bravof | c973b57 | 2020-10-21 16:58:50 -0300 | [diff] [blame] | 127 | |
| 128 | async def main_task(client): |
| garciadeblas | efa4c2b | 2022-09-07 22:35:53 +0200 | [diff] [blame^] | 129 | stored_jobs = get_jobs(client) |
| 130 | print(f"Jobs detected : {len(stored_jobs):d}") |
| 131 | generated_prometheus_config = generate_prometheus_config( |
| 132 | stored_jobs, prometheus_config_file |
| 133 | ) |
| 134 | print(f"Writing new config file to {prometheus_config_file}") |
| 135 | config_file = open(prometheus_config_file, "w") |
| 136 | config_file.truncate(0) |
| 137 | config_file.write(yaml.dump(generated_prometheus_config)) |
| 138 | config_file.close() |
| 139 | print("New config written, updating prometheus") |
| 140 | update_resp = await reload_prometheus_config(prometheus_url) |
| 141 | is_valid = await validate_configuration( |
| 142 | prometheus_url, generated_prometheus_config |
| 143 | ) |
| 144 | if update_resp and is_valid: |
| 145 | print("Prometheus config update successful") |
| 146 | save_successful_jobs(client, stored_jobs) |
| 147 | else: |
| 148 | print( |
| 149 | "Error while updating prometheus config: " |
| 150 | "current config doesn't match with updated values" |
| 151 | ) |
| 152 | |
| bravof | c973b57 | 2020-10-21 16:58:50 -0300 | [diff] [blame] | 153 | |
| 154 | async def main(): |
| garciadeblas | efa4c2b | 2022-09-07 22:35:53 +0200 | [diff] [blame^] | 155 | client = pymongo.MongoClient(mongodb_url) |
| 156 | print("Connected to MongoDB!") |
| bravof | c973b57 | 2020-10-21 16:58:50 -0300 | [diff] [blame] | 157 | |
| bravof | 9af7d42 | 2021-11-23 17:21:58 -0300 | [diff] [blame] | 158 | try: |
| garciadeblas | efa4c2b | 2022-09-07 22:35:53 +0200 | [diff] [blame^] | 159 | print("Refreshing prometheus config file for first time") |
| bravof | 9af7d42 | 2021-11-23 17:21:58 -0300 | [diff] [blame] | 160 | await main_task(client) |
| bravof | 9af7d42 | 2021-11-23 17:21:58 -0300 | [diff] [blame] | 161 | except Exception as error: |
| garciadeblas | efa4c2b | 2022-09-07 22:35:53 +0200 | [diff] [blame^] | 162 | print("Error in first configuration attempt!") |
| 163 | print(error) |
| 164 | |
| 165 | while True: |
| 166 | try: |
| 167 | # Needs mongodb in replica mode as this feature relies in OpLog |
| 168 | change_stream = client[target_database].prometheus_jobs.watch( |
| 169 | [ |
| 170 | { |
| 171 | "$match": { |
| 172 | # If you want to modify a particular job, |
| 173 | # delete and insert it again |
| 174 | "operationType": {"$in": ["insert", "delete"]} |
| 175 | } |
| 176 | } |
| 177 | ] |
| 178 | ) |
| 179 | |
| 180 | # Single thread, no race conditions and ops are queued up in order |
| 181 | print("Listening to changes in prometheus jobs collection") |
| 182 | for change in change_stream: |
| 183 | print("Change detected, updating prometheus config") |
| 184 | await main_task(client) |
| 185 | print() |
| 186 | except Exception as error: |
| 187 | print(error) |
| 188 | print( |
| 189 | "Detected failure while listening to prometheus jobs collection, " |
| 190 | "retrying..." |
| 191 | ) |
| 192 | time.sleep(5) |
| 193 | |
| bravof | c973b57 | 2020-10-21 16:58:50 -0300 | [diff] [blame] | 194 | |
| 195 | asyncio.run(main()) |