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'] |
| 61 | if imsi in devices: |
| 62 | # log.info("{} added to list of monitored devices".format(imsi)) |
| 63 | devices[imsi].ip = metric['mobile_ip'] |
| 64 | else: |
| 65 | log.error("Ignoring device as it was not reported by ROC - {}".format(metric)) |
| 66 | else: |
| 67 | log.error("Ignoring device as imsi or mobile-ip not found - {}".format(metric)) |
| 68 | |
| 69 | def dump(self): |
| 70 | print(pyaml.dump(self.get_ips())) |
| 71 | |
| 72 | |
| 73 | if __name__ == '__main__': |
| 74 | log.basicConfig() |
| 75 | log.getLogger().setLevel(log.DEBUG) |
| 76 | requests_log = log.getLogger("requests.packages.urllib3") |
| 77 | requests_log.setLevel(log.DEBUG) |
| 78 | requests_log.propagate = True |
| 79 | |
| 80 | # use Rancher secret-key:bearer-token |
| 81 | prom = Prometheus("secret-key", "bearer-token") |
| 82 | |
| 83 | # prom.get_ips() |
| 84 | prom.dump() |