feature/DO-638--imap_monitoring (!4)

[DO-638]

Reviewed-on: https://git.avroid.tech/DevOps/avroid_scripts/pulls/4
This commit is contained in:
Boris Shestov
2024-06-17 12:15:21 +03:00
parent f07b54ea36
commit f2225a2900
5 changed files with 93 additions and 0 deletions

11
imap_exporter/Dockerfile Normal file
View File

@@ -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"]

View File

@@ -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)

View File

@@ -0,0 +1 @@
prometheus_client==0.20.0

View File

@@ -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