blob: 9dfa4daff1d746ac7d4a3b11eac84f226b185cee [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
Shad Ansarid88692c2022-02-01 22:47:43 +000013PROMETHEUS = "https://rancher.aetherproject.org/k8s/clusters/c-xp25p/api/v1/namespaces/cattle-monitoring-system" \
14 "/services/http:rancher-monitoring-prometheus:9090/proxy/api/v1/query"
15AUTH = ('token-m44n6', 'sgnxmckznp5kl6wqqxxk5pzd5ptc8qx9bqtfkfqxfx29qvqr8gld2f')
16
17
18class Prometheus(object):
19
20 def __init__(self, key, token):
21 self.key = key
22 self.token = token
23
24 def get_ips(self):
25 ips = {}
26 params = (
27 ('query', 'subscribers_info>0'),
28 )
Shad Ansari907b7712022-02-07 21:48:04 +000029 try:
30 response = requests.get(PROMETHEUS, params=params, auth=(self.key, self.token), timeout=10)
31 except requests.ReadTimeout as e:
32 log.error("Rancher/Prometheus request timeout, error={}".format(e))
33 sys.exit()
Shad Ansarid88692c2022-02-01 22:47:43 +000034 if response.status_code != 200:
35 log.error("get_subscriberinfo() failed, status_code: {}".format(response.status_code))
36 return None
37 metrics = json.loads(response.text)['data']['result']
38 for metric in metrics:
39 device = metric['metric']
40 if 'imsi' in device and 'mobile_ip' in device:
41 ips[device['imsi']] = device['mobile_ip']
42 return ips
43
44 def update_devices(self, devices):
45 params = (
46 ('query', 'subscribers_info'),
47 )
48
Shad Ansari907b7712022-02-07 21:48:04 +000049 try:
50 response = requests.get(PROMETHEUS, params=params, auth=(self.key, self.token), timeout=10)
51 except requests.ReadTimeout as e:
52 log.error("Rancher/Prometheus request timeout, error={}".format(e))
53 sys.exit()
Shad Ansarid88692c2022-02-01 22:47:43 +000054 if response.status_code != 200:
55 log.error("get_subscriberinfo() failed, status_code: {}".format(response.status_code))
56 sys.exit()
Shad Ansarid88692c2022-02-01 22:47:43 +000057
58 metrics = json.loads(response.text)['data']['result']
59
60 # log.debug(pyaml.dump(metrics))
61
62 for elem in metrics:
63 metric = elem['metric']
64 if 'imsi' in metric and 'mobile_ip' in metric:
65 imsi = metric['imsi']
Shad Ansariae3903e2022-02-05 01:03:01 +000066 for k in devices:
67 if devices[k].imsi == imsi:
68 devices[k].ip = metric['mobile_ip']
Shad Ansarid88692c2022-02-01 22:47:43 +000069 else:
70 log.error("Ignoring device as imsi or mobile-ip not found - {}".format(metric))
71
Shad Ansari907b7712022-02-07 21:48:04 +000072 return devices
73
Shad Ansarid88692c2022-02-01 22:47:43 +000074 def dump(self):
75 print(pyaml.dump(self.get_ips()))
76
77
78if __name__ == '__main__':
79 log.basicConfig()
80 log.getLogger().setLevel(log.DEBUG)
81 requests_log = log.getLogger("requests.packages.urllib3")
82 requests_log.setLevel(log.DEBUG)
83 requests_log.propagate = True
84
85 # use Rancher secret-key:bearer-token
86 prom = Prometheus("secret-key", "bearer-token")
87
88 # prom.get_ips()
89 prom.dump()