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