feature/DO-638--imap_monitoring (!4)
[DO-638] Reviewed-on: https://git.avroid.tech/DevOps/avroid_scripts/pulls/4
This commit is contained in:
11
imap_exporter/Dockerfile
Normal file
11
imap_exporter/Dockerfile
Normal 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"]
|
||||
38
imap_exporter/imap_exporter.py
Normal file
38
imap_exporter/imap_exporter.py
Normal 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)
|
||||
1
imap_exporter/requirements.txt
Normal file
1
imap_exporter/requirements.txt
Normal file
@@ -0,0 +1 @@
|
||||
prometheus_client==0.20.0
|
||||
34
imap_exporter/restart_imap.sh
Normal file
34
imap_exporter/restart_imap.sh
Normal 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
|
||||
Reference in New Issue
Block a user