#!/usr/bin/php -q
<?php

//
//  retrieve details from the /status/size page of given endpoint
//
//  Ian Millard <icm@ecs.soton.ac.uk>
//


if ($argc !== 3) die("\nUsage: {$argv[0]} <host> <port>\n\n");

$status_url = "http://{$argv[1]}:{$argv[2]}/status/";
$status = @file($status_url);
if ($status == false || count($status) == 0)
	die("\nError: could not retrieve details from $status_url\n\n");

$size_url = "http://{$argv[1]}:{$argv[2]}/status/size/";
$size = @file($size_url);
if ($size == false || count($size) == 0)
	die("\nError: could not retrieve details from $size_url\n\n");

preg_match_all('@<td>(.*?)</td>@', $size[count($size) - 2], $fields);
$s['quads'] = number_format($fields[1][0]);
$s['resources'] = number_format($fields[1][3]);
$s['models'] = number_format($fields[1][2]);
$s['segments'] = count($size) - 7;

$s['queries_running'] = number_format(substr($status[4], 39, -11));
$s['queries_waiting'] = number_format(substr($status[5], 36, -11));

$k_len = 0; 
$v_len = 0; 
foreach($s as $k => $v) {
	$k_len = max(strlen($k), $k_len);
	$v_len = max(strlen($v), $v_len);
}

print "\n" . substr($status[3], 7, -6) . ' (' . $argv[1] . ':' . $argv[2] . ")\n\n";

foreach($s as $k => $v)
	print str_pad($k, $k_len) . ' ' . str_pad($v, $v_len, ' ', STR_PAD_LEFT) . "\n";

print "\n";
