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 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 ADD mikrotik.py /mikrotik.py
ENTRYPOINT /mikrotik.py ENTRYPOINT /mikrotik.py
EXPOSE 3001 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.connect()
await mk.login() await mk.login()
mk.talk_sentence(["/interface/print"]) async for obj in mk.query("/interface/print"):
res = await mk.read_full_answer()
for resp, _, obj in res:
if resp in ("!trap", "!done"):
break
labels = {"host": target, "port": obj["name"], "type": obj["type"]} labels = {"host": target, "port": obj["name"], "type": obj["type"]}
yield "interface_rx_bytes", "counter", obj["rx-byte"], labels 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_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
mk.talk_sentence(["/interface/ethernet/print"])
port_count = 0 port_count = 0
res = await mk.read_full_answer() res = mk.query("/interface/ethernet/print")
for resp, _, obj in res: async for obj in res:
if resp in ("!trap", "!done"):
break
port_count += 1 port_count += 1
ports = ",".join([str(j) for j in range(0, port_count)]) ports = ",".join([str(j) for j in range(1, port_count)])
yield "port_count", "gauge", port_count, {"host": target }
mk.talk_sentence(["/interface/ethernet/monitor", "=once=", "=numbers=%s" % ports]) async for obj in mk.query("/interface/ethernet/monitor", "=once=", "=numbers=%s" % ports):
res = await mk.read_full_answer()
for resp, _, obj in res:
if resp in ("!trap", "!done"):
break
labels = {"host": target, "port": obj["name"]} labels = {"host": target, "port": obj["name"]}
try: try:
@ -120,12 +109,14 @@ async def scrape_mikrotik(target, port):
pass pass
yield "interface_status", "gauge", 1, labels yield "interface_status", "gauge", 1, labels
mk.talk_sentence(["/interface/ethernet/poe/monitor", "=once=", "=numbers=%s" % ports]) poe_ports = set()
res = await mk.read_full_answer() res = mk.query("/interface/ethernet/poe/print", optional=True)
for resp, _, obj in res: async for obj in res:
if resp in ("!trap", "!done"): poe_ports.add(int(obj[".id"][1:],16)-1)
break
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"]} labels = {"host": target, "port": obj["name"]}
try: try:
yield "poe_out_voltage", "gauge", float(obj["poe-out-voltage"]), labels 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"] labels["status"] = obj["poe-out-status"]
yield "poe_out_status", "gauge", 1, labels yield "poe_out_status", "gauge", 1, labels
mk.talk_sentence(["/system/resource/print"]) async for obj in mk.query("/system/resource/print"):
res = await mk.read_full_answer()
for resp, _, obj in res:
if resp in ("!trap", "!done"):
break
labels = {"host": target} labels = {"host": target}
yield "system_write_sect_total", "counter", obj["write-sect-total"], labels yield "system_write_sect_total", "counter", obj["write-sect-total"], labels
yield "system_free_memory", "gauge", obj["free-memory"], 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] labels[key.replace("-", "_")] = obj[key]
yield "system_version", "gauge", 1, labels yield "system_version", "gauge", 1, labels
mk.talk_sentence(["/system/health/print"]) async for obj in mk.query("/system/health/print"):
res = await mk.read_full_answer()
for resp, _, obj in res:
if resp in ("!trap", "!done"):
break
for key, value in obj.items(): for key, value in obj.items():
labels = {"host": target} labels = {"host": target}
try: try: