文档首页> 应用运维管理 AOM> API参考> API> 监控> 查询系统中已有服务发现规则
更新时间:2024-02-01 GMT+08:00

查询系统中已有服务发现规则

功能介绍

该接口用于查询系统当前已存在的服务发现规则。(当前接口未上线墨西哥城二、圣地亚哥、圣保罗一)

调用方法

请参见如何调用API

URI

GET /v1/{project_id}/inv/servicediscoveryrules

表1 路径参数

参数

是否必选

参数类型

描述

project_id

String

租户从IAM申请到的projectid,一般为32位字符串。

表2 Query参数

参数

是否必选

参数类型

描述

id

String

具体的服务发现规则ID,可以精确匹配到一条服务发现规则。不传时返回project下所有服务发现规则的列表。

请求参数

表3 请求Header参数

参数

是否必选

参数类型

描述

X-Auth-Token

String

从IAM服务获取的用户Token。

Content-Type

String

指定类型为application/json。

枚举值:

  • application/json

响应参数

状态码: 200

表4 响应Body参数

参数

参数类型

描述

appRules

Array of AppRules objects

查询结果规则信息。

errorCode

String

响应码,AOM_INVENTORY_2000000代表正常返回。

errorMessage

String

响应信息描述。

id

Array of strings

服务发现规则id列表,多AZ配置同步时使用。

表5 AppRules

参数

参数类型

描述

createTime

String

规则创建时间(创建时不传,修改时传查询返回的createTime)。

缺省值:1599098476654

enable

Boolean

true、false 规则是否启用。

eventName

String

aom_inventory_rules_event规则事件名称,对于服务发现固定为"aom_inventory_rules_event"。

hostid

Array of strings

主机ID(暂不使用,传空即可)。

id

String

创建时填空,修改时填规则ID。

name

String

规则名称。 字符长度为4到63位,以小写字母a-z开头,只能包含0-9/a-z/-,不能以-结尾。

projectid

String

租户从IAM申请到的projectid,一般为32位字符串。

spec

AppRulesSpec object

规则详情。

desc

String

自定义描述信息

表6 AppRulesSpec

参数

参数类型

描述

appType

String

服务类型,用于标记服务的分类,仅用于规则分类和界面展示。可以填写任意字段,如按技术栈分类可填写Java,Python。按作用分类可填写collector(采集),database(数据库)等。

attrList

Array of strings

属性列表(暂不使用,可不传)。 cmdLine、env

detectLog

String

是否开启日志采集。 true、false

discoveryRule

Array of DiscoveryRule objects

规则发现部分,数组中有多个对象时表示需要同时满足所有条件的进程才会被匹配到。checkType为cmdLine时checkMode填contain,checkContent格式为["xxx"]表示进程命令行参数中需要包含xxx。checkType为env时checkMode填contain,checkContent格式为 ["k1","v1"]表示进程环境变量中需要包含名为k1值为v1的环境变量。checkType为scope时checkMode填equals,checkContent格式为节点ID数组["hostId1","hostId2"],表示规则仅会在这些节点上生效(如果不指定节点范围,规则将下发到该项目所有的节点)。

isDefaultRule

String

是否为默认规则。 true、false

isDetect

String

是否为规则预探测场景(预探测场景不会保存规则,仅用于规则下发之前验证该规则能否有效发现节点上的进程)。true、false

logFileFix

Array of strings

日志文件的后缀。 log、trace、out

logPathRule

Array of LogPathRule objects

日志路径配置规则。当cmdLineHash为固定字符串时,指定日志路径或者日志文件。否则只采集进程当前打开的以.log和.trace结尾的文件。nameType取值cmdLineHash时,args格式为["00001"],value格式为["/xxx/xx.log"],表示当启动命令是00001时,日志路径为/xxx/xx.log。

nameRule

NameRule object

发现上来的服务、应用的命名规则。

priority

Integer

规则优先级。1~9999的整数字符串,默认取值为9999

dataSource

String

数据源

editable

String

是否支持编辑 true、false

aom_metric_relabel_configs

Object

指标配置

表7 DiscoveryRule

参数

参数类型

描述

checkContent

Array of strings

匹配值。

checkMode

String

匹配条件。contain、equals

checkType

String

匹配类型。cmdLine、env、scope

表8 LogPathRule

参数

参数类型

描述

args

Array of strings

命令行。

nameType

String

取值类型。 cmdLineHash

value

Array of strings

日志路径。

表9 NameRule

参数

参数类型

描述

appNameRule

Array of AppNameRule objects

服务命名部分,数组中有多个对象时表示将每个对象抽取到的字符串拼接作为服务的名称。nameType取值cmdLine时args格式为["start","end"],表示抽取命令行中start、end之间的字符。nameType取值cmdLine时args格式为["aa"],表示抽取环境变量名为aa对应的环境变量值。nameType取值str时,args格式为["fix"],表示服务名称最后拼接固定文字fix。nameType取值cmdLineHash时,args格式为["0001"],value格式为["ser"],表示当启动命令是0001时,服务名称为ser。

applicationNameRule

Array of ApplicationNameRule objects

应用命名部分。nameType取值cmdLine时args格式为["start","end"],表示抽取命令行中start、end之间的字符。nameType取值cmdLine时args格式为 ["aa"],表示抽取环境变量名为aa对应的环境变量值。nameType取值str时,args格式为["fix"],表示服务名称最后拼接固定文字fix。nameType取值cmdLineHash时,args格式为["0001"],value格式为["ser"],表示当启动命令是0001时,应用名称为ser。

表10 AppNameRule

参数

参数类型

描述

nameType

