-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathForeman.py
executable file
·242 lines (218 loc) · 7.5 KB
/
Foreman.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
#!/usr/bin/python
#coding=utf-8
import requests, json, socket
import time, os
from datetime import datetime
import urllib3; urllib3.disable_warnings()
# ToDo: Make sure that it works correctly on EthOS
# > https://www.reddit.com/r/gpumining/comments/8q1hie/a_persistant_telegram_dashboard_for_claymore_with/e0k53qb/
# ToDo: Adapt to more mining software: xmr-stak
# > https://www.reddit.com/r/gpumining/comments/8q1hie/a_persistant_telegram_dashboard_for_claymore_with/e0hask4/
# ToDo: Adapt to more mining software: PhoenixMiner
# > tg:@IcedMilo
# Constants
__version__ = 0.4
__debug = False
__request_timeout = 10 # Timeout for HTTP requests
__sleep_success = 180 # Time before rechecking the miner if the operation (get_data+send) is successful
__sleep_failure_min = 10 # Used as a base for retries AND as a retry time when server can't be reached
__sleep_failure_max = __sleep_success # When miner check fails, the waited time will increase until it hits the the max
__max_tries = 3 # Max retries before giving up on an operation
def load_file_json(file_name):
with open(file_name, 'r') as _file:
content = _file.read()
content_dict = json.loads(content)
_file.close()
return content_dict
# Config
__config = load_file_json("config.json")
# Variables
clear_order = True
def contact_miner(command, ip, port, psw=None, timeout=10):
__miner_commands = {
"stats": "miner_getstat1",
"restart": "miner_restart",
"reboot": "miner_reboot"
}
try:
assert command in __miner_commands
#
data = {"jsonrpc": "2.0", "id": 0, "method": __miner_commands[command]}
if psw is not None: data["psw"] = psw.encode("ascii")
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.settimeout(timeout)
s.connect((ip, port))
data = bytearray(str(data).replace('\'', '\"'), "ascii")
s.sendall(data)
res = s.recv(1024)
s.close()
if res == '':
return {"success": False, "message": "No data received."}
else:
return {"success": True, "result": json.loads(res.decode("utf-8"))["result"]}
except AssertionError:
return {"success": False, "message": "Invalid command."}
except socket.error:
return {"success": False, "message": "Can't establish a connection."}
def handle_stats(stats):
if not stats["success"]:
return {"success": False, "message": "No stats available"}
else:
_result = stats["result"]
print(_result)
result_dict = {
"version": _result[0],
"uptime": _result[1],
"primary_hash_total": int(_result[2].split(";")[0]), # kH/s
"primary_shares_total": int(_result[2].split(";")[1]),
"primary_rejected_total": int(_result[2].split(";")[2]),
"primary_hash_gpu": _result[3].split(";"), # kH/s,
"dual_hash_total": int(_result[4].split(';')[0]), # kH/s
"dual_shares_total": int(_result[4].split(';')[1]),
"dual_rejected_total": int(_result[4].split(';')[2]),
"dual_hash_gpu": _result[5].split(';'), # kH/s, OFF if no DCR
"gpu_temp": _result[6].split(';')[::2],
"gpu_fan": _result[6].split(';')[1::2],
"pool_current": _result[7].split(';'), # array of [primary, dual]
"primary_invalid_total": int(_result[8].split(';')[0]),
"primary_pool_switches": int(_result[8].split(';')[1]),
"dual_invalid_total": int(_result[8].split(';')[2]),
"dual_pool_switches": int(_result[8].split(';')[3])
}
return {"success": True, "result": result_dict}
def try_send_data(data):
global clear_order
# build data
_data = {
"user": __config["user"]["id"],
"key": __config["user"]["key"],
"params": __config["params"], # these are optional
"timestamp": time.time(),
"clear_order": clear_order,
"version": __version__,
"data": data
}
# send data
i = 0
_req = None
_message = "Unknown error"
while i < __max_tries:
try:
i += 1
_req = requests.post(
url=__config["user"]["server"],
data=json.dumps(_data),
headers={"content-type": "application/json", "connection": "close"},
timeout=__request_timeout,
verify=False
)
if _req.status_code == 200:
i = __max_tries
print(">>> ORDER = '%s'" % _req.text)
_order = _req.text
if not (_order is None or _order == ""):
if _order == "cleared":
clear_order = False
else:
execute_order(_order)
else:
if i < __max_tries: time.sleep(int(__sleep_failure_min/__max_tries)+1)
except requests.exceptions.Timeout:
time.sleep(int(__sleep_failure_min/__max_tries)+1)
_message = "Connection timed out."
except requests.ConnectionError:
time.sleep(int(__sleep_failure_min/__max_tries)+1)
_message = "Can not establish a connection with remote server."
except:
time.sleep(int(__sleep_failure_min/__max_tries)+1)
print("Unhandled error.")
_message = "Unhandled error."
# Return status
if _req is None:
return {"success": False, "message": _message}
else:
if _req.status_code != 200:
return {"success": False, "message": "Sorry, couldn't send the message. Error: %s" % _req.status_code}
else:
return {"success": True, "result": "Data sent correctly!"}
def try_get_data():
_max_tries = __max_tries
i = 0
_data = {"success": False, "message": "Something went really, really wrong."}
while i < _max_tries:
if __debug: print("Loading...")
_data = handle_stats(contact_miner("stats", __config["miner"]["ip"], __config["miner"]["port"], psw=__config["miner"]["pass"], timeout=__request_timeout))
if __debug: print("+++ tried: %i" % (i + 1))
if _data["success"]:
i = _max_tries
else:
i += 1
if i < _max_tries: time.sleep(int(__sleep_failure_min / _max_tries) + 1)
return _data
def execute_order(order):
global clear_order
_res = try_execute_order(order)
if _res["success"]:
clear_order = True
def try_execute_order(order):
_max_tries = __max_tries
i = 0
_res = {"success": False, "message": "Something went really, really wrong."}
while i < _max_tries:
if __debug: print("Trying to execute order '%s'..." % order)
_method = "restart"
_pass = None
if order == "reboot": _method = "reboot"
if "pass" in __config["miner"]: _pass = __config["miner"]["pass"]
_res = contact_miner(
_method,
__config["miner"]["ip"],
__config["miner"]["port"],
psw=_pass,
timeout=__request_timeout
)
if _res["success"]:
i = _max_tries
else:
i += 1
if i < _max_tries: time.sleep(int(__sleep_failure_max / _max_tries) + 1)
return _res
def main():
# Variables
_sleep_failure = __sleep_failure_min
_fails = 0
# loop
while True:
# Retrieve data from miner
_data = try_get_data()
# Check if data is good
if _data["success"]:
if __debug: print(_data["result"])
# Clean
_fails = 0
_sleep_failure = __sleep_failure_min
# Send the data to server
_req = try_send_data(_data)
if __debug: print(_req)
# If data is sent, wait __sleep_success, else, consider as failure and try as fast as possible
if _req["success"]:
# Assume server cleaned the orders as requested
print(
"\nUpdated on %s\nWaiting %i seconds..."
% (datetime.fromtimestamp(time.time()).strftime("%Y-%m-%dT%H:%M:%S"), __sleep_success)
)
time.sleep(__sleep_success)
else:
print(
"\nMiner was available at %s but the server is unreachable.\nTrying to send data in %i seconds..."
% (datetime.fromtimestamp(time.time()).strftime("%Y-%m-%dT%H:%M:%S"), __sleep_failure_min)
)
time.sleep(__sleep_failure_min)
else:
_fails += 1
print(_data["message"])
print("\nWaiting %i seconds..." % _sleep_failure)
time.sleep(_sleep_failure)
if _sleep_failure < __sleep_failure_max: _sleep_failure += __sleep_failure_min
if __name__ == "__main__":
main()