9
0
Fork 0

Switch to DNS service discovery

This commit is contained in:
Lauri Võsandi 2021-10-15 13:11:46 +03:00
parent 0a42485318
commit deb11aa409
3 changed files with 44 additions and 7 deletions

19
config/prometheus.yml Normal file
View File

@ -0,0 +1,19 @@
global:
scrape_interval: 10s
evaluation_interval: 5s
scrape_timeout: 5s
scrape_configs:
- job_name: mikrotik-exporter
bearer_token: ZwRZm7Qe6J
dns_sd_configs:
- names:
- '_mikrotik._tcp.mgmt.k-space.ee'
relabel_configs:
- source_labels: [ __address__ ]
target_label: __param_target
- source_labels: [ __param_target ]
target_label: instance
- target_label: __address__
replacement: 127.0.0.1:3001

View File

@ -1,8 +1,17 @@
version: '3.7'
services:
app:
mikrotik-exporter:
build:
context: .
env_file: .env
environment:
- PROMETHEUS_BEARER_TOKEN=ZwRZm7Qe6J
network_mode: host
prometheus:
network_mode: "host"
image: prom/prometheus:latest
command:
- --config.file=/config/prometheus.yml
volumes:
- ./config:/config:ro

View File

@ -1,6 +1,6 @@
#!/usr/bin/env python
import os
from aio_api_ros import create_rosapi_connection
from aio_api_ros.connection import ApiRosConnection
from aiostream import stream
from sanic import Sanic, response, exceptions
@ -39,14 +39,15 @@ async def wrap(i):
value)
async def scrape_mikrotik(target):
mk = await create_rosapi_connection(
async def scrape_mikrotik(target, port):
mk = ApiRosConnection(
mk_ip=target,
mk_port=8728,
mk_port=port,
mk_user=MIKROTIK_USER,
mk_psw=MIKROTIK_PASSWORD,
)
await mk.connect()
await mk.login()
mk.talk_sentence(["/interface/print"])
res = await mk.read_full_answer()
@ -173,9 +174,17 @@ async def scrape_mikrotik(target):
async def view_export(request):
if request.token != PROMETHEUS_BEARER_TOKEN:
raise exceptions.Forbidden("Invalid bearer token")
target = request.args.get("target")
if not target:
raise exceptions.InvalidUsage("Invalid or no target specified")
if ":" in target:
target, port = target.split(":")
port = int(port)
else:
port = 8728
async def streaming_fn(response):
async for line in wrap(scrape_mikrotik(request.args.get("target"))):
async for line in wrap(scrape_mikrotik(target, port)):
await response.write(line + "\n")
return response.stream(streaming_fn, content_type="text/plain")