10
0

Use simplified interfaces

This commit is contained in:
Lauri Võsandi 2021-10-18 17:03:28 +03:00
parent 0497d33f44
commit cfc4959796
4 changed files with 31 additions and 42 deletions

3
.gitmodules vendored Normal file
View File

@ -0,0 +1,3 @@
[submodule "aio_api_ros"]
path = aio_api_ros
url = git@github.com:laurivosandi/aio_api_ros.git

View File

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

1
aio_api_ros Submodule

@ -0,0 +1 @@
Subproject commit ec4b0a31ea895bf3afee9f29907f705ad98cd252

View File

@ -49,11 +49,8 @@ async def scrape_mikrotik(target, port):
await mk.connect()
await mk.login()
mk.talk_sentence(["/interface/print"])
res = await mk.read_full_answer()
for resp, _, obj in res:
if resp in ("!trap", "!done"):
break
async for obj in mk.query("/interface/print"):
labels = {"host": target, "port": obj["name"], "type": obj["type"]}
yield "interface_rx_bytes", "counter", obj["rx-byte"], labels
@ -73,21 +70,13 @@ async def scrape_mikrotik(target, port):
yield "interface_running", "gauge", int(obj["tx-byte"]), labels
yield "interface_actual_mtu", "gauge", obj["actual-mtu"], labels
mk.talk_sentence(["/interface/ethernet/print"])
port_count = 0
res = await mk.read_full_answer()
for resp, _, obj in res:
if resp in ("!trap", "!done"):
break
res = mk.query("/interface/ethernet/print")
async for obj in res:
port_count += 1
ports = ",".join([str(j) for j in range(0, port_count)])
yield "port_count", "gauge", port_count, {"host": target }
ports = ",".join([str(j) for j in range(1, port_count)])
mk.talk_sentence(["/interface/ethernet/monitor", "=once=", "=numbers=%s" % ports])
res = await mk.read_full_answer()
for resp, _, obj in res:
if resp in ("!trap", "!done"):
break
async for obj in mk.query("/interface/ethernet/monitor", "=once=", "=numbers=%s" % ports):
labels = {"host": target, "port": obj["name"]}
try:
@ -120,12 +109,14 @@ async def scrape_mikrotik(target, port):
pass
yield "interface_status", "gauge", 1, labels
mk.talk_sentence(["/interface/ethernet/poe/monitor", "=once=", "=numbers=%s" % ports])
res = await mk.read_full_answer()
for resp, _, obj in res:
if resp in ("!trap", "!done"):
break
poe_ports = set()
res = mk.query("/interface/ethernet/poe/print", optional=True)
async for obj in res:
poe_ports.add(int(obj[".id"][1:],16)-1)
if poe_ports:
res = mk.query("/interface/ethernet/poe/monitor", "=once=", "=numbers=%s" % ",".join([str(j) for j in poe_ports]))
async for obj in res:
labels = {"host": target, "port": obj["name"]}
try:
yield "poe_out_voltage", "gauge", float(obj["poe-out-voltage"]), labels
@ -136,12 +127,7 @@ async def scrape_mikrotik(target, port):
labels["status"] = obj["poe-out-status"]
yield "poe_out_status", "gauge", 1, labels
mk.talk_sentence(["/system/resource/print"])
res = await mk.read_full_answer()
for resp, _, obj in res:
if resp in ("!trap", "!done"):
break
async for obj in mk.query("/system/resource/print"):
labels = {"host": target}
yield "system_write_sect_total", "counter", obj["write-sect-total"], labels
yield "system_free_memory", "gauge", obj["free-memory"], labels
@ -154,11 +140,7 @@ async def scrape_mikrotik(target, port):
labels[key.replace("-", "_")] = obj[key]
yield "system_version", "gauge", 1, labels
mk.talk_sentence(["/system/health/print"])
res = await mk.read_full_answer()
for resp, _, obj in res:
if resp in ("!trap", "!done"):
break
async for obj in mk.query("/system/health/print"):
for key, value in obj.items():
labels = {"host": target}
try: