Skip to content

Instantly share code, notes, and snippets.

@vanch
Last active March 28, 2016 15:56
Show Gist options
  • Save vanch/94e3e3e9ebbda84ed3d6 to your computer and use it in GitHub Desktop.
Save vanch/94e3e3e9ebbda84ed3d6 to your computer and use it in GitHub Desktop.
qminer
#!/usr/bin/env ruby
# Copyright 2015 Ivan Bondarev
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
# This is query-miner script that can query miners for particular data
# using TCP API. Tested on cgminer and bfgminer determining GHS 5s data field.
#
# Usage:
# [<script name>] [<miner IP] [<API request>] [<required field]
#
# Sample:
# ./qminer 10.12.5.6 summary "GHS 5s"
#
require 'json'
require 'socket'
require 'timeout'
class API
def initialize(ip = '127.0.0.1', port = 4028, timeout = 60)
@ip = ip
@port = port.to_i
@timeout = timeout.to_i
end
def make_request(command, parameter = nil)
begin
Timeout.timeout(@timeout) do
s = TCPSocket.open @ip, @port
if parameter == nil
s.write({:command => command}.to_json)
else
s.write({:command => command,
:parameter => parameter}.to_json)
end
response = s.read.strip
s.close
JSON.parse response
end
rescue Timeout::Error
raise Errno::ETIME, "Suspended"
end
end # API.make_request
end
ip = ARGV[0]
cmd = ARGV[1]
cmd = 'pools'
field = ARGV[2]
api = API.new ip
result = '0'
begin
if cmd == 'pools'
response = api.make_request(cmd)[cmd.upcase].find { |pool| pool['Stratum Active'] == true }
if response != nil
result = response['URL']
end
else
response = 'Command not supported'
end
rescue Errno::EHOSTUNREACH, Errno::ECONNREFUSED
result = '0'
end
puts result
#!/usr/bin/env ruby
# Copyright 2015 Ivan Bondarev
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
# This is query-miner script that can query miners for particular data
# using TCP API. Tested on cgminer and bfgminer determining GHS 5s data field.
#
# Usage:
# [<script name>] [<miner IP] [<API request>] [<required field]
#
# Sample:
# ./qminer 10.12.5.6 summary "GHS 5s"
#
require 'json'
require 'socket'
require 'timeout'
class API
def initialize(ip = '127.0.0.1', port = 4028, timeout = 60)
@ip = ip
@port = port.to_i
@timeout = timeout.to_i
end
def make_request(command, parameter = nil)
begin
Timeout.timeout(@timeout) do
s = TCPSocket.open @ip, @port
if parameter == nil
s.write({:command => command}.to_json)
else
s.write({:command => command,
:parameter => parameter}.to_json)
end
response = s.read.strip
s.close
JSON.parse response
end
rescue Timeout::Error
raise Errno::ETIME, "Suspended"
end
end # API.make_request
end
ip = ARGV[0]
cmd = ARGV[1]
field = ARGV[2]
api = API.new ip
response = 0
begin
response = api.make_request(cmd)[cmd.upcase][0][field]
rescue Errno::EHOSTUNREACH, Errno::ECONNREFUSED
response = 0
end
puts response
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment