blob: 3562e8e79f18d68b5c97289bda4d145e04ee99ea [file] [log] [blame]
Shad Ansarid88692c2022-02-01 22:47:43 +00001"""
Shad Ansari7e28dc62022-06-07 13:43:22 -07002SPDX-FileCopyrightText: 2020-present Intel Corporation
Shad Ansarid88692c2022-02-01 22:47:43 +00003SPDX-FileCopyrightText: 2020-present Open Networking Foundation <info@opennetworking.org>
4SPDX-License-Identifier: LicenseRef-ONF-Member-1.01
5"""
6
7import sys
8import json
9import logging as log
10
11import pyaml
12import requests
13
Shad Ansarid88692c2022-02-01 22:47:43 +000014PROMETHEUS = "https://rancher.aetherproject.org/k8s/clusters/c-xp25p/api/v1/namespaces/cattle-monitoring-system" \
15 "/services/http:rancher-monitoring-prometheus:9090/proxy/api/v1/query"
16AUTH = ('token-m44n6', 'sgnxmckznp5kl6wqqxxk5pzd5ptc8qx9bqtfkfqxfx29qvqr8gld2f')
17
18
19class Prometheus(object):
20
21 def __init__(self, key, token):
22 self.key = key
23 self.token = token
24
25 def get_ips(self):
26 ips = {}
27 params = (
28 ('query', 'subscribers_info>0'),
29 )
Shad Ansari907b7712022-02-07 21:48:04 +000030 try:
31 response = requests.get(PROMETHEUS, params=params, auth=(self.key, self.token), timeout=10)
32 except requests.ReadTimeout as e:
33 log.error("Rancher/Prometheus request timeout, error={}".format(e))
34 sys.exit()
Shad Ansarid88692c2022-02-01 22:47:43 +000035 if response.status_code != 200:
36 log.error("get_subscriberinfo() failed, status_code: {}".format(response.status_code))
37 return None
38 metrics = json.loads(response.text)['data']['result']
39 for metric in metrics:
40 device = metric['metric']
41 if 'imsi' in device and 'mobile_ip' in device:
42 ips[device['imsi']] = device['mobile_ip']
43 return ips
44
45 def update_devices(self, devices):
46 params = (
47 ('query', 'subscribers_info'),
48 )
49
Shad Ansari907b7712022-02-07 21:48:04 +000050 try:
51 response = requests.get(PROMETHEUS, params=params, auth=(self.key, self.token), timeout=10)
52 except requests.ReadTimeout as e:
53 log.error("Rancher/Prometheus request timeout, error={}".format(e))
54 sys.exit()
Shad Ansarid88692c2022-02-01 22:47:43 +000055 if response.status_code != 200:
56 log.error("get_subscriberinfo() failed, status_code: {}".format(response.status_code))
57 sys.exit()
Shad Ansarid88692c2022-02-01 22:47:43 +000058
59 metrics = json.loads(response.text)['data']['result']
60
61 # log.debug(pyaml.dump(metrics))
62
63 for elem in metrics:
64 metric = elem['metric']
65 if 'imsi' in metric and 'mobile_ip' in metric:
66 imsi = metric['imsi']
Shad Ansariae3903e2022-02-05 01:03:01 +000067 for k in devices:
68 if devices[k].imsi == imsi:
69 devices[k].ip = metric['mobile_ip']
Shad Ansarid88692c2022-02-01 22:47:43 +000070 else:
71 log.error("Ignoring device as imsi or mobile-ip not found - {}".format(metric))
72
Shad Ansari907b7712022-02-07 21:48:04 +000073 return devices
74
Shad Ansarid88692c2022-02-01 22:47:43 +000075 def dump(self):
76 print(pyaml.dump(self.get_ips()))
77
78
79if __name__ == '__main__':
80 log.basicConfig()
81 log.getLogger().setLevel(log.DEBUG)
82 requests_log = log.getLogger("requests.packages.urllib3")
83 requests_log.setLevel(log.DEBUG)
84 requests_log.propagate = True
85
86 # use Rancher secret-key:bearer-token
87 prom = Prometheus("secret-key", "bearer-token")
88
89 # prom.get_ips()
90 prom.dump()