blob: 1b2c075ef9e225519efee359aeebd5df92509218 [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 Ansari0a905032022-02-10 19:37:15 +000055@app.route("/config")
56def config():
57 global args
58 period = request.args.get('period')
59 if period is not None:
60 args.period = int(period)
61 config = vars(args)
62 config.pop('token', None)
63 config.pop('user', None)
64 config.pop('password', None)
65 return config
Shad Ansari6fcfa292022-01-28 00:34:13 +000066
Shad Ansari5b9d1f52022-01-29 01:42:45 +000067
68def build_argparser():
69 parser = ArgumentParser(add_help=False)
70 args = parser.add_argument_group('Options')
71 args.add_argument('-h', '--help',
72 action='help',
73 default=SUPPRESS,
74 help='Show this help message and exit.')
75 args.add_argument("--user",
76 help="ROC username",
77 type=str)
78 args.add_argument("--password",
79 help="ROC password",
80 type=str)
Shad Ansarid88692c2022-02-01 22:47:43 +000081 args.add_argument("--token",
82 help="Rancher bearer token",
83 type=str)
Shad Ansari500f9a02022-02-04 21:15:24 +000084 args.add_argument("--port",
85 help="Service port",
86 type=str,
87 default="3333")
Shad Ansari0a905032022-02-10 19:37:15 +000088 args.add_argument("--period",
89 help="Probing period in sec",
90 type=int,
91 default=180)
Shad Ansari5b9d1f52022-01-29 01:42:45 +000092 return parser
93
Shad Ansariae3903e2022-02-05 01:03:01 +000094def update(roc, prom, old):
95 new = roc.update_devices(old)
Shad Ansari907b7712022-02-07 21:48:04 +000096 if new is not None:
97 new = prom.update_devices(new)
98 else:
99 new = old
Shad Ansariae3903e2022-02-05 01:03:01 +0000100 return new
Shad Ansarid88692c2022-02-01 22:47:43 +0000101
Shad Ansari500f9a02022-02-04 21:15:24 +0000102def probe(devices):
Shad Ansarid88692c2022-02-01 22:47:43 +0000103 for imsi_id, device in devices.items():
104 if device.ip is None:
105 continue
106 if ping(device.ip):
107 device.reachable = True
108 device.last_reachable = datetime.now()
109 log.info("{}/{}/{} - reachable".format(device.imsi_id, device.imsi, device.ip))
110 else:
111 device.reachable = False
112 log.info("{}/{}/{} - unreachable".format(device.imsi_id, device.imsi, device.ip))
113
Shad Ansari500f9a02022-02-04 21:15:24 +0000114def work_thread(roc, prom):
Shad Ansari0a905032022-02-10 19:37:15 +0000115 global devices, lock, args
Shad Ansari500f9a02022-02-04 21:15:24 +0000116 while True:
Shad Ansariae3903e2022-02-05 01:03:01 +0000117 new = update(roc, prom, devices)
118 probe(new)
Shad Ansari500f9a02022-02-04 21:15:24 +0000119 with lock:
Shad Ansariae3903e2022-02-05 01:03:01 +0000120 devices = new
121
Shad Ansari0a905032022-02-10 19:37:15 +0000122 time.sleep(args.period)
Shad Ansari5b9d1f52022-01-29 01:42:45 +0000123
Shad Ansari500f9a02022-02-04 21:15:24 +0000124if __name__ == '__main__':
125
126 log.basicConfig(
127 format='%(asctime)s %(levelname)-8s %(message)s',
128 level=log.DEBUG,
129 datefmt='%Y-%m-%d %H:%M:%S',
130 stream=sys.stdout)
131
Shad Ansari500f9a02022-02-04 21:15:24 +0000132 log.info("Starting network-diag-app...")
133
134 args = build_argparser().parse_args()
135
136 roc = Roc(args.user, args.password)
137 prom = Prometheus(args.token.split(':')[0], args.token.split(':')[1])
138
139 t = threading.Thread(target=work_thread, args=(roc, prom,))
140 t.start()
141
142 app.run('0.0.0.0', args.port)