Shad Ansari | d88692c | 2022-02-01 22:47:43 +0000 | [diff] [blame] | 1 | """ |
Shad Ansari | 7e28dc6 | 2022-06-07 13:43:22 -0700 | [diff] [blame^] | 2 | SPDX-FileCopyrightText: 2020-present Intel Corporation |
Shad Ansari | d88692c | 2022-02-01 22:47:43 +0000 | [diff] [blame] | 3 | SPDX-FileCopyrightText: 2020-present Open Networking Foundation <info@opennetworking.org> |
| 4 | SPDX-License-Identifier: LicenseRef-ONF-Member-1.01 |
| 5 | """ |
| 6 | |
| 7 | import sys |
| 8 | import json |
| 9 | import logging as log |
| 10 | |
| 11 | import pyaml |
| 12 | import requests |
| 13 | |
Shad Ansari | d88692c | 2022-02-01 22:47:43 +0000 | [diff] [blame] | 14 | PROMETHEUS = "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" |
| 16 | AUTH = ('token-m44n6', 'sgnxmckznp5kl6wqqxxk5pzd5ptc8qx9bqtfkfqxfx29qvqr8gld2f') |
| 17 | |
| 18 | |
| 19 | class 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 Ansari | 907b771 | 2022-02-07 21:48:04 +0000 | [diff] [blame] | 30 | 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 Ansari | d88692c | 2022-02-01 22:47:43 +0000 | [diff] [blame] | 35 | 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 Ansari | 907b771 | 2022-02-07 21:48:04 +0000 | [diff] [blame] | 50 | 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 Ansari | d88692c | 2022-02-01 22:47:43 +0000 | [diff] [blame] | 55 | if response.status_code != 200: |
| 56 | log.error("get_subscriberinfo() failed, status_code: {}".format(response.status_code)) |
| 57 | sys.exit() |
Shad Ansari | d88692c | 2022-02-01 22:47:43 +0000 | [diff] [blame] | 58 | |
| 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 Ansari | ae3903e | 2022-02-05 01:03:01 +0000 | [diff] [blame] | 67 | for k in devices: |
| 68 | if devices[k].imsi == imsi: |
| 69 | devices[k].ip = metric['mobile_ip'] |
Shad Ansari | d88692c | 2022-02-01 22:47:43 +0000 | [diff] [blame] | 70 | else: |
| 71 | log.error("Ignoring device as imsi or mobile-ip not found - {}".format(metric)) |
| 72 | |
Shad Ansari | 907b771 | 2022-02-07 21:48:04 +0000 | [diff] [blame] | 73 | return devices |
| 74 | |
Shad Ansari | d88692c | 2022-02-01 22:47:43 +0000 | [diff] [blame] | 75 | def dump(self): |
| 76 | print(pyaml.dump(self.get_ips())) |
| 77 | |
| 78 | |
| 79 | if __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() |