Here is my quick and dirty Perl implementation of api-example. Just copy and save this as api-example.pl. Note, you need to have the JSON Perl module installed to use this (perl-JSON for RH-based distros).
#!/usr/bin/perl -w
#
#
# Simple Perl script to query and parse cgminer API stats in JSON
#
######################
## Usage ##
######################
#
# perl api-example.pl command
#
######################
## Revision History ##
######################
#
# v0.02 4/10/2012 JinTu ([email protected])
# Bugfix: Support for multi element arrays in responses
#
# v0.01 12/28/2011 JinTu ([email protected])
# First working version. Supports cgminer 2.1.0 API JSON format
#
##
use strict;
use IO::Socket;
use JSON -support_by_pp;
my $cgminer_host = "localhost";
my $cgminer_port = "4028";
my $remote = IO::Socket::INET->new(
Proto => "tcp",
PeerAddr => $cgminer_host,
PeerPort => $cgminer_port,
) or die "cannot connect to port $cgminer_host:$cgminer_port, $!\n";
my $command;
my $parameter;
if ($ARGV[0]) {
# look for parameters in addition to a command
unless (($command,$parameter) = $ARGV[0] =~ m/(.*)\|(.*)/) {
$command = $ARGV[0];
}
}
else {
# We didn't get an argument so default to sending summary
$command = "summary";
}
# Send the JSON query
if ($parameter) {
print $remote '{"command":"' . $command . '","parameter":"' . $parameter . '"}';
}
else {
print $remote '{"command":"' . $command . '"}';
}
# Now read the result
my $response = <$remote>;
print $command . " returned \'$response\'\n";
# Now parse the JSON
my $json = new JSON;
# these are some nice json options to relax restrictions a bit:
my $json_text = $json->allow_nonref->utf8->relaxed->escape_slash->loose->allow_singlequote->allow_barekey->decode($response);
foreach my $tlc (keys %{$json_text}){
print "\n$tlc\n";
if ($json_text->{$tlc} =~ /array/i) {
for (my $i = 0; $i <= (scalar @{$json_text->{$tlc}} - 1); $i++) {
print "->[$i]\n";
foreach my $cat (keys %{$json_text->{$tlc}->[$i]}) {
print "\t$cat=" . $json_text->{$tlc}->[$i]->{$cat} . "\n";
}
}
}
else {
# This is not an array so just print it
print "\t" . $json_text->{$tlc} . "\n";
}
}
# make sure we close the socket
$remote->close();
This will give you output like the following:
[JinTu@acheron ]$ perl api-example.pl
summary returned '{"STATUS":[{"STATUS":"S","Code":11,"Msg":"Summary","Description":"cgminer 2.3.1"}],"SUMMARY":[{"Elapsed":17227,"MHS av":855.59,"Found Blocks":0,"Getworks":5060,"Accepted":3546,"Rejected":10,"Hardware Errors":0,"Utility":12.35,"Discarded":373,"Stale":5,"Get Failures":10,"Local Work":0,"Remote Failures":5,"Network Blocks":31,"Total MH":14739525.4682}],"id":1}'
SUMMARY
->[0]
Local Work=0
Hardware Errors=0
Total MH=14739525.4682
MHS av=855.59
Stale=5
Network Blocks=31
Elapsed=17227
Rejected=10
Discarded=373
Utility=12.35
Get Failures=10
Found Blocks=0
Getworks=5060
Remote Failures=5
Accepted=3546
STATUS
->[0]
Code=11
Msg=Summary
STATUS=S
Description=cgminer 2.3.1
id
1
[JinTu@acheron ]$ perl api-example.pl devs
devs returned '{"STATUS":[{"STATUS":"S","Code":9,"Msg":"4 GPU(s)","Description":"cgminer 2.3.1"}],"DEVS":[{"GPU":0,"Enabled":"Y","Status":"Alive","Temperature":76 .00,"Fan Speed":3108,"Fan Percent":50,"GPU Clock":470,"Memory Clock":150,"GPU Voltage":1.000,"GPU Activity":94,"Powertune":0,"MHS av":195.33,"MHS 5s":192.98,"Acce pted":824,"Rejected":4,"Hardware Errors":0,"Utility":2.90,"Intensity":"D","Last Share Pool":0,"Last Share Time":1334101418,"Total MH":3325785.6696},{"GPU":1,"Enab led":"Y","Status":"Alive","Temperature":75.50,"Fan Speed":-1,"Fan Percent":50,"GPU Clock":500,"Memory Clock":150,"GPU Voltage":1.000,"GPU Activity":99,"Powertune" :0,"MHS av":219.29,"MHS 5s":219.97,"Accepted":888,"Rejected":4,"Hardware Errors":0,"Utility":3.13,"Intensity":"8","Last Share Pool":0,"Last Share Time":1334101419 ,"Total MH":3733853.3069},{"GPU":2,"Enabled":"Y","Status":"Alive","Temperature":72.50,"Fan Speed":3034,"Fan Percent":50,"GPU Clock":500,"Memory Clock":150,"GPU Vo ltage":1.000,"GPU Activity":99,"Powertune":0,"MHS av":220.79,"MHS 5s":220.81,"Accepted":883,"Rejected":2,"Hardware Errors":0,"Utility":3.11,"Intensity":"8","Last Share Pool":0,"Last Share Time":1334101386,"Total MH":3759237.2347},{"GPU":3,"Enabled":"Y","Status":"Alive","Temperature":75.00,"Fan Speed":-1,"Fan Percent":50,"G PU Clock":500,"Memory Clock":150,"GPU Voltage":1.000,"GPU Activity":0,"Powertune":0,"MHS av":220.07,"MHS 5s":220.24,"Accepted":911,"Rejected":0,"Hardware Errors": 0,"Utility":3.21,"Intensity":"8","Last Share Pool":0,"Last Share Time":1334101436,"Total MH":3747107.3075}],"id":1}'
DEVS
->[0]
Total MH=3325785.6696
Last Share Time=1334101418
Fan Percent=50
Fan Speed=3108
Status=Alive
Last Share Pool=0
Utility=2.9
MHS 5s=192.98
Temperature=76
Memory Clock=150
GPU Activity=94
Accepted=824
Hardware Errors=0
MHS av=195.33
Enabled=Y
GPU Voltage=1
Rejected=4
GPU Clock=470
GPU=0
Intensity=D
Powertune=0
->[1]
Total MH=3733853.3069
Last Share Time=1334101419
Fan Percent=50
Fan Speed=-1
Status=Alive
Last Share Pool=0
Utility=3.13
MHS 5s=219.97
Temperature=75.5
Memory Clock=150
GPU Activity=99
Accepted=888
Hardware Errors=0
MHS av=219.29
Enabled=Y
GPU Voltage=1
Rejected=4
GPU Clock=500
GPU=1
Intensity=8
Powertune=0
->[2]
Total MH=3759237.2347
Last Share Time=1334101386
Fan Percent=50
Fan Speed=3034
Status=Alive
Last Share Pool=0
Utility=3.11
MHS 5s=220.81
Temperature=72.5
Memory Clock=150
GPU Activity=99
Accepted=883
Hardware Errors=0
MHS av=220.79
Enabled=Y
GPU Voltage=1
Rejected=2
GPU Clock=500
GPU=2
Intensity=8
Powertune=0
->[3]
Total MH=3747107.3075
Last Share Time=1334101436
Fan Percent=50
Fan Speed=-1
Status=Alive
Last Share Pool=0
Utility=3.21
MHS 5s=220.24
Temperature=75
Memory Clock=150
GPU Activity=0
Accepted=911
Hardware Errors=0
MHS av=220.07
Enabled=Y
GPU Voltage=1
Rejected=0
GPU Clock=500
GPU=3
Intensity=8
Powertune=0
STATUS
->[0]
Code=9
Msg=4 GPU(s)
STATUS=S
Description=cgminer 2.3.1
id
1