11import requests
2+ import logging
23
34
45class Collector (object ):
56 def __init__ (self , backend_url , bot_token ):
67 self .backend_url = backend_url
78 self .bot_token = bot_token
89
9- def fetch_accounts_entities (self , protocol ):
10+ def fetch_job_configs (self , protocol ):
11+ """
12+ Returns pairs (account_id, entity_info), where entity_info is everything needed for collecting data
13+ from the entity - credentials and list of sensors (with intervals) for selected protocol.
14+ The data is cleaned up as much as possible, so that it only contains the things necessary for collectors
15+ to do their job.
16+ """
1017 # find all the accounts we have access to:
1118 r = requests .get ('{}/accounts/?b={}' .format (self .backend_url , self .bot_token ))
1219 if r .status_code != 200 :
@@ -31,32 +38,45 @@ def fetch_accounts_entities(self, protocol):
3138 # make sure that the protocol is enabled on the entity:
3239 if protocol not in entity_info ["protocols" ]:
3340 continue
34- # and hide all other protocols: (not strictly necessary, just cleaner)
35- entity_info ["protocols" ] = {
36- protocol : entity_info [ "protocols" ][ protocol ]
37- }
38-
41+ # and that credential is set:
42+ if not entity_info ["protocols" ][ protocol ][ "credential" ]:
43+ continue
44+ credential_id = entity_info [ "protocols" ][ protocol ][ "credential" ]
45+ # and that there is at least one sensor enabled for this protocol:
3946 if not entity_info ["protocols" ][protocol ]["sensors" ]:
4047 continue
4148
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+ r = requests .get ('{}/accounts/{}/credentials/{}?b={}' .format (self .backend_url , account_id , credential_id , self .bot_token ))
50+ if r .status_code != 200 :
51+ raise Exception ("Network error, got status {} while retrieving {}/accounts/{}/credentials/{}" .format (r .status_code , self .backend_url , account_id , credential_id ))
52+ credential = r .json ()
53+ entity_info ["credential_details" ] = credential ["details" ]
4954
5055 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 ))
56+ for sensor_info in entity_info ["protocols" ][protocol ]["sensors" ]:
57+ sensor_id = sensor_info ["sensor" ]
58+ r = requests .get ('{}/accounts/{}/sensors/{}?b={}' .format (self .backend_url , account_id , sensor_id , self .bot_token ))
5359 if r .status_code != 200 :
5460 raise Exception ("Network error, got status {} while retrieving {}/accounts/{}/sensors/{}" .format (r .status_code , self .backend_url , account_id , sensor ["sensor" ]))
61+ sensor = r .json ()
62+
63+ # determine interval, since this part is generic:
64+ if sensor_info ["interval" ] is not None :
65+ interval = sensor_info ["interval" ]
66+ elif sensor ["default_interval" ] is not None :
67+ interval = sensor ["default_interval" ]
68+ else :
69+ logging .warn ("Interval not set, ignoring sensor {} on entity {}!" .format (sensor_id , entity_id ))
70+ continue
71+ del sensor ["default_interval" ] # cleanup - nobody should need this anymore
72+
5573 sensors .append ({
56- "sensor " : r . json () ,
57- "interval" : sensor [ " interval" ] ,
74+ "sensor_details " : sensor [ "details" ] ,
75+ "interval" : interval ,
5876 })
59- entity_info ["protocols" ][protocol ]["sensors" ] = sensors
77+ # and hide all other protocols, saving just sensors for selected one: (not strictly necessary, just cleaner)
78+ entity_info ["sensors" ] = sensors
79+ del entity_info ["protocols" ]
6080
61- yield account_id , entity_info , credential
81+ yield account_id , entity_info
6282
0 commit comments