init
This commit is contained in:
commit
31eea082ab
3 changed files with 50 additions and 0 deletions
10
Dockerfile
Normal file
10
Dockerfile
Normal file
|
@ -0,0 +1,10 @@
|
|||
FROM reg.zknt.org/zknt/python
|
||||
|
||||
COPY requirements.txt /app/requirements.txt
|
||||
WORKDIR /app
|
||||
RUN apk add --no-cache libffi && pip3 install -r requirements.txt && apk del libffi
|
||||
|
||||
COPY . /app
|
||||
ENV FLASK_APP=rspamd_exporter.py
|
||||
EXPOSE 5000
|
||||
ENTRYPOINT flask run --host=0.0.0.0
|
2
requirements.txt
Normal file
2
requirements.txt
Normal file
|
@ -0,0 +1,2 @@
|
|||
requests==2.24.0
|
||||
Flask==1.1.2
|
38
rspamd_exporter.py
Normal file
38
rspamd_exporter.py
Normal file
|
@ -0,0 +1,38 @@
|
|||
import os
|
||||
|
||||
import requests
|
||||
from flask import Flask
|
||||
|
||||
|
||||
password = os.environ.get("RSPAMD_EXPORTER_PASSWORD")
|
||||
url = os.environ.get("RSPAMD_EXPORTER_URL")
|
||||
instance = os.environ.get("RSPAMD_EXPORTER_INSTANCE")
|
||||
|
||||
|
||||
def json_to_prom(json, prefix, instance):
|
||||
outlines = ""
|
||||
for k,v in json.items():
|
||||
k = k.replace(' ', '_')
|
||||
k = k.replace('.', '_')
|
||||
if type(k) != list:
|
||||
v = [v]
|
||||
for item in v:
|
||||
if len(v) != 1:
|
||||
prefix = prefix + "_" + v.index(item)
|
||||
if type(item) is bool:
|
||||
outlines += "{}_{}{{instance=\"{}\"}} {}\n".format(prefix, k, instance, 1 if item else 0)
|
||||
if type(item) in [int, str]:
|
||||
outlines += "{}_{}{{instance=\"{}\"}} {}\n".format(prefix, k, instance,item)
|
||||
if type(item) is dict:
|
||||
outlines += json_to_prom(item, "{}_{}".format(prefix, k), instance)
|
||||
return outlines
|
||||
|
||||
|
||||
app = Flask(__name__)
|
||||
|
||||
@app.route('/metrics')
|
||||
def metrics():
|
||||
rspamd_json = requests.get(url, headers={"Password": password}).json()
|
||||
prom_lines = json_to_prom(rspamd_json, "rspamd", instance)
|
||||
prom_lines += 'up{{instance="{}"}} 1'.format(instance)
|
||||
return prom_lines
|
Loading…
Reference in a new issue