String

取值类型。从cmdLineHash、cmdLine、env、str里面选取。

args

Array of strings

输入值。

value

Array of strings

服务名(仅nameType为cmdLineHash时填写)。

表11 ApplicationNameRule

参数

参数类型

描述

nameType

String

取值类型。 从cmdLineHash、cmdLine、env、str里面选取。

args

Array of strings

输入值。

value

Array of strings

服务名(仅nameType为cmdLineHash时填写)。

请求示例

响应示例

状态码: 200

OK 请求响应成功。

{
  "appRules" : [ {
    "createTime" : "1694705814424",
    "enable" : true,
    "name" : "ica**nt",
    "eventName" : "aom_inventory_rules_event",
    "hostid" : [ ],
    "id" : "b53a5152-****-****-****-302367e04c0b",
    "projectid" : "684fc87a79d7xxxx22e62a7da95b",
    "spec" : {
      "detectLog" : "true",
      "editable" : null,
      "logPathRule" : [ ],
      "priority" : 9999,
      "attrList" : [ "cmdLine" ],
      "nameRule" : {
        "appNameRule" : [ {
          "args" : [ "/opt/********* -DNFW=ica**nt" ],
          "nameType" : "cmdLineHash",
          "value" : [ "aicagentserver" ]
        } ],
        "applicationNameRule" : [ {
          "args" : [ "/opt/*********** -DNFW=ica**nt" ],
          "nameType" : "cmdLineHash",
          "value" : [ "aica**nt" ]
        } ]
      },
      "appType" : "",
      "aom_metric_relabel_configs" : null,
      "logFileFix" : [ "log", "trace", "out" ],
      "isDetect" : "false",
      "isDefaultRule" : null,
      "dataSource" : null,
      "discoveryRule" : [ {
        "checkType" : "cmdLine",
        "checkContent" : [ "-DNFW=ica**nt" ],
        "checkMode" : "contain"
      } ]
    },
    "desc" : "自定义描述/User-defined description"
  } ],
  "errorMessage" : null,
  "errorCode" : "SVCSTG.INV.2000000",
  "id" : [ ]
}

状态码: 404

NotFound 所请求的资源不存在。建议直接修改该请求,不要重试该请求。

{
  "appRules" : [ ],
  "errorMessage" : "Inventory does not exists",
  "errorCode" : "SVCSTG.INV.4040000",
  "id" : [ ]
}

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
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.aom.v2.region.AomRegion;
import com.huaweicloud.sdk.aom.v2.*;
import com.huaweicloud.sdk.aom.v2.model.*;


public class ListServiceDiscoveryRulesSolution {

    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");

        ICredential auth = new BasicCredentials()
                .withAk(ak)
                .withSk(sk);

        AomClient client = AomClient.newBuilder()
                .withCredential(auth)
                .withRegion(AomRegion.valueOf("<YOUR REGION>"))
                .build();
        ListServiceDiscoveryRulesRequest request = new ListServiceDiscoveryRulesRequest();
        request.withId("<id>");
        try {
            ListServiceDiscoveryRulesResponse response = client.listServiceDiscoveryRules(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
# coding: utf-8

from huaweicloudsdkcore.auth.credentials import BasicCredentials
from huaweicloudsdkaom.v2.region.aom_region import AomRegion
from huaweicloudsdkcore.exceptions import exceptions
from huaweicloudsdkaom.v2 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.getenv("CLOUD_SDK_AK")
    sk = os.getenv("CLOUD_SDK_SK")

    credentials = BasicCredentials(ak, sk) \

    client = AomClient.new_builder() \
        .with_credentials(credentials) \
        .with_region(AomRegion.value_of("<YOUR REGION>")) \
        .build()

    try:
        request = ListServiceDiscoveryRulesRequest()
        request.id = "<id>"
        response = client.list_service_discovery_rules(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
package main

import (
	"fmt"
	"github.com/huaweicloud/huaweicloud-sdk-go-v3/core/auth/basic"
    aom "github.com/huaweicloud/huaweicloud-sdk-go-v3/services/aom/v2"
	"github.com/huaweicloud/huaweicloud-sdk-go-v3/services/aom/v2/model"
    region "github.com/huaweicloud/huaweicloud-sdk-go-v3/services/aom/v2/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")

    auth := basic.NewCredentialsBuilder().
        WithAk(ak).
        WithSk(sk).
        Build()

    client := aom.NewAomClient(
        aom.AomClientBuilder().
            WithRegion(region.ValueOf("<YOUR REGION>")).
            WithCredential(auth).
            Build())

    request := &model.ListServiceDiscoveryRulesRequest{}
	idRequest:= "<id>"
	request.Id = &idRequest
	response, err := client.ListServiceDiscoveryRules(request)
	if err == nil {
        fmt.Printf("%+v\n", response)
    } else {
        fmt.Println(err)
    }
}

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

状态码

状态码

描述

200

OK 请求响应成功。

400

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

401

Unauthorized 在客户端提供认证信息后,返回该状态码,表明服务端指出客户端所提供的认证信息不正确或非法。

403

Forbidden 请求被拒绝访问。返回该状态码,表明请求能够到达服务端,且服务端能够理解用户请求,但是拒绝做更多的事情,因为该请求被设置为拒绝访问,建议直接修改该请求,不要重试该请求。

404

NotFound 所请求的资源不存在。建议直接修改该请求,不要重试该请求。

500

InternalServerError 表明服务端能被请求访问到,但是不能理解用户的请求。

503

ServiceUnavailable 被请求的服务无效。建议直接修改该请求,不要重试该请求。

错误码

请参见错误码