blob: 3adb9dd2584ba313825d4f6b842a1fc8524dfeda [file] [log] [blame]
"""
SPDX-FileCopyrightText: 2020-present Open Networking Foundation <info@opennetworking.org>
SPDX-License-Identifier: LicenseRef-ONF-Member-1.01
"""
import sys
import re
from datetime import datetime
import time
from flask import Flask, request
import logging as log
from argparse import ArgumentParser, SUPPRESS
import threading
from roc import Roc
from prom import Prometheus
from ping import ping
import device
app = Flask(__name__)
devices = {} # dict imsi:device
lock = threading.Lock()
@app.route("/devices")
def get_devices():
global devices, lock
with lock:
all = {}
for _, device in devices.items():
all[device.imsi_id] = {'ip':device.ip, 'imsi':device.imsi, 'last_reachable':'{:%Y-%m-%d %H:%M:%S}'.format(device.last_reachable)}
return all
@app.route("/devices/reachable")
def get_devices_reachable():
global devices, lock
with lock:
reachable = {}
for _, device in devices.items():
if device.reachable is True:
reachable[device.imsi_id] = {'ip':device.ip, 'imsi':device.imsi, 'last_reachable':'{:%Y-%m-%d %H:%M:%S}'.format(device.last_reachable)}
return reachable
@app.route("/devices/unreachable")
def get_devices_unreachable():
global devices, lock
with lock:
unreachable = {}
for _, device in devices.items():
if device.reachable is False:
unreachable[device.imsi_id] = {'ip':device.ip, 'imsi':device.imsi, 'last_reachable':'{:%Y-%m-%d %H:%M:%S}'.format(device.last_reachable)}
return unreachable
def build_argparser():
parser = ArgumentParser(add_help=False)
args = parser.add_argument_group('Options')
args.add_argument('-h', '--help',
action='help',
default=SUPPRESS,
help='Show this help message and exit.')
args.add_argument("--user",
help="ROC username",
type=str)
args.add_argument("--password",
help="ROC password",
type=str)
args.add_argument("--token",
help="Rancher bearer token",
type=str)
args.add_argument("--port",
help="Service port",
type=str,
default="3333")
return parser
def update(roc, prom, old):
new = roc.update_devices(old)
if new is not None:
new = prom.update_devices(new)
else:
new = old
return new
def probe(devices):
for imsi_id, device in devices.items():
if device.ip is None:
continue
if ping(device.ip):
device.reachable = True
device.last_reachable = datetime.now()
log.info("{}/{}/{} - reachable".format(device.imsi_id, device.imsi, device.ip))
else:
device.reachable = False
log.info("{}/{}/{} - unreachable".format(device.imsi_id, device.imsi, device.ip))
def work_thread(roc, prom):
global devices, lock
while True:
new = update(roc, prom, devices)
probe(new)
with lock:
devices = new
time.sleep(5)
if __name__ == '__main__':
log.basicConfig(
format='%(asctime)s %(levelname)-8s %(message)s',
level=log.DEBUG,
datefmt='%Y-%m-%d %H:%M:%S',
stream=sys.stdout)
log.info("Starting network-diag-app...")
args = build_argparser().parse_args()
roc = Roc(args.user, args.password)
prom = Prometheus(args.token.split(':')[0], args.token.split(':')[1])
t = threading.Thread(target=work_thread, args=(roc, prom,))
t.start()
app.run('0.0.0.0', args.port)