forked from transeos/ethos_monitor
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathminer.py
executable file
·127 lines (95 loc) · 2.68 KB
/
miner.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
#!/usr/bin/python
# -*- Python -*-
#*****************************************************************
#
#
# WARRANTY:
# Use all material in this file at your own risk. Hiranmoy Basak
# makes no claims about any material contained in this file.
#
# Contact: [email protected]
import os
import sys
import time
import datetime
import json
from urllib import urlopen
gRigName = "-"
gJsonSite = "-"
gDebugMode = 0
gGpuNotHashing = 0
gLogFile = "/home/ethos/gpu_crash.log"
# ================================ functions =============================
def DumpActivity(dumpStr):
print dumpStr
try:
# writes input string in a file
pLogFile = open(gLogFile, "a")
pLogFile.write("%s @ %s\n" % (dumpStr, str(datetime.datetime.now())))
pLogFile.close()
except:
print "File write error in - " + gLogFile
# ============================== process arguments ============================
def ProcessArguments():
# arg#0: rig name
# arg#1: json site
# arg#2: (optional) set debug mode
global gRigName, gJsonSite, gDebugMode
argStr = ""
argIdx = 0
while (1):
argIdx += 1
if (argIdx >= len(sys.argv)):
break
arg = sys.argv[argIdx]
if (argIdx == 1):
gRigName = arg
elif(argIdx == 2):
gJsonSite = arg
elif(argIdx == 3):
gDebugMode = arg
if (str(gDebugMode) == "1"):
DumpActivity("debug mode")
else:
DumpActivity("invalid number of arguments, arg#0: rig name")
DumpActivity("Rig name: " + gRigName + ", Json: " + gJsonSite)
# =================================== run ================================
ProcessArguments()
while 1:
# wait for 4 min
time.sleep(240)
# read site content
try:
url = urlopen(gJsonSite).read()
except:
DumpActivity("invalid url")
continue
# convert site content to json
try:
result = json.loads(url)
except:
DumpActivity("invalid json")
continue
# extract data
try:
numGpus = result["rigs"][gRigName]["gpus"]
numRunningGpus = result["rigs"][gRigName]["miner_instance"]
hashRate = result["rigs"][gRigName]["miner_hashes"]
except:
DumpActivity("invalid rig name")
continue
if (str(gDebugMode) == "1"):
DumpActivity("Gpus: " + str(numRunningGpus) + "/" + str(numGpus) + " - " + str(hashRate))
# check if any gpu is down
if (int(numRunningGpus) != int(numGpus)):
if (gGpuNotHashing == 1):
# reboot
DumpActivity("Rebooting (" + str(hashRate) + ")")
os.system("sudo reboot")
else:
# wait for another 2 min before rebooting
DumpActivity("One or more Gpu(s) might have crashed")
gGpuNotHashing = 1
else:
# reset reboot pending counter
gGpuNotHashing = 0