#! /usr/bin/python3 # coding=utf-8 import requests import json import sys from pyzabbix import ZabbixMetric, ZabbixSender import os import re 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 hashrate_str_to_int(self, hashrate_raw): if "H" in hashrate_raw: m = hashrate_raw[-4:-3] if m[0] == ' ': hashrate_mult = 1 else: hashrate_mult = self.mult_dict[hashrate_raw[-4:-3]] hashrate_raw = "".join(hashrate_raw.split()) a = float(hashrate_raw[:-5].replace(',', '.')) * hashrate_mult else: a = 0 return int(a) 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: total_hashrate = self.hashrate_str_to_int(total_hashrate_raw) 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 = self.hashrate_str_to_int(hashrate_raw) else: hashrate = 0 hashrate_avg_raw = miner['speedInfo']['avgHashrate'] if hashrate_avg_raw: hashrate_avg = self.hashrate_str_to_int(hashrate_avg_raw) else: hashrate_avg = 0 minername = str(miner['name']) self.packet.append(ZabbixMetric(self.host, "{}[{}]".format('hashrate', minername), hashrate)) self.packet.append(ZabbixMetric(self.host, "{}[{}]".format('hashrate_avg', minername), hashrate_avg)) self.packet.append(ZabbixMetric(self.host, "{}[{}]".format('hashrate_avg_min', minername), int(hashrate_avg * 0.8))) 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(): path = os.path.dirname(os.path.realpath(__file__)) conf_file = os.path.join(path, 'config.json') with open(conf_file) as f: conf = json.load(f) return conf def main(): conf = read_conf() zabbix_server = conf["zabbix_server"] aw_url = conf["aw_url"] hostname = conf["hostname"] 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()