Switch to DNS service discovery
This commit is contained in:
parent
0a42485318
commit
deb11aa409
19
config/prometheus.yml
Normal file
19
config/prometheus.yml
Normal 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
|
||||||
|
|
@ -1,8 +1,17 @@
|
|||||||
version: '3.7'
|
version: '3.7'
|
||||||
|
|
||||||
services:
|
services:
|
||||||
app:
|
mikrotik-exporter:
|
||||||
build:
|
build:
|
||||||
context: .
|
context: .
|
||||||
env_file: .env
|
env_file: .env
|
||||||
|
environment:
|
||||||
|
- PROMETHEUS_BEARER_TOKEN=ZwRZm7Qe6J
|
||||||
network_mode: host
|
network_mode: host
|
||||||
|
prometheus:
|
||||||
|
network_mode: "host"
|
||||||
|
image: prom/prometheus:latest
|
||||||
|
command:
|
||||||
|
- --config.file=/config/prometheus.yml
|
||||||
|
volumes:
|
||||||
|
- ./config:/config:ro
|
||||||
|
21
mikrotik.py
21
mikrotik.py
@ -1,6 +1,6 @@
|
|||||||
#!/usr/bin/env python
|
#!/usr/bin/env python
|
||||||
import os
|
import os
|
||||||
from aio_api_ros import create_rosapi_connection
|
from aio_api_ros.connection import ApiRosConnection
|
||||||
from aiostream import stream
|
from aiostream import stream
|
||||||
from sanic import Sanic, response, exceptions
|
from sanic import Sanic, response, exceptions
|
||||||
|
|
||||||
@ -39,14 +39,15 @@ async def wrap(i):
|
|||||||
value)
|
value)
|
||||||
|
|
||||||
|
|
||||||
async def scrape_mikrotik(target):
|
async def scrape_mikrotik(target, port):
|
||||||
mk = await create_rosapi_connection(
|
mk = ApiRosConnection(
|
||||||
mk_ip=target,
|
mk_ip=target,
|
||||||
mk_port=8728,
|
mk_port=port,
|
||||||
mk_user=MIKROTIK_USER,
|
mk_user=MIKROTIK_USER,
|
||||||
mk_psw=MIKROTIK_PASSWORD,
|
mk_psw=MIKROTIK_PASSWORD,
|
||||||
|
|
||||||
)
|
)
|
||||||
|
await mk.connect()
|
||||||
|
await mk.login()
|
||||||
|
|
||||||
mk.talk_sentence(["/interface/print"])
|
mk.talk_sentence(["/interface/print"])
|
||||||
res = await mk.read_full_answer()
|
res = await mk.read_full_answer()
|
||||||
@ -173,9 +174,17 @@ async def scrape_mikrotik(target):
|
|||||||
async def view_export(request):
|
async def view_export(request):
|
||||||
if request.token != PROMETHEUS_BEARER_TOKEN:
|
if request.token != PROMETHEUS_BEARER_TOKEN:
|
||||||
raise exceptions.Forbidden("Invalid 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 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")
|
await response.write(line + "\n")
|
||||||
|
|
||||||
return response.stream(streaming_fn, content_type="text/plain")
|
return response.stream(streaming_fn, content_type="text/plain")
|
||||||
|
Reference in New Issue
Block a user