function exec_ec2_query($creds, $ec2_api_action)
{
    $query_time = time();
    // Define query string keys/values
    $http_query_params = array('Action' => $ec2_api_action, 'AWSAccessKeyId' => $creds['access_key'], 'Timestamp' => gmdate('Y-m-d\\TH:i:s\\Z'), 'Version' => '2010-06-15', 'SignatureVersion' => 2, 'SignatureMethod' => 'HmacSHA256');
    // Note that for "DescribeImages" we are filtering for AMI images for which the "self account"
    // has implicit (i.e. is the owner of the image) or explicit permission.
    if ($ec2_api_action == "DescribeImages") {
        //$http_query_params['Owner.1'] = "self" ;
        //$http_query_params['Owner.2'] = "explicit" ;
        $http_query_params['Owner'] = "self";
    }
    // Note that for "DescribeSpotPriceHistory" we need to provide the start time and end time
    // for the spot price. If we do not (the times are optional), then we get a long, long, long
    // dataset for the spot price - i.e. the spotprice history will begin from whatever is
    // the oldest cached/stored in the Amazon API system.
    // Consequently, we request the price within the last 1 hour.
    if ($ec2_api_action == "DescribeSpotPriceHistory") {
        $http_query_params['StartTime'] = gmdate('Y-m-d\\TH:i:s\\Z', mktime() - 30);
        $http_query_params['EndTime'] = gmdate('Y-m-d\\TH:i:s\\Z', mktime());
    }
    // EC2 API requires the query parameters to be
    // sorted in "natural order" and then URL encoded
    uksort($http_query_params, 'strnatcmp');
    $query_string = '';
    foreach ($http_query_params as $key => $val) {
        $query_string .= "&{$key}=" . rawurlencode($val);
    }
    $query_string = substr($query_string, 1);
    // Formulate the HTTP query string
    $http_get_string = "GET\n" . "{$creds['aws_endpoint']}\n" . "/\n" . $query_string;
    // Generate base64-encoded RFC 2104-compliant HMAC-SHA256
    // signature with Secret Key using PHP 5's native
    // hash_hmac function.
    $http_query_params['Signature'] = base64_encode(hash_hmac('sha256', $http_get_string, $creds['secret_key'], true));
    // simple GET request to EC2 Query API with regular URL
    // encoded query string
    $http_request = "https://{$creds['aws_endpoint']}/?" . http_build_query($http_query_params);
    debug_output(__FILE__ . ':' . __FUNCTION__ . ':' . __LINE__ . ':' . "HTTP request sent to EC2 for {$ec2_api_action} = {$http_request}");
    $http_request_result = file_get_contents($http_request);
    // Check for errors in HTTPS  GET operation
    if (is_null($http_request_result) or empty($http_request_result)) {
        write_to_log_file("Error: HTTP result from EC2 was either null/empty or errors encountered\n");
        write_to_log_file("HTTP Result : {$http_request_result}\n");
        exit;
    }
    // The result from EC2 EC2 is in XML format - so convert it to an XML object.
    $xml_object = simplexml_load_string($http_request_result);
    // Since every successful query to the EC2 API will have a
    // "RequestId" returned, check for that first.
    $requestId = $xml_object->requestId;
    if (is_null($requestId) or empty($requestId)) {
        write_to_log_file(STDERR, "Error: EC2 requestId was either null or empty\n");
        exit;
    }
    return $xml_object;
}
        $shard_info .= $shard['_id'] . ' = ' . $shard['host'] . ' || ';
    }
    write_to_data_file($zabbix_name, "shard_info", $shard_info);
    $collection = $mongo_connection->selectDB('config')->selectCollection('databases');
    $cursor = $collection->find();
    $db_array = iterator_to_array($cursor);
    $db_info = '';
    foreach ($db_array as $db) {
        if ($db['partitioned']) {
            $partitioned = 'yes';
        } else {
            $partitioned = 'no';
        }
        $db_info .= $db['_id'] . ' : ' . 'partitioned = ' . $partitioned . ', primary = ' . $db['primary'] . ' || ';
    }
    write_to_data_file($zabbix_name, "db_info", $db_info);
}
//-------------------------------------------------------------------------//
// Get data collection end time (we will use this to compute the total data collection time)
$end_time = time();
$data_collection_time = $end_time - $start_time;
write_to_data_file($zabbix_name, "mongoDB_plugin_data_collection_time", $data_collection_time);
write_to_data_file($zabbix_name, "mongoDB_plugin_version", $command_version);
write_to_data_file($zabbix_name, "mongoDB_plugin_checksum", $md5_checksum_string);
fclose($data_file_handle);
exec("zabbix_sender -vv -z {$zabbix_server} -p {$zabbix_server_port} -i {$data_file_name} 2>&1", $log_file_data);
foreach ($log_file_data as $log_line) {
    write_to_log_file("{$log_line}\n");
}
fclose($log_file_handle);
exit;
//-------------------------------------------------------------------------//
$zabbix_name = $options['z'];
// Remove spaces from zabbix name for file data and log file creation
$file_base_name = str_replace(' ', '_', $zabbix_name);
$zabbix_server = $options['H'] ? $options['H'] : '127.0.0.1';
$zabbix_server_port = $options['P'] ? $options['P'] : '10051';
$debug_mode = isset($options['D']);
$ssl = isset($options['ssl']);
if ($ssl && !MONGO_SUPPORTS_SSL) {
    echo "WARNING: --ssl option is specified, but we will not use it, because the PHP Mongo extension does not support SSL!\n";
    $ssl = false;
}
$data_lines = array();
$md5_checksum_string = md5_file($argv[0]);
if ($debug_mode) {
    write_to_log_file("version {$command_version}");
}
//-------------------------------------------------------------------------//
//-------------------------------------------------------------------------//
function write_to_log($output_line)
{
    global $command_name;
    fprintf(STDERR, "%s: %s\n", $command_name, $output_line);
}
//-------------------------------------------------------------------------//
//-------------------------------------------------------------------------//
function write_to_data_lines($zabbix_name, $key, $value)
{
    global $data_lines;
    // Only if we have a value do we want to record this metric
    if (isset($value) && $value !== '') {