|
|
- #! /usr/bin/python3
- # coding=utf-8
-
- import requests
- import json
- import sys
- from pyzabbix import ZabbixMetric, ZabbixSender
-
- hostname = 'hostname1'
- zabbix_server = '10.3.2.5'
- aw_url = 'http://10.5.0.1:17790/api/miners'
-
-
- class AwZabbix:
- def __init__(self, server, url, host):
- self.url = url
- self.server = server
- self.host = host
- self.data = ''
- self.mult_dict = {'G': 1000000000, 'M': 1000000, 'T': 1000000000000, 'K': 1000, 'N': 0, }
- self.packet = []
- self.lld_data = []
-
- def get_data(self):
- if len(self.data) == 0:
- self.data = requests.get(self.url).json()
-
- def make_packet(self):
- self.get_data()
- if len(self.packet) != 0:
- return self.packet
- total_hashrate_raw = self.data['totalHashrate5s']
- if total_hashrate_raw:
- hashrate_mult = self.mult_dict[total_hashrate_raw[-4:-3]]
- total_hashrate = float(total_hashrate_raw[:-5].replace(',', '.')) * hashrate_mult
- total_hashrate = int(total_hashrate)
- self.packet.append(ZabbixMetric(self.host, 'total[hashrate]', total_hashrate))
- for group in self.data['groupList']:
- for miner in group['minerList']:
- hashrate_raw = miner['speedInfo']['hashrate']
- if hashrate_raw:
- hashrate_mult = self.mult_dict[hashrate_raw[-4:-3]]
- hashrate = float(hashrate_raw[:-5].replace(',', '.')) * hashrate_mult
- hashrate = int(hashrate)
- else:
- hashrate = 0
- minername = str(miner['name'])
- self.packet.append(ZabbixMetric(self.host, "{}[{}]".format('hashrate', minername), hashrate))
- self.packet.append(ZabbixMetric(self.host, "{}[{}]".format('ip', minername), miner['hostname']))
- self.packet.append(ZabbixMetric(self.host, "{}[{}]".format('group', minername), miner['groupId']))
- self.packet.append(ZabbixMetric(self.host, "{}[{}]".format('name', minername), miner['name']))
- self.packet.append(ZabbixMetric(self.host, "{}[{}]".format('temp', minername),
- miner['temperature'][:-3]))
- self.packet.append(ZabbixMetric(self.host, "{}[{}]".format('status', minername),
- miner['statusInfo']['statusDisplay']))
- return self.packet
-
- def send_packet(self):
- if len(self.packet) == 0:
- self.make_packet()
- return ZabbixSender(self.server).send(self.packet)
-
- def lld(self):
- self.get_data()
- if len(self.lld_data) != 0:
- return self.lld_data
- self.lld_data = {}
- jdat = []
- for group in self.data['groupList']:
- for miner in group['minerList']:
- jdat.append({"{#MINERID}": str(miner['id']),
- "{#IP}": str(miner['hostname']),
- "{#MINERNAME}": str(miner['name'])})
- self.lld_data = {'data': jdat}
- return self.lld_data
-
-
- def read_conf():
- with open('config.json') as f:
- conf = json.load(f)
- return conf
-
-
- def main():
- conf = read_conf()
-
- z = AwZabbix(zabbix_server, aw_url, hostname)
-
- if len(sys.argv) == 1:
- print(json.dumps(z.lld(), indent=4, sort_keys=True))
- elif len(sys.argv) == 2 and sys.argv[1] == 'send':
- z.send_packet()
- print(1)
- elif len(sys.argv) == 2 and sys.argv[1] == 'send_debug':
- print(z.send_packet())
- for i in z.packet:
- print(i)
- else:
- print('Wrong args.')
-
-
- if __name__ == '__main__':
- main()
|