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