add version in label and uptime

This commit is contained in:
Yann Verry 2021-12-25 21:01:09 +01:00
parent 6fa2ff68b8
commit 7724e991b7
Signed by: yann
GPG key ID: EB9E679A66B8C7A1

63
main.py
View file

@ -5,6 +5,7 @@ from requests.packages.urllib3.exceptions import InsecureRequestWarning
from prometheus_client import start_http_server, Gauge
import time, os
import re
def mkt_restapi(router_ip,router_username,router_password, path):
url = 'https://' + router_ip + '/rest/' + path
@ -65,6 +66,62 @@ def prom_cpu_request(data,prom,routerip):
value = key['irq']
prom[name].labels(routerip=routerip,cpu=cpu).set(value)
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):
# extract hour
try:
re_h = re.compile("(\d)h")
hour = re_h.match(uptime).group(1)
hour = int(hour)
except:
hour = 0
# extract minute
try:
re_m = re.compile("(\d)m")
minute = re_m.match(uptime).group(1)
minute = int(minute)
except:
minute = 0
# extract second
try:
re_s = re.compile("(\d)s")
second = re_s.match(uptime).group(1)
second = int(second)
except:
second = 0
# current timestamp
ts = round(time.time())
# remove all value extracted
uptime = ts - 3600 * hour - minute * 60 - second
return uptime
def prom_resource_request(data,prom,routerip):
print(data)
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)
if __name__ == '__main__':
@ -101,6 +158,10 @@ if __name__ == '__main__':
data_cpu = mkt_restapi(router_ip,router_username,router_password,'system/resource/cpu')
prom_cpu = init_prometheus_cpu_metrics(data_cpu,router_ip)
# system/resource
print("resource")
data_resource = mkt_restapi(router_ip,router_username,router_password,'system/resource')
prom_resource = init_prometheus_resource(data_resource,router_ip)
# Generate requests.
while True:
@ -111,4 +172,6 @@ if __name__ == '__main__':
data_cpu = mkt_restapi(router_ip,router_username,router_password,'system/resource/cpu')
prom_cpu_request(data_cpu,prom_cpu,router_ip)
prom_resource_request(data_resource,prom_resource,router_ip)
time.sleep(interval)