Compare commits

..

No commits in common. "9af0739d80a861c61aa4137becbdbf7980893052" and "182cde368a42d4efe4d1ee99751906803f749248" have entirely different histories.

5 changed files with 15 additions and 119 deletions

View file

@ -1,14 +0,0 @@
#
# Dockerfile
#
FROM python:alpine
MAINTAINER Yann Verry <docker@verry.org>
WORKDIR .
RUN pip install --no-cache-dir pipenv && \
pipenv install --system --deploy
USER nobody
CMD python /routeros-restapi/main.py

View file

@ -1,7 +0,0 @@
all: pull build
pull:
docker pull python:alpine
build:
docker build --network yann_default -t python-routeros .

68
Pipfile.lock generated
View file

@ -1,68 +0,0 @@
{
"_meta": {
"hash": {
"sha256": "d6bc38cdbe538e25214de6d4e7185b5b544a0b692423b40b8b66e181816e4d2f"
},
"pipfile-spec": 6,
"requires": {
"python_version": "3.9"
},
"sources": [
{
"name": "pypi",
"url": "https://pypi.org/simple",
"verify_ssl": true
}
]
},
"default": {
"certifi": {
"hashes": [
"sha256:1a4995114262bffbc2413b159f2a1a480c969de6e6eb13ee966d470af86af59c",
"sha256:719a74fb9e33b9bd44cc7f3a8d94bc35e4049deebe19ba7d8e108280cfd59830"
],
"version": "==2020.12.5"
},
"chardet": {
"hashes": [
"sha256:0d6f53a15db4120f2b08c94f11e7d93d2c911ee118b6b30a04ec3ee8310179fa",
"sha256:f864054d66fd9118f2e67044ac8981a54775ec5b67aed0441892edb553d21da5"
],
"markers": "python_version >= '2.7' and python_version not in '3.0, 3.1, 3.2, 3.3, 3.4'",
"version": "==4.0.0"
},
"idna": {
"hashes": [
"sha256:b307872f855b18632ce0c21c5e45be78c0ea7ae4c15c828c20788b26921eb3f6",
"sha256:b97d804b1e9b523befed77c48dacec60e6dcb0b5391d57af6a65a312a90648c0"
],
"markers": "python_version >= '2.7' and python_version not in '3.0, 3.1, 3.2, 3.3'",
"version": "==2.10"
},
"prometheus-client": {
"hashes": [
"sha256:030e4f9df5f53db2292eec37c6255957eb76168c6f974e4176c711cf91ed34aa",
"sha256:b6c5a9643e3545bcbfd9451766cbaa5d9c67e7303c7bc32c750b6fa70ecb107d"
],
"index": "pypi",
"version": "==0.10.1"
},
"requests": {
"hashes": [
"sha256:27973dd4a904a4f13b263a19c866c13b92a39ed1c964655f025f3f8d3d75b804",
"sha256:c210084e36a42ae6b9219e00e48287def368a26d03a048ddad7bfee44f75871e"
],
"index": "pypi",
"version": "==2.25.1"
},
"urllib3": {
"hashes": [
"sha256:2f4da4594db7e1e110a944bb1b551fdf4e6c136ad42e4234131391e21eb5b0df",
"sha256:e7b021f7241115872f92f43c6508082facffbd1c048e3c6e2bb9c2a157e28937"
],
"markers": "python_version >= '2.7' and python_version not in '3.0, 3.1, 3.2, 3.3, 3.4' and python_version < '4'",
"version": "==1.26.4"
}
},
"develop": {}
}

View file

@ -1,4 +0,0 @@
# RouterOS Restful API
## Dev

39
main.py
View file

@ -1,49 +1,38 @@
import requests as req
import json
from requests.models import Response
from requests.packages.urllib3.exceptions import InsecureRequestWarning
from prometheus_client import start_http_server, Gauge
import time, os
import random, time, os
def process_request(router_ip,router_username,router_password):
url = 'https://' + router_ip + '/rest/interface'
req.packages.urllib3.disable_warnings(InsecureRequestWarning)
try:
response = req.get(url, auth=(router_username,router_password), verify=False, timeout=5)
except urllib3.exceptions.ReadTimeoutError:
print("Connect timeout")
response = req.get(url, auth=(router_username,router_password), verify=False, timeout=5)
if response.status_code != 200:
quit()
return response.json()
def declare_prometheus_metrics(data,router_ip):
def declare_prometheus_metrics(data):
prom = dict()
metrics = ['fp_rx_byte','fp_tx_byte','tx_byte','rx_byte']
metrics = ['fp-rx-byte','fp-tx-byte','tx-byte','rx-byte']
# declare all metrics
for key in metrics:
name = 'routeros_interface_' + key
prom[name] = Gauge(name,"RouterOS interface " + key, ['routerip','interface'])
for key in data:
name = 'routeros_' + key['name'] + '_rx_byte'
prom[name] = Gauge(name, "rx_byte " + key['name'])
return prom
def prom_request(data,prom,routerip):
metrics = {'fp_rx_byte' : 'fp-rx-byte','fp_tx_byte' : 'fp-tx-byte','tx_byte' : 'tx-byte','rx_byte' : 'rx-byte'}
def prom_request(data,prom):
for key in data:
for m in metrics:
name = 'routeros_interface_' + m
interface = key['name']
value = key[metrics[m]]
prom[name].labels(routerip=routerip,interface=interface).set(value)
name = 'routeros_' + key['name'] + '_rx_byte'
print(name)
prom[name].set(key['rx-byte'])
if __name__ == '__main__':
router_ip = os.environ['ROUTER_IP']
router_username = os.environ['ROUTER_USERNAME']
router_password = os.environ['ROUTER_PASSWORD']
@ -53,10 +42,10 @@ if __name__ == '__main__':
# init metrics
data = process_request(router_ip,router_username,router_password)
prom = declare_prometheus_metrics(data,router_ip)
prom = declare_prometheus_metrics(data)
# Generate requests.
while True:
data = process_request(router_ip,router_username,router_password)
prom_request(data,prom,router_ip)
prom_request(data,prom)
time.sleep(3)