forked from thetinkeringtypist/verium-cli-monitor
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmonitor.py
executable file
·381 lines (308 loc) · 11.9 KB
/
monitor.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
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
#!/usr/bin/env python3
#
#! Author: Bezeredi, Evan D.
#
#! CLI Monitor to display cpuminer information for each worker on the LAN
#
#! NOTE: Requires a list of hostnames/IPs on the LAN in
# /home/<username>/.chosts
#
#! Also requires miner-apid.py to running on each worker.
#
#! I have this script running on my LAN controller so that finding each machine
# on the LAN is not a problem
import time
import signal
import threading
import curses
import socket
from pathlib import Path
#! Thread variables
port = 4048
threads = []
kill_threads = threading.Event()
#! Display varaibles
stdscr = curses.initscr()
windows = []
hosts = []
statinfo_list = []
#! Initialize display variables
def init_display():
(term_height, term_width) = stdscr.getmaxyx()
for host in hosts:
#! Only (offline, host) since no value will be accessed
# other than these if the host is offline
statinfo_list.append((False, host))
#! Header window (index 0)
windows.append(curses.newwin(3, term_width, 0, 0))
#! Hosts window (pad) (index 1)
if len(hosts) < (term_height - 7): #! Header and footer
windows.append(curses.newpad(term_height - 7, term_width))
else:
windows.append(curses.newpad(len(hosts), term_width))
#! Footer window (index 2)
windows.append(curses.newwin(4, term_width, term_height - 4, 0))
if curses.has_colors():
curses.start_color()
curses.use_default_colors()
init_colors()
windows[0].attrset(curses.color_pair(7))
windows[1].attrset(curses.color_pair(7))
windows[2].attrset(curses.color_pair(7))
curses.noecho()
curses.cbreak()
windows[1].keypad(True)
windows[1].nodelay(True) #! Nonblocking user input
curses.curs_set(0)
stdscr.clear()
return
#! Define custom colors
def init_colors():
curses.use_default_colors()
for i in range(0, curses.COLORS):
curses.init_pair(i, i, -1)
#! Interrupt signal handler
def signal_handler(signal, frame):
kill_program()
#! Kill program
def kill_program():
#! Kill Threads
kill_threads.set()
print("Waiting for background threads...")
for t in threads:
t.join()
exit()
return
#! Change display to reflect that the host is offline
def set_host_offline(host):
index = hosts.index(host)
statinfo_list[index] = (False, host)
return
#! Parse message received from server
def parse_summary_msg(host, summary):
#! Get the index of the host
index = hosts.index(host)
#! Calculate hpm
hpm = summary['khps'] * 1000 * 60
total = summary['accepted'] + summary['rejected'] if summary['accepted'] + summary['rejected'] > 0 else 1
percent = summary['accepted'] / (total) * 100
#! Build the display string entry
#! (online, host, hpm, percent, blocks, difficulty, cpus, temp)
statinfo_list[index] = (
True, host, hpm, percent, summary['solved'],summary['diff'], summary['cpus'],summary['cpu_temp'])
return
#! Calc totals and averages
def get_totals_avgs():
total_hashrate = 0.0
total_solved_blocks = 0
total_cpus = 0
online_hosts = list(filter(lambda info: info[0] == True, statinfo_list))
length = len(online_hosts) if len(online_hosts) > 0 else 1
#! Calculate totals
total_hashrate = sum(i for _,_,i,_,_,_,_,_ in online_hosts)
total_solved_blocks = sum(i for _,_,_,_,i,_,_,_ in online_hosts)
total_cpus = sum(i for _,_,_,_,_,_,i,_ in online_hosts)
#! Calculate averages
avg_hashrate = total_hashrate / length
avg_share_percent = sum(i for _,_,_,i,_,_,_,_ in online_hosts) / length
avg_solved_blocks = total_solved_blocks / length
avg_difficulty = sum(i for _,_,_,_,_,i,_,_ in online_hosts) / length
avg_cpus = total_cpus / length
avg_cpu_temp = sum(i for _,_,_,_,_,_,_,i in online_hosts) / length
#! Formulate Average String
avg_str = ("Average {0:>18.3f} H/m {1:>6.2f}% {2:>6} {3:<8f} "
"│ {4:>4.2f} {5:>5.1f}°C".format(
avg_hashrate,avg_share_percent,avg_solved_blocks,
avg_difficulty,avg_cpus,avg_cpu_temp))
#! Formulate Average String
total_str = ("Total {0:>18.3f} H/m ---.--% {1:>6} -.------ "
"│ {2:>4} ---.-°C".format(
total_hashrate,total_solved_blocks,total_cpus))
return (total_str, avg_str)
#! The display and user input loop
def run_display_user_input():
#! Window info
header_win = windows[0]
hosts_win = windows[1]
footer_win = windows[2]
(term_height, term_width) = stdscr.getmaxyx()
(header_height, _) = header_win.getmaxyx()
(hosts_height, _) = hosts_win.getmaxyx()
(footer_height, _) = footer_win.getmaxyx()
hosts_scroll_max = term_height - header_height - footer_height - 1
footer_start = term_height - footer_height - 1
hosts_len = len(hosts)
hl_host = 0
start_y = 0
#! Print header information
header_win.addstr(0,0, " ┌─────────────────┬──────────────┬─────────┬────────┬────────────┬──────┬─────────┐")
header_win.clrtoeol()
header_win.addstr(1,0, " │ Hostname/IP │ Hashrate H/m │ Share % │ Blocks │ Difficulty │ CPUs │ Temp °C │")
header_win.clrtoeol()
header_win.addstr(2,0, "┌─┼─────────────────┴──────────────┴─────────┴────────┴────────────┼──────┴─────────┤")
header_win.clrtoeol()
header_win.refresh()
while True:
#! Write hosts to screen and frame the scroll window
write_to_scr(hl_host)
#! Get user input
c = hosts_win.getch() #! Calls stdscr.refresh()
if c == curses.KEY_DOWN:
if hl_host < (hosts_height - 1) and hl_host < (hosts_len - 1):
hl_host += 1
if start_y <= (hl_host - hosts_scroll_max - 1):
start_y += 1
elif c == curses.KEY_UP:
if hl_host > 0:
hl_host -= 1
if hl_host < start_y and start_y > 0:
start_y -= 1
elif c == curses.KEY_HOME:
hl_host = 0
start_y = 0
elif c == curses.KEY_END:
hl_host = hosts_height - 1
start_y = hl_host - hosts_scroll_max
elif c == ord('q') or c == 27:
#! Either q or ESC to quit
break
else:
pass #! Leave everything as is
hosts_win.refresh( start_y,0, 3,0, footer_start,term_width)
footer_win.refresh()
#! Negligable refresh lag while
# keeping CPU usage down
# (for arrowing up and down)
#! I want to use a keyboard event
# listener, but I can't find an
# implementation that works over
# ssh
time.sleep(0.03)
return
#! Write strings to the screen
def write_to_scr(hl_host):
hosts_win = windows[1]
footer_win = windows[2]
(hosts_height, _) = hosts_win.getmaxyx()
i = 0
for statinfo in statinfo_list:
#! Highlight host
hl = (True if i == hl_host else False)
apply_formatting(i, statinfo, hl)
i += 1
#! Print empty lines to fill the terminal
for b in range(i, hosts_height):
hosts_win.addstr(b,0,"│ │ │ │")
hosts_win.clrtoeol()
#! Calculate totals and averages
(total_str,avg_str) = get_totals_avgs()
footer_win.addstr(0,0, "├─┼────────────────────────────────────────────────────────────────┼────────────────┤")
footer_win.clrtoeol()
footer_win.addstr(1,0, "│ │ {0} │".format(avg_str))
footer_win.clrtoeol()
footer_win.addstr(2,0, "│ │ {0} │".format(total_str))
footer_win.clrtoeol()
footer_win.addstr(3,0, "└─┴────────────────────────────────────────────────────────────────┴────────────────┘")
footer_win.clrtoeol()
return
#! Applies formatting and coloring for written lines
def apply_formatting(line, statinfo, hl):
hosts_win = windows[1]
hl_prefix = "│>│"
prefix = "│ │"
#! Host online, highlighted
if statinfo[0] == True and hl == True:
hosts_win.addstr(line, 0, hl_prefix)
#! Three spaces between each. Space, bar, space between diff and cpus
hosts_win.addstr(" {0:<15} ".format(statinfo[1]), curses.A_REVERSE)
hosts_win.addstr("{0:>8.3f} H/m".format(statinfo[2]), curses.A_REVERSE) #! HPM
hosts_win.addstr(" ", curses.A_REVERSE)
hosts_win.addstr("{0:>6.2f}%".format(statinfo[3]), curses.A_REVERSE) #! Share %
hosts_win.addstr(" {0:>6} {1:<8} │ {2:>4} ".format(
statinfo[4], statinfo[5], statinfo[6]), curses.A_REVERSE)
hosts_win.addstr("{0:>5.1f}°C ".format(statinfo[7]), curses.A_REVERSE) #! CPU Temp
#! Host online, not highlighted
elif statinfo[0] == True and hl == False:
hosts_win.addstr(line, 0, prefix)
hosts_win.addstr(" {0:<15} ".format(statinfo[1]))
hosts_win.addstr("{0:>8.3f} H/m".format(statinfo[2])) #! HPM
hosts_win.addstr(" ")
hosts_win.addstr("{0:>6.2f}%".format(statinfo[3])) #! Share %
hosts_win.addstr(" {0:>6} {1:<8} │ {2:>4} ".format(
statinfo[4], statinfo[5], statinfo[6]))
hosts_win.addstr("{0:>5.1f}°C ".format(statinfo[7])) #! CPU Temp
#! Host offline, highlighted
elif statinfo[0] == False and hl == True:
hosts_win.addstr(line, 0, hl_prefix)
hosts_win.addstr(" {0:<15} ----.-- H/m ---.--% ------ -.------ │ ---- ---.-°C ".format(statinfo[1]), curses.A_REVERSE)
#! host offline, non-highlighted
else:
hosts_win.addstr(line, 0, prefix)
hosts_win.addstr(" {0:<15} ----.-- H/m ---.--% ------ -.------ │ ---- ---.-°C ".format(statinfo[1]))
#! End of Line
hosts_win.addstr("│")
hosts_win.clrtoeol()
return
def process_workermsg(host,port):
buffer_size = 4096
while not kill_threads.is_set():
time.sleep(1)
try:
#! Get data from the miners
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.connect((host, port))
s.send("summary".encode())
recvstr = s.recv(buffer_size).decode()
summary = parse_summarystr(recvstr)
parse_summary_msg(host,summary)
s.close()
except:
set_host_offline(host)
return
#! Parse the thread output from the miner
def parse_summarystr(recvstr):
summary = {}
datastr = recvstr.rsplit('|')[0]
summary_data_list = datastr.split(';')
summary['name'] = summary_data_list[ 0].split('=')[1]
summary['version'] = summary_data_list[ 1].split('=')[1]
summary['api'] = summary_data_list[ 2].split('=')[1]
summary['algo'] = summary_data_list[ 3].split('=')[1]
summary['cpus'] = int( summary_data_list[ 4].split('=')[1])
summary['khps'] = float(summary_data_list[ 5].split('=')[1])
summary['solved'] = int( summary_data_list[ 6].split('=')[1])
summary['accepted'] = int( summary_data_list[ 7].split('=')[1])
summary['rejected'] = int( summary_data_list[ 8].split('=')[1])
summary['accpm'] = float(summary_data_list[ 9].split('=')[1])
summary['diff'] = float(summary_data_list[10].split('=')[1])
summary['cpu_temp'] = float(summary_data_list[11].split('=')[1])
summary['cpu_fan'] = int( summary_data_list[12].split('=')[1])
summary['cpu_freq'] = int( summary_data_list[13].split('=')[1])
summary['uptime'] = int( summary_data_list[14].split('=')[1])
summary['time_sec'] = int( summary_data_list[15].split('=')[1])
return summary
#! Main function
def main(stdscr):
kill_threads.clear()
#! Create list of hosts
hosts_file = open("{0}/.chosts".format(Path.home()),'r')
for line in hosts_file:
hosts.append(line.rstrip())
hosts_file.close()
#! Initialize
init_display()
signal.signal(signal.SIGINT, signal_handler)
#! Create threads and start
print(hosts)
for host in hosts:
t = threading.Thread(target=process_workermsg, args=(host,port))
threads.append(t)
t.start()
#! Run display and user input loop
run_display_user_input()
#! Kill the program
return
#! Run the program
if __name__ == "__main__":
curses.wrapper(main)
kill_program()