Add prometheus labels, interface only

This commit is contained in:
Yann Verry 2021-05-29 22:46:31 +02:00
parent 182cde368a
commit 9ca2319662
Signed by: yann
GPG key ID: 19754E8692B8416C

38
main.py
View file

@ -2,37 +2,49 @@ 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 random, time, os
def process_request(router_ip,router_username,router_password):
url = 'https://' + router_ip + '/rest/interface'
response = req.get(url, auth=(router_username,router_password), verify=False, timeout=5)
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")
if response.status_code != 200:
quit()
return response.json()
def declare_prometheus_metrics(data):
def declare_prometheus_metrics(data,router_ip):
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 data:
name = 'routeros_' + key['name'] + '_rx_byte'
prom[name] = Gauge(name, "rx_byte " + key['name'])
for key in metrics:
name = 'routeros_interface_' + key
prom[name] = Gauge(name,"RouterOS interface " + key, ['routerip','interface'])
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:
name = 'routeros_' + key['name'] + '_rx_byte'
print(name)
prom[name].set(key['rx-byte'])
for m in metrics:
name = 'routeros_interface_' + m
interface = key['name']
value = key[metrics[m]]
prom[name].labels(routerip=routerip,interface=interface).set(value)
if __name__ == '__main__':
router_ip = os.environ['ROUTER_IP']
router_username = os.environ['ROUTER_USERNAME']
router_password = os.environ['ROUTER_PASSWORD']
@ -42,10 +54,10 @@ if __name__ == '__main__':
# init metrics
data = process_request(router_ip,router_username,router_password)
prom = declare_prometheus_metrics(data)
prom = declare_prometheus_metrics(data,router_ip)
# Generate requests.
while True:
data = process_request(router_ip,router_username,router_password)
prom_request(data,prom)
prom_request(data,prom,router_ip)
time.sleep(3)