Help Center/ Application Operations Management/ API Reference/ APIs/ Monitoring/ Querying Existing Service Discovery Rules
Updated on 2024-06-28 GMT+08:00

Querying Existing Service Discovery Rules

Function

This API is used to query existing service discovery rules in the system.

Calling Method

For details, see Calling APIs.

URI

GET /v1/{project_id}/inv/servicediscoveryrules

Table 1 Path Parameters

Parameter

Mandatory

Type

Description

project_id

Yes

String

Project ID obtained from IAM. Generally, a project ID contains 32 characters.

Table 2 Query Parameters

Parameter

Mandatory

Type

Description

id

No

String

Service discovery rule ID, which corresponds to a service discovery rule. If this parameter is left blank, all service discovery rules in the project are returned.

Request Parameters

Table 3 Request header parameters

Parameter

Mandatory

Type

Description

X-Auth-Token

Yes

String

User token obtained from IAM.

Content-Type

Yes

String

Content type, which is application/json.

Enumeration values:

  • application/json

Response Parameters

Status code: 200

Table 4 Response body parameters

Parameter

Type

Description

appRules

Array of AppRules objects

Rule information.

errorCode

String

Response code. AOM_INVENTORY_2000000: Success response.

errorMessage

String

Response message.

id

Array of strings

Service discovery rule ID list. This parameter is used during cross-AZ configuration synchronization.

Table 5 AppRules

Parameter

Type

Description

createTime

String

Creation time. When creating a service discovery rule, leave this parameter blank. When modifying a service discovery rule, enter the returned createTime.

Default: 1599098476654

enable

Boolean

Whether a rule is enabled. Values: true and false.

eventName

String

aom_inventory_rules_event Rule event name. For service discovery, the fixed value is aom_inventory_rules_event.

hostid

Array of strings

Host ID. Currently, this parameter is not used and can be left blank.

id

String

Rule ID. When creating a service discovery rule, leave this parameter blank. When modifying a service discovery rule, enter a rule ID.

name

String

Rule name, which contains a maximum of 64 characters. It must start with a lowercase letter but cannot end with a hyphen (-). Only digits, lowercase letters, and hyphens are allowed.

projectid

String

Project ID obtained from IAM. Generally, a project ID contains 32 characters.

spec

AppRulesSpec object

Rule details.

desc

String

Custom description

Table 6 AppRulesSpec

Parameter

Type

Description

appType

String

Service type, which is used only for rule classification and UI display. You can enter any field. For example, enter Java or Python by technology stack, or enter collector or database by function.

attrList

Array of strings

Attribute list. Currently, this parameter is not used and can be left blank. Values: cmdLine and env.

detectLog

String

Whether to enable log collection. Values: true and false.

discoveryRule

Array of DiscoveryRule objects

Discovery rule. If the array contains multiple conditions, only the processes that meet all the conditions will be matched. If the value of checkType is cmdLine, set the value of checkMode to contain. checkContent is in the format of ["xxx"], indicating that the process must contain the xxx parameter. If the value of checkType is env, set the value of checkMode to contain. checkContent is in the format of ["k1","v1"], indicating that the process must contain the environment variable whose name is k1 and value is v1. If the value of checkType is scope, set the value of checkMode to equals. checkContent is in the format of ["hostId1","hostId2"], indicating that the rule takes effect only on specified nodes. If no nodes are specified, the rule applies to all nodes of the project.

isDefaultRule

String

Whether the rule is the default one. Values: true and false.

isDetect

String

Whether the scenario is a pre-check scenario. No rules will be saved in the pre-check scenario. This scenario is designed only to check whether a rule can detect node processes before it is delivered. Values: true and false.

logFileFix

Array of strings

Log file suffix. Values: log, trace, and out.

logPathRule

Array of LogPathRule objects

Log path configuration rule. If cmdLineHash is a fixed string, logs in the specified log path or log file are collected. Otherwise, only the files whose names end with .log or .trace are collected. If the value of nameType is cmdLineHash, args is in the format of ["00001"] and value is in the format of ["/xxx/xx.log"], indicating that the log path is /xxx/xx.log when the startup command is 00001.

nameRule

NameRule object

