From f2225a290053443797764de27b636ef3c834989d Mon Sep 17 00:00:00 2001 From: Boris Shestov Date: Mon, 17 Jun 2024 12:15:21 +0300 Subject: [PATCH] feature/DO-638--imap_monitoring (!4) [DO-638] Reviewed-on: https://git.avroid.tech/DevOps/avroid_scripts/pulls/4 --- .gitignore | 9 ++++++++ imap_exporter/Dockerfile | 11 ++++++++++ imap_exporter/imap_exporter.py | 38 ++++++++++++++++++++++++++++++++++ imap_exporter/requirements.txt | 1 + imap_exporter/restart_imap.sh | 34 ++++++++++++++++++++++++++++++ 5 files changed, 93 insertions(+) create mode 100644 .gitignore create mode 100644 imap_exporter/Dockerfile create mode 100644 imap_exporter/imap_exporter.py create mode 100644 imap_exporter/requirements.txt create mode 100644 imap_exporter/restart_imap.sh diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..4b5d1a2 --- /dev/null +++ b/.gitignore @@ -0,0 +1,9 @@ +.idea +.vscode +.vagrant +.venv +venv +__pycache__ +*~ +**/.DS_Store +._* diff --git a/imap_exporter/Dockerfile b/imap_exporter/Dockerfile new file mode 100644 index 0000000..c17f0c3 --- /dev/null +++ b/imap_exporter/Dockerfile @@ -0,0 +1,11 @@ +FROM python:3.12.3-alpine + +COPY ["requirements.txt", "imap_exporter.py", "./"] + +RUN apk add openssl + +RUN python3 -m pip install -r requirements.txt --no-cache-dir + +EXPOSE 9119 + +CMD ["python", "-u", "imap_exporter.py"] diff --git a/imap_exporter/imap_exporter.py b/imap_exporter/imap_exporter.py new file mode 100644 index 0000000..a2752a9 --- /dev/null +++ b/imap_exporter/imap_exporter.py @@ -0,0 +1,38 @@ +import time +import os +import re +import subprocess +from prometheus_client import start_http_server, Gauge + + +rs = r"CN=\*.avroid.tech" +host = os.environ.get("IMAP_HOST", "imap-app.avroid.tech:993") +re_search = os.environ.get("RE_SEARCH", rs) + +# Create a metric to track time spent and requests made. +g = Gauge('connection_success', 'Подключение к imap серверу', ["imap_host"]) + +def get_connection_status(imap_host): + try: + response = subprocess.check_output(f"openssl s_client -connect {imap_host} > /dev/null < /dev/null", shell=True,stderr=subprocess.STDOUT) + except subprocess.CalledProcessError as e: + response = e.output + + text_response = response.decode('utf-8') + + if re.search(re_search, text_response): + print(f"Connection to {host} successfull") + connection_status = 1 + else: + print(f"Connection to {host} failure") + connection_status = 0 + return connection_status + + +if __name__ == '__main__': + # Start up the server to expose the metrics. + start_http_server(9120) + while True: + cs = get_connection_status(host) + g.labels(host).set(cs) + time.sleep(5) diff --git a/imap_exporter/requirements.txt b/imap_exporter/requirements.txt new file mode 100644 index 0000000..dfe66d6 --- /dev/null +++ b/imap_exporter/requirements.txt @@ -0,0 +1 @@ +prometheus_client==0.20.0 diff --git a/imap_exporter/restart_imap.sh b/imap_exporter/restart_imap.sh new file mode 100644 index 0000000..be7078b --- /dev/null +++ b/imap_exporter/restart_imap.sh @@ -0,0 +1,34 @@ +#!/bin/bash + +# Переменная для tmp lock-файла +LOCK_FILE="/tmp/restart_imap.lock" + +# Функция для проверки блокировки +check_lock() { + if [ -f "$LOCK_FILE" ]; then + echo "$(date) Скрипт уже запущен, выход..." >> /var/log/restart_imap.log + exit 1 + fi +} + +# Функция для установки блокировки +set_lock() { + touch "$LOCK_FILE" +} + +# Функция для удаления блокировки +remove_lock() { + rm -f "$LOCK_FILE" +} + +# Основной код скрипта +check_lock +set_lock + +openssl s_client -connect imap-app.avroid.tech:993 > /dev/null < /dev/null > /dev/null 2>&1 +if [ $? -ne 0 ]; then + echo "$(date) Restart imap docker container" >> /var/log/restart_imap.log + docker restart imap +fi + +remove_lock