Compare commits
2 commits
182cde368a
...
9af0739d80
Author | SHA1 | Date | |
---|---|---|---|
Yann Verry | 9af0739d80 | ||
Yann Verry | 9ca2319662 |
14
Dockerfile
Normal file
14
Dockerfile
Normal file
|
@ -0,0 +1,14 @@
|
||||||
|
#
|
||||||
|
# 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
|
7
Makefile
Normal file
7
Makefile
Normal file
|
@ -0,0 +1,7 @@
|
||||||
|
all: pull build
|
||||||
|
|
||||||
|
pull:
|
||||||
|
docker pull python:alpine
|
||||||
|
|
||||||
|
build:
|
||||||
|
docker build --network yann_default -t python-routeros .
|
68
Pipfile.lock
generated
Normal file
68
Pipfile.lock
generated
Normal file
|
@ -0,0 +1,68 @@
|
||||||
|
{
|
||||||
|
"_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": {}
|
||||||
|
}
|
41
main.py
41
main.py
|
@ -1,38 +1,49 @@
|
||||||
import requests as req
|
import requests as req
|
||||||
import json
|
|
||||||
|
|
||||||
from requests.models import Response
|
from requests.models import Response
|
||||||
|
from requests.packages.urllib3.exceptions import InsecureRequestWarning
|
||||||
|
|
||||||
from prometheus_client import start_http_server, Gauge
|
from prometheus_client import start_http_server, Gauge
|
||||||
import random, time, os
|
import time, os
|
||||||
|
|
||||||
def process_request(router_ip,router_username,router_password):
|
def process_request(router_ip,router_username,router_password):
|
||||||
url = 'https://' + router_ip + '/rest/interface'
|
url = 'https://' + router_ip + '/rest/interface'
|
||||||
|
req.packages.urllib3.disable_warnings(InsecureRequestWarning)
|
||||||
response = req.get(url, auth=(router_username,router_password), verify=False, timeout=5)
|
try:
|
||||||
|
response = req.get(url, auth=(router_username,router_password), verify=False, timeout=5)
|
||||||
|
except urllib3.exceptions.ReadTimeoutError:
|
||||||
|
print("Connect timeout")
|
||||||
|
|
||||||
if response.status_code != 200:
|
if response.status_code != 200:
|
||||||
quit()
|
quit()
|
||||||
|
|
||||||
return response.json()
|
return response.json()
|
||||||
|
|
||||||
def declare_prometheus_metrics(data):
|
def declare_prometheus_metrics(data,router_ip):
|
||||||
|
|
||||||
prom = dict()
|
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
|
# declare all metrics
|
||||||
for key in data:
|
for key in metrics:
|
||||||
name = 'routeros_' + key['name'] + '_rx_byte'
|
name = 'routeros_interface_' + key
|
||||||
prom[name] = Gauge(name, "rx_byte " + key['name'])
|
prom[name] = Gauge(name,"RouterOS interface " + key, ['routerip','interface'])
|
||||||
|
|
||||||
return prom
|
return prom
|
||||||
|
|
||||||
def prom_request(data,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'}
|
||||||
|
|
||||||
for key in data:
|
for key in data:
|
||||||
name = 'routeros_' + key['name'] + '_rx_byte'
|
for m in metrics:
|
||||||
print(name)
|
name = 'routeros_interface_' + m
|
||||||
prom[name].set(key['rx-byte'])
|
interface = key['name']
|
||||||
|
value = key[metrics[m]]
|
||||||
|
prom[name].labels(routerip=routerip,interface=interface).set(value)
|
||||||
|
|
||||||
|
|
||||||
if __name__ == '__main__':
|
if __name__ == '__main__':
|
||||||
|
|
||||||
|
|
||||||
router_ip = os.environ['ROUTER_IP']
|
router_ip = os.environ['ROUTER_IP']
|
||||||
router_username = os.environ['ROUTER_USERNAME']
|
router_username = os.environ['ROUTER_USERNAME']
|
||||||
router_password = os.environ['ROUTER_PASSWORD']
|
router_password = os.environ['ROUTER_PASSWORD']
|
||||||
|
@ -42,10 +53,10 @@ if __name__ == '__main__':
|
||||||
|
|
||||||
# init metrics
|
# init metrics
|
||||||
data = process_request(router_ip,router_username,router_password)
|
data = process_request(router_ip,router_username,router_password)
|
||||||
prom = declare_prometheus_metrics(data)
|
prom = declare_prometheus_metrics(data,router_ip)
|
||||||
|
|
||||||
# Generate requests.
|
# Generate requests.
|
||||||
while True:
|
while True:
|
||||||
data = process_request(router_ip,router_username,router_password)
|
data = process_request(router_ip,router_username,router_password)
|
||||||
prom_request(data,prom)
|
prom_request(data,prom,router_ip)
|
||||||
time.sleep(3)
|
time.sleep(3)
|
||||||
|
|
Loading…
Reference in a new issue