blob: 3adb9dd2584ba313825d4f6b842a1fc8524dfeda [file] [log] [blame]
Shad Ansari6fcfa292022-01-28 00:34:13 +00001"""
2SPDX-FileCopyrightText: 2020-present Open Networking Foundation <info@opennetworking.org>
3SPDX-License-Identifier: LicenseRef-ONF-Member-1.01
4"""
5import sys
Shad Ansarid88692c2022-02-01 22:47:43 +00006import re
7from datetime import datetime
Shad Ansari500f9a02022-02-04 21:15:24 +00008import time
Shad Ansari6fcfa292022-01-28 00:34:13 +00009
Shad Ansari1dcfdb32022-01-24 23:13:06 +000010from flask import Flask, request
Shad Ansari6fcfa292022-01-28 00:34:13 +000011import logging as log
Shad Ansari5b9d1f52022-01-29 01:42:45 +000012from argparse import ArgumentParser, SUPPRESS
Shad Ansari500f9a02022-02-04 21:15:24 +000013import threading
Shad Ansari5b9d1f52022-01-29 01:42:45 +000014
15from roc import Roc
Shad Ansarid88692c2022-02-01 22:47:43 +000016from prom import Prometheus
17from ping import ping
18import device
Shad Ansari1dcfdb32022-01-24 23:13:06 +000019
20app = Flask(__name__)
Shad Ansari1dcfdb32022-01-24 23:13:06 +000021
Shad Ansarid88692c2022-02-01 22:47:43 +000022devices = {} # dict imsi:device
Shad Ansari500f9a02022-02-04 21:15:24 +000023lock = threading.Lock()
Shad Ansari6fcfa292022-01-28 00:34:13 +000024
Shad Ansari5b9d1f52022-01-29 01:42:45 +000025
Shad Ansari467862f2022-02-08 00:40:40 +000026@app.route("/devices")
27def get_devices():
28 global devices, lock
29 with lock:
30 all = {}
31 for _, device in devices.items():
32 all[device.imsi_id] = {'ip':device.ip, 'imsi':device.imsi, 'last_reachable':'{:%Y-%m-%d %H:%M:%S}'.format(device.last_reachable)}
33 return all
Shad Ansarid88692c2022-02-01 22:47:43 +000034
Shad Ansari467862f2022-02-08 00:40:40 +000035@app.route("/devices/reachable")
36def get_devices_reachable():
37 global devices, lock
38 with lock:
39 reachable = {}
40 for _, device in devices.items():
41 if device.reachable is True:
42 reachable[device.imsi_id] = {'ip':device.ip, 'imsi':device.imsi, 'last_reachable':'{:%Y-%m-%d %H:%M:%S}'.format(device.last_reachable)}
43 return reachable
Shad Ansari5b9d1f52022-01-29 01:42:45 +000044
Shad Ansari467862f2022-02-08 00:40:40 +000045@app.route("/devices/unreachable")
46def get_devices_unreachable():
47 global devices, lock
48 with lock:
49 unreachable = {}
50 for _, device in devices.items():
51 if device.reachable is False:
52 unreachable[device.imsi_id] = {'ip':device.ip, 'imsi':device.imsi, 'last_reachable':'{:%Y-%m-%d %H:%M:%S}'.format(device.last_reachable)}
53 return unreachable
Shad Ansari1dcfdb32022-01-24 23:13:06 +000054
Shad Ansari6fcfa292022-01-28 00:34:13 +000055
Shad Ansari5b9d1f52022-01-29 01:42:45 +000056
57def build_argparser():
58 parser = ArgumentParser(add_help=False)
59 args = parser.add_argument_group('Options')
60 args.add_argument('-h', '--help',
61 action='help',
62 default=SUPPRESS,
63 help='Show this help message and exit.')
64 args.add_argument("--user",
65 help="ROC username",
66 type=str)
67 args.add_argument("--password",
68 help="ROC password",
69 type=str)
Shad Ansarid88692c2022-02-01 22:47:43 +000070 args.add_argument("--token",
71 help="Rancher bearer token",
72 type=str)
Shad Ansari500f9a02022-02-04 21:15:24 +000073 args.add_argument("--port",
74 help="Service port",
75 type=str,
76 default="3333")
Shad Ansari5b9d1f52022-01-29 01:42:45 +000077 return parser
78
Shad Ansariae3903e2022-02-05 01:03:01 +000079def update(roc, prom, old):
80 new = roc.update_devices(old)
Shad Ansari907b7712022-02-07 21:48:04 +000081 if new is not None:
82 new = prom.update_devices(new)
83 else:
84 new = old
Shad Ansariae3903e2022-02-05 01:03:01 +000085 return new
Shad Ansarid88692c2022-02-01 22:47:43 +000086
Shad Ansari500f9a02022-02-04 21:15:24 +000087def probe(devices):
Shad Ansarid88692c2022-02-01 22:47:43 +000088 for imsi_id, device in devices.items():
89 if device.ip is None:
90 continue
91 if ping(device.ip):
92 device.reachable = True
93 device.last_reachable = datetime.now()
94 log.info("{}/{}/{} - reachable".format(device.imsi_id, device.imsi, device.ip))
95 else:
96 device.reachable = False
97 log.info("{}/{}/{} - unreachable".format(device.imsi_id, device.imsi, device.ip))
98
Shad Ansari500f9a02022-02-04 21:15:24 +000099def work_thread(roc, prom):
100 global devices, lock
101 while True:
Shad Ansariae3903e2022-02-05 01:03:01 +0000102 new = update(roc, prom, devices)
103 probe(new)
Shad Ansari500f9a02022-02-04 21:15:24 +0000104 with lock:
Shad Ansariae3903e2022-02-05 01:03:01 +0000105 devices = new
106
Shad Ansari500f9a02022-02-04 21:15:24 +0000107 time.sleep(5)
Shad Ansari5b9d1f52022-01-29 01:42:45 +0000108
Shad Ansari500f9a02022-02-04 21:15:24 +0000109if __name__ == '__main__':
110
111 log.basicConfig(
112 format='%(asctime)s %(levelname)-8s %(message)s',
113 level=log.DEBUG,
114 datefmt='%Y-%m-%d %H:%M:%S',
115 stream=sys.stdout)
116
Shad Ansari500f9a02022-02-04 21:15:24 +0000117 log.info("Starting network-diag-app...")
118
119 args = build_argparser().parse_args()
120
121 roc = Roc(args.user, args.password)
122 prom = Prometheus(args.token.split(':')[0], args.token.split(':')[1])
123
124 t = threading.Thread(target=work_thread, args=(roc, prom,))
125 t.start()
126
127 app.run('0.0.0.0', args.port)