diff --git a/telegraf/hiveos/farm.conf.j2 b/telegraf/hiveos/farm.conf.j2 index e8c3ada..4c0b3b6 100644 --- a/telegraf/hiveos/farm.conf.j2 +++ b/telegraf/hiveos/farm.conf.j2 @@ -6,3 +6,4 @@ data_format = "json" tagexclude = ["url"] tag_keys = ["name", "id"] + diff --git a/telegraf/hiveos/generate.py b/telegraf/hiveos/generate.py index 763af53..41837e7 100644 --- a/telegraf/hiveos/generate.py +++ b/telegraf/hiveos/generate.py @@ -35,31 +35,52 @@ class HiveAPI: self.session = session self.base_url = 'https://api2.hiveos.farm/api/v2' self.farms = {} - for farm_id in self.fetch_farms(): - self.farms[farm_id] = self.fetch_workers(farm_id=farm_id) + for farm in self.fetch_farms(): + self.farms[farm] = self.fetch_workers(farm_id=farm.number) def fetch_farms(self): r = self.session.get(f'{self.base_url}/farms') r.raise_for_status() farms = r.json().get('data') if farms: - return [f.get('id') for f in farms] + return [Farm(number=f['id'], name=f['name']) for f in farms] def fetch_workers(self, farm_id): r = self.session.get(f'{self.base_url}/farms/{farm_id}/workers') r.raise_for_status() workers = r.json().get('data') if workers: - return [w.get('id') for w in workers] + return [Worker(number=w['id'], farm_id=farm_id, name=w['name']) for w in workers] + + +class Farm: + def __init__(self, number, name): + self.number = number + self.name = name + + def __repr__(self): + return f'' + + +class Worker: + def __init__(self, number, farm_id, name): + self.number = number + self.farm_id = farm_id + self.name = name + + def __repr__(self): + return f'' def create_farm_configuration(farm_id): - return create_configuration(template_name='farm.conf.j2', template_variables={'farm_id': farm_id}) + template_variables = {'farm_id': farm_id} + return create_configuration(template_name='farm.conf.j2', template_variables=template_variables) -def create_worker_configuration(farm_id, worker_id): - return create_configuration(template_name='worker.conf.j2', - template_variables={'farm_id': farm_id, 'worker_id': worker_id}) +def create_worker_configuration(farm_id, farm_name, worker_id, worker_name): + template_variables = {'farm_id': farm_id, 'farm_name': farm_name, 'worker_id': worker_id, + 'worker_name': worker_name} + return create_configuration(template_name='worker.conf.j2', template_variables=template_variables) def create_configuration(template_name, template_variables): @@ -84,12 +105,13 @@ def main(): api = HiveAPI(token=token) configurations = [] - for farm_id in api.farms: - logger.info(f'generating configuration for farm {farm_id}') - configurations.append(create_farm_configuration(farm_id=farm_id)) - for worker_id in api.farms[farm_id]: - logger.info(f'generating configuration for worker {worker_id}') - configurations.append(create_worker_configuration(farm_id=farm_id, worker_id=worker_id)) + for farm in api.farms: + logger.info(f"generating configuration for farm {farm}") + configurations.append(create_farm_configuration(farm_id=farm.number)) + for worker in api.farms[farm]: + logger.info(f'generating configuration for worker {worker}') + configurations.append(create_worker_configuration(farm_id=farm.number, farm_name=farm.name, + worker_id=worker.number, worker_name=worker.name)) logger.info('writing configuration to file') with open('hiveos.conf', 'w') as fd: diff --git a/telegraf/hiveos/hiveos.conf b/telegraf/hiveos/hiveos.conf deleted file mode 100644 index 1cfdb9f..0000000 --- a/telegraf/hiveos/hiveos.conf +++ /dev/null @@ -1,20 +0,0 @@ -[[inputs.http]] - name_override = "hiveos_farm" - urls = ["https://api2.hiveos.farm/api/v2/farms/418240"] - headers = {"Content-Type" = "application/json"} - headers = {"Authorization" = "Bearer ${HIVEOS_TOKEN}"} - data_format = "json" - tagexclude = ["url"] - tag_keys = ["name", "id"] -[[inputs.http]] - name_override = "hiveos_gpu" - urls = ["https://api2.hiveos.farm/api/v2/farms/418240/workers/1150566"] - headers = {"Content-Type" = "application/json"} - headers = {"Authorization" = "Bearer ${HIVEOS_TOKEN}"} - data_format = "json" - tagexclude = ["url"] - tag_keys = ["bus_number"] - json_query = "gpu_stats" - [inputs.http.tags] - farm_id = "418240" - worker_id = "1150566" diff --git a/telegraf/hiveos/worker.conf.j2 b/telegraf/hiveos/worker.conf.j2 index 86acba3..a69ad20 100644 --- a/telegraf/hiveos/worker.conf.j2 +++ b/telegraf/hiveos/worker.conf.j2 @@ -9,5 +9,7 @@ json_query = "gpu_stats" [inputs.http.tags] farm_id = "{{farm_id}}" + farm_name = "{{farm_name}}" worker_id = "{{worker_id}}" + worker_name = "{{worker_name}}"