|
1 | 1 | import os |
2 | | -import requests |
3 | 2 |
|
4 | 3 |
|
5 | | -class Collector(object): |
6 | | - def __init__(self, backend_url, bot_token): |
7 | | - self.backend_url = backend_url |
8 | | - self.bot_token = bot_token |
9 | | - |
10 | | - def fetch_accounts_entities(self, protocol): |
11 | | - # find all the accounts we have access to: |
12 | | - r = requests.get('{}/accounts/?b={}'.format(self.backend_url, self.bot_token)) |
13 | | - if r.status_code != 200: |
14 | | - raise Exception("Invalid bot token or network error, got status {} while retrieving {}/accounts".format(r.status_code, self.backend_url)) |
15 | | - j = r.json() |
16 | | - accounts_ids = [a["id"] for a in j["list"]] |
17 | | - |
18 | | - # find all entities for each of the accounts: |
19 | | - for account_id in accounts_ids: |
20 | | - r = requests.get('{}/accounts/{}/entities/?b={}'.format(self.backend_url, account_id, self.bot_token)) |
21 | | - if r.status_code != 200: |
22 | | - raise Exception("Network error, got status {} while retrieving {}/accounts/{}/entities".format(r.status_code, self.backend_url, account_id)) |
23 | | - j = r.json() |
24 | | - entities_ids = [e["id"] for e in j["list"]] |
25 | | - |
26 | | - for entity_id in entities_ids: |
27 | | - r = requests.get('{}/accounts/{}/entities/{}?b={}'.format(self.backend_url, account_id, entity_id, self.bot_token)) |
28 | | - if r.status_code != 200: |
29 | | - raise Exception("Network error, got status {} while retrieving {}/accounts/{}/entities/{}".format(r.status_code, self.backend_url, account_id, entity_id)) |
30 | | - entity_info = r.json() |
31 | | - |
32 | | - # make sure that the protocol is enabled on the entity: |
33 | | - if protocol not in entity_info["protocols"]: |
34 | | - continue |
35 | | - # and hide all other protocols: (not strictly necessary, just cleaner) |
36 | | - entity_info["protocols"] = { |
37 | | - protocol: entity_info["protocols"][protocol] |
38 | | - } |
39 | | - |
40 | | - if not entity_info["protocols"][protocol]["sensors"]: |
41 | | - continue |
42 | | - |
43 | | - credential = None |
44 | | - if entity_info["protocols"][protocol]["credential"]: |
45 | | - credential_id = entity_info["protocols"][protocol]["credential"] |
46 | | - r = requests.get('{}/accounts/{}/credentials/{}?b={}'.format(self.backend_url, account_id, credential_id, self.bot_token)) |
47 | | - if r.status_code != 200: |
48 | | - raise Exception("Network error, got status {} while retrieving {}/accounts/{}/credentials/{}".format(r.status_code, self.backend_url, account_id, credential_id)) |
49 | | - credential = r.json() |
50 | | - |
51 | | - sensors = [] |
52 | | - for sensor in entity_info["protocols"][protocol]["sensors"]: |
53 | | - r = requests.get('{}/accounts/{}/sensors/{}?b={}'.format(self.backend_url, account_id, sensor["sensor"], self.bot_token)) |
54 | | - if r.status_code != 200: |
55 | | - raise Exception("Network error, got status {} while retrieving {}/accounts/{}/sensors/{}".format(r.status_code, self.backend_url, account_id, sensor["sensor"])) |
56 | | - sensors.append({ |
57 | | - "sensor": r.json(), |
58 | | - "interval": sensor["interval"], |
59 | | - }) |
60 | | - entity_info["protocols"][protocol]["sensors"] = sensors |
61 | | - |
62 | | - yield account_id, entity_info, credential |
| 4 | +from collector import Collector |
63 | 5 |
|
64 | 6 |
|
65 | 7 | class SNMPCollector(Collector): |
|
0 commit comments