Last active
December 17, 2015 00:00
-
-
Save aeris/5518129 to your computer and use it in GitHub Desktop.
Bitcoin mining pools monitoring
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env ruby | |
$: << '/usr/lib/ruby/1.9.1/x86_64-linux' | |
require 'RRD' | |
require 'json' | |
require 'httparty' | |
COLORS = { | |
:dark => { | |
:red => '#CC3118', | |
:orange => '#CC7016', | |
:yellow => '#C9B215', | |
:green => '#24BC14', | |
:blue => '#1598C3', | |
:pink => '#B415C7', | |
:purple => '#4D18E4' | |
}, | |
:light => { | |
:red => '#EA644A', | |
:orange => '#EC9D48', | |
:yellow => '#ECD748', | |
:green => '#54EC48', | |
:blue => '#48C4EC', | |
:pink => '#DE48EC', | |
:purple => '#7648EC' | |
} | |
} | |
CONFIG = { | |
'bitcoin.cz' => { | |
api_key: 'FIX_ME' | |
}, | |
'bitminter' => { | |
user: 'FIX_ME', | |
api_key: 'FIX_ME' | |
}, | |
'50btc' => { | |
api_key: 'FIX_ME' | |
} | |
} | |
rrd_dir = "#{ENV['HOME']}/.bin" | |
png_dir = 'FIX_ME www published dir' | |
balance_rrd = "#{rrd_dir}/pools_balance.rrd" | |
balance_step = 60*10 # 10min | |
balance_delta = '2weeks' | |
rate_rrd = "#{rrd_dir}/pools_rate.rrd" | |
rate_step = 60*60 # 1hour | |
rate_delta = balance_delta | |
trend = 60*60*24 | |
unless File.exist? balance_rrd | |
RRD.create balance_rrd, '--step', balance_step, | |
'--start', 'now-1month', | |
"DS:bitminter:GAUGE:#{2*balance_step}:0:U", | |
"DS:bitcoin_cz:GAUGE:#{2*balance_step}:0:U", | |
"DS:fifth_btc:GAUGE:#{2*balance_step}:0:U", | |
'RRA:AVERAGE:0.5:1:144', # 1 * 10min = 10min * 144 = 1day | |
'RRA:AVERAGE:0.5:6:504', # 6 * 10min = 1hour * 504 = 3weeks | |
'RRA:AVERAGE:0.5:36:124', # 36 * 10min = 6hours * 124 = 1month | |
'RRA:AVERAGE:0.5:144:365' # 144 * 10min = 1day * 365 = 1year | |
end | |
unless File.exist? rate_rrd | |
RRD.create rate_rrd, '--step', rate_step, | |
'--start', 'now-1month', | |
"DS:bitminter:DERIVE:#{2*rate_step}:0:U", | |
"DS:bitcoin_cz:DERIVE:#{2*rate_step}:0:U", | |
"DS:fifth_btc:DERIVE:#{2*rate_step}:0:U", | |
'RRA:LAST:0.5:1:504', # 1 * 1hour = 1hour * 504 = 3weeks | |
'RRA:LAST:0.5:6:124', # 6 * 1hour = 6hours * 124 = 1month | |
'RRA:LAST:0.5:25:365' # 24 * 1hour = 1day * 365 = 1year | |
end | |
def bitminter | |
response = HTTParty.get "https://bitminter.com/api/users/#{CONFIG['bitminter'][:user]}", | |
:headers => { 'Authorization' => "key=#{CONFIG['bitminter'][:api_key]}" } | |
if response.code == 200 | |
response = JSON.parse response.body | |
response['balances']['BTC'] | |
else | |
'U' | |
end | |
rescue Exception => e | |
p e | |
'U' | |
end | |
def bitcoin_cz | |
response = HTTParty.get "https://mining.bitcoin.cz/accounts/profile/json/#{CONFIG['bitcoin.cz'][:api_key]}" | |
if response.code == 200 | |
response = JSON.parse response.body | |
confirmed = response['confirmed_reward'].to_f | |
unconfirmed = response['unconfirmed_reward'].to_f | |
confirmed + unconfirmed | |
else | |
'U' | |
end | |
rescue Exception => e | |
p e | |
'U' | |
end | |
def fifth_btc | |
response = HTTParty.get "https://50btc.com/api/#{CONFIG['50btc'][:api_key]}" | |
if response.code == 200 | |
response = JSON.parse response.body | |
response['user']['confirmed_rewards'].to_f | |
else | |
'U' | |
end | |
rescue Exception => e | |
p e | |
'U' | |
end | |
coeff = 100000000 | |
values = [ bitminter, bitcoin_cz, fifth_btc ] | |
update = "N:#{values.join ':'}" | |
puts update | |
RRD.update balance_rrd, update | |
update = "N:#{values.collect { |i| i == 'U' ? 'U' : (i*coeff).to_i }.join ':'}" | |
puts update | |
RRD.update rate_rrd, update | |
height = 300 | |
width = 500 | |
RRD.graph "#{png_dir}/pools_balance_area.png", '--title', "Pools rewards at #{DateTime.now}", | |
'--start', "now - #{balance_delta}", '--step', balance_step, | |
'--width', width, '--height', height, | |
'--lower-limit', 0, '--alt-autoscale-max', | |
"DEF:fifth_btc=#{balance_rrd}:fifth_btc:AVERAGE", | |
"DEF:bitminter=#{balance_rrd}:bitminter:AVERAGE", | |
"DEF:bitcoin_cz=#{balance_rrd}:bitcoin_cz:AVERAGE", | |
'CDEF:line1=fifth_btc', | |
'CDEF:line2=bitminter,fifth_btc,fifth_btc,0,IF,+', | |
'CDEF:line3=bitcoin_cz,bitminter,bitminter,0,IF,fifth_btc,fifth_btc,0,IF,+,+', | |
"AREA:fifth_btc#{COLORS[:light][:yellow]}:50btc.com", | |
"AREA:bitminter#{COLORS[:light][:orange]}:bitminter.com:STACK", | |
"AREA:bitcoin_cz#{COLORS[:light][:red]}:bitcoin.cz:STACK", | |
"LINE1:line1#{COLORS[:dark][:yellow]}", | |
"LINE1:line2#{COLORS[:dark][:orange]}", | |
"LINE1:line3#{COLORS[:dark][:red]}", | |
'COMMENT:\j', | |
'GPRINT:fifth_btc:LAST:%lf%s', | |
'GPRINT:bitminter:LAST:%lf%s', | |
'GPRINT:bitcoin_cz:LAST:%lf%s' | |
RRD.graph "#{png_dir}/pools_balance_line.png", '--title', "Pools rewards at #{DateTime.now}", | |
'--start', "now - #{balance_delta}", '--step', balance_step, | |
'--width', width, '--height', height, | |
'--alt-autoscale-max', | |
"DEF:fifth_btc=#{balance_rrd}:fifth_btc:AVERAGE", | |
"DEF:bitminter=#{balance_rrd}:bitminter:AVERAGE", | |
"DEF:bitcoin_cz=#{balance_rrd}:bitcoin_cz:AVERAGE", | |
'LINE1:0.050#000080', | |
"LINE2:fifth_btc#{COLORS[:light][:yellow]}:50btc.com", | |
"LINE2:bitminter#{COLORS[:light][:orange]}:bitminter.com", | |
"LINE2:bitcoin_cz#{COLORS[:light][:red]}:bitcoin.cz", | |
'COMMENT:\j', | |
'GPRINT:fifth_btc:LAST:%lf%s', | |
'GPRINT:bitminter:LAST:%lf%s', | |
'GPRINT:bitcoin_cz:LAST:%lf%s' | |
RRD.graph "#{png_dir}/pools_rate_line.png", '--title', "Pools rates at #{DateTime.now}", | |
'--start', "now - #{rate_delta}", '--step', rate_step, | |
'--width', width, '--height', height, | |
# '--logarithmic', | |
"DEF:fifth_btc_rrd=#{rate_rrd}:fifth_btc:LAST", | |
"DEF:bitminter_rrd=#{rate_rrd}:bitminter:LAST", | |
"DEF:bitcoin_cz_rrd=#{rate_rrd}:bitcoin_cz:LAST", | |
# "CDEF:fifth_btc=fifth_btc_rrd,#{coeff},/", | |
# "CDEF:bitminter=bitminter_rrd,#{coeff},/", | |
# "CDEF:bitcoin_cz=bitcoin_cz_rrd,#{coeff},/", | |
"CDEF:fifth_btc=fifth_btc_rrd,#{trend},TRENDNAN,#{coeff},/", | |
"CDEF:bitminter=bitminter_rrd,#{trend},TRENDNAN,#{coeff},/", | |
"CDEF:bitcoin_cz=bitcoin_cz_rrd,#{trend},TRENDNAN,#{coeff},/", | |
"LINE2:fifth_btc#{COLORS[:light][:yellow]}:50btc.com", | |
"LINE2:bitminter#{COLORS[:light][:orange]}:bitminter.com", | |
"LINE2:bitcoin_cz#{COLORS[:light][:red]}:bitcoin.cz", | |
'COMMENT:\j', | |
'GPRINT:fifth_btc:AVERAGE:%lf%s', | |
'GPRINT:bitminter:AVERAGE:%lf%s', | |
'GPRINT:bitcoin_cz:AVERAGE:%lf%s' | |
RRD.graph "#{png_dir}/pools_rate_area.png", '--title', "Pools rates at #{DateTime.now}", | |
'--start', "now - #{rate_delta}", '--step', rate_step, | |
'--width', width, '--height', height, | |
# '--logarithmic', | |
"DEF:fifth_btc_rrd=#{rate_rrd}:fifth_btc:LAST", | |
"DEF:bitminter_rrd=#{rate_rrd}:bitminter:LAST", | |
"DEF:bitcoin_cz_rrd=#{rate_rrd}:bitcoin_cz:LAST", | |
# "CDEF:fifth_btc=fifth_btc_rrd,#{coeff},/", | |
# "CDEF:bitminter=bitminter_rrd,#{coeff},/", | |
# "CDEF:bitcoin_cz=bitcoin_cz_rrd,#{coeff},/", | |
"CDEF:fifth_btc=fifth_btc_rrd,#{trend},TRENDNAN,#{coeff},/", | |
"CDEF:bitminter=bitminter_rrd,#{trend},TRENDNAN,#{coeff},/", | |
"CDEF:bitcoin_cz=bitcoin_cz_rrd,#{trend},TRENDNAN,#{coeff},/", | |
'CDEF:line1=fifth_btc', | |
'CDEF:line2=bitminter,fifth_btc,fifth_btc,0,IF,+', | |
'CDEF:line3=bitcoin_cz,bitminter,bitminter,0,IF,fifth_btc,fifth_btc,0,IF,+,+', | |
"AREA:fifth_btc#{COLORS[:light][:yellow]}:50btc.com", | |
"AREA:bitminter#{COLORS[:light][:orange]}:bitminter.com:STACK", | |
"AREA:bitcoin_cz#{COLORS[:light][:red]}:bitcoin.cz:STACK", | |
"LINE1:line1#{COLORS[:dark][:yellow]}", | |
"LINE1:line2#{COLORS[:dark][:orange]}", | |
"LINE1:line3#{COLORS[:dark][:red]}", | |
'COMMENT:\j', | |
'GPRINT:bitcoin_cz:AVERAGE:%lf%s', | |
'GPRINT:bitminter:AVERAGE:%lf%s', | |
'GPRINT:fifth_btc:AVERAGE:%lf%s' |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment