-
Notifications
You must be signed in to change notification settings - Fork 23
/
mph_switch
129 lines (109 loc) · 4.58 KB
/
mph_switch
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
#!/usr/bin/env python2.7
import pprint
import json
import urllib2
import sys
import datetime
import time
import subprocess
import os
# load config
cfg=json.loads(open(sys.argv[1]).read())
outputDir=cfg["output_dir"]
card_type=cfg["card_type"]
miners=cfg["miners"]
user_name=cfg["user_name"]
miner_name=cfg["miner_name"]
currency=cfg["currency"]
pwrcost=cfg["pwrcost"]
min_profit=cfg["min_profit"]
os.environ["DISPLAY"]=":0"
# grab something from a website
def fetch(url):
r=urllib2.Request(url)
r.add_header("User-Agent", "Lynx/2.8.8dev.3 libwww-FM/2.14 SSL-MM/1.4.1")
r.add_header("Pragma", "no-cache")
return urllib2.build_opener().open(r).read()
# main
exchrate=float(json.loads(urllib2.urlopen("https://api.coinbase.com/v2/exchange-rates?currency=BTC").read())["data"]["rates"][currency])
data=json.loads(fetch("http://miningpoolhub.com/?page=api&action=getminingandprofitsstatistics"))["return"]
coins={}
for i in reversed(data): # weed out unconfigured algos, do string substitutions
try:
miner=miners[i["algo"]]
coins[i["coin_name"]]=miner["bin"].format(HOST=i["host"], PORT=str(i["port"]), NAME=user_name, MINER=miner_name)
except:
data.remove(i)
sort={}
for i in data:
sort[i["coin_name"]+" ("+i["algo"]+")"]=i["normalized_profit"]*miners[i["algo"]]["speed"]-24.0*miners[i["algo"]]["power"]*pwrcost/exchrate
sort=sorted(sort.items(), key=lambda x:x[1], reverse=True)
log=open(outputDir + "/current-profit", "w")
for i in sort:
log.write(i[0]+": "+format(i[1], ".8f")+" BTC/day ("+format(i[1]*exchrate, ".2f")+" "+currency+"/day)\n")
log.close()
max_profit_val=0 # find the most profitable coin
for i in data:
if i["normalized_profit"]*miners[i["algo"]]["speed"]-24.0*miners[i["algo"]]["power"]*pwrcost/exchrate>max_profit_val:
max_profit=i
max_profit_val=i["normalized_profit"]*miners[i["algo"]]["speed"]-24.0*miners[i["algo"]]["power"]*pwrcost/exchrate
miner=miners[max_profit["algo"]]
coin=coins[max_profit["coin_name"]]
# exit if maximum is below minimum
if (max_profit<min_profit):
algo_log=open(outputDir + "/algo-log", "a")
algo_log.write(str(datetime.datetime.now())+": **NONE**\n")
algo_log.close()
for algo in coins:
subprocess.call(["pkill", "-f", "^"+coins[algo].replace("+", "\\+")])
sys.exit()
# see if miner's already running
try:
subprocess.check_output(["pgrep", "-f", "^"+coin.replace("+", "\\+")])
current=1
except:
current=0
if (current==0):
other=0
for algo in coins:
try:
subprocess.check_output(["pgrep", "-f", "^"+coins[algo].replace("+", "\\+")])
other=1
except:
pass
if (current==0):
# log a change
algo_log=open(outputDir + "/algo-log", "a")
algo_log.write(str(datetime.datetime.now())+": "+max_profit["coin_name"]+" ("+max_profit["algo"]+")\n")
algo_log.close()
if (other==1):
# kill existing miners
for algo in coins:
subprocess.call(["pkill", "-f", "^"+coins[algo].replace("+", "\\+")])
time.sleep(3)
if "cuda_ver" in miner:
cudaVer=miner["cuda_ver"]
else:
cudaVer="cuda"
if card_type=="nvidia": # update card settings
cards=int(subprocess.check_output("nvidia-smi --query-gpu=count --format=csv,noheader,nounits".split(" ")).split("\n")[-2])
nv_settings="nvidia-settings"
for i in range(0, cards):
# power limit
if type(miner["power_limit"]) is int:
subprocess.call(("sudo nvidia-smi -i "+str(i)+" -pl "+str(miner["power_limit"])).split(" "))
else:
subprocess.call(("sudo nvidia-smi -i "+str(i)+" -pl "+str(miner["power_limit"][i])).split(" "))
# core overclock
if type(miner["gpu_oc"]) is int:
nv_settings+=" -a [gpu:"+str(i)+"]/GPUGraphicsClockOffset[2]="+str(miner["gpu_oc"])+" -a [gpu:"+str(i)+"]/GPUGraphicsClockOffset[3]="+str(miner["gpu_oc"])
else:
nv_settings+=" -a [gpu:"+str(i)+"]/GPUGraphicsClockOffset[2]="+str(miner["gpu_oc"][i])+" -a [gpu:"+str(i)+"]/GPUGraphicsClockOffset[3]="+str(miner["gpu_oc"][i])
# memory overclock
if type(miner["mem_oc"]) is int:
nv_settings+=" -a [gpu:"+str(i)+"]/GPUMemoryTransferRateOffset[2]="+str(miner["mem_oc"])+" -a [gpu:"+str(i)+"]/GPUMemoryTransferRateOffset[3]="+str(miner["mem_oc"])
else:
nv_settings+=" -a [gpu:"+str(i)+"]/GPUMemoryTransferRateOffset[2]="+str(miner["mem_oc"][i])+" -a [gpu:"+str(i)+"]/GPUMemoryTransferRateOffset[3]="+str(miner["mem_oc"][i])
subprocess.call(nv_settings.split(" "))
# launch new miner
subprocess.call("PATH=/usr/local/"+str(cudaVer)+"/bin:$PATH LD_LIBRARY_PATH=/usr/local/"+str(cudaVer)+"/lib64:$LD_LIBRARY_PATH screen -c "+str(outputDir)+"/screenrc-miner -dmSL miner "+coin, shell=True)