Initial commit

This commit is contained in:
Lauri Võsandi 2021-05-31 13:21:25 +03:00
commit 0f1e99a237
2 changed files with 30 additions and 0 deletions

4
Dockerfile Normal file
View File

@ -0,0 +1,4 @@
FROM python
RUN pip install motor sanic
ADD exporter.py /exporter.py
CMD /exporter.py

26
exporter.py Executable file
View File

@ -0,0 +1,26 @@
#!/usr/bin/env python
import os
from motor.motor_asyncio import AsyncIOMotorClient
from sanic import Sanic, response
app = Sanic("exporter")
MONGO_URI = os.getenv("MONGO_URI", "mongodb://127.0.0.1:27017/default")
@app.listener('before_server_start')
async def setup_db(app, loop):
app.ctx.db = AsyncIOMotorClient(MONGO_URI).get_default_database()
@app.route("/metrics")
async def view_export(request):
coll = app.ctx.db["certidude_certificates"]
async def streaming_fn(response):
await response.write("# HELP pinecrypt_remote_last_seen Remote client last seen\n")
await response.write("# TYPE pinecrypt_remote_last_seen gauge\n")
async for doc in coll.find({"status":"signed", "last_seen":{"$exists":True}}, {"common_name":1, "last_seen":1}):
await response.write("pinecrypt_remote_last_seen{cn=\"%s\"} %d\n" % (
doc["common_name"], doc["last_seen"].timestamp()))
return response.stream(streaming_fn, content_type='text/plain')
app.run(port=3001)