aliyun-log-sdk python PS ruby

梁丘波
2023-12-01

Python

文档 https://www.alibabacloud.com/help/zh/doc-detail/407684.htm

pip install -U aliyun-log-python-sdk

import time
from aliyun.log import *
endpoint = "cn-beijing-intranet.log.aliyuncs.com"
access_key_id = 'xxxx'
access_key = 'xxxx'
project='k8s-log-xxxx'
logstore='xxxx-api-production'
client = LogClient(endpoint, access_key_id, access_key)
query = "logFrom and lograge | select regexp_extract(content, '(\d+\.\d+\.\d+\.\d+)',1) as ips from log"
query="logFrom and lograge | select count(1) as PV"
from_time = int(time.time()) - 3600
to_time = int(time.time()) + 3600
request = GetLogsRequest(project, logstore, from_time, to_time, to_time, query=query, line=3, offset=0, reverse=False)
response = client.get_logs(request)
for log in response.get_logs():
  print(log.contents.items())

success

dict_items([('ips', '223.104.3.166')])
dict_items([('ips', '223.104.3.166')])
dict_items([('ips', '223.101.71.181')])
dict_items([('ips', '175.171.128.145')])
dict_items([('ips', '223.104.63.196')])
dict_items([('ips', '175.171.128.145')])
dict_items([('ips', '114.86.10.82')])
.........

Ruby

文档 https://www.rubydoc.info/gems/aliyun-log

gem install aliyun-log

endpoint = "https://cn-beijing-intranet.log.aliyuncs.com"
access_key_id = 'xxxx'
access_key = 'xxxx'
aliyun_log_client = Aliyun::Log::Client.new(endpoint: endpoint, access_key_id: access_key_id, access_key_secret: access_key)
project='k8s-log-xxxx'
logstore='xxxx-api-production'
current_project = aliyun_log_client.get_project(project)
current_logstore = current_project.get_logstore(logstore)
query= "logFrom and lograge | select regexp_extract(content, '(abc)',1) as ips from log"
query= "logFrom and lograge | select regexp_extract(content, '(\d+\.\d+\.\d+\.\d+)',1) as ips from log"
query= "logFrom and lograge | select regexp_extract(content, '(\\d+\\.\\d+\\.\\d+\\.\\d+)',1) as ips from log"
response = @current_logstore.get_logs(from: Time.now.beginning_of_day.to_i, to: Time.now.to_i,query: query)

not error

query= "logFrom and lograge | select regexp_extract(content, '(abc)',1) as ips from log"

error

E, [2022-02-23T14:28:12.056044 #394] ERROR -- : error_code: SignatureNotMatch message: signature y1jpypGaweG4qQEgLaJE9yjJVds= not match RequestId:  401 {"errorCode":"SignatureNotMatch","errorMessage":"signature y1jpypGaweG4qQEgLaJE9yjJVds= not match"}
Traceback (most recent call last):
        1: from (irb):256
Aliyun::Log::ServerError (error_code: SignatureNotMatch message: signature y1jpypGaweG4qQEgLaJE9yjJVds= not match RequestId: )
 类似资料: