Exemplo n.º 1
0
class AliyunSLS:
    def __init__(self):
        self.client = LogClient(configs['endpoint'], configs['accessKeyId'],
                                configs['accessKey'])
        self.project = configs['logProjectName']

    def create_alert(self, detail, project=None):
        dashboard_name = detail['configuration']['dashboard']
        self.ensure_dashboard(dashboard_name, project=project)
        resp = self.client.create_alert(project or self.project, detail)
        return resp.body

    def update_alert(self, detail, project=None):
        resp = self.client.update_alert(project or self.project, detail)
        return resp.body

    def delete_alert(self, name, project=None):
        resp = self.client.delete_alert(project or self.project, name)
        return resp.body

    def get_alert(self, name, project=None):
        resp = self.client.get_alert(project or self.project, name)
        return resp.body

    def ensure_dashboard(self, name, project=None):
        try:
            self.client.get_dashboard(project or self.project, name)
        except LogException as e:
            self.client.create_dashboard(
                project or self.project, {
                    'dashboardName': name,
                    'displayName': name,
                    'charts': [],
                    'description': ''
                })
Exemplo n.º 2
0
res = client.create_project(log_project_name, '操作审计事件日志项目')
res.log_print()


# 等待一段时间,因为创建日志项目是异步的
# Wait for a while, because create a Log Project is asynchronous
time.sleep(120)

# 创建日志库
# Create Log Store
res = client.create_logstore(log_project_name, log_store_name, shard_count=3, preserve_storage=True)
res.log_print()

# 从log_index.json中读取索引配置
# Get Log Index config from json file
index_json = get_json_data('./log_config/log_index.json')
index_detail = IndexConfig()
index_detail.from_json(index_json)
# 创建索引
# Create Log Index
res = client.create_index(log_project_name, log_store_name, index_detail)
res.log_print()


#  从log_dashboard.json中读取报表配置
# Get Log Dashboard config from json file
dashboard_detail = get_json_data('./log_config/log_dashboard.json')
# 创建报表 
# Create Log Dashboard
client.create_dashboard(log_project_name, dashboard_detail)