blob: c3e3238507d12754242a2497b0659e64818ed5ca [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 Ansari6fcfa292022-01-28 00:34:13 +00008
Shad Ansari1dcfdb32022-01-24 23:13:06 +00009from flask import Flask, request
10from flask_restful import Resource, Api
Shad Ansari6fcfa292022-01-28 00:34:13 +000011import logging as log
Shad Ansari5b9d1f52022-01-29 01:42:45 +000012from argparse import ArgumentParser, SUPPRESS
13
14from roc import Roc
Shad Ansarid88692c2022-02-01 22:47:43 +000015from prom import Prometheus
16from ping import ping
17import device
Shad Ansari1dcfdb32022-01-24 23:13:06 +000018
19app = Flask(__name__)
20api = Api(app)
21
Shad Ansarid88692c2022-02-01 22:47:43 +000022devices = {} # dict imsi:device
Shad Ansari6fcfa292022-01-28 00:34:13 +000023
Shad Ansari5b9d1f52022-01-29 01:42:45 +000024
Shad Ansarid88692c2022-02-01 22:47:43 +000025
26class ReachableDevices(Resource):
Shad Ansari1dcfdb32022-01-24 23:13:06 +000027 def get(self):
Shad Ansarid88692c2022-02-01 22:47:43 +000028 global devices
29 return device.get_reachable(devices)
Shad Ansari5b9d1f52022-01-29 01:42:45 +000030
Shad Ansarid88692c2022-02-01 22:47:43 +000031class UnreachableDevices(Resource):
Shad Ansari5b9d1f52022-01-29 01:42:45 +000032 def get(self):
Shad Ansarid88692c2022-02-01 22:47:43 +000033 global devices
34 return device.get_unreachable(devices)
Shad Ansari1dcfdb32022-01-24 23:13:06 +000035
Shad Ansari6fcfa292022-01-28 00:34:13 +000036
Shad Ansarid88692c2022-02-01 22:47:43 +000037api.add_resource(ReachableDevices, '/reachable-devices')
38api.add_resource(UnreachableDevices, '/unreachable-devices')
Shad Ansari5b9d1f52022-01-29 01:42:45 +000039
40
41def build_argparser():
42 parser = ArgumentParser(add_help=False)
43 args = parser.add_argument_group('Options')
44 args.add_argument('-h', '--help',
45 action='help',
46 default=SUPPRESS,
47 help='Show this help message and exit.')
48 args.add_argument("--user",
49 help="ROC username",
50 type=str)
51 args.add_argument("--password",
52 help="ROC password",
53 type=str)
Shad Ansarid88692c2022-02-01 22:47:43 +000054 args.add_argument("--token",
55 help="Rancher bearer token",
56 type=str)
Shad Ansari5b9d1f52022-01-29 01:42:45 +000057 return parser
58
Shad Ansari1dcfdb32022-01-24 23:13:06 +000059
60if __name__ == '__main__':
Shad Ansari6fcfa292022-01-28 00:34:13 +000061 log.basicConfig(
Shad Ansari5b9d1f52022-01-29 01:42:45 +000062 format='%(asctime)s %(levelname)-8s %(message)s',
63 level=log.DEBUG,
64 datefmt='%Y-%m-%d %H:%M:%S',
65 stream=sys.stdout)
Shad Ansari6fcfa292022-01-28 00:34:13 +000066
67 log.info("Starting network-diag-app...")
68
Shad Ansari5b9d1f52022-01-29 01:42:45 +000069 args = build_argparser().parse_args()
70
71 roc = Roc(args.user, args.password)
Shad Ansarid88692c2022-02-01 22:47:43 +000072 prom = Prometheus(args.token.split(':')[0], args.token.split(':')[1])
73
74 roc.update_devices(devices)
75
76 prom.update_devices(devices)
77
78 for imsi_id, device in devices.items():
79 if device.ip is None:
80 continue
81 if ping(device.ip):
82 device.reachable = True
83 device.last_reachable = datetime.now()
84 log.info("{}/{}/{} - reachable".format(device.imsi_id, device.imsi, device.ip))
85 else:
86 device.reachable = False
87 log.info("{}/{}/{} - unreachable".format(device.imsi_id, device.imsi, device.ip))
88
Shad Ansari5b9d1f52022-01-29 01:42:45 +000089
Shad Ansari6fcfa292022-01-28 00:34:13 +000090 app.run('0.0.0.0', '3333')