blob: 5c4668c93f0c43522f25f36bb139b55f9da80f2e [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 )
31 response = requests.get(PROMETHEUS, params=params, auth=(self.key, self.token))
32 if response.status_code != 200:
33 log.error("get_subscriberinfo() failed, status_code: {}".format(response.status_code))
34 return None
35 metrics = json.loads(response.text)['data']['result']
36 for metric in metrics:
37 device = metric['metric']
38 if 'imsi' in device and 'mobile_ip' in device:
39 ips[device['imsi']] = device['mobile_ip']
40 return ips
41
42 def update_devices(self, devices):
43 params = (
44 ('query', 'subscribers_info'),
45 )
46
47 response = requests.get(PROMETHEUS, params=params, auth=(self.key, self.token))
48 if response.status_code != 200:
49 log.error("get_subscriberinfo() failed, status_code: {}".format(response.status_code))
50 sys.exit()
51 #return None
52
53 metrics = json.loads(response.text)['data']['result']
54
55 # log.debug(pyaml.dump(metrics))
56
57 for elem in metrics:
58 metric = elem['metric']
59 if 'imsi' in metric and 'mobile_ip' in metric:
60 imsi = metric['imsi']
Shad Ansariae3903e2022-02-05 01:03:01 +000061 for k in devices:
62 if devices[k].imsi == imsi:
63 devices[k].ip = metric['mobile_ip']
Shad Ansarid88692c2022-02-01 22:47:43 +000064 else:
65 log.error("Ignoring device as imsi or mobile-ip not found - {}".format(metric))
66
67 def dump(self):
68 print(pyaml.dump(self.get_ips()))
69
70
71if __name__ == '__main__':
72 log.basicConfig()
73 log.getLogger().setLevel(log.DEBUG)
74 requests_log = log.getLogger("requests.packages.urllib3")
75 requests_log.setLevel(log.DEBUG)
76 requests_log.propagate = True
77
78 # use Rancher secret-key:bearer-token
79 prom = Prometheus("secret-key", "bearer-token")
80
81 # prom.get_ips()
82 prom.dump()