sample.php
8.42 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
<?php
/**
* Copyright (C) Alibaba Cloud Computing
* All rights reserved
*/
require_once realpath(dirname(__FILE__) . '/../Log_Autoload.php');
function putLogs(Aliyun_Log_Client $client, $project, $logstore) {
$topic = 'TestTopic';
$contents = array( // key-value pair
'TestKey'=>'TestContent'
);
$logItem = new Aliyun_Log_Models_LogItem();
$logItem->setTime(time());
$logItem->setContents($contents);
$logitems = array($logItem);
$request = new Aliyun_Log_Models_PutLogsRequest($project, $logstore,
$topic, null, $logitems);
try {
$response = $client->putLogs($request);
logVarDump($response);
} catch (Aliyun_Log_Exception $ex) {
logVarDump($ex);
} catch (Exception $ex) {
logVarDump($ex);
}
}
function listLogstores(Aliyun_Log_Client $client, $project) {
try{
$request = new Aliyun_Log_Models_ListLogstoresRequest($project);
$response = $client->listLogstores($request);
logVarDump($response);
} catch (Aliyun_Log_Exception $ex) {
logVarDump($ex);
} catch (Exception $ex) {
logVarDump($ex);
}
}
function listTopics(Aliyun_Log_Client $client, $project, $logstore) {
$request = new Aliyun_Log_Models_ListTopicsRequest($project, $logstore);
try {
$response = $client->listTopics($request);
logVarDump($response);
} catch (Aliyun_Log_Exception $ex) {
logVarDump($ex);
} catch (Exception $ex) {
logVarDump($ex);
}
}
function getLogs(Aliyun_Log_Client $client, $project, $logstore) {
$topic = 'TestTopic';
$from = time()-3600;
$to = time();
$request = new Aliyun_Log_Models_GetLogsRequest($project, $logstore, $from, $to, $topic, '', 100, 0, False);
try {
$response = $client->getLogs($request);
foreach($response -> getLogs() as $log)
{
print $log -> getTime()."\t";
foreach($log -> getContents() as $key => $value){
print $key.":".$value."\t";
}
print "\n";
}
} catch (Aliyun_Log_Exception $ex) {
logVarDump($ex);
} catch (Exception $ex) {
logVarDump($ex);
}
}
function getHistograms(Aliyun_Log_Client $client, $project, $logstore) {
$topic = 'TestTopic';
$from = time()-3600;
$to = time();
$request = new Aliyun_Log_Models_GetHistogramsRequest($project, $logstore, $from, $to, $topic, '');
try {
$response = $client->getHistograms($request);
logVarDump($response);
} catch (Aliyun_Log_Exception $ex) {
logVarDump($ex);
} catch (Exception $ex) {
logVarDump($ex);
}
}
function listShard(Aliyun_Log_Client $client,$project,$logstore){
$request = new Aliyun_Log_Models_ListShardsRequest($project,$logstore);
try
{
$response = $client -> listShards($request);
logVarDump($response);
} catch (Aliyun_Log_Exception $ex) {
logVarDump($ex);
} catch (Exception $ex) {
logVarDump($ex);
}
}
function batchGetLogs(Aliyun_Log_Client $client,$project,$logstore)
{
$listShardRequest = new Aliyun_Log_Models_ListShardsRequest($project,$logstore);
$listShardResponse = $client -> listShards($listShardRequest);
foreach($listShardResponse-> getShardIds() as $shardId)
{
$getCursorRequest = new Aliyun_Log_Models_GetCursorRequest($project,$logstore,$shardId,null, time() - 60);
$response = $client -> getCursor($getCursorRequest);
$cursor = $response-> getCursor();
$count = 100;
while(true)
{
$batchGetDataRequest = new Aliyun_Log_Models_BatchGetLogsRequest($project,$logstore,$shardId,$count,$cursor);
logVarDump($batchGetDataRequest);
$response = $client -> batchGetLogs($batchGetDataRequest);
if($cursor == $response -> getNextCursor())
{
break;
}
$logGroupList = $response -> getLogGroupList();
foreach($logGroupList as $logGroup)
{
print ($logGroup->getCategory());
foreach($logGroup -> getLogsArray() as $log)
{
foreach($log -> getContentsArray() as $content)
{
print($content-> getKey().":".$content->getValue()."\t");
}
print("\n");
}
}
$cursor = $response -> getNextCursor();
}
}
}
function batchGetLogsWithRange(Aliyun_Log_Client $client,$project,$logstore)
{
$listShardRequest = new Aliyun_Log_Models_ListShardsRequest($project,$logstore);
$listShardResponse = $client -> listShards($listShardRequest);
foreach($listShardResponse-> getShardIds() as $shardId)
{
//pull data which reached server at time range [now - 60s, now) for every shard
$curTime = time();
$beginCursorResponse = $client->getCursor(new Aliyun_Log_Models_GetCursorRequest($project,$logstore,$shardId,null,$curTime - 60));
$beginCursor = $beginCursorResponse-> getCursor();
$endCursorResponse = $client -> getCursor(new Aliyun_Log_Models_GetCursorRequest($project,$logstore,$shardId,null,$curTime));
$endCursor = $endCursorResponse-> getCursor();
$cursor = $beginCursor;
print("-----------------------------------------\nbatchGetLogs for shard: ".$shardId.", cursor range: [".$beginCursor.", ".$endCursor.")\n");
$count = 100;
while(true)
{
$batchGetDataRequest = new Aliyun_Log_Models_BatchGetLogsRequest($project,$logstore,$shardId,$count,$cursor,$endCursor);
$response = $client -> batchGetLogs($batchGetDataRequest);
$logGroupList = $response -> getLogGroupList();
$logGroupCount = 0;
$logCount = 0;
foreach($logGroupList as $logGroup)
{
$logGroupCount += 1;
foreach($logGroup -> getLogsArray() as $log)
{
$logCount += 1;
foreach($log -> getContentsArray() as $content)
{
print($content-> getKey().":".$content->getValue()."\t");
}
print("\n");
}
}
$nextCursor = $response -> getNextCursor();
print("batchGetLogs once, cursor: ".$cursor.", nextCursor: ".nextCursor.", logGroups: ".$logGroupCount.", logs: ".$logCount."\n");
if($cursor == $nextCursor)
{
//read data finished
break;
}
$cursor = $nextCursor;
}
}
}
function mergeShard(Aliyun_Log_Client $client,$project,$logstore,$shardId)
{
$request = new Aliyun_Log_Models_MergeShardsRequest($project,$logstore,$shardId);
try
{
$response = $client -> mergeShards($request);
logVarDump($response);
}catch (Aliyun_Log_Exception $ex) {
logVarDump($ex);
} catch (Exception $ex) {
logVarDump($ex);
}
}
function splitShard(Aliyun_Log_Client $client,$project,$logstore,$shardId,$midHash)
{
$request = new Aliyun_Log_Models_SplitShardRequest($project,$logstore,$shardId,$midHash);
try
{
$response = $client -> splitShard($request);
logVarDump($response);
}catch (Aliyun_Log_Exception $ex) {
logVarDump($ex);
} catch (Exception $ex) {
logVarDump($ex);
}
}
function logVarDump($expression){
print "<br>loginfo begin = ".get_class($expression)."<br>";
var_dump($expression);
print "<br>loginfo end<br>";
}
/*
* please refer to aliyun sdk document for detail:
* http://help.aliyun-inc.com/internaldoc/detail/29074.html?spm=0.0.0.0.tqUNn5
*/
$endpoint = 'http://cn-shanghai-corp.sls.aliyuncs.com';
$accessKeyId = '';
$accessKey = '';
$project = '';
$logstore = '';
$token = "";
$client = new Aliyun_Log_Client($endpoint, $accessKeyId, $accessKey,$token);
listShard($client,$project,$logstore);
mergeShard($client,$project,$logstore,2);
deleteShard($client,$project,$logstore,2);
splitShard($client,$project,$logstore,2,"80000000000000000000000000000001");
putLogs($client, $project, $logstore);
listShard($client,$project,$logstore);
batchGetLogs($client,$project,$logstore);
batchGetLogsWithRange($client,$project,$logstore);
listLogstores($client, $project);
listTopics($client, $project, $logstore);
getHistograms($client, $project, $logstore);
getLogs($client, $project, $logstore);