Files
scripts/imap_exporter/imap_exporter.py
2024-06-17 12:15:21 +03:00

39 lines
1.1 KiB
Python

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)