2021-05-19 21:05:54 +00:00
|
|
|
import requests as req
|
|
|
|
|
|
|
|
from requests.models import Response
|
2021-05-29 20:46:31 +00:00
|
|
|
from requests.packages.urllib3.exceptions import InsecureRequestWarning
|
|
|
|
|
2021-05-19 21:05:54 +00:00
|
|
|
from prometheus_client import start_http_server, Gauge
|
2021-05-29 21:14:26 +00:00
|
|
|
import time, os
|
2021-12-25 20:01:09 +00:00
|
|
|
import re
|
2021-05-19 21:05:54 +00:00
|
|
|
|
2021-10-08 20:06:28 +00:00
|
|
|
def mkt_restapi(router_ip,router_username,router_password, path):
|
|
|
|
url = 'https://' + router_ip + '/rest/' + path
|
2021-05-29 20:46:31 +00:00
|
|
|
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")
|
|
|
|
|
2021-05-19 21:05:54 +00:00
|
|
|
if response.status_code != 200:
|
|
|
|
quit()
|
|
|
|
|
|
|
|
return response.json()
|
|
|
|
|
2021-10-08 20:06:28 +00:00
|
|
|
def init_prometheus_interface_metrics(data,router_ip):
|
2021-05-19 21:05:54 +00:00
|
|
|
|
|
|
|
prom = dict()
|
2021-05-29 20:46:31 +00:00
|
|
|
metrics = ['fp_rx_byte','fp_tx_byte','tx_byte','rx_byte']
|
2021-05-19 21:05:54 +00:00
|
|
|
# declare all metrics
|
2021-05-29 20:46:31 +00:00
|
|
|
for key in metrics:
|
|
|
|
name = 'routeros_interface_' + key
|
|
|
|
prom[name] = Gauge(name,"RouterOS interface " + key, ['routerip','interface'])
|
|
|
|
|
2021-05-19 21:05:54 +00:00
|
|
|
return prom
|
|
|
|
|
2021-05-29 20:46:31 +00:00
|
|
|
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'}
|
|
|
|
|
2021-05-19 21:05:54 +00:00
|
|
|
for key in data:
|
2021-05-29 20:46:31 +00:00
|
|
|
for m in metrics:
|
|
|
|
name = 'routeros_interface_' + m
|
|
|
|
interface = key['name']
|
|
|
|
value = key[metrics[m]]
|
|
|
|
prom[name].labels(routerip=routerip,interface=interface).set(value)
|
2021-05-19 21:05:54 +00:00
|
|
|
|
|
|
|
|
2021-10-08 20:06:28 +00:00
|
|
|
def init_prometheus_cpu_metrics(data,router_ip):
|
|
|
|
|
|
|
|
prom = dict()
|
|
|
|
|
|
|
|
name = 'routeros_load'
|
|
|
|
prom[name] = Gauge(name,"RouterOS CPU load " + name, ['routerip','cpu'])
|
|
|
|
name = 'routeros_irq'
|
|
|
|
prom[name] = Gauge(name,"RouterOS CPU IRQ " + name, ['routerip','cpu'])
|
|
|
|
|
|
|
|
return prom
|
|
|
|
|
|
|
|
def prom_cpu_request(data,prom,routerip):
|
|
|
|
|
|
|
|
for key in data:
|
|
|
|
name = 'routeros_load'
|
|
|
|
cpu = key['cpu']
|
|
|
|
value = key['load']
|
|
|
|
prom[name].labels(routerip=routerip,cpu=cpu).set(value)
|
|
|
|
|
|
|
|
name = 'routeros_irq'
|
|
|
|
cpu = key['cpu']
|
|
|
|
value = key['irq']
|
|
|
|
prom[name].labels(routerip=routerip,cpu=cpu).set(value)
|
|
|
|
|
2021-12-25 20:01:09 +00:00
|
|
|
def init_prometheus_resource(data,router_ip):
|
|
|
|
|
|
|
|
prom = dict()
|
|
|
|
|
|
|
|
name = 'routeros_resource_version'
|
|
|
|
prom[name] = Gauge(name,"RouterOS version ", ['routerip','version'])
|
|
|
|
name = 'routeros_resource_uptime'
|
|
|
|
prom[name] = Gauge(name,"RouterOS Uptime" , ['routerip'])
|
|
|
|
|
|
|
|
return prom
|
|
|
|
|
|
|
|
def uptime_extractor(uptime):
|
2021-12-30 22:44:09 +00:00
|
|
|
# extract day
|
|
|
|
try:
|
|
|
|
day = re.findall("(^|\D)(\d+)d",uptime)[0][1]
|
|
|
|
day = int(day)
|
|
|
|
except:
|
2022-01-06 12:12:51 +00:00
|
|
|
day = 0
|
2021-12-30 22:44:09 +00:00
|
|
|
|
2021-12-25 20:01:09 +00:00
|
|
|
# extract hour
|
|
|
|
try:
|
2021-12-30 22:44:09 +00:00
|
|
|
hour = re.findall("(^|\D)(\d+)h",uptime)[0][1]
|
2021-12-25 20:01:09 +00:00
|
|
|
hour = int(hour)
|
|
|
|
except:
|
|
|
|
hour = 0
|
|
|
|
|
|
|
|
# extract minute
|
|
|
|
try:
|
2021-12-30 22:44:09 +00:00
|
|
|
minute = re.findall("(^|\D)(\d+)m",uptime)[0][1]
|
2021-12-25 20:01:09 +00:00
|
|
|
minute = int(minute)
|
|
|
|
except:
|
|
|
|
minute = 0
|
|
|
|
|
|
|
|
# extract second
|
|
|
|
try:
|
2021-12-30 22:44:09 +00:00
|
|
|
second = re.findall("(^|\D)(\d+)s",uptime)[0][1]
|
2021-12-25 20:01:09 +00:00
|
|
|
second = int(second)
|
|
|
|
except:
|
|
|
|
second = 0
|
|
|
|
# remove all value extracted
|
2021-12-30 22:44:09 +00:00
|
|
|
uptime = 86400 * day + 3600 * hour + minute * 60 + second
|
2021-12-25 20:01:09 +00:00
|
|
|
|
|
|
|
return uptime
|
|
|
|
|
|
|
|
|
|
|
|
def prom_resource_request(data,prom,routerip):
|
|
|
|
name = 'routeros_resource_version'
|
|
|
|
version = data['version']
|
|
|
|
value = 1
|
|
|
|
prom[name].labels(routerip=routerip,version=version).set(value)
|
|
|
|
|
|
|
|
name = 'routeros_resource_uptime'
|
|
|
|
uptime = uptime_extractor(data['uptime'])
|
|
|
|
prom[name].labels(routerip=routerip).set(uptime)
|
|
|
|
|
|
|
|
|
2021-10-08 20:06:28 +00:00
|
|
|
|
2021-05-19 21:05:54 +00:00
|
|
|
if __name__ == '__main__':
|
|
|
|
|
2021-05-29 20:46:31 +00:00
|
|
|
|
2021-10-08 20:29:49 +00:00
|
|
|
try:
|
|
|
|
router_ip = os.environ['ROUTER_IP']
|
|
|
|
router_username = os.environ['ROUTER_USERNAME']
|
|
|
|
router_password = os.environ['ROUTER_PASSWORD']
|
|
|
|
except KeyError as e:
|
|
|
|
print("Fatal Error missing: " + str(e) + " variable, exiting")
|
|
|
|
os._exit(-1)
|
|
|
|
|
|
|
|
# PORT
|
|
|
|
try:
|
|
|
|
exporter_port = os.environ['PORT']
|
|
|
|
except KeyError:
|
|
|
|
exporter_port = 8000
|
|
|
|
|
|
|
|
# Interval
|
|
|
|
try:
|
|
|
|
interval = os.environ['INTERVAL']
|
|
|
|
except KeyError:
|
|
|
|
interval = 30
|
2021-05-19 21:05:54 +00:00
|
|
|
|
|
|
|
# Start up the server to expose the metrics.
|
2021-10-08 20:29:49 +00:00
|
|
|
start_http_server(exporter_port)
|
|
|
|
print("Running RouterOS exporter on port " + str(exporter_port) + " fetch interval is " + str(interval) + "s")
|
2021-10-08 20:06:28 +00:00
|
|
|
|
|
|
|
# interface metrics
|
|
|
|
data_interface = mkt_restapi(router_ip,router_username,router_password,'interface')
|
|
|
|
prom_interface = init_prometheus_interface_metrics(data_interface,router_ip)
|
|
|
|
|
|
|
|
# system/resource/cpu metrics
|
|
|
|
data_cpu = mkt_restapi(router_ip,router_username,router_password,'system/resource/cpu')
|
|
|
|
prom_cpu = init_prometheus_cpu_metrics(data_cpu,router_ip)
|
2021-05-19 21:05:54 +00:00
|
|
|
|
2021-12-25 20:01:09 +00:00
|
|
|
# system/resource
|
|
|
|
data_resource = mkt_restapi(router_ip,router_username,router_password,'system/resource')
|
|
|
|
prom_resource = init_prometheus_resource(data_resource,router_ip)
|
2021-05-19 21:05:54 +00:00
|
|
|
|
|
|
|
# Generate requests.
|
|
|
|
while True:
|
2021-10-08 20:06:28 +00:00
|
|
|
# interface
|
|
|
|
data_interface = mkt_restapi(router_ip,router_username,router_password,'interface')
|
|
|
|
prom_request(data_interface,prom_interface,router_ip)
|
|
|
|
# system/resource/cpu
|
|
|
|
data_cpu = mkt_restapi(router_ip,router_username,router_password,'system/resource/cpu')
|
|
|
|
prom_cpu_request(data_cpu,prom_cpu,router_ip)
|
|
|
|
|
2021-12-25 20:01:09 +00:00
|
|
|
prom_resource_request(data_resource,prom_resource,router_ip)
|
|
|
|
|
2021-10-08 20:29:49 +00:00
|
|
|
time.sleep(interval)
|