Naming rules for discovered services and applications.

priority

Integer

Rule priority. Value range: 1 to 9999. Default value: 9999.

dataSource

String

Data source.

editable

String

Whether to support editing. Options: true and false.

aom_metric_relabel_configs

Object

Metric configuration.

Table 7 DiscoveryRule

Parameter

Type

Description

checkContent

Array of strings

Matched value.

checkMode

String

Match condition. Values: contain and equals.

checkType

String

Match type. Values: cmdLine, env, and scope.

Table 8 LogPathRule

Parameter

Type

Description

args

Array of strings

Command.

nameType

String

Value type, which can be cmdLineHash.

value

Array of strings

Log path.

Table 9 NameRule

Parameter

Type

Description

appNameRule

Array of AppNameRule objects

Service name rule. If there are multiple objects in the array, the character strings extracted from these objects constitute the service name. If the value of nameType is cmdLine, args is in the format of ["start", "end"], indicating that the characters between start and end in the command are extracted.If the value of nameType is cmdLine, args is in the format of ["aa"], indicating that the environment variable named aa is extracted.If the value of nameType is str, args is in the format of ["fix"], indicating that the service name is suffixed with fix.If the value of nameType is cmdLineHash, args is in the format of ["0001"] and value is in the format of ["ser"], indicating that the service name is ser when the startup command is 0001.

applicationNameRule

Array of ApplicationNameRule objects

Application name rule.If the value of nameType is cmdLine, args is in the format of ["start", "end"], indicating that the characters between start and end in the command are extracted.If the value of nameType is cmdLine, args is in the format of ["aa"], indicating that the environment variable named aa is extracted.If the value of nameType is str, args is in the format of ["fix"], indicating that the service name is suffixed with fix.If the value of nameType is cmdLineHash, args is in the format of ["0001"] and value is in the format of ["ser"], indicating that the application name is ser when the startup command is 0001.

Table 10 AppNameRule

Parameter

Type

Description

nameType

String

Value type. Values: cmdLineHash, cmdLine, env, and str.

args

Array of strings

Input value.

value

Array of strings

Service name, which is mandatory only when the value of nameType is cmdLineHash.

Table 11 ApplicationNameRule

Parameter

Type

Description

nameType

String

Value type. Values: cmdLineHash, cmdLine, env, and str.

args

Array of strings

Input value.

value

Array of strings

Service name, which is mandatory only when the value of nameType is cmdLineHash.

Example Requests

Query the application discovery rule whose ID is -6066-****-8cc7-.

https://{endpoint}/v1/{project_id}/inv/servicediscoveryrules?id=********-6066-****-8cc7-********

Example Responses

Status code: 200

OK: The request is successful.

{
  "appRules" : [ {
    "createTime" : "1694705814424",
    "enable" : true,
    "name" : "icaaant",
    "eventName" : "aom_inventory_rules_event",
    "hostid" : [ ],
    "id" : "********-6066-****-8cc7-********",
    "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" : "Custom description"
  } ],
  "errorMessage" : null,
  "errorCode" : "SVCSTG.INV.2000000",
  "id" : [ ]
}

Status code: 404

Not Found: The requested resource could not be found. The client should not repeat this request without modification.

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

SDK Sample Code

The SDK sample code is as follows.

Java

 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());
        }
    }
}

Python

 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
# coding: utf-8

import os
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.environ["CLOUD_SDK_AK"]
    sk = os.environ["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)

Go

 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)
    }
}

More

For SDK sample code of more programming languages, see the Sample Code tab in API Explorer. SDK sample code can be automatically generated.

Status Codes

Status Code

Description

200

OK: The request is successful.

400

Bad Request: The request is invalid. The client should not repeat the request without modifications.

401

Unauthorized: The authentication information is incorrect or invalid.

403

Forbidden: The request is rejected. The server has received the request and understood it, but the server refuses to respond to it. The client should not repeat the request without modifications.

404

Not Found: The requested resource could not be found. The client should not repeat this request without modification.

500

Internal Server Error: The server is able to receive the request but unable to understand the request.

503

Service Unavailable: The requested service is invalid. The client should not repeat the request without modifications.

Error Codes

See Error Codes.