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 | ) |
| 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 Ansari | ae3903e | 2022-02-05 01:03:01 +0000 | [diff] [blame] | 61 | for k in devices: |
| 62 | if devices[k].imsi == imsi: |
| 63 | devices[k].ip = metric['mobile_ip'] |
Shad Ansari | d88692c | 2022-02-01 22:47:43 +0000 | [diff] [blame] | 64 | 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 | |
| 71 | if __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() |