-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathminer.py
executable file
·162 lines (121 loc) · 3.7 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
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
#!/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
import commands
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(gotPanelInfo):
# arg#0: rig name (required if "/var/run/ethos/stats.file" not available)
# arg#1: json site (required if "/var/run/ethos/url.file" not available)
# "-debug" : (optional) set debug mode
global gRigName, gJsonSite, gDebugMode
if (gotPanelInfo != 1):
DumpActivity("Taking rig name and panel url from arguments")
argStr = ""
argIdx = 0
argProcessed = 0
while (1):
argIdx += 1
if (argIdx >= len(sys.argv)):
break
arg = sys.argv[argIdx]
if (str(arg) == "-debug"):
gDebugMode = 1
DumpActivity("debug mode")
continue
if (gotPanelInfo == 1):
DumpActivity("Ignoring argument : " + str(arg))
continue
argProcessed += 1
if (argProcessed == 1):
gRigName = arg
elif(argProcessed == 2):
gJsonSite = arg
def GetPanelInfo():
global gRigName, gJsonSite
commandOutput = commands.getstatusoutput('\grep http /var/run/ethos/url.file')
if (commandOutput[0] != 0):
DumpActivity("/var/run/ethos/url.file is not availble")
return 0
gJsonSite = commandOutput[1]
gJsonSite = gJsonSite+"/?json=yes"
commandOutput = commands.getstatusoutput("\grep hostname /var/run/ethos/stats.file")
if (commandOutput[0] != 0):
DumpActivity("/var/run/ethos/stats.file is not avaible")
return 0
gRigName = commandOutput[1][9:]
return 1
# =================================== run ================================
success = GetPanelInfo()
ProcessArguments(success)
DumpActivity("Rig name: " + gRigName + ", Json: " + gJsonSite)
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"]
status = result["rigs"][gRigName]["condition"]
except:
DumpActivity("invalid rig name")
continue
if (str(gDebugMode) == "1"):
DumpActivity("<" + status + "> Gpus: " + str(numRunningGpus) + "/" + str(numGpus) + " - " + str(hashRate))
if (status == "unreachable"):
gGpuNotHashing = 0
DumpActivity("[Warning] panel is not updating")
continue;
# 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