Client.php 38 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891
  1. <?php
  2. /**
  3. * Copyright (C) Alibaba Cloud Computing
  4. * All rights reserved
  5. */
  6. date_default_timezone_set ( 'Asia/Shanghai' );
  7. require_once realpath ( dirname ( __FILE__ ) . '/../../Log_Autoload.php' );
  8. require_once realpath ( dirname ( __FILE__ ) . '/requestcore.class.php' );
  9. require_once realpath ( dirname ( __FILE__ ) . '/sls.proto.php' );
  10. require_once realpath ( dirname ( __FILE__ ) . '/protocolbuffers.inc.php' );
  11. if(!defined('API_VERSION'))
  12. define('API_VERSION', '0.6.0');
  13. if(!defined('USER_AGENT'))
  14. define('USER_AGENT', 'log-php-sdk-v-0.6.0');
  15. /**
  16. * Aliyun_Log_Client class is the main class in the SDK. It can be used to
  17. * communicate with LOG server to put/get data.
  18. *
  19. * @author log_dev
  20. */
  21. class Aliyun_Log_Client {
  22. /**
  23. * @var string aliyun accessKey
  24. */
  25. protected $accessKey;
  26. /**
  27. * @var string aliyun accessKeyId
  28. */
  29. protected $accessKeyId;
  30. /**
  31. *@var string aliyun sts token
  32. */
  33. protected $stsToken;
  34. /**
  35. * @var string LOG endpoint
  36. */
  37. protected $endpoint;
  38. /**
  39. * @var string Check if the host if row ip.
  40. */
  41. protected $isRowIp;
  42. /**
  43. * @var integer Http send port. The dafault value is 80.
  44. */
  45. protected $port;
  46. /**
  47. * @var string log sever host.
  48. */
  49. protected $logHost;
  50. /**
  51. * @var string the local machine ip address.
  52. */
  53. protected $source;
  54. /**
  55. * Aliyun_Log_Client constructor
  56. *
  57. * @param string $endpoint
  58. * LOG host name, for example, http://cn-hangzhou.sls.aliyuncs.com
  59. * @param string $accessKeyId
  60. * aliyun accessKeyId
  61. * @param string $accessKey
  62. * aliyun accessKey
  63. */
  64. public function __construct($endpoint, $accessKeyId, $accessKey,$token = "") {
  65. $this->setEndpoint ( $endpoint ); // set $this->logHost
  66. $this->accessKeyId = $accessKeyId;
  67. $this->accessKey = $accessKey;
  68. $this->stsToken = $token;
  69. $this->source = Aliyun_Log_Util::getLocalIp();
  70. }
  71. private function setEndpoint($endpoint) {
  72. $pos = strpos ( $endpoint, "://" );
  73. if ($pos !== false) { // be careful, !==
  74. $pos += 3;
  75. $endpoint = substr ( $endpoint, $pos );
  76. }
  77. $pos = strpos ( $endpoint, "/" );
  78. if ($pos !== false) // be careful, !==
  79. $endpoint = substr ( $endpoint, 0, $pos );
  80. $pos = strpos ( $endpoint, ':' );
  81. if ($pos !== false) { // be careful, !==
  82. $this->port = ( int ) substr ( $endpoint, $pos + 1 );
  83. $endpoint = substr ( $endpoint, 0, $pos );
  84. } else
  85. $this->port = 80;
  86. $this->isRowIp = Aliyun_Log_Util::isIp ( $endpoint );
  87. $this->logHost = $endpoint;
  88. $this->endpoint = $endpoint . ':' . ( string ) $this->port;
  89. }
  90. /**
  91. * GMT format time string.
  92. *
  93. * @return string
  94. */
  95. protected function getGMT() {
  96. return gmdate ( 'D, d M Y H:i:s' ) . ' GMT';
  97. }
  98. /**
  99. * Decodes a JSON string to a JSON Object.
  100. * Unsuccessful decode will cause an Aliyun_Log_Exception.
  101. *
  102. * @return string
  103. * @throws Aliyun_Log_Exception
  104. */
  105. protected function parseToJson($resBody, $requestId) {
  106. if (! $resBody)
  107. return NULL;
  108. $result = json_decode ( $resBody, true );
  109. if ($result === NULL){
  110. throw new Aliyun_Log_Exception ( 'BadResponse', "Bad format,not json: $resBody", $requestId );
  111. }
  112. return $result;
  113. }
  114. /**
  115. * @return array
  116. */
  117. protected function getHttpResponse($method, $url, $body, $headers) {
  118. $request = new RequestCore ( $url );
  119. foreach ( $headers as $key => $value )
  120. $request->add_header ( $key, $value );
  121. $request->set_method ( $method );
  122. $request->set_useragent(USER_AGENT);
  123. if ($method == "POST" || $method == "PUT")
  124. $request->set_body ( $body );
  125. $request->send_request ();
  126. $response = array ();
  127. $response [] = ( int ) $request->get_response_code ();
  128. $response [] = $request->get_response_header ();
  129. $response [] = $request->get_response_body ();
  130. return $response;
  131. }
  132. /**
  133. * @return array
  134. * @throws Aliyun_Log_Exception
  135. */
  136. private function sendRequest($method, $url, $body, $headers) {
  137. try {
  138. list ( $responseCode, $header, $resBody ) =
  139. $this->getHttpResponse ( $method, $url, $body, $headers );
  140. } catch ( Exception $ex ) {
  141. throw new Aliyun_Log_Exception ( $ex->getMessage (), $ex->__toString () );
  142. }
  143. $requestId = isset ( $header ['x-log-requestid'] ) ? $header ['x-log-requestid'] : '';
  144. if ($responseCode == 200) {
  145. return array ($resBody,$header);
  146. }
  147. else {
  148. $exJson = $this->parseToJson ( $resBody, $requestId );
  149. if (isset($exJson ['error_code']) && isset($exJson ['error_message'])) {
  150. throw new Aliyun_Log_Exception ( $exJson ['error_code'],
  151. $exJson ['error_message'], $requestId );
  152. } else {
  153. if ($exJson) {
  154. $exJson = ' The return json is ' . json_encode($exJson);
  155. } else {
  156. $exJson = '';
  157. }
  158. throw new Aliyun_Log_Exception ( 'RequestError',
  159. "Request is failed. Http code is $responseCode.$exJson", $requestId );
  160. }
  161. }
  162. }
  163. /**
  164. * @return array
  165. * @throws Aliyun_Log_Exception
  166. */
  167. private function send($method, $project, $body, $resource, $params, $headers) {
  168. if ($body) {
  169. $headers ['Content-Length'] = strlen ( $body );
  170. if(isset($headers ["x-log-bodyrawsize"])==false)
  171. $headers ["x-log-bodyrawsize"] = 0;
  172. $headers ['Content-MD5'] = Aliyun_Log_Util::calMD5 ( $body );
  173. } else {
  174. $headers ['Content-Length'] = 0;
  175. $headers ["x-log-bodyrawsize"] = 0;
  176. $headers ['Content-Type'] = ''; // If not set, http request will add automatically.
  177. }
  178. $headers ['x-log-apiversion'] = API_VERSION;
  179. $headers ['x-log-signaturemethod'] = 'hmac-sha1';
  180. if(strlen($this->stsToken) >0)
  181. $headers ['x-acs-security-token'] = $this -> stsToken;
  182. if(is_null($project))$headers ['Host'] = $this->logHost;
  183. else $headers ['Host'] = "$project.$this->logHost";
  184. $headers ['Date'] = $this->GetGMT ();
  185. $signature = Aliyun_Log_Util::getRequestAuthorization ( $method, $resource, $this->accessKey,$this->stsToken, $params, $headers );
  186. $headers ['Authorization'] = "LOG $this->accessKeyId:$signature";
  187. $url = $resource;
  188. if ($params)
  189. $url .= '?' . Aliyun_Log_Util::urlEncode ( $params );
  190. if ($this->isRowIp)
  191. $url = "http://$this->endpoint$url";
  192. else{
  193. if(is_null($project))
  194. $url = "http://$this->endpoint$url";
  195. else $url = "http://$project.$this->endpoint$url";
  196. }
  197. return $this->sendRequest ( $method, $url, $body, $headers );
  198. }
  199. /**
  200. * Put logs to Log Service.
  201. * Unsuccessful opertaion will cause an Aliyun_Log_Exception.
  202. *
  203. * @param Aliyun_Log_Models_PutLogsRequest $request the PutLogs request parameters class
  204. * @throws Aliyun_Log_Exception
  205. * @return Aliyun_Log_Models_PutLogsResponse
  206. */
  207. public function putLogs(Aliyun_Log_Models_PutLogsRequest $request) {
  208. if (count ( $request->getLogitems () ) > 4096)
  209. throw new Aliyun_Log_Exception ( 'InvalidLogSize', "logItems' length exceeds maximum limitation: 4096 lines." );
  210. $logGroup = new LogGroup ();
  211. $topic = $request->getTopic () !== null ? $request->getTopic () : '';
  212. $logGroup->setTopic ( $request->getTopic () );
  213. $source = $request->getSource ();
  214. if ( ! $source )
  215. $source = $this->source;
  216. $logGroup->setSource ( $source );
  217. $logitems = $request->getLogitems ();
  218. foreach ( $logitems as $logItem ) {
  219. $log = new Log ();
  220. $log->setTime ( $logItem->getTime () );
  221. $content = $logItem->getContents ();
  222. foreach ( $content as $key => $value ) {
  223. $content = new Log_Content ();
  224. $content->setKey ( $key );
  225. $content->setValue ( $value );
  226. $log->addContents ( $content );
  227. }
  228. $logGroup->addLogs ( $log );
  229. }
  230. $body = Aliyun_Log_Util::toBytes ( $logGroup );
  231. unset ( $logGroup );
  232. $bodySize = strlen ( $body );
  233. if ($bodySize > 3 * 1024 * 1024) // 3 MB
  234. throw new Aliyun_Log_Exception ( 'InvalidLogSize', "logItems' size exceeds maximum limitation: 3 MB." );
  235. $params = array ();
  236. $headers = array ();
  237. $headers ["x-log-bodyrawsize"] = $bodySize;
  238. $headers ['x-log-compresstype'] = 'deflate';
  239. $headers ['Content-Type'] = 'application/x-protobuf';
  240. $body = gzcompress ( $body, 6 );
  241. $logstore = $request->getLogstore () !== null ? $request->getLogstore () : '';
  242. $project = $request->getProject () !== null ? $request->getProject () : '';
  243. $shardKey = $request -> getShardKey();
  244. $resource = "/logstores/" . $logstore.($shardKey== null?"/shards/lb":"/shards/route");
  245. if($shardKey)
  246. $params["key"]=$shardKey;
  247. list ( $resp, $header ) = $this->send ( "POST", $project, $body, $resource, $params, $headers );
  248. $requestId = isset ( $header ['x-log-requestid'] ) ? $header ['x-log-requestid'] : '';
  249. $resp = $this->parseToJson ( $resp, $requestId );
  250. return new Aliyun_Log_Models_PutLogsResponse ( $header );
  251. }
  252. /**
  253. * create logstore
  254. * Unsuccessful opertaion will cause an Aliyun_Log_Exception.
  255. *
  256. * @param Aliyun_Log_Models_CreateLogstoreRequest $request the CreateLogStore request parameters class.
  257. * @throws Aliyun_Log_Exception
  258. * return Aliyun_Log_Models_CreateLogstoreResponse
  259. */
  260. public function createLogstore(Aliyun_Log_Models_CreateLogstoreRequest $request){
  261. $headers = array ();
  262. $params = array ();
  263. $resource = '/logstores';
  264. $project = $request->getProject () !== null ? $request->getProject () : '';
  265. $headers["x-log-bodyrawsize"] = 0;
  266. $headers["Content-Type"] = "application/json";
  267. $body = array(
  268. "logstoreName" => $request -> getLogstore(),
  269. "ttl" => (int)($request -> getTtl()),
  270. "shardCount" => (int)($request -> getShardCount())
  271. );
  272. $body_str = json_encode($body);
  273. list($resp,$header) = $this -> send("POST",$project,$body_str,$resource,$params,$headers);
  274. $requestId = isset ( $header ['x-log-requestid'] ) ? $header ['x-log-requestid'] : '';
  275. $resp = $this->parseToJson ( $resp, $requestId );
  276. return new Aliyun_Log_Models_CreateLogstoreResponse($resp,$header);
  277. }
  278. /**
  279. * update logstore
  280. * Unsuccessful opertaion will cause an Aliyun_Log_Exception.
  281. *
  282. * @param Aliyun_Log_Models_UpdateLogstoreRequest $request the UpdateLogStore request parameters class.
  283. * @throws Aliyun_Log_Exception
  284. * return Aliyun_Log_Models_UpdateLogstoreResponse
  285. */
  286. public function updateLogstore(Aliyun_Log_Models_UpdateLogstoreRequest $request){
  287. $headers = array ();
  288. $params = array ();
  289. $project = $request->getProject () !== null ? $request->getProject () : '';
  290. $headers["x-log-bodyrawsize"] = 0;
  291. $headers["Content-Type"] = "application/json";
  292. $body = array(
  293. "logstoreName" => $request -> getLogstore(),
  294. "ttl" => (int)($request -> getTtl()),
  295. "shardCount" => (int)($request -> getShardCount())
  296. );
  297. $resource = '/logstores/'.$request -> getLogstore();
  298. $body_str = json_encode($body);
  299. list($resp,$header) = $this -> send("PUT",$project,$body_str,$resource,$params,$headers);
  300. $requestId = isset ( $header ['x-log-requestid'] ) ? $header ['x-log-requestid'] : '';
  301. $resp = $this->parseToJson ( $resp, $requestId );
  302. return new Aliyun_Log_Models_UpdateLogstoreResponse($resp,$header);
  303. }
  304. /**
  305. * List all logstores of requested project.
  306. * Unsuccessful opertaion will cause an Aliyun_Log_Exception.
  307. *
  308. * @param Aliyun_Log_Models_ListLogstoresRequest $request the ListLogstores request parameters class.
  309. * @throws Aliyun_Log_Exception
  310. * @return Aliyun_Log_Models_ListLogstoresResponse
  311. */
  312. public function listLogstores(Aliyun_Log_Models_ListLogstoresRequest $request) {
  313. $headers = array ();
  314. $params = array ();
  315. $resource = '/logstores';
  316. $project = $request->getProject () !== null ? $request->getProject () : '';
  317. list ( $resp, $header ) = $this->send ( "GET", $project, NULL, $resource, $params, $headers );
  318. $requestId = isset ( $header ['x-log-requestid'] ) ? $header ['x-log-requestid'] : '';
  319. $resp = $this->parseToJson ( $resp, $requestId );
  320. return new Aliyun_Log_Models_ListLogstoresResponse ( $resp, $header );
  321. }
  322. /**
  323. * Delete logstore
  324. * Unsuccessful opertaion will cause an Aliyun_Log_Exception.
  325. *
  326. * @param Aliyun_Log_Models_DeleteLogstoreRequest $request the DeleteLogstores request parameters class.
  327. * @throws Aliyun_Log_Exception
  328. * @return Aliyun_Log_Models_DeleteLogstoresResponse
  329. */
  330. public function deleteLogstore(Aliyun_Log_Models_DeleteLogstoreRequest $request) {
  331. $headers = array ();
  332. $params = array ();
  333. $project = $request->getProject () !== null ? $request->getProject () : '';
  334. $logstore = $request -> getLogstore() != null ? $request -> getLogstore() :"";
  335. $resource = "/logstores/$logstore";
  336. list ( $resp, $header ) = $this->send ( "DELETE", $project, NULL, $resource, $params, $headers );
  337. $requestId = isset ( $header ['x-log-requestid'] ) ? $header ['x-log-requestid'] : '';
  338. $resp = $this->parseToJson ( $resp, $requestId );
  339. return new Aliyun_Log_Models_DeleteLogstoreResponse ( $resp, $header );
  340. }
  341. /**
  342. * List all topics in a logstore.
  343. * Unsuccessful opertaion will cause an Aliyun_Log_Exception.
  344. *
  345. * @param Aliyun_Log_Models_ListTopicsRequest $request the ListTopics request parameters class.
  346. * @throws Aliyun_Log_Exception
  347. * @return Aliyun_Log_Models_ListTopicsResponse
  348. */
  349. public function listTopics(Aliyun_Log_Models_ListTopicsRequest $request) {
  350. $headers = array ();
  351. $params = array ();
  352. if ($request->getToken () !== null)
  353. $params ['token'] = $request->getToken ();
  354. if ($request->getLine () !== null)
  355. $params ['line'] = $request->getLine ();
  356. $params ['type'] = 'topic';
  357. $logstore = $request->getLogstore () !== null ? $request->getLogstore () : '';
  358. $project = $request->getProject () !== null ? $request->getProject () : '';
  359. $resource = "/logstores/$logstore";
  360. list ( $resp, $header ) = $this->send ( "GET", $project, NULL, $resource, $params, $headers );
  361. $requestId = isset ( $header ['x-log-requestid'] ) ? $header ['x-log-requestid'] : '';
  362. $resp = $this->parseToJson ( $resp, $requestId );
  363. return new Aliyun_Log_Models_ListTopicsResponse ( $resp, $header );
  364. }
  365. /**
  366. * Get histograms of requested query from log service.
  367. * Unsuccessful opertaion will cause an Aliyun_Log_Exception.
  368. *
  369. * @param Aliyun_Log_Models_GetHistogramsRequest $request the GetHistograms request parameters class.
  370. * @throws Aliyun_Log_Exception
  371. * @return array(json body, http header)
  372. */
  373. public function getHistogramsJson(Aliyun_Log_Models_GetHistogramsRequest $request) {
  374. $headers = array ();
  375. $params = array ();
  376. if ($request->getTopic () !== null)
  377. $params ['topic'] = $request->getTopic ();
  378. if ($request->getFrom () !== null)
  379. $params ['from'] = $request->getFrom ();
  380. if ($request->getTo () !== null)
  381. $params ['to'] = $request->getTo ();
  382. if ($request->getQuery () !== null)
  383. $params ['query'] = $request->getQuery ();
  384. $params ['type'] = 'histogram';
  385. $logstore = $request->getLogstore () !== null ? $request->getLogstore () : '';
  386. $project = $request->getProject () !== null ? $request->getProject () : '';
  387. $resource = "/logstores/$logstore";
  388. list ( $resp, $header ) = $this->send ( "GET", $project, NULL, $resource, $params, $headers );
  389. $requestId = isset ( $header ['x-log-requestid'] ) ? $header ['x-log-requestid'] : '';
  390. $resp = $this->parseToJson ( $resp, $requestId );
  391. return array($resp, $header);
  392. }
  393. /**
  394. * Get histograms of requested query from log service.
  395. * Unsuccessful opertaion will cause an Aliyun_Log_Exception.
  396. *
  397. * @param Aliyun_Log_Models_GetHistogramsRequest $request the GetHistograms request parameters class.
  398. * @throws Aliyun_Log_Exception
  399. * @return Aliyun_Log_Models_GetHistogramsResponse
  400. */
  401. public function getHistograms(Aliyun_Log_Models_GetHistogramsRequest $request) {
  402. $ret = $this->getHistogramsJson($request);
  403. $resp = $ret[0];
  404. $header = $ret[1];
  405. return new Aliyun_Log_Models_GetHistogramsResponse ( $resp, $header );
  406. }
  407. /**
  408. * Get logs from Log service.
  409. * Unsuccessful opertaion will cause an Aliyun_Log_Exception.
  410. *
  411. * @param Aliyun_Log_Models_GetLogsRequest $request the GetLogs request parameters class.
  412. * @throws Aliyun_Log_Exception
  413. * @return array(json body, http header)
  414. */
  415. public function getLogsJson(Aliyun_Log_Models_GetLogsRequest $request) {
  416. $headers = array ();
  417. $params = array ();
  418. if ($request->getTopic () !== null)
  419. $params ['topic'] = $request->getTopic ();
  420. if ($request->getFrom () !== null)
  421. $params ['from'] = $request->getFrom ();
  422. if ($request->getTo () !== null)
  423. $params ['to'] = $request->getTo ();
  424. if ($request->getQuery () !== null)
  425. $params ['query'] = $request->getQuery ();
  426. $params ['type'] = 'log';
  427. if ($request->getLine () !== null)
  428. $params ['line'] = $request->getLine ();
  429. if ($request->getOffset () !== null)
  430. $params ['offset'] = $request->getOffset ();
  431. if ($request->getOffset () !== null)
  432. $params ['reverse'] = $request->getReverse () ? 'true' : 'false';
  433. $logstore = $request->getLogstore () !== null ? $request->getLogstore () : '';
  434. $project = $request->getProject () !== null ? $request->getProject () : '';
  435. $resource = "/logstores/$logstore";
  436. list ( $resp, $header ) = $this->send ( "GET", $project, NULL, $resource, $params, $headers );
  437. $requestId = isset ( $header ['x-log-requestid'] ) ? $header ['x-log-requestid'] : '';
  438. $resp = $this->parseToJson ( $resp, $requestId );
  439. return array($resp, $header);
  440. //return new Aliyun_Log_Models_GetLogsResponse ( $resp, $header );
  441. }
  442. /**
  443. * Get logs from Log service.
  444. * Unsuccessful opertaion will cause an Aliyun_Log_Exception.
  445. *
  446. * @param Aliyun_Log_Models_GetLogsRequest $request the GetLogs request parameters class.
  447. * @throws Aliyun_Log_Exception
  448. * @return Aliyun_Log_Models_GetLogsResponse
  449. */
  450. public function getLogs(Aliyun_Log_Models_GetLogsRequest $request) {
  451. $ret = $this->getLogsJson($request);
  452. $resp = $ret[0];
  453. $header = $ret[1];
  454. return new Aliyun_Log_Models_GetLogsResponse ( $resp, $header );
  455. }
  456. /**
  457. * Get logs from Log service with shardid conditions.
  458. * Unsuccessful opertaion will cause an Aliyun_Log_Exception.
  459. *
  460. * @param Aliyun_Log_Models_BatchGetLogsRequest $request the BatchGetLogs request parameters class.
  461. * @throws Aliyun_Log_Exception
  462. * @return Aliyun_Log_Models_BatchGetLogsResponse
  463. */
  464. public function batchGetLogs(Aliyun_Log_Models_BatchGetLogsRequest $request) {
  465. $params = array();
  466. $headers = array();
  467. $project = $request->getProject()!==null?$request->getProject():'';
  468. $logstore = $request->getLogstore()!==null?$request->getLogstore():'';
  469. $shardId = $request->getShardId()!==null?$request->getShardId():'';
  470. if($request->getCount()!==null)
  471. $params['count']=$request->getCount();
  472. if($request->getCursor()!==null)
  473. $params['cursor']=$request->getCursor();
  474. $params['type']='log';
  475. $headers['Accept-Encoding']='gzip';
  476. $headers['accept']='application/x-protobuf';
  477. $resource = "/logstores/$logstore/shards/$shardId";
  478. list($resp,$header) = $this->send("GET",$project,NULL,$resource,$params,$headers);
  479. //$resp is a byteArray
  480. $resp = gzuncompress($resp);
  481. if($resp===false)$resp = new LogGroupList();
  482. else {
  483. $resp = new LogGroupList($resp);
  484. }
  485. return new Aliyun_Log_Models_BatchGetLogsResponse ( $resp, $header );
  486. }
  487. /**
  488. * List Shards from Log service with Project and logstore conditions.
  489. * Unsuccessful opertaion will cause an Aliyun_Log_Exception.
  490. *
  491. * @param Aliyun_Log_Models_ListShardsRequest $request the ListShards request parameters class.
  492. * @throws Aliyun_Log_Exception
  493. * @return Aliyun_Log_Models_ListShardsResponse
  494. */
  495. public function listShards(Aliyun_Log_Models_ListShardsRequest $request) {
  496. $params = array();
  497. $headers = array();
  498. $project = $request->getProject()!==null?$request->getProject():'';
  499. $logstore = $request->getLogstore()!==null?$request->getLogstore():'';
  500. $resource='/logstores/'.$logstore.'/shards';
  501. list($resp,$header) = $this->send("GET",$project,NULL,$resource,$params,$headers);
  502. $requestId = isset ( $header ['x-log-requestid'] ) ? $header ['x-log-requestid'] : '';
  503. $resp = $this->parseToJson ( $resp, $requestId );
  504. return new Aliyun_Log_Models_ListShardsResponse ( $resp, $header );
  505. }
  506. /**
  507. * split a shard into two shards with Project and logstore and shardId and midHash conditions.
  508. * Unsuccessful opertaion will cause an Aliyun_Log_Exception.
  509. *
  510. * @param Aliyun_Log_Models_SplitShardRequest $request the SplitShard request parameters class.
  511. * @throws Aliyun_Log_Exception
  512. * @return Aliyun_Log_Models_ListShardsResponse
  513. */
  514. public function splitShard(Aliyun_Log_Models_SplitShardRequest $request) {
  515. $params = array();
  516. $headers = array();
  517. $project = $request->getProject()!==null?$request->getProject():'';
  518. $logstore = $request->getLogstore()!==null?$request->getLogstore():'';
  519. $shardId = $request -> getShardId()!= null ? $request -> getShardId():-1;
  520. $midHash = $request -> getMidHash()!= null?$request -> getMidHash():"";
  521. $resource='/logstores/'.$logstore.'/shards/'.$shardId;
  522. $params["action"] = "split";
  523. $params["key"] = $midHash;
  524. list($resp,$header) = $this->send("POST",$project,NULL,$resource,$params,$headers);
  525. $requestId = isset ( $header ['x-log-requestid'] ) ? $header ['x-log-requestid'] : '';
  526. $resp = $this->parseToJson ( $resp, $requestId );
  527. return new Aliyun_Log_Models_ListShardsResponse ( $resp, $header );
  528. }
  529. /**
  530. * merge two shards into one shard with Project and logstore and shardId and conditions.
  531. * Unsuccessful opertaion will cause an Aliyun_Log_Exception.
  532. *
  533. * @param Aliyun_Log_Models_MergeShardsRequest $request the MergeShards request parameters class.
  534. * @throws Aliyun_Log_Exception
  535. * @return Aliyun_Log_Models_ListShardsResponse
  536. */
  537. public function MergeShards(Aliyun_Log_Models_MergeShardsRequest $request) {
  538. $params = array();
  539. $headers = array();
  540. $project = $request->getProject()!==null?$request->getProject():'';
  541. $logstore = $request->getLogstore()!==null?$request->getLogstore():'';
  542. $shardId = $request -> getShardId()!= null ? $request -> getShardId():-1;
  543. $resource='/logstores/'.$logstore.'/shards/'.$shardId;
  544. $params["action"] = "merge";
  545. list($resp,$header) = $this->send("POST",$project,NULL,$resource,$params,$headers);
  546. $requestId = isset ( $header ['x-log-requestid'] ) ? $header ['x-log-requestid'] : '';
  547. $resp = $this->parseToJson ( $resp, $requestId );
  548. return new Aliyun_Log_Models_ListShardsResponse ( $resp, $header );
  549. }
  550. /**
  551. * delete a read only shard with Project and logstore and shardId conditions.
  552. * Unsuccessful opertaion will cause an Aliyun_Log_Exception.
  553. *
  554. * @param Aliyun_Log_Models_DeleteShardRequest $request the DeleteShard request parameters class.
  555. * @throws Aliyun_Log_Exception
  556. * @return Aliyun_Log_Models_ListShardsResponse
  557. */
  558. public function DeleteShard(Aliyun_Log_Models_DeleteShardRequest $request) {
  559. $params = array();
  560. $headers = array();
  561. $project = $request->getProject()!==null?$request->getProject():'';
  562. $logstore = $request->getLogstore()!==null?$request->getLogstore():'';
  563. $shardId = $request -> getShardId()!= null ? $request -> getShardId():-1;
  564. $resource='/logstores/'.$logstore.'/shards/'.$shardId;
  565. list($resp,$header) = $this->send("DELETE",$project,NULL,$resource,$params,$headers);
  566. $requestId = isset ( $header ['x-log-requestid'] ) ? $header ['x-log-requestid'] : '';
  567. return new Aliyun_Log_Models_DeleteShardResponse ( $header );
  568. }
  569. /**
  570. * Get cursor from Log service.
  571. * Unsuccessful opertaion will cause an Aliyun_Log_Exception.
  572. *
  573. * @param Aliyun_Log_Models_GetCursorRequest $request the GetCursor request parameters class.
  574. * @throws Aliyun_Log_Exception
  575. * @return Aliyun_Log_Models_GetCursorResponse
  576. */
  577. public function getCursor(Aliyun_Log_Models_GetCursorRequest $request){
  578. $params = array();
  579. $headers = array();
  580. $project = $request->getProject()!==null?$request->getProject():'';
  581. $logstore = $request->getLogstore()!==null?$request->getLogstore():'';
  582. $shardId = $request->getShardId()!==null?$request->getShardId():'';
  583. $mode = $request->getMode()!==null?$request->getMode():'';
  584. $fromTime = $request->getFromTime()!==null?$request->getFromTime():-1;
  585. if((empty($mode) xor $fromTime==-1)==false){
  586. if(!empty($mode))
  587. throw new Aliyun_Log_Exception ( 'RequestError',"Request is failed. Mode and fromTime can not be not empty simultaneously");
  588. else
  589. throw new Aliyun_Log_Exception ( 'RequestError',"Request is failed. Mode and fromTime can not be empty simultaneously");
  590. }
  591. if(!empty($mode) && strcmp($mode,'begin')!==0 && strcmp($mode,'end')!==0)
  592. throw new Aliyun_Log_Exception ( 'RequestError',"Request is failed. Mode value invalid:$mode");
  593. if($fromTime!==-1 && (is_integer($fromTime)==false || $fromTime<0))
  594. throw new Aliyun_Log_Exception ( 'RequestError',"Request is failed. FromTime value invalid:$fromTime");
  595. $params['type']='cursor';
  596. if($fromTime!==-1)$params['from']=$fromTime;
  597. else $params['mode'] = $mode;
  598. $resource='/logstores/'.$logstore.'/shards/'.$shardId;
  599. list($resp,$header) = $this->send("GET",$project,NULL,$resource,$params,$headers);
  600. $requestId = isset ( $header ['x-log-requestid'] ) ? $header ['x-log-requestid'] : '';
  601. $resp = $this->parseToJson ( $resp, $requestId );
  602. return new Aliyun_Log_Models_GetCursorResponse($resp,$header);
  603. }
  604. public function createConfig(Aliyun_Log_Models_CreateConfigRequest $request){
  605. $params = array();
  606. $headers = array();
  607. $body=null;
  608. if($request->getConfig()!==null){
  609. $body = json_encode($request->getConfig()->toArray());
  610. }
  611. $headers ['Content-Type'] = 'application/json';
  612. $resource = '/configs';
  613. list($resp,$header) = $this->send("POST",NULL,$body,$resource,$params,$headers);
  614. return new Aliyun_Log_Models_CreateConfigResponse($header);
  615. }
  616. public function updateConfig(Aliyun_Log_Models_UpdateConfigRequest $request){
  617. $params = array();
  618. $headers = array();
  619. $body=null;
  620. $configName='';
  621. if($request->getConfig()!==null){
  622. $body = json_encode($request->getConfig()->toArray());
  623. $configName=($request->getConfig()->getConfigName()!==null)?$request->getConfig()->getConfigName():'';
  624. }
  625. $headers ['Content-Type'] = 'application/json';
  626. $resource = '/configs/'.$configName;
  627. list($resp,$header) = $this->send("PUT",NULL,$body,$resource,$params,$headers);
  628. return new Aliyun_Log_Models_UpdateConfigResponse($header);
  629. }
  630. public function getConfig(Aliyun_Log_Models_GetConfigRequest $request){
  631. $params = array();
  632. $headers = array();
  633. $configName = ($request->getConfigName()!==null)?$request->getConfigName():'';
  634. $resource = '/configs/'.$configName;
  635. list($resp,$header) = $this->send("GET",NULL,NULL,$resource,$params,$headers);
  636. $requestId = isset ( $header ['x-log-requestid'] ) ? $header ['x-log-requestid'] : '';
  637. $resp = $this->parseToJson ( $resp, $requestId );
  638. return new Aliyun_Log_Models_GetConfigResponse($resp,$header);
  639. }
  640. public function deleteConfig(Aliyun_Log_Models_DeleteConfigRequest $request){
  641. $params = array();
  642. $headers = array();
  643. $configName = ($request->getConfigName()!==null)?$request->getConfigName():'';
  644. $resource = '/configs/'.$configName;
  645. list($resp,$header) = $this->send("DELETE",NULL,NULL,$resource,$params,$headers);
  646. return new Aliyun_Log_Models_DeleteConfigResponse($header);
  647. }
  648. public function listConfigs(Aliyun_Log_Models_ListConfigsRequest $request){
  649. $params = array();
  650. $headers = array();
  651. if($request->getConfigName()!==null)$params['configName'] = $request->getConfigName();
  652. if($request->getOffset()!==null)$params['offset'] = $request->getOffset();
  653. if($request->getSize()!==null)$params['size'] = $request->getSize();
  654. $resource = '/configs';
  655. list($resp,$header) = $this->send("GET",NULL,NULL,$resource,$params,$headers);
  656. $requestId = isset ( $header ['x-log-requestid'] ) ? $header ['x-log-requestid'] : '';
  657. $resp = $this->parseToJson ( $resp, $requestId );
  658. return new Aliyun_Log_Models_ListConfigsResponse($resp,$header);
  659. }
  660. public function createMachineGroup(Aliyun_Log_Models_CreateMachineGroupRequest $request){
  661. $params = array();
  662. $headers = array();
  663. $body=null;
  664. if($request->getMachineGroup()!==null){
  665. $body = json_encode($request->getMachineGroup()->toArray());
  666. }
  667. $headers ['Content-Type'] = 'application/json';
  668. $resource = '/machinegroups';
  669. list($resp,$header) = $this->send("POST",NULL,$body,$resource,$params,$headers);
  670. return new Aliyun_Log_Models_CreateMachineGroupResponse($header);
  671. }
  672. public function updateMachineGroup(Aliyun_Log_Models_UpdateMachineGroupRequest $request){
  673. $params = array();
  674. $headers = array();
  675. $body=null;
  676. $groupName='';
  677. if($request->getMachineGroup()!==null){
  678. $body = json_encode($request->getMachineGroup()->toArray());
  679. $groupName=($request->getMachineGroup()->getGroupName()!==null)?$request->getMachineGroup()->getGroupName():'';
  680. }
  681. $headers ['Content-Type'] = 'application/json';
  682. $resource = '/machinegroups/'.$groupName;
  683. list($resp,$header) = $this->send("PUT",NULL,$body,$resource,$params,$headers);
  684. return new Aliyun_Log_Models_UpdateMachineGroupResponse($header);
  685. }
  686. public function getMachineGroup(Aliyun_Log_Models_GetMachineGroupRequest $request){
  687. $params = array();
  688. $headers = array();
  689. $groupName = ($request->getGroupName()!==null)?$request->getGroupName():'';
  690. $resource = '/machinegroups/'.$groupName;
  691. list($resp,$header) = $this->send("GET",NULL,NULL,$resource,$params,$headers);
  692. $requestId = isset ( $header ['x-log-requestid'] ) ? $header ['x-log-requestid'] : '';
  693. $resp = $this->parseToJson ( $resp, $requestId );
  694. return new Aliyun_Log_Models_GetMachineGroupResponse($resp,$header);
  695. }
  696. public function deleteMachineGroup(Aliyun_Log_Models_DeleteMachineGroupRequest $request){
  697. $params = array();
  698. $headers = array();
  699. $groupName = ($request->getGroupName()!==null)?$request->getGroupName():'';
  700. $resource = '/machinegroups/'.$groupName;
  701. list($resp,$header) = $this->send("DELETE",NULL,NULL,$resource,$params,$headers);
  702. return new Aliyun_Log_Models_DeleteMachineGroupResponse($header);
  703. }
  704. public function listMachineGroups(Aliyun_Log_Models_ListMachineGroupsRequest $request){
  705. $params = array();
  706. $headers = array();
  707. if($request->getGroupName()!==null)$params['groupName'] = $request->getGroupName();
  708. if($request->getOffset()!==null)$params['offset'] = $request->getOffset();
  709. if($request->getSize()!==null)$params['size'] = $request->getSize();
  710. $resource = '/machinegroups';
  711. list($resp,$header) = $this->send("GET",NULL,NULL,$resource,$params,$headers);
  712. $requestId = isset ( $header ['x-log-requestid'] ) ? $header ['x-log-requestid'] : '';
  713. $resp = $this->parseToJson ( $resp, $requestId );
  714. return new Aliyun_Log_Models_ListMachineGroupsResponse($resp,$header);
  715. }
  716. public function applyConfigToMachineGroup(Aliyun_Log_Models_ApplyConfigToMachineGroupRequest $request){
  717. $params = array();
  718. $headers = array();
  719. $configName=$request->getConfigName();
  720. $groupName=$request->getGroupName();
  721. $headers ['Content-Type'] = 'application/json';
  722. $resource = '/machinegroups/'.$groupName.'/configs/'.$configName;
  723. list($resp,$header) = $this->send("PUT",NULL,NULL,$resource,$params,$headers);
  724. return new Aliyun_Log_Models_ApplyConfigToMachineGroupResponse($header);
  725. }
  726. public function removeConfigFromMachineGroup(Aliyun_Log_Models_RemoveConfigFromMachineGroupRequest $request){
  727. $params = array();
  728. $headers = array();
  729. $configName=$request->getConfigName();
  730. $groupName=$request->getGroupName();
  731. $headers ['Content-Type'] = 'application/json';
  732. $resource = '/machinegroups/'.$groupName.'/configs/'.$configName;
  733. list($resp,$header) = $this->send("DELETE",NULL,NULL,$resource,$params,$headers);
  734. return new Aliyun_Log_Models_RemoveConfigFromMachineGroupResponse($header);
  735. }
  736. public function getMachine(Aliyun_Log_Models_GetMachineRequest $request){
  737. $params = array();
  738. $headers = array();
  739. $uuid = ($request->getUuid()!==null)?$request->getUuid():'';
  740. $resource = '/machines/'.$uuid;
  741. list($resp,$header) = $this->send("GET",NULL,NULL,$resource,$params,$headers);
  742. $requestId = isset ( $header ['x-log-requestid'] ) ? $header ['x-log-requestid'] : '';
  743. $resp = $this->parseToJson ( $resp, $requestId );
  744. return new Aliyun_Log_Models_GetMachineResponse($resp,$header);
  745. }
  746. public function createACL(Aliyun_Log_Models_CreateACLRequest $request){
  747. $params = array();
  748. $headers = array();
  749. $body=null;
  750. if($request->getAcl()!==null){
  751. $body = json_encode($request->getAcl()->toArray());
  752. }
  753. $headers ['Content-Type'] = 'application/json';
  754. $resource = '/acls';
  755. list($resp,$header) = $this->send("POST",NULL,$body,$resource,$params,$headers);
  756. $requestId = isset ( $header ['x-log-requestid'] ) ? $header ['x-log-requestid'] : '';
  757. $resp = $this->parseToJson ( $resp, $requestId );
  758. return new Aliyun_Log_Models_CreateACLResponse($resp,$header);
  759. }
  760. public function updateACL(Aliyun_Log_Models_UpdateACLRequest $request){
  761. $params = array();
  762. $headers = array();
  763. $body=null;
  764. $aclId='';
  765. if($request->getAcl()!==null){
  766. $body = json_encode($request->getAcl()->toArray());
  767. $aclId=($request->getAcl()->getAclId()!==null)?$request->getAcl()->getAclId():'';
  768. }
  769. $headers ['Content-Type'] = 'application/json';
  770. $resource = '/acls/'.$aclId;
  771. list($resp,$header) = $this->send("PUT",NULL,$body,$resource,$params,$headers);
  772. return new Aliyun_Log_Models_UpdateACLResponse($header);
  773. }
  774. public function getACL(Aliyun_Log_Models_GetACLRequest $request){
  775. $params = array();
  776. $headers = array();
  777. $aclId = ($request->getAclId()!==null)?$request->getAclId():'';
  778. $resource = '/acls/'.$aclId;
  779. list($resp,$header) = $this->send("GET",NULL,NULL,$resource,$params,$headers);
  780. $requestId = isset ( $header ['x-log-requestid'] ) ? $header ['x-log-requestid'] : '';
  781. $resp = $this->parseToJson ( $resp, $requestId );
  782. return new Aliyun_Log_Models_GetACLResponse($resp,$header);
  783. }
  784. public function deleteACL(Aliyun_Log_Models_DeleteACLRequest $request){
  785. $params = array();
  786. $headers = array();
  787. $aclId = ($request->getAclId()!==null)?$request->getAclId():'';
  788. $resource = '/acls/'.$aclId;
  789. list($resp,$header) = $this->send("DELETE",NULL,NULL,$resource,$params,$headers);
  790. return new Aliyun_Log_Models_DeleteACLResponse($header);
  791. }
  792. public function listACLs(Aliyun_Log_Models_ListACLsRequest $request){
  793. $params = array();
  794. $headers = array();
  795. if($request->getPrincipleId()!==null)$params['principleId'] = $request->getPrincipleId();
  796. if($request->getOffset()!==null)$params['offset'] = $request->getOffset();
  797. if($request->getSize()!==null)$params['size'] = $request->getSize();
  798. $resource = '/acls';
  799. list($resp,$header) = $this->send("GET",NULL,NULL,$resource,$params,$headers);
  800. $requestId = isset ( $header ['x-log-requestid'] ) ? $header ['x-log-requestid'] : '';
  801. $resp = $this->parseToJson ( $resp, $requestId );
  802. return new Aliyun_Log_Models_ListACLsResponse($resp,$header);
  803. }
  804. }