10
0

Use forked aio_api_ros library

This commit is contained in:
Lauri Võsandi 2021-07-02 09:01:24 +03:00
parent e030f5c9a0
commit c88d0bbc69
2 changed files with 11 additions and 16 deletions

View File

@ -1,5 +1,5 @@
FROM python:3 FROM python:3
RUN pip install aio_api_ros aiostream sanic RUN pip install aiostream sanic git+https://github.com/laurivosandi/aio_api_ros
ADD mikrotik.py /mikrotik.py ADD mikrotik.py /mikrotik.py
ENTRYPOINT /mikrotik.py ENTRYPOINT /mikrotik.py
EXPOSE 3001 EXPOSE 3001

View File

@ -1,8 +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 import create_rosapi_connection
from aio_api_ros.unpacker import SentenceUnpacker
from aio_api_ros.parser import parse_sentence
from aiostream import stream from aiostream import stream
from sanic import Sanic, response, exceptions from sanic import Sanic, response, exceptions
@ -42,14 +40,6 @@ async def wrap(i):
value) value)
async def mikrotik_run(conn, cmd):
conn.talk_sentence(cmd)
data = await conn.read(full_answer=True, parse=False)
unpacker = SentenceUnpacker()
unpacker.feed(data)
return [parse_sentence(sentence) for sentence in unpacker]
async def scrape_mikrotik(target): async def scrape_mikrotik(target):
mk = await create_rosapi_connection( mk = await create_rosapi_connection(
mk_ip=target, mk_ip=target,
@ -61,7 +51,8 @@ async def scrape_mikrotik(target):
ports = ",".join([str(j) for j in range(0, 24)]) ports = ",".join([str(j) for j in range(0, 24)])
res = await mikrotik_run(mk, ["/interface/print"]) mk.talk_sentence(["/interface/print"])
res = await mk.read_full_answer()
for resp, _, obj in res: for resp, _, obj in res:
if resp in ("!trap", "!done"): if resp in ("!trap", "!done"):
break break
@ -84,7 +75,8 @@ async def scrape_mikrotik(target):
yield "interface-running", "gauge", int(obj["tx-byte"]), labels yield "interface-running", "gauge", int(obj["tx-byte"]), labels
yield "interface-actual-mtu", "gauge", obj["actual-mtu"], labels yield "interface-actual-mtu", "gauge", obj["actual-mtu"], labels
res = await mikrotik_run(mk, ["/interface/ethernet/monitor", "=once=", "=numbers=%s" % ports]) mk.talk_sentence(["/interface/ethernet/monitor", "=once=", "=numbers=%s" % ports])
res = await mk.read_full_answer()
for resp, _, obj in res: for resp, _, obj in res:
if resp in ("!trap", "!done"): if resp in ("!trap", "!done"):
break break
@ -120,7 +112,8 @@ async def scrape_mikrotik(target):
pass pass
yield "interface-status", "gauge", 1, labels yield "interface-status", "gauge", 1, labels
res = await mikrotik_run(mk, ["/interface/ethernet/poe/monitor", "=once=", "=numbers=%s" % ports]) mk.talk_sentence(["/interface/ethernet/poe/monitor", "=once=", "=numbers=%s" % ports])
res = await mk.read_full_answer()
for resp, _, obj in res: for resp, _, obj in res:
if resp in ("!trap", "!done"): if resp in ("!trap", "!done"):
break break
@ -135,7 +128,8 @@ async def scrape_mikrotik(target):
labels["status"] = obj["poe-out-status"] labels["status"] = obj["poe-out-status"]
yield "poe-out-status", "gauge", 1, labels yield "poe-out-status", "gauge", 1, labels
res = await mikrotik_run(mk, ["/system/resource/print"]) mk.talk_sentence(["/system/resource/print"])
res = await mk.read_full_answer()
for resp, _, obj in res: for resp, _, obj in res:
if resp in ("!trap", "!done"): if resp in ("!trap", "!done"):
break break
@ -152,7 +146,8 @@ async def scrape_mikrotik(target):
labels[key] = obj[key] labels[key] = obj[key]
yield "system-version", "gauge", 1, labels yield "system-version", "gauge", 1, labels
res = await mikrotik_run(mk, ["/system/health/print"]) mk.talk_sentence(["/system/health/print"])
res = await mk.read_full_answer()
for resp, _, obj in res: for resp, _, obj in res:
if resp in ("!trap", "!done"): if resp in ("!trap", "!done"):
break break