blob: 44c91ec4ae4f2822038c1bcd82703ff2115417ae [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
11from flask_restful import Resource, Api
Shad Ansari6fcfa292022-01-28 00:34:13 +000012import logging as log
Shad Ansari5b9d1f52022-01-29 01:42:45 +000013from argparse import ArgumentParser, SUPPRESS
Shad Ansari500f9a02022-02-04 21:15:24 +000014import threading
Shad Ansari5b9d1f52022-01-29 01:42:45 +000015
16from roc import Roc
Shad Ansarid88692c2022-02-01 22:47:43 +000017from prom import Prometheus
18from ping import ping
19import device
Shad Ansari1dcfdb32022-01-24 23:13:06 +000020
21app = Flask(__name__)
22api = Api(app)
23
Shad Ansarid88692c2022-02-01 22:47:43 +000024devices = {} # dict imsi:device
Shad Ansari500f9a02022-02-04 21:15:24 +000025lock = threading.Lock()
Shad Ansari6fcfa292022-01-28 00:34:13 +000026
Shad Ansari5b9d1f52022-01-29 01:42:45 +000027
Shad Ansari500f9a02022-02-04 21:15:24 +000028class Devices(Resource):
29 def get(self):
30 global devices, lock
31 with lock:
32 all = {}
33 for _, device in devices.items():
34 all[device.imsi_id] = {'ip':device.ip, 'imsi':device.imsi, 'last_reachable':'{:%Y-%m-%d %H:%M:%S}'.format(device.last_reachable)}
35 return all
Shad Ansarid88692c2022-02-01 22:47:43 +000036
37class ReachableDevices(Resource):
Shad Ansari1dcfdb32022-01-24 23:13:06 +000038 def get(self):
Shad Ansari500f9a02022-02-04 21:15:24 +000039 global devices, lock
40 with lock:
41 reachable = {}
42 for _, device in devices.items():
43 if device.reachable is True:
44 reachable[device.imsi_id] = {'ip':device.ip, 'imsi':device.imsi, 'last_reachable':'{:%Y-%m-%d %H:%M:%S}'.format(device.last_reachable)}
45 return reachable
Shad Ansari5b9d1f52022-01-29 01:42:45 +000046
Shad Ansarid88692c2022-02-01 22:47:43 +000047class UnreachableDevices(Resource):
Shad Ansari5b9d1f52022-01-29 01:42:45 +000048 def get(self):
Shad Ansari500f9a02022-02-04 21:15:24 +000049 global devices, lock
50 with lock:
51 unreachable = {}
52 for _, device in devices.items():
53 if device.reachable is False:
54 unreachable[device.imsi_id] = {'ip':device.ip, 'imsi':device.imsi, 'last_reachable':'{:%Y-%m-%d %H:%M:%S}'.format(device.last_reachable)}
55 return unreachable
Shad Ansari1dcfdb32022-01-24 23:13:06 +000056
Shad Ansari6fcfa292022-01-28 00:34:13 +000057
Shad Ansari5b9d1f52022-01-29 01:42:45 +000058
59def build_argparser():
60 parser = ArgumentParser(add_help=False)
61 args = parser.add_argument_group('Options')
62 args.add_argument('-h', '--help',
63 action='help',
64 default=SUPPRESS,
65 help='Show this help message and exit.')
66 args.add_argument("--user",
67 help="ROC username",
68 type=str)
69 args.add_argument("--password",
70 help="ROC password",
71 type=str)
Shad Ansarid88692c2022-02-01 22:47:43 +000072 args.add_argument("--token",
73 help="Rancher bearer token",
74 type=str)
Shad Ansari500f9a02022-02-04 21:15:24 +000075 args.add_argument("--port",
76 help="Service port",
77 type=str,
78 default="3333")
Shad Ansari5b9d1f52022-01-29 01:42:45 +000079 return parser
80
Shad Ansari500f9a02022-02-04 21:15:24 +000081def update(roc, prom, devices):
Shad Ansarid88692c2022-02-01 22:47:43 +000082 roc.update_devices(devices)
Shad Ansarid88692c2022-02-01 22:47:43 +000083 prom.update_devices(devices)
84
Shad Ansari500f9a02022-02-04 21:15:24 +000085def probe(devices):
Shad Ansarid88692c2022-02-01 22:47:43 +000086 for imsi_id, device in devices.items():
87 if device.ip is None:
88 continue
89 if ping(device.ip):
90 device.reachable = True
91 device.last_reachable = datetime.now()
92 log.info("{}/{}/{} - reachable".format(device.imsi_id, device.imsi, device.ip))
93 else:
94 device.reachable = False
95 log.info("{}/{}/{} - unreachable".format(device.imsi_id, device.imsi, device.ip))
96
Shad Ansari500f9a02022-02-04 21:15:24 +000097def work_thread(roc, prom):
98 global devices, lock
99 while True:
100 temp_devices = {}
101 update(roc, prom, temp_devices)
102 probe(temp_devices)
103 with lock:
104 devices = temp_devices
105 time.sleep(5)
Shad Ansari5b9d1f52022-01-29 01:42:45 +0000106
Shad Ansari500f9a02022-02-04 21:15:24 +0000107if __name__ == '__main__':
108
109 log.basicConfig(
110 format='%(asctime)s %(levelname)-8s %(message)s',
111 level=log.DEBUG,
112 datefmt='%Y-%m-%d %H:%M:%S',
113 stream=sys.stdout)
114
115 api.add_resource(Devices, '/devices')
116 api.add_resource(ReachableDevices, '/devices/reachable')
117 api.add_resource(UnreachableDevices, '/devices/unreachable')
118
119 log.info("Starting network-diag-app...")
120
121 args = build_argparser().parse_args()
122
123 roc = Roc(args.user, args.password)
124 prom = Prometheus(args.token.split(':')[0], args.token.split(':')[1])
125
126 t = threading.Thread(target=work_thread, args=(roc, prom,))
127 t.start()
128
129 app.run('0.0.0.0', args.port)