Skip to content
Snippets Groups Projects
Commit aa14b1ed authored by hahn's avatar hahn
Browse files

update foreman class: fetch response infos for better debugging

parent 29df4d11
No related branches found
No related tags found
No related merge requests found
...@@ -114,8 +114,18 @@ class ForemanApi { ...@@ -114,8 +114,18 @@ class ForemanApi {
); );
/**
* last request
* @var type
*/
protected $_aRequest=array(); protected $_aRequest=array();
/**
* last response
* @var type
*/
protected $_aResponse=array();
// ---------------------------------------------------------------------- // ----------------------------------------------------------------------
// constructor // constructor
...@@ -144,8 +154,10 @@ class ForemanApi { ...@@ -144,8 +154,10 @@ class ForemanApi {
*/ */
protected function log($sMessage, $sLevel = "info") { protected function log($sMessage, $sLevel = "info") {
global $oCLog; global $oCLog;
if($oCLog && method_exists($oLog, 'add')){
return $oCLog->add(basename(__FILE__) . " class " . __CLASS__ . " - " . $sMessage, $sLevel); return $oCLog->add(basename(__FILE__) . " class " . __CLASS__ . " - " . $sMessage, $sLevel);
} }
}
/** /**
* search url prefix in $this->_aAllowedUrls by given key * search url prefix in $this->_aAllowedUrls by given key
...@@ -228,6 +240,7 @@ class ForemanApi { ...@@ -228,6 +240,7 @@ class ForemanApi {
if ($aRequest){ if ($aRequest){
$this->_aRequest=$aRequest; $this->_aRequest=$aRequest;
} }
$this->_aResponse=array();
$this->log(__FUNCTION__ . " start <pre>".print_r($this->_aRequest,1)."</pre>"); $this->log(__FUNCTION__ . " start <pre>".print_r($this->_aRequest,1)."</pre>");
if (!function_exists("curl_init")) { if (!function_exists("curl_init")) {
die("ERROR: PHP CURL module is not installed."); die("ERROR: PHP CURL module is not installed.");
...@@ -262,7 +275,6 @@ class ForemanApi { ...@@ -262,7 +275,6 @@ class ForemanApi {
$aReturn=array('info'=>curl_getinfo($ch), 'error'=>curl_error($ch)); $aReturn=array('info'=>curl_getinfo($ch), 'error'=>curl_error($ch));
curl_close($ch); curl_close($ch);
$this->log(__FUNCTION__ . " status ".$aReturn['info']['http_code'].' '.$this->_aRequest['method']." $sFullUrl"); $this->log(__FUNCTION__ . " status ".$aReturn['info']['http_code'].' '.$this->_aRequest['method']." $sFullUrl");
$sHeader=substr($res, 0, $aReturn['info']['header_size']); $sHeader=substr($res, 0, $aReturn['info']['header_size']);
$aReturn['header']=explode("\n", $sHeader); $aReturn['header']=explode("\n", $sHeader);
$aReturn['body']=str_replace($sHeader, "", $res); $aReturn['body']=str_replace($sHeader, "", $res);
...@@ -333,7 +345,7 @@ class ForemanApi { ...@@ -333,7 +345,7 @@ class ForemanApi {
// check status // check status
$iStatuscode=$aReturn['info']['http_code']; $iStatuscode=$aReturn['info']['http_code'];
if ($iStatuscode===0){ if ($iStatuscode===0){
$sStatus='wrong host'; $sStatus='wrong host or no connect';
} }
if ($iStatuscode>=200 && $iStatuscode<400){ if ($iStatuscode>=200 && $iStatuscode<400){
$sStatus='OK'; $sStatus='OK';
...@@ -363,6 +375,7 @@ class ForemanApi { ...@@ -363,6 +375,7 @@ class ForemanApi {
$aReturn['_OK']=$bOk; $aReturn['_OK']=$bOk;
$aReturn['_status']=$sStatus; $aReturn['_status']=$sStatus;
$this->_writeDebug(__FUNCTION__ . ' result of request <pre>'.print_r($aReturn,1).'</pre>'); $this->_writeDebug(__FUNCTION__ . ' result of request <pre>'.print_r($aReturn,1).'</pre>');
$this->_aResponse=$aReturn;
return $aReturn; return $aReturn;
} }
...@@ -513,4 +526,90 @@ class ForemanApi { ...@@ -513,4 +526,90 @@ class ForemanApi {
*/ */
} }
// ----------------------------------------------------------------------
// get response infos
// ----------------------------------------------------------------------
/**
* get curl info data from last response
* Array
(
[url] => https://foreman/api/hostgroups/?&per_page=1000
[content_type] => application/json; charset=utf-8
[http_code] => 200
[header_size] => 1034
[request_size] => 218
[filetime] => -1
[ssl_verify_result] => 0
[redirect_count] => 0
[total_time] => 1.644417
[namelookup_time] => 0.007198
[connect_time] => 0.009012
[pretransfer_time] => 0.0332
[size_upload] => 0
[size_download] => 119602
[speed_download] => 72732
[speed_upload] => 0
[download_content_length] => -1
[upload_content_length] => 0
[starttransfer_time] => 1.642775
[redirect_time] => 0
[redirect_url] =>
[primary_ip] => 10.0.2.10
[certinfo] => Array
(
)
[primary_port] => 443
[local_ip] => 10.0.2.15
[local_port] => 33906
)
* @param string $sKey get value of given key only
* @return any
*/
public function getResponseInfo($sKey=false){
if($sKey){
return isset($this->_aResponse['info'][$sKey])
? $this->_aResponse['info'][$sKey]
: false
;
}
return isset($this->_aResponse['info'])
? $this->_aResponse['info']
: false
;
/*
return isset($sKey) && $sKey
? isset($this->_aResponse['info'][$sKey])
? $this->_aResponse['info'][$sKey]
: false
: isset($this->_aResponse['info'])
? $this->_aResponse['info']
: false
;
*/
}
/**
* get array of last http response header
* @return array
*/
public function getResponseHeader(){
return isset($this->_aResponse['header'])
? $this->_aResponse['header']
: false
;
}
/**
* get status of last request
* @return string
*/
public function getResponseStatus(){
// print_r($this->_aResponse); die();
return isset($this->_aResponse['_status'])
? $this->_aResponse['_status']
: false
;
}
} }
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment