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 | |
| 13 | #from device import Devices |
| 14 | |
| 15 | PROMETHEUS = "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" |
| 17 | AUTH = ('token-m44n6', 'sgnxmckznp5kl6wqqxxk5pzd5ptc8qx9bqtfkfqxfx29qvqr8gld2f') |
| 18 | |
| 19 | |
| 20 | class 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 Ansari | 907b771 | 2022-02-07 21:48:04 +0000 | [diff] [blame] | 31 | 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 Ansari | d88692c | 2022-02-01 22:47:43 +0000 | [diff] [blame] | 36 | 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 Ansari | 907b771 | 2022-02-07 21:48:04 +0000 | [diff] [blame] | 51 | 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 Ansari | d88692c | 2022-02-01 22:47:43 +0000 | [diff] [blame] | 56 | 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 Ansari | ae3903e | 2022-02-05 01:03:01 +0000 | [diff] [blame] | 69 | for k in devices: |
| 70 | if devices[k].imsi == imsi: |
| 71 | devices[k].ip = metric['mobile_ip'] |
Shad Ansari | d88692c | 2022-02-01 22:47:43 +0000 | [diff] [blame] | 72 | else: |
| 73 | log.error("Ignoring device as imsi or mobile-ip not found - {}".format(metric)) |
| 74 | |
Shad Ansari | 907b771 | 2022-02-07 21:48:04 +0000 | [diff] [blame] | 75 | return devices |
| 76 | |
Shad Ansari | d88692c | 2022-02-01 22:47:43 +0000 | [diff] [blame] | 77 | def dump(self): |
| 78 | print(pyaml.dump(self.get_ips())) |
| 79 | |
| 80 | |
| 81 | if __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() |