クエリ文の結果を頻繁に表示する必要がある場合は、クエリ文を保存済み検索として保存できます。 このトピックでは、Simple Log Service SDK for Pythonを使用して保存済み検索を作成、変更、クエリ、および削除する方法とサンプルコードについて説明します。
前提条件
RAM (Resource Access Management) ユーザーが作成され、必要な権限がRAMユーザーに付与されます。 詳細については、「RAMユーザーの作成とRAMユーザーへの権限付与」をご参照ください。
ALIBABA_CLOUD_ACCESS_KEY_IDおよびALIBABA_CLOUD_ACCESS_KEY_SECRET環境変数が設定されています。 詳細については、「環境変数の設定」をご参照ください。
重要Alibaba CloudアカウントのAccessKeyペアには、すべてのAPI操作に対する権限があります。 RAMユーザーのAccessKeyペアを使用して、API操作を呼び出したり、ルーチンのO&Mを実行したりすることを推奨します。
プロジェクトコードにAccessKey IDまたはAccessKey secretを保存しないことを推奨します。 そうしないと、AccessKeyペアが漏洩し、アカウント内のすべてのリソースのセキュリティが侵害される可能性があります。
Python用のSimple Log Service SDKがインストールされています。 詳細については、「Simple Log Service SDK For Pythonのインストール」をご参照ください。
ログはLogstoreに書き込まれます。 詳細については、「データ収集の概要」をご参照ください。
使用上の注意
この例では、中国 (杭州) リージョンのパブリックSimple Log Serviceエンドポイントが使用されています。これは https://cn-hangzhou.log.aliyuncs.com です。 プロジェクトと同じリージョンにある他のAlibaba Cloudサービスを使用してSimple Log Serviceにアクセスする場合は、内部のSimple Log Serviceエンドポイント ( https://cn-hangzhou-intranet.log.aliyuncs.com ) を使用できます。 Simple Log Serviceのサポートされているリージョンとエンドポイントの詳細については、「エンドポイント」をご参照ください。
生ログ
body_bytes_sent:1750
host:www.example.com
http_referer:www.example.com
http_user_agent:Mozilla/5.0 (Macintosh; U; Intel Mac OS X 10_6_6; it-it) AppleWebKit/533.20.25 (KHTML, like Gecko) Version/5.0.4 Safari/533.20.27
http_x_forwarded_for:203.0.XX.XX
remote_addr:203.0.XX.XX
remote_user:p288
request_length:13741
request_method:GET
request_time:71
request_uri:/request/path-1/file-1
http_code:200
time_local:11/Aug/2021:06:52:27
upstream_response_time:0.66保存済み検索の作成に使用されるサンプルコード
次のサンプルコードは、ali-test-savedsearchという名前の保存済み検索を作成する方法の例を示しています。
aliyun.log import LogClientからの
from aliyun.log import LogClient
import os
# Configure environment variables. In this example, the AccessKey ID and AccessKey secret are obtained from environment variables.
accessKeyId = os.environ.get('ALIBABA_CLOUD_ACCESS_KEY_ID', '')
accessKey = os.environ.get('ALIBABA_CLOUD_ACCESS_KEY_SECRET', '')
# The Simple Log Service endpoint. In this example, the Simple Log Service endpoint for the China (Hangzhou) region is used. Replace the parameter value with the actual endpoint.
endpoint = "cn-hangzhou.log.aliyuncs.com"
# Create a Simple Log Service client.
client = LogClient(endpoint, accessKeyId, accessKey)
# The name of the project.
project_name = "ali-test-project"
# The name of the Logstore.
logstore_name = "ali-test-logstore"
# The name of the saved search.
savedsearch_name = "ali-test-savedsearch"
# Calculate the number of logs that contain failed GET requests or failed POST requests.
query = "(request_method:GET or request_method:POST) not status in [200 299]|select COUNT(*) as pv,status group by status"
if __name__ == '__main__':
print("ready to create savedsearch")
# The JSON-formatted configurations of the saved search. The configurations include the name, query and analysis statement, and Logstore of the saved search.
my_json_query_str = {
"savedsearchName": savedsearch_name,
"searchQuery": query,
"logstore": logstore_name,
"topic": "",
"displayName": ""
}
# Call the API operation to create the saved search.
client.create_savedsearch(project_name, detail=my_json_query_str)
print("create savedsearch success") 期待される結果
savedsearchを作成する準備ができました
savedsearchの成功を作成する
保存された検索を変更するために使用されるサンプルコード
次のサンプルコードは、ali-test-savedsearchのセーブ検索を変更する方法の例を示しています。
from aliyun.log import LogClient
import os
# Configure environment variables. In this example, the AccessKey ID and AccessKey secret are obtained from environment variables.
accessKeyId = os.environ.get('ALIBABA_CLOUD_ACCESS_KEY_ID', '')
accessKey = os.environ.get('ALIBABA_CLOUD_ACCESS_KEY_SECRET', '')
# The Simple Log Service endpoint. In this example, the Simple Log Service endpoint for the China (Hangzhou) region is used. Replace the parameter value with the actual endpoint.
endpoint = "cn-hangzhou.log.aliyuncs.com"
# Create a Simple Log Service client.
client = LogClient(endpoint, accessKeyId, accessKey)
# The name of the project.
project_name = "ali-test-project"
# The name of the Logstore.
logstore_name = "ali-test-logstore"
# The name of the saved search.
savedsearch_name = "ali-test-savedsearch"
# Query the logs that contain failed GET requests or failed POST requests.
query = "(request_method:GET or request_method:POST) not status in [200 299]"
if __name__ == '__main__':
print("ready to update savedsearch")
print("Before update, the savedsearch is:")
res = client.get_savedsearch(project_name, savedsearch_name)
print(res.get_body())
# The JSON-formatted configurations of the saved search. In this example, the logs that contain failed GET requests or failed POST requests are queried after the configurations of the saved search are modified.
my_json_query_str = {
"savedsearchName": savedsearch_name,
"searchQuery": query,
"logstore": logstore_name,
"topic": "",
"displayName": ""
}
# Call the API operation to modify the saved search.
client.update_savedsearch(project_name, detail=my_json_query_str)
print("After update, the savedsearch is:")
res = client.get_savedsearch(project_name, savedsearch_name)
print(res.get_body())
print("update savedsearch success")期待される結果
ready to update savedsearch
Before update, the savedsearch is:
{'displayName': '', 'logstore': 'ali-test-logstore', 'savedsearchName': 'ali-test-savedsearch', 'searchQuery': "*|select date_format(__time__-__time__%60, '%H:%i:%s') as time, COUNT(*) as pv group by time", 'topic': ''}
After update, the savedsearch is:
{'displayName': '', 'logstore': 'ali-test-logstore', 'savedsearchName': 'ali-test-savedsearch', 'searchQuery': '(request_method:GET or request_method:POST) not status in [200 299]', 'topic': ''}
update savedsearch success保存されたすべての検索のクエリに使用されるサンプルコード
次のサンプルコードは、指定したプロジェクト内のすべての保存済み検索をクエリする方法の例を示しています。
from aliyun.log import LogClient
import os
# Configure environment variables. In this example, the AccessKey ID and AccessKey secret are obtained from environment variables.
accessKeyId = os.environ.get('ALIBABA_CLOUD_ACCESS_KEY_ID', '')
accessKey = os.environ.get('ALIBABA_CLOUD_ACCESS_KEY_SECRET', '')
# The Simple Log Service endpoint. In this example, the Simple Log Service endpoint for the China (Hangzhou) region is used. Replace the parameter value with the actual endpoint.
endpoint = "cn-hangzhou.log.aliyuncs.com"
# Create a Simple Log Service client.
client = LogClient(endpoint, accessKeyId, accessKey)
# The name of the project.
project_name = "ali-test-project"
if __name__ == '__main__':
print("ready to list savedsearch")
# Query all saved searches.
res = client.list_savedsearch(project_name, 0 ,100)
print(res.get_entities())
print("list savedsearch success")期待される結果
ready to list savedsearch
['ali-test-savedsearch', 'testali-test-savedsearch2', 'ali-test-savedsearch2']
update savedsearch success保存済み検索のクエリに使用されるサンプルコード
次のサンプルコードは、指定した保存済み検索に関する情報を照会する方法の例を示しています。
from aliyun.log import LogClient
import os
# Configure environment variables. In this example, the AccessKey ID and AccessKey secret are obtained from environment variables.
accessKeyId = os.environ.get('ALIBABA_CLOUD_ACCESS_KEY_ID', '')
accessKey = os.environ.get('ALIBABA_CLOUD_ACCESS_KEY_SECRET', '')
# The Simple Log Service endpoint. In this example, the Simple Log Service endpoint for the China (Hangzhou) region is used. Replace the parameter value with the actual endpoint.
endpoint = "cn-hangzhou.log.aliyuncs.com"
# Create a Simple Log Service client.
client = LogClient(endpoint, accessKeyId, accessKey)
# The name of the project.
project_name = "ali-test-project"
# The name of the saved search.
savedsearch_name = "ali-test-savedsearch"
if __name__ == '__main__':
print("ready to get savedsearch")
# Query the information about the saved search.
res = client.get_savedsearch(project_name, savedsearch_name)
print(res.get_body())
print("get savedsearch success")期待される結果
ready to get savedsearch
{'displayName': 'ali-test-savedsearch', 'logstore': 'ali-test-logstore', 'savedsearchName': 'ali-test-savedsearch', 'searchQuery': '(request_method:GET or request_method:POST) not status in [200 299]', 'topic': ''}
get savedsearch success保存された検索を削除するために使用されるサンプルコード
次のサンプルコードは、指定したプロジェクトで保存した検索を削除する方法の例を示しています。
from aliyun.log import LogClient
import os
# Configure environment variables. In this example, the AccessKey ID and AccessKey secret are obtained from environment variables.
accessKeyId = os.environ.get('ALIBABA_CLOUD_ACCESS_KEY_ID', '')
accessKey = os.environ.get('ALIBABA_CLOUD_ACCESS_KEY_SECRET', '')
# The Simple Log Service endpoint. In this example, the Simple Log Service endpoint for the China (Hangzhou) region is used. Replace the parameter value with the actual endpoint.
endpoint = "cn-hangzhou.log.aliyuncs.com"
# Create a Simple Log Service client.
client = LogClient(endpoint, accessKeyId, accessKey)
# The name of the project.
project_name = "ali-test-project"
# The name of the saved search.
savedsearch_name = "ali-test-savedsearch"
if __name__ == '__main__':
print("ready to delete savedsearch")
print("Before delete, the savedsearch is:")
res = client.list_savedsearch(project_name, 0, 100)
print(res.get_entities())
print("After delete, the savedsearch is:")
res = client.list_savedsearch(project_name, 0, 100)
print(res.get_entities())
# Call the API operation to delete the saved search.
client.delete_savedsearch(project_name, savedsearch_name)
print("delete savedsearch success")期待される結果
ready to delete savedsearch
Before delete, the savedsearch is:
['ali-test-savedsearch', 'testali-test-savedsearch2', 'ali-test-savedsearch2']
After delete, the savedsearch is:
['testali-test-savedsearch2', 'ali-test-savedsearch2']
delete savedsearch success関連ドキュメント
Alibaba Cloud OpenAPI Explorerは、デバッグ機能、SDK、サンプル、および関連ドキュメントを提供します。 OpenAPI Explorerを使用して、リクエストを手動でカプセル化したり署名したりすることなく、Log Service API操作をデバッグできます。 詳細については、をご覧ください。 OpenAPIポータル。
Log Serviceは、Log Serviceの自動設定の要件を満たすコマンドラインインターフェイス (CLI) を提供します。 詳細については、「Log Service CLI」をご参照ください。
保存された検索関連のAPI操作の詳細については、以下のトピックを参照してください。
サンプルコードの詳細については、GitHubの「Alibaba Cloud Simple Log Service SDK For Python」をご参照ください。