Skip to content

Instantly share code, notes, and snippets.

@dbiesecke
Last active September 17, 2018 07:23
Show Gist options
  • Select an option

  • Save dbiesecke/9c5032c467604d705c42 to your computer and use it in GitHub Desktop.

Select an option

Save dbiesecke/9c5032c467604d705c42 to your computer and use it in GitHub Desktop.
CGMiner API Example

Example ( Antminer-3 )

echo '{"command":"summary","parameter":"0"}' | nc -v antminer-1 4028

      {
        "STATUS": [
          {
            "STATUS": "S",
            "When": 1415057522,
            "Code": 11,
            "Msg": "Summary",
            "Description": "cgminer 3.12.0"
          }
        ],
        "SUMMARY": [
          {
            "Elapsed": 112838,
            "GHS 5s": 416.81,
            "GHS av": 419.96,
            "Found Blocks": 0,
            "Getworks": 11712,
            "Accepted": 6684,
            "Rejected": 40,
            "Hardware Errors": 215,
            "Utility": 3.55,
            "Discarded": 839931,
            "Stale": 6,
            "Get Failures": 8,
            "Local Work": 25336132,
            "Remote Failures": 0,
            "Network Blocks": 234,
            "Total MH": 47387095720.383,
            "Work Utility": 5866.74,
            "Difficulty Accepted": 11225402.12488,
            "Difficulty Rejected": 67713.737557,
            "Difficulty Stale": 12288,
            "Best Share": 7466,
            "Device Hardware%": 0.0019,
            "Device Rejected%": 0.6137,
            "Pool Rejected%": 0.599,
            "Pool Stale%": 0.1087,
            "Last getwork": 1415057522
          }
        ],
        "id": 1
      }

Example for dashing

$VAR1 = { 'summary' => [ { 'accepted' => 6769, 'local_work' => 25749250, 'last_getwork' => 1415059306, 'ghs_av' => '420.01', 'pool_rejected_percentage' => '0.5899', 'device_rejected_percentage' => '0.6041', 'remote_failures' => 0, 'difficulty_rejected' => '67713.737557', 'found_blocks' => 0, 'rejected' => 40, 'work_utility' => '5867.53', 'ghs_5s' => '430.31', 'elapsed' => 114620, 'hardware_errors' => 216, 'pool_stale_percentage' => '0.107', 'discarded' => 856511, 'device_hardware_percentage' => '0.0019', 'difficulty_accepted' => '11399482.12488', 'network_blocks' => 239, 'get_failures' => 8, 'difficulty_stale' => '12288', 'getworks' => 11928, 'utility' => '3.54', 'best_share' => 7466, 'stale' => 6, 'total_mh' => '48142047891.6372' } ], 'status' => [ { 'msg' => 'summary', 'code' => 11, 'when' => 1415059306, 'description' => 'cgminer_3.12.0', 'status' => 's' } ], 'id' => 1 }; $VAR1 = { 'status' => [ { 'description' => 'cgminer_3.12.0', 'when' => 1415059306, 'status' => 's', 'msg' => '3_pool(s)', 'code' => 7 } ], 'id' => 1, 'pools' => [ { 'user' => '1jhtq38ezyskhzgvrr7a6ebrzutprctwcn', 'stratum_active' => 'true', 'status' => 'alive', 'discarded' => 508595, 'pool_stale_percentage' => '0.2022', 'proxy_type' => '', 'rejected' => 36, 'priority' => 0, 'difficulty_accepted' => '5999888.423512', 'diff1_shares' => 70099, 'stale' => 6, 'pool' => 0, 'best_share' => 3477, 'getworks' => 7202, 'difficulty_stale' => '12288', 'get_failures' => 8, 'stratum_url' => 'stratum.nicehash.com', 'has_gbt' => 'false', 'accepted' => 3380, 'long_poll' => 'n', 'pool_rejected_percentage' => '1.0616', 'has_stratum' => 'true', 'diff' => '2.05k', 'quota' => 1, 'last_share_time' => '0:00:35', 'last_share_difficulty' => '2048', 'url' => 'stratum+tcp://stratum.nicehash.com:3334', 'difficulty_rejected' => '64512', 'proxy' => '', 'remote_failures' => 0 }, { 'stratum_active' => 'false', 'user' => '1jhtq38ezyskhzgvrr7a6ebrzutprctwcn', 'proxy_type' => '', 'priority' => 1, 'rejected' => 4, 'status' => 'alive', 'discarded' => 347916, 'pool_stale_percentage' => '0', 'diff1_shares' => 67770, 'difficulty_accepted' => '5399593.701368', 'get_failures' => 0, 'difficulty_stale' => '0', 'getworks' => 4718, 'stale' => 0, 'pool' => 1, 'best_share' => 7466, 'accepted' => 3389, 'has_gbt' => 'false', 'stratum_url' => '', 'long_poll' => 'n', 'pool_rejected_percentage' => '0.0593', 'has_stratum' => 'true', 'last_share_time' => '16:03:25', 'quota' => 1, 'diff' => '1.02k', 'difficulty_rejected' => '3201.737557', 'proxy' => '', 'url' => 'stratum+tcp://stratum.westhash.com:3334', 'remote_failures' => 0, 'last_share_difficulty' => '1024' }, { 'user' => '1jhtq38ezyskhzgvrr7a6ebrzutprctwcn', 'stratum_active' => 'false', 'status' => 'alive', 'pool_stale_percentage' => '0', 'discarded' => 0, 'proxy_type' => '', 'rejected' => 0, 'priority' => 2, 'difficulty_accepted' => '0', 'diff1_shares' => 0, 'stale' => 0, 'pool' => 2, 'best_share' => 0, 'get_failures' => 0, 'difficulty_stale' => '0', 'getworks' => 8, 'stratum_url' => '', 'has_gbt' => 'false', 'accepted' => 0, 'pool_rejected_percentage' => '0', 'has_stratum' => 'true', 'long_poll' => 'n', 'diff' => '2', 'quota' => 1, 'last_share_time' => '0', 'last_share_difficulty' => '0', 'difficulty_rejected' => '0', 'proxy' => '', 'url' => 'stratum+tcp://de3.miningpool.co:3921', 'remote_failures' => 0 } ] }; $VAR1 = { 'devs' => [ { 'name' => 'bmm', 'asc' => 0, 'last_share_time' => 1415059271, 'last_share_pool' => 0, 'last_share_difficulty' => '2048', 'no_device' => 'false', 'difficulty_rejected' => '62593.737557', 'enabled' => 'y', 'last_valid_work' => 1415059306, 'accepted' => 6722, 'device_rejected_percentage' => '0.5584', 'mhs_5s' => '405562.37', 'difficulty_accepted' => '11315514.12488', 'total_mh' => '48142047891.6372', 'diff1_work' => 11209005, 'id' => 0, 'mhs_av' => '420012.8', 'utility' => '3.52', 'device_elapsed' => 114620, 'temperature' => '40.5', 'device_hardware_percentage' => '0.0019', 'status' => 'alive', 'hardware_errors' => 216, 'rejected' => 37 } ], 'id' => 1, 'status' => [ { 'when' => 1415059306, 'description' => 'cgminer_3.12.0', 'status' => 's', 'msg' => '1_asc(s)', 'code' => 9 } ] };

=============================

#!/usr/bin/perl
# = CGminer.pm
#
# == Description
#
# Perl module to interface with the cgminer api
#
# == License
#
# "THE BEER-WARE LICENSE" (Revision 42-mrenz):
# Michael Renz <cryptographrix@gmail.com> wrote this file. As long as you
# retain this notice you can do whatever you want with this stuff. If we
# meet some day, and you think this stuff is worth it, you can buy me a beer.
#
use strict;
use warnings;
package CGminer;
use JSON;
use IO::Socket::INET;
$| = 1;
# == Important variables
#
# === $cgminer_server
# defaults to 127.0.0.1 but can be overriden in $main::cgminer_server
# $cgminer_port - defaults to 4028 but can be overridden in $main::cgminer_port
#
my $cgminer_server = $main::cgminer_server || "antminer-1";
my $cgminer_port = $main::cgminer_port || "4028";
# == Sub: cgminer_send_commands
#
# === Description
#
# This is the sub that connects with the CGminer socket.
#
# It is dumb and is called like:
# cgminer_send_commands($command,$parameter);
#
# It returns the exact response from the server.
sub cgminer_send_commands {
# Takes command, parameter
my $cgminer_command->{'command'} = $_[0];
$cgminer_command->{'parameter'} = $_[1] || undef;
my $cgminer_socket = IO::Socket::INET->new( Proto =>"tcp",
PeerPort =>$cgminer_port,
PeerAddr =>$cgminer_server,
Timeout => 1, Type => SOCK_STREAM )
or die "Can't make TCP connection: $@";
my $response;
my $command_encoded = encode_json $cgminer_command;
$cgminer_socket->send($command_encoded."\n");
$response = <$cgminer_socket>;
$response =~ s/false/"false"/g;
$response =~ s/true/"true"/g;
$response =~ s/%/ percentage/g;
$response =~ s/ /_/g;
$response =~ tr/A-Z/a-z/;
return $response;
}
# == Sub: cgm_spc
#
# === Description
#
# This is the sub that wraps cgminer_send_commands and converts
# the server response to a perl data structure.
sub cgm_spc {
return decode_json cgminer_send_commands($_[0],$_[1] || undef);
}
sub version {
return cgm_spc('version') ;
}
sub config {
return cgm_spc('config') ;
}
sub summary {
return cgm_spc('summary') ;
}
# == Sub: pools
#
# === Description
#
# This will provide a list of pools in response->{'POOLS'}
sub pools {
return cgm_spc('pools') ;
}
sub devs {
return cgm_spc('devs') ;
}
sub pga {
return cgm_spc('pga',$_[0]) ;
}
sub pgacount {
return cgm_spc('pgacount') ;
}
sub switchpool {
return cgm_spc('switchpool',$_[0]) ;
}
sub enablepool {
return cgm_spc('enablepool',$_[0]) ;
}
# == Sub: addpool
#
# === Description
#
# Takes URL, USER, PASSWORD as such:
# addpool($url, $username, $password);
#
# Will use '123' as the default password if none is specified.
#
sub addpool {
return cgm_spc('addpool',$_[0].",".$_[1].",".$_[2] || '123') ;
}
# == Sub: poolpriority
#
# === Description
#
# Takes a list of pool IDs and prioritizes them as such:
# poolpriority($pool_one,$pool_two,$pool_three);
#
sub poolpriority {
return cgm_spc('poolpriority', join(",",@_) ) ;
}
# == Sub: poolquota
#
# === Description
#
# Takes pool ID, quota value as such:
# poolquota($pool_id, $pool_quota);
#
sub poolquota {
return cgm_spc('poolquota', $_[0].",".$_[1]) ;
}
# == Sub: disablepool
#
# === Description
#
# Disables the pool whose ID you specify as such:
# disablepool($pool_id);
#
sub disablepool {
return cgm_spc('disablepool',$_[0]) ;
}
# == Sub: removepool
#
# === Description
#
# Removes the pool whose ID you specify as such:
# removepool($pool_id);
#
sub removepool {
return cgm_spc('removepool',$_[0]) ;
}
# == Sub: save
#
# === Description
#
# Saves the cgminer config to filename as such:
# save($filename);
#
# Will save to "/root/.cgminer/cgminer.conf" if $filename is not specified.
sub save {
return cgm_spc('save',$_[0] || "/root/.cgminer/cgminer.conf") ;
}
sub quit {
return cgm_spc('quit') ;
}
sub notify {
return cgm_spc('notify') ;
}
sub privileged {
return cgm_spc('privileged') ;
}
sub pgaenable {
return cgm_spc('pgaenable',$_[0]) ;
}
sub pgadisable {
return cgm_spc('pgadisable',$_[0]) ;
}
sub pgaidentify {
return cgm_spc('pgaidentify',$_[0]) ;
}
sub devdetails {
return cgm_spc('devdetails') ;
}
sub restart {
return cgm_spc('restart') ;
}
sub stats {
return cgm_spc('stats') ;
}
sub check {
return cgm_spc('check',$_[0]) ;
}
sub failover_only {
return cgm_spc('failover-only',$_[0]) ;
}
sub coin {
return cgm_spc('coin') ;
}
sub debug {
return cgm_spc('debug',$_[0]) ;
}
sub setconfig {
return cgm_spc('setconfig',$_[0].",".$_[1]) ;
}
sub usbstats {
return cgm_spc('usbstats') ;
}
sub pgaset {
return cgm_spc('pgaset',$_[0]) ;
}
sub zero {
return cgm_spc('zero',$_[0]) ;
}
sub hotplug {
return cgm_spc('hotplug',$_[0]) ;
}
sub asc {
return cgm_spc('asc',$_[0]) ;
}
sub ascenable {
return cgm_spc('ascenable',$_[0]) ;
}
sub ascdisable {
return cgm_spc('ascdisable',$_[0]) ;
}
sub ascidentify {
return cgm_spc('ascidentify',$_[0]) ;
}
sub asccount {
return cgm_spc('asccount') ;
}
sub ascset {
return cgm_spc('ascset',$_[0]) ;
}
sub lockstats {
return cgm_spc('lockstats') ;
}
1;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment