Add debug info to commands sent over Icinga 2's API

This commit is contained in:
Eric Lippmann 2016-09-08 09:23:49 +02:00
parent a5da4afb5c
commit 9402c1ffa6

View File

@ -4,6 +4,7 @@
namespace Icinga\Module\Monitoring\Web\Rest; namespace Icinga\Module\Monitoring\Web\Rest;
use Exception; use Exception;
use Icinga\Application\Logger;
/** /**
* REST Request * REST Request
@ -206,27 +207,46 @@ class RestRequest
CURLOPT_RETURNTRANSFER => true CURLOPT_RETURNTRANSFER => true
); );
// Record cURL command line for debugging
$curlCmd = array('curl', '-s', '-X', $this->method, '-H', escapeshellarg('Accept: application/json'));
if ($this->strictSsl) { if ($this->strictSsl) {
$options[CURLOPT_SSL_VERIFYHOST] = 2; $options[CURLOPT_SSL_VERIFYHOST] = 2;
$options[CURLOPT_SSL_VERIFYPEER] = true; $options[CURLOPT_SSL_VERIFYPEER] = true;
} else { } else {
$options[CURLOPT_SSL_VERIFYHOST] = false; $options[CURLOPT_SSL_VERIFYHOST] = false;
$options[CURLOPT_SSL_VERIFYPEER] = false; $options[CURLOPT_SSL_VERIFYPEER] = false;
$curlCmd[] = '-k';
} }
if ($this->hasBasicAuth) { if ($this->hasBasicAuth) {
$options[CURLOPT_USERPWD] = sprintf('%s:%s', $this->username, $this->password); $options[CURLOPT_USERPWD] = sprintf('%s:%s', $this->username, $this->password);
$curlCmd[] = sprintf('-u %s:%s', escapeshellarg($this->username), escapeshellarg($this->password));
} }
if (! empty($this->payload)) { if (! empty($this->payload)) {
$payload = $this->serializePayload($this->payload, $this->contentType); $payload = $this->serializePayload($this->payload, $this->contentType);
$options[CURLOPT_POSTFIELDS] = $payload; $options[CURLOPT_POSTFIELDS] = $payload;
$curlCmd[] = sprintf('-d %s', escapeshellarg($payload));
} }
$options[CURLOPT_HTTPHEADER] = $headers; $options[CURLOPT_HTTPHEADER] = $headers;
$stream = null;
if (Logger::getInstance()->getLevel() === Logger::DEBUG) {
$stream = fopen('php://temp', 'w');
$options[CURLOPT_VERBOSE] = true;
$options[CURLOPT_STDERR] = $stream;
}
curl_setopt_array($ch, $options); curl_setopt_array($ch, $options);
Logger::debug(
'Executing %s %s',
implode(' ', $curlCmd),
escapeshellarg($this->uri)
);
$result = curl_exec($ch); $result = curl_exec($ch);
if ($result === false) { if ($result === false) {
@ -235,6 +255,12 @@ class RestRequest
curl_close($ch); curl_close($ch);
if (is_resource($stream)) {
rewind($stream);
Logger::debug(stream_get_contents($stream));
fclose($stream);
}
$response = @json_decode($result, true); $response = @json_decode($result, true);
if ($response === null) { if ($response === null) {