Last active
March 18, 2021 10:19
-
-
Save jffz/3570e0c1d3fea0112e82c27078c6e4e4 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python | |
import time | |
import json | |
import paho.mqtt.client as mqtt | |
import socket | |
MQTT_BROKER = "192.168.1.5" | |
MQTT_TOPIC = "hiveos" | |
TAIL_FILE = "/var/log/hive-agent.log" | |
MQTT_USER = "" | |
MQTT_PASSWORD = "" | |
def send_mqtt(broker, topic, payload, user=None, password=None): | |
mqttc = mqtt.Client() | |
if user: | |
# auth = {"username": user, "password": password} | |
mqttc.username_pw_set(MQTT_USER, password=password) | |
mqttc.connect(broker) | |
mqttc.publish(topic, payload=payload) | |
def is_json(myjson): | |
try: | |
json_object = json.loads(myjson) | |
except ValueError as e: | |
return False | |
else: | |
return True | |
def rework_dict(line): | |
if not '>' in line and not is_json(line): | |
return | |
try: | |
json_line = json.loads(line.split('>')[1]) | |
except: | |
return | |
# Handle only interesting lines | |
if not 'method' in json_line or not json_line['method'] == 'stats' or ('method' in json_line and json_line['method'] == 'stats' and json_line['params']['miner_stats'] is None): | |
return | |
print(json_line) | |
params = json_line['params'] | |
# Delete igpu stats | |
for stat in ['power', 'fan', 'temp']: | |
if stat in params and 0 in params[stat]: | |
params[stat].remove(0) | |
# Format weird fields | |
gpu_errors = [int(x) | |
for x in params['miner_stats']['ar'][-1].split(';') if x] | |
# RIG | |
rig = {} | |
rig['name'] = socket.gethostname() | |
rig['id'] = params['rig_id'] | |
rig['miner'] = params['meta'] | |
rig['cputemp'] = params['cputemp'][0] | |
rig['miner'] = params['miner'] | |
rig['mined_coin'] = params['meta'][rig['miner']]['coin'] | |
rig['disk_free'] = params['df'] | |
rig['mem_total'] = params['mem'][0] | |
rig['mem_free'] = params['mem'][1] | |
rig['cpu_avg_1'] = params['cpuavg'][0] | |
rig['cpu_avg_5'] = params['cpuavg'][1] | |
rig['cpu_avg_15'] = params['cpuavg'][2] | |
rig['khs'] = params['total_khs'] | |
rig['uptime'] = params['miner_stats']['uptime'] | |
rig['version'] = params['miner_stats']['ver'] | |
rig['accepted_hash'] = params['miner_stats']['ar'][0] | |
rig['rejected_hash'] = params['miner_stats']['ar'][1] | |
rig['rejected_hash_percent'] = ( | |
rig['rejected_hash'] * 100) / rig['accepted_hash'] | |
rig['gpu_errors'] = sum(gpu_errors) | |
rig['gpus'] = len(params['miner_stats']['bus_numbers']) | |
# GPUs | |
for gpu in range(rig['gpus']): | |
infos = {} | |
infos['hs'] = params['miner_stats']['hs'][gpu] | |
infos['power'] = params['power'][gpu] | |
infos['bus_number'] = params['miner_stats']['bus_numbers'][gpu] | |
infos['temp'] = params['miner_stats']['temp'][gpu] | |
infos['fan'] = params['miner_stats']['fan'][gpu] | |
infos['errors'] = gpu_errors[gpu] | |
rig['GPU' + str(gpu + 1)] = infos | |
return json.dumps(rig) | |
def tail_f(file): | |
with open(file, 'r') as file_: | |
file_.seek(0, 2) | |
while True: | |
curr_position = file_.tell() | |
line = file_.readline() | |
if not line: | |
file_.seek(curr_position) | |
time.sleep(1) | |
else: | |
newlog = rework_dict(line) | |
if newlog: | |
send_mqtt(MQTT_BROKER, MQTT_TOPIC, newlog) | |
print(newlog) | |
if __name__ == "__main__": | |
tail_f(TAIL_FILE) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
- type: entities | |
show_header_toggle: false | |
state_color: true | |
title: HiveOS | |
icon: mdi:bitcoin | |
entities: | |
- entity: sensor.hiveos_rig | |
name: Name | |
type: attribute | |
attribute: name | |
icon: mdi:pound | |
- entity: sensor.hiveos_rig | |
name: Hash | |
type: attribute | |
attribute: khs | |
suffix: H/s | |
icon: mdi:pound | |
- entity: sensor.hiveos_rig | |
name: CPU Temp | |
type: attribute | |
attribute: cputemp | |
suffix: °C | |
icon: mdi:thermometer | |
- entity: sensor.hiveos_rig | |
name: Reject hash ratio | |
type: attribute | |
attribute: rejected_hash_percent | |
suffix: "%" | |
icon: mdi:check | |
- entity: sensor.hiveos_gpu1 | |
name: Hash | |
type: attribute | |
attribute: hs | |
icon: mdi:expansion-card | |
- entity: sensor.hiveos_gpu1 | |
name: Consommation | |
type: attribute | |
attribute: power | |
suffix: W | |
icon: mdi:power | |
- entity: sensor.hiveos_gpu1 | |
name: Fans | |
type: attribute | |
attribute: fan | |
icon: mdi:fan | |
suffix: "%" | |
- entity: sensor.hiveos_gpu1 | |
name: Temp | |
type: attribute | |
attribute: temp | |
suffix: °C | |
icon: mdi:thermometer | |
- entity: sensor.hiveos_gpu1 | |
name: Errors | |
type: attribute | |
attribute: errors | |
icon: mdi:alert-circle | |
- entity: sensor.hiveos_gpu2 | |
name: Hash | |
type: attribute | |
attribute: hs | |
icon: mdi:expansion-card | |
- entity: sensor.hiveos_gpu2 | |
name: Consommation | |
type: attribute | |
attribute: power | |
suffix: W | |
icon: mdi:power | |
- entity: sensor.hiveos_gpu2 | |
name: Fans | |
type: attribute | |
attribute: fan | |
icon: mdi:fan | |
suffix: "%" | |
- entity: sensor.hiveos_gpu2 | |
name: Temp | |
type: attribute | |
attribute: temp | |
suffix: °C | |
icon: mdi:thermometer | |
- entity: sensor.hiveos_gpu2 | |
name: Errors | |
type: attribute | |
attribute: errors | |
icon: mdi:alert-circle |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
- platform: mqtt | |
name: hiveos_rig | |
state_topic: "hiveos" | |
value_template: "{{ value_json.id }}" | |
json_attributes_topic: "hiveos" | |
json_attributes_template: "{{ value_json | to_json }}" | |
- platform: mqtt | |
name: hiveos_gpu1 | |
state_topic: "hiveos" | |
value_template: "{{ value_json.GPU1 }}" | |
json_attributes_topic: "hiveos" | |
json_attributes_template: "{{ value_json.GPU1 | to_json }}" | |
- platform: mqtt | |
name: hiveos_gpu2 | |
state_topic: "hiveos" | |
value_template: "{{ value_json.GPU2 }}" | |
json_attributes_topic: "hiveos" | |
json_attributes_template: "{{ value_json.GPU2 | to_json }}" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Hi !
Thanks for your work !
With Python script i have an error on broker side
Socket error on client <unknown>, disconnecting
Can you help me please ?
Regards
KP