Add prometheus labels, interface only
This commit is contained in:
parent
182cde368a
commit
9ca2319662
36
main.py
36
main.py
|
@ -2,37 +2,49 @@ import requests as req
|
||||||
import json
|
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 random, 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)
|
||||||
|
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:
|
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 +54,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