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