blob: e7ede6ab9ed1f5d91fd9fad27c750537fca93299 [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 Ansariae3903e2022-02-05 01:03:01 +000081def update(roc, prom, old):
82 new = roc.update_devices(old)
83 prom.update_devices(new)
84 return new
Shad Ansarid88692c2022-02-01 22:47:43 +000085
Shad Ansari500f9a02022-02-04 21:15:24 +000086def probe(devices):
Shad Ansarid88692c2022-02-01 22:47:43 +000087 for imsi_id, device in devices.items():
88 if device.ip is None:
89 continue
90 if ping(device.ip):
91 device.reachable = True
92 device.last_reachable = datetime.now()
93 log.info("{}/{}/{} - reachable".format(device.imsi_id, device.imsi, device.ip))
94 else:
95 device.reachable = False
96 log.info("{}/{}/{} - unreachable".format(device.imsi_id, device.imsi, device.ip))
97
Shad Ansari500f9a02022-02-04 21:15:24 +000098def work_thread(roc, prom):
99 global devices, lock
100 while True:
Shad Ansariae3903e2022-02-05 01:03:01 +0000101 new = update(roc, prom, devices)
102 probe(new)
Shad Ansari500f9a02022-02-04 21:15:24 +0000103 with lock:
Shad Ansariae3903e2022-02-05 01:03:01 +0000104 devices = new
105
Shad Ansari500f9a02022-02-04 21:15:24 +0000106 time.sleep(5)
Shad Ansari5b9d1f52022-01-29 01:42:45 +0000107
Shad Ansari500f9a02022-02-04 21:15:24 +0000108if __name__ == '__main__':
109
110 log.basicConfig(
111 format='%(asctime)s %(levelname)-8s %(message)s',
112 level=log.DEBUG,
113 datefmt='%Y-%m-%d %H:%M:%S',
114 stream=sys.stdout)
115
116 api.add_resource(Devices, '/devices')
117 api.add_resource(ReachableDevices, '/devices/reachable')
118 api.add_resource(UnreachableDevices, '/devices/unreachable')
119
120 log.info("Starting network-diag-app...")
121
122 args = build_argparser().parse_args()
123
124 roc = Roc(args.user, args.password)
125 prom = Prometheus(args.token.split(':')[0], args.token.split(':')[1])
126
127 t = threading.Thread(target=work_thread, args=(roc, prom,))
128 t.start()
129
130 app.run('0.0.0.0', args.port)