Skip to content

Commit 7e02375

Browse files
author
Anze
committed
Separate generic (protocol agnostic) and SNMP specific code
1 parent a03d98f commit 7e02375

File tree

2 files changed

+63
-59
lines changed

2 files changed

+63
-59
lines changed

collector.py

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

grafolean-worker-snmp.py

Lines changed: 1 addition & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -1,65 +1,7 @@
11
import os
2-
import requests
32

43

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
635

646

657
class SNMPCollector(Collector):

0 commit comments

Comments
 (0)