文档首页/ 云搜索服务 CSS/ API参考/ API V1/ 日志管理/ 开启日志备份或日志采集功能
更新时间:2025-09-10 GMT+08:00
分享

开启日志备份或日志采集功能

功能介绍

该接口用于开启集群的日志备份或日志采集功能。支持独立开启日志备份功能和日志采集功能。

-日志备份:通过定期将集群日志同步至OBS桶,用户可随时下载日志文件进行深度分析。

-日志采集:实时将集群日志传输至当前集群或同网络的其他集群(需满足版本及网络兼容性要求),通过Kibana或其他工具实现日志的可视化搜索与分析。

接口约束

  • 仅当Elasticsearch集群版本号为7.10.2且镜像版本号不低于7.10.2_24.2.0_×.×.×时,才支持日志采集功能。

  • 仅当OpenSearch集群版本号为1.3.6或2.19.0,且镜像版本号不低于×.×.×_24.2.0_×.×.×时,才支持日志采集功能。

调用方法

请参见如何调用API

URI

POST /v1.0/{project_id}/clusters/{cluster_id}/logs/open

表1 路径参数

参数

是否必选

参数类型

描述

project_id

String

参数解释

项目ID。获取方法请参见获取项目ID和名称

约束限制

不涉及

取值范围

账户的项目ID。

默认取值

不涉及

cluster_id

String

参数解释

指定开启日志备份或日志采集的集群ID。获取方法请参见获取集群ID

约束限制

不涉及

取值范围

集群ID。

默认取值

不涉及

表2 Query参数

参数

是否必选

参数类型

描述

action

String

参数解释

配置集群开启日志备份或者日志采集。

约束限制

不涉及

取值范围

  • base_log_collect:开启日志备份。

  • real_time_log_collect:开启日志采集。

默认取值

base_log_collect

请求参数

表3 请求Body参数

参数

是否必选

参数类型

描述

agency

String

参数解释

委托名称,快照数据存储到OBS桶中,需要OBS的操作权限。选择IAM委托,授权当前账号访问和使用OBS的权限。

约束限制

当query参数action为base_log_collect时,需要填写该参数。

取值范围

无法配置含特殊字符和中文字符的委托,允许使用的委托名称仅可包含a-zA-Z0-9-_字符。

默认取值

不涉及

log_base_path

String

参数解释

设置日志在OBS桶中的备份路径。

说明:
服务会将日志备份到对应备份路径下的${集群名称}目录下。

约束限制

当query参数action为base_log_collect时,需要填写该参数。

取值范围

  • 备份路径不能包括下列符号 :\:*?"<>|'{}

  • 备份路径不能以“/”开头

  • 备份路径不能以“.”开头或结尾

  • 备份路径不能包含两个以上连续的斜杆(/)或点(.)

  • 备份路径的总长度不能超过512字符。

默认取值

不涉及

log_bucket

String

参数解释

设置日志备份在OBS中的桶名。

约束限制

当query参数action为base_log_collect时,需要填写该参数。

取值范围

不涉及

默认取值

不涉及

auto_enable

Boolean

参数解释

集群日志是否开启自动备份。

约束限制

当参数action为base_log_collect时,参数auto_enable有效。

取值范围

  • true:集群日志开启自动备份。

  • false:集群日志不开启自动备份。

默认取值

false

period

String

参数解释

集群日志备份开始时间。

约束限制

当参数action为base_log_collect且参数auto_enable为true时,该参数必填。

取值范围

格式:格林威治标准时间(GMT)。比如:00:00 GMT+08:00。

默认取值

不涉及

index_prefix

String

参数解释

采集日志的索引前缀名称。设置后采集的日志索引名称为“索引前缀名称+日志采集日期”,日志采集日期以天为单位。

约束限制

当query参数action为real_time_log_collect时,需要填写该参数。

取值范围

索引名称只能包含数字、小写字母、下划线、中划线,长度1-128个字符。

默认取值

不涉及

keep_days

Integer

参数解释

采集日志的保留周期。当采集的日志超过保存周期时,日志将会被删除。

约束限制

当query参数action为real_time_log_collect时,需要填写该参数。

取值范围

1-3650

默认取值

不涉及

target_cluster_id

String

参数解释

设置保存日志的目标集群ID。

约束限制

  • 当query参数action为real_time_log_collect时,需要填写该参数。

  • 目标集群可以是本集群,也可以是其他集群。选择采集到其他集群时,需要通过连通性测试接口来判断该集群是否可以作为目标集群,如果状态码返回的是200,则表示该集群可以作为保存日志的目标集群,否则,表示该集群不能作为保存日志的目标集群。

取值范围

不涉及

默认取值

不涉及

响应参数

状态码:200

请求已成功。

请求示例

  • 开启日志备份功能。

    POST https://{Endpoint}/v1.0/{project_id}/clusters/4f3deec3-efa8-4598-bf91-560aad1377a3/logs/open
    
    {
      "agency" : "css_obs_agency",
      "log_base_path" : "css_repository/logs",
      "log_Bucket" : "000-words",
      "auto_enable" : true,
      "period" : "00:00 GMT+08:00"
    }
  • 开启日志实时采集功能。

    POST https://{Endpoint}/v1.0/{project_id}/clusters/4f3deec3-efa8-4598-bf91-560aad1377a3/logs/open?action=real_time_log_collect
    
    {
      "index_prefix" : "css_log",
      "keep_days" : 30,
      "target_cluster_id" : "4f3deec3-efa8-4598-bf91-560aad1377a3"
    }

响应示例

SDK代码示例

SDK代码示例如下。

  • 开启日志备份功能。

     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
    package com.huaweicloud.sdk.test;
    
    import com.huaweicloud.sdk.core.auth.ICredential;
    import com.huaweicloud.sdk.core.auth.BasicCredentials;
    import com.huaweicloud.sdk.core.exception.ConnectionException;
    import com.huaweicloud.sdk.core.exception.RequestTimeoutException;
    import com.huaweicloud.sdk.core.exception.ServiceResponseException;
    import com.huaweicloud.sdk.css.v1.region.CssRegion;
    import com.huaweicloud.sdk.css.v1.*;
    import com.huaweicloud.sdk.css.v1.model.*;
    
    
    public class StartLogsSolution {
    
        public static void main(String[] args) {
            // The AK and SK used for authentication are hard-coded or stored in plaintext, which has great security risks. It is recommended that the AK and SK be stored in ciphertext in configuration files or environment variables and decrypted during use to ensure security.
            // In this example, AK and SK are stored in environment variables for authentication. Before running this example, set environment variables CLOUD_SDK_AK and CLOUD_SDK_SK in the local environment
            String ak = System.getenv("CLOUD_SDK_AK");
            String sk = System.getenv("CLOUD_SDK_SK");
            String projectId = "{project_id}";
    
            ICredential auth = new BasicCredentials()
                    .withProjectId(projectId)
                    .withAk(ak)
                    .withSk(sk);
    
            CssClient client = CssClient.newBuilder()
                    .withCredential(auth)
                    .withRegion(CssRegion.valueOf("<YOUR REGION>"))
                    .build();
            StartLogsRequest request = new StartLogsRequest();
            request.withClusterId("{cluster_id}");
            StartLogsReq body = new StartLogsReq();
            body.withPeriod("00:00 GMT+08:00");
            body.withAutoEnable(true);
            body.withLogBasePath("css_repository/logs");
            body.withAgency("css_obs_agency");
            request.withBody(body);
            try {
                StartLogsResponse response = client.startLogs(request);
                System.out.println(response.toString());
            } catch (ConnectionException e) {
                e.printStackTrace();
            } catch (RequestTimeoutException e) {
                e.printStackTrace();
            } catch (ServiceResponseException e) {
                e.printStackTrace();
                System.out.println(e.getHttpStatusCode());
                System.out.println(e.getRequestId());
                System.out.println(e.getErrorCode());
                System.out.println(e.getErrorMsg());
            }
        }
    }
    
  • 开启日志实时采集功能。

     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
    package com.huaweicloud.sdk.test;
    
    import com.huaweicloud.sdk.core.auth.ICredential;
    import com.huaweicloud.sdk.core.auth.BasicCredentials;
    import com.huaweicloud.sdk.core.exception.ConnectionException;
    import com.huaweicloud.sdk.core.exception.RequestTimeoutException;
    import com.huaweicloud.sdk.core.exception.ServiceResponseException;
    import com.huaweicloud.sdk.css.v1.region.CssRegion;
    import com.huaweicloud.sdk.css.v1.*;
    import com.huaweicloud.sdk.css.v1.model.*;
    
    
    public class StartLogsSolution {
    
        public static void main(String[] args) {
            // The AK and SK used for authentication are hard-coded or stored in plaintext, which has great security risks. It is recommended that the AK and SK be stored in ciphertext in configuration files or environment variables and decrypted during use to ensure security.
            // In this example, AK and SK are stored in environment variables for authentication. Before running this example, set environment variables CLOUD_SDK_AK and CLOUD_SDK_SK in the local environment
            String ak = System.getenv("CLOUD_SDK_AK");
            String sk = System.getenv("CLOUD_SDK_SK");
            String projectId = "{project_id}";
    
            ICredential auth = new BasicCredentials()
                    .withProjectId(projectId)
                    .withAk(ak)
                    .withSk(sk);
    
            CssClient client = CssClient.newBuilder()
                    .withCredential(auth)
                    .withRegion(CssRegion.valueOf("<YOUR REGION>"))
                    .build();
            StartLogsRequest request = new StartLogsRequest();
            request.withClusterId("{cluster_id}");
            StartLogsReq body = new StartLogsReq();
            body.withTargetClusterId("4f3deec3-efa8-4598-bf91-560aad1377a3");
            body.withKeepDays(30);
            body.withIndexPrefix("css_log");
            request.withBody(body);
            try {
                StartLogsResponse response = client.startLogs(request);
                System.out.println(response.toString());
            } catch (ConnectionException e) {
                e.printStackTrace();
            } catch (RequestTimeoutException e) {
                e.printStackTrace();
            } catch (ServiceResponseException e) {
                e.printStackTrace();
                System.out.println(e.getHttpStatusCode());
                System.out.println(e.getRequestId());
                System.out.println(e.getErrorCode());
                System.out.println(e.getErrorMsg());
            }
        }
    }
    
  • 开启日志备份功能。

     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
    # coding: utf-8
    
    import os
    from huaweicloudsdkcore.auth.credentials import BasicCredentials
    from huaweicloudsdkcss.v1.region.css_region import CssRegion
    from huaweicloudsdkcore.exceptions import exceptions
    from huaweicloudsdkcss.v1 import *
    
    if __name__ == "__main__":
        # The AK and SK used for authentication are hard-coded or stored in plaintext, which has great security risks. It is recommended that the AK and SK be stored in ciphertext in configuration files or environment variables and decrypted during use to ensure security.
        # In this example, AK and SK are stored in environment variables for authentication. Before running this example, set environment variables CLOUD_SDK_AK and CLOUD_SDK_SK in the local environment
        ak = os.environ["CLOUD_SDK_AK"]
        sk = os.environ["CLOUD_SDK_SK"]
        projectId = "{project_id}"
    
        credentials = BasicCredentials(ak, sk, projectId)
    
        client = CssClient.new_builder() \
            .with_credentials(credentials) \
            .with_region(CssRegion.value_of("<YOUR REGION>")) \
            .build()
    
        try:
            request = StartLogsRequest()
            request.cluster_id = "{cluster_id}"
            request.body = StartLogsReq(
                period="00:00 GMT+08:00",
                auto_enable=True,
                log_base_path="css_repository/logs",
                agency="css_obs_agency"
            )
            response = client.start_logs(request)
            print(response)
        except exceptions.ClientRequestException as e:
            print(e.status_code)
            print(e.request_id)
            print(e.error_code)
            print(e.error_msg)
    
  • 开启日志实时采集功能。

     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
    # coding: utf-8
    
    import os
    from huaweicloudsdkcore.auth.credentials import BasicCredentials
    from huaweicloudsdkcss.v1.region.css_region import CssRegion
    from huaweicloudsdkcore.exceptions import exceptions
    from huaweicloudsdkcss.v1 import *
    
    if __name__ == "__main__":
        # The AK and SK used for authentication are hard-coded or stored in plaintext, which has great security risks. It is recommended that the AK and SK be stored in ciphertext in configuration files or environment variables and decrypted during use to ensure security.
        # In this example, AK and SK are stored in environment variables for authentication. Before running this example, set environment variables CLOUD_SDK_AK and CLOUD_SDK_SK in the local environment
        ak = os.environ["CLOUD_SDK_AK"]
        sk = os.environ["CLOUD_SDK_SK"]
        projectId = "{project_id}"
    
        credentials = BasicCredentials(ak, sk, projectId)
    
        client = CssClient.new_builder() \
            .with_credentials(credentials) \
            .with_region(CssRegion.value_of("<YOUR REGION>")) \
            .build()
    
        try:
            request = StartLogsRequest()
            request.cluster_id = "{cluster_id}"
            request.body = StartLogsReq(
                target_cluster_id="4f3deec3-efa8-4598-bf91-560aad1377a3",
                keep_days=30,
                index_prefix="css_log"
            )
            response = client.start_logs(request)
            print(response)
        except exceptions.ClientRequestException as e:
            print(e.status_code)
            print(e.request_id)
            print(e.error_code)
            print(e.error_msg)
    
  • 开启日志备份功能。

     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
    package main
    
    import (
    	"fmt"
    	"github.com/huaweicloud/huaweicloud-sdk-go-v3/core/auth/basic"
        css "github.com/huaweicloud/huaweicloud-sdk-go-v3/services/css/v1"
    	"github.com/huaweicloud/huaweicloud-sdk-go-v3/services/css/v1/model"
        region "github.com/huaweicloud/huaweicloud-sdk-go-v3/services/css/v1/region"
    )
    
    func main() {
        // The AK and SK used for authentication are hard-coded or stored in plaintext, which has great security risks. It is recommended that the AK and SK be stored in ciphertext in configuration files or environment variables and decrypted during use to ensure security.
        // In this example, AK and SK are stored in environment variables for authentication. Before running this example, set environment variables CLOUD_SDK_AK and CLOUD_SDK_SK in the local environment
        ak := os.Getenv("CLOUD_SDK_AK")
        sk := os.Getenv("CLOUD_SDK_SK")
        projectId := "{project_id}"
    
        auth := basic.NewCredentialsBuilder().
            WithAk(ak).
            WithSk(sk).
            WithProjectId(projectId).
            Build()
    
        client := css.NewCssClient(
            css.CssClientBuilder().
                WithRegion(region.ValueOf("<YOUR REGION>")).
                WithCredential(auth).
                Build())
    
        request := &model.StartLogsRequest{}
    	request.ClusterId = "{cluster_id}"
    	periodStartLogsReq:= "00:00 GMT+08:00"
    	autoEnableStartLogsReq:= true
    	request.Body = &model.StartLogsReq{
    		Period: &periodStartLogsReq,
    		AutoEnable: &autoEnableStartLogsReq,
    		LogBasePath: "css_repository/logs",
    		Agency: "css_obs_agency",
    	}
    	response, err := client.StartLogs(request)
    	if err == nil {
            fmt.Printf("%+v\n", response)
        } else {
            fmt.Println(err)
        }
    }
    
  • 开启日志实时采集功能。

     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
    package main
    
    import (
    	"fmt"
    	"github.com/huaweicloud/huaweicloud-sdk-go-v3/core/auth/basic"
        css "github.com/huaweicloud/huaweicloud-sdk-go-v3/services/css/v1"
    	"github.com/huaweicloud/huaweicloud-sdk-go-v3/services/css/v1/model"
        region "github.com/huaweicloud/huaweicloud-sdk-go-v3/services/css/v1/region"
    )
    
    func main() {
        // The AK and SK used for authentication are hard-coded or stored in plaintext, which has great security risks. It is recommended that the AK and SK be stored in ciphertext in configuration files or environment variables and decrypted during use to ensure security.
        // In this example, AK and SK are stored in environment variables for authentication. Before running this example, set environment variables CLOUD_SDK_AK and CLOUD_SDK_SK in the local environment
        ak := os.Getenv("CLOUD_SDK_AK")
        sk := os.Getenv("CLOUD_SDK_SK")
        projectId := "{project_id}"
    
        auth := basic.NewCredentialsBuilder().
            WithAk(ak).
            WithSk(sk).
            WithProjectId(projectId).
            Build()
    
        client := css.NewCssClient(
            css.CssClientBuilder().
                WithRegion(region.ValueOf("<YOUR REGION>")).
                WithCredential(auth).
                Build())
    
        request := &model.StartLogsRequest{}
    	request.ClusterId = "{cluster_id}"
    	targetClusterIdStartLogsReq:= "4f3deec3-efa8-4598-bf91-560aad1377a3"
    	keepDaysStartLogsReq:= int32(30)
    	indexPrefixStartLogsReq:= "css_log"
    	request.Body = &model.StartLogsReq{
    		TargetClusterId: &targetClusterIdStartLogsReq,
    		KeepDays: &keepDaysStartLogsReq,
    		IndexPrefix: &indexPrefixStartLogsReq,
    	}
    	response, err := client.StartLogs(request)
    	if err == nil {
            fmt.Printf("%+v\n", response)
        } else {
            fmt.Println(err)
        }
    }
    

更多编程语言的SDK代码示例,请参见API Explorer的代码示例页签,可生成自动对应的SDK代码示例。

状态码

状态码

描述

200

请求已成功。

400

非法请求。

建议直接修改该请求,不要重试该请求。

409

服务器在完成请求时发生冲突。

返回该状态码,表明客户端尝试创建的资源已经存在,或者由于冲突请求的更新操作不能被完成。

412

未满足前提条件,服务器未满足请求者在请求中设置的其中一个前提条件。

错误码

请参见错误码

相关文档