IT インフラストラクチャのパフォーマンスを監視するために、Terraform プラットフォームを使用して、Cisco Cloud Observability テナントで正常性ルールを作成および管理できます。Terraform は、コードを使用して IT インフラストラクチャを作成およびプロビジョニングできるオープンソースツールです。

たとえば、Terraform を使用して、新しい Amazon Web Services(AWS)アカウントをプロビジョニングし、EC2 や Load Balancer などのサービスを追加しました。同じ Terraform プラットフォームを使用して構成ファイル(スクリプト)を作成し、これらの AWS エンティティで正常性ルールを作成および管理できます。

Terraform は、Cisco Cloud Observability Public API にリクエストを送信して、正常性ルールを作成および管理します。パブリック API へのアクセスには、OAuth 2.0 プロトコルが使用されます。OAuth プロセスの一環として、クラウドテナントのパブリック API にアクセスするために使用されるアクセストークンを生成する必要があります。OAuth API の詳細については、「AppDynamics Cloud OAuth API」を参照してください。

前提条件

  • ローカルマシンに Terraform をインストールします。

    この記事は、Terraform バージョン 1.2.9 を使用してテストされています。

  • サービスプリンシパルの作成を行い、Terraform を AppDynamics Cloud Health Rules APICisco Cloud Observability テナント)に安全に接続します。サービスプリンシパルを作成すると、Cisco Cloud Observability テナントに固有のテナント ID、クライアント ID、およびクライアントシークレットが取得されます。

Terraform を使用した正常性ルールの操作

Terraform を使用して、正常性ルールに対して次の操作を実行できます。

正常性ルールの作成

正常性ルールを作成するには、次の手順を実行します。

  1. 正常性ルールを作成するための Terraform 構成ファイル(スクリプト)を書き込むディレクトリをローカルマシンに作成します。
  2. create.tf という名前のファイルを作成し、次のコードを追加します。

    terraform {
      required_providers {
        http = {
          source = "hashicorp/http"
          version = "3.1.0"
        }
        local = {
          source = "hashicorp/local"
          version = "2.2.3"
        }
      }
    }
    
    provider "http" {
      # Configuration options
    }
    
    
    data "http" "get_auth_token" {
      provider = http
      url = "${var.appdURL}/auth/${var.tenant}/default/oauth2/token"
    
      request_headers = {
        Content-Type  =  "application/x-www-form-urlencoded"
        Authorization = "Basic ${base64encode("${var.clientId}:${var.clientSecret}")}"
      }
      request_body = "grant_type=client_credentials"
      method = "POST"
      lifecycle {
        postcondition {
          condition     = contains([200], self.status_code)
          error_message = "Status code invalid, Auth Token not generated successfully. ${self.response_body}"
        }
      }
    }
    
    
    locals {
      auth_response = jsondecode(data.http.get_auth_token.response_body)
      json_data = file("${path.module}/createHR.json")
    }
    
    
    
    data "http" "create_healthrule"{
    provider = http
      url = "${var.appdURL}/alerting/v1beta/healthRules"
      request_headers = {
        Content-Type   = "application/json"
        Authorization  ="Bearer ${local.auth_response.access_token}"
        Appd-Tenant-Id = "${var.tenant}"
        Accept         = "application/json"
      }
      method = "POST"
      request_body = "${local.json_data}"
    }
    
    output "get_response" {
      value = "Health rule create response is ${data.http.create_healthrule.response_body} with response code ${data.http.create_healthrule.status_code}"
    }
    
    resource "local_file" "json-data" {
        content  = data.http.create_healthrule.response_body
        filename = "CreateHRResponse.json"
    }
    CODE



  3. createHR.json という名前のファイルを作成し、正常性ルールのペイロードとして機能する次のコードを(例として)追加します。

    {
        "name": "Create HR Using terraform Script",
        "description": "Health Rule : Create HR Using terraform Script",
        "enabled": true,
        "scheduleName": "Always",
        "waitTimeAfterViolation": "5m",
        "evaluationObjects": "k8s:pod()",
        "criticalCriteria": {
            "criteriaExpression": "A",
            "conditions": [
                {
                    "name": "Condition 1",
                    "label": "A",
                    "conditionExpression": "metric:k8s.memory.usage.cmin(2m)>95",
                    "evaluateToTrueOnNoData": false,
                    "source": "infra-agent"
                }
            ]
        },
        "warningCriteria": {
            "criteriaExpression": "A",
            "conditions": [
                {
                    "name": "Condition 1",
                    "label": "A",
                    "conditionExpression": "metric:k8s.memory.usage.cmin(2m)>75",
                    "evaluateToTrueOnNoData": false,
                    "source": "infra-agent"
                }
            ]
        },
        "associateHealthTo": [
            {
                "objectExpr": "this"
            }
        ]
    }
    CODE



  4. variables.tf という名前のファイルを作成します。次のコードを追加し、ご利用の環境の変数のデフォルト値を入力します。

    variable "appdURL" {
      description = "Endpoint of the Cloud Native Application Observability applications"
      type        = string
      default     = "<Your FSO Tenant URL>"
    }
    
    variable "tenant" {
      description = "Tenant ID"
      type        = string
      default     = "<Your FSO Tenant ID as per your Service Principals>"
    }
    
    variable "clientId" {
      description = "Generated Client ID"
      type        = string
      default     = "<Your Client ID as per your Service Principals>"
    }
    
    variable "clientSecret" {
      description = "Generated Client Secret"
      type        = string
      default     = "<Your Client Secret Key as per your Service Principals>"
    }
    CODE



  5. 次のコマンドを実行して、作業ディレクトリを初期化します。

    terraform init
    CODE
  6. 次のコマンドを実行して、Terraform 構成ファイルで定義されているアクションを実行します。

    terraform apply
    CODE

Terraform コマンドを実行すると、正常性ルールが作成され、応答が同じ作業ディレクトリの CreateHRResponse.json ファイルに保存されます.

正常性ルールを削除する

正常性ルールを削除するには、次の手順を実行します。

  1. 正常性ルールを削除するための Terraform 構成ファイル(スクリプト)を書き込むディレクトリをローカルマシンに作成します。
  2. deleteHR.tf という名前のファイルを作成し、次のコードを追加します。

    terraform {
      required_providers {
        http-client = {
          source = "dmachard/http-client"
          version = "0.0.3"
        }
      }
    }
    
    provider "http-client" {
      # Configuration options
    }
    
    provider "http" {
      # Configuration options
    }
    
    
    data "http" "get_auth_token" {
      provider = http
      url = "${var.appdURL}/auth/${var.tenant}/default/oauth2/token"
    
      request_headers = {
        Content-Type  =  "application/x-www-form-urlencoded"
        Authorization = "Basic ${base64encode("${var.clientId}:${var.clientSecret}")}"
      }
      request_body = "grant_type=client_credentials"
      method = "POST"
      lifecycle {
        postcondition {
          condition     = contains([200], self.status_code)
          error_message = "Status code invalid, Auth Token not generated successfully. ${self.response_body}"
        }
      }
    }
    
    
    locals {
      auth_response = jsondecode(data.http.get_auth_token.response_body)
    }
    
    
    
    data "httpclient_request" "delete_healthrule"{
    provider = http-client
      url = "${var.appdURL}/alerting/v1beta/healthRules/${var.hrID}"
      request_headers = {
        Content-Type   = "application/json"
        Authorization  ="Bearer ${local.auth_response.access_token}"
        Appd-Tenant-Id = "${var.tenant}"
        Accept         = "application/json"
      }
      request_method = "DELETE"
    }
    
    output "get_response_code" {
      value = data.httpclient_request.delete_healthrule.response_code
    }
    
    output "get_response_body" {
      value = data.httpclient_request.delete_healthrule.response_body
    }
    
    
    resource "local_file" "json-data" {
        content  = data.httpclient_request.delete_healthrule.response_body
        filename = "deleteHRResponse.json"
    }
    CODE
  3. variables.tf という名前のファイルを作成します。次のコードを追加して、ご利用の環境の変数のデフォルト値を入力します。A

    variable "appdURL" {
      description = "Value of the AppD application"
      type        = string
      default     = "<Your FSO Tenant URL>"
    }
    
    variable "tenant" {
      description = "tenant ID for the AppD login"
      type        = string
      default     = "<Your FSO Tenant ID as per your Service Principals>"
    }
    
    variable "clientId" {
      description = "Service principal client id"
      type        = string
      default     = "<Your Client ID as per your Service Principals>"
    }
    
    variable "clientSecret" {
      description = "Value of Service principal client secret"
      type        = string
      default     = "<Your Client Secret Key as per your Service Principals>"
    }
    
    variable "hrID" {
      description = "HR ID that needs to be deleted"
      type        = string
      default     = "<The Health Rule ID>"
    }
    
    
    CODE
  4. 次のコマンドを実行して、作業ディレクトリを初期化します。

    terraform init
    CODE
  5. 次のコマンドを実行して、Terraform 構成ファイルで定義されているアクションを実行します。

    terraform apply
    CODE

Terraform コマンドを実行すると、指定された正常性ルールが削除され、応答が同じ作業ディレクトリの deleteHRResponse.json ファイルに保存されます.

構成されたすべての正常性ルールを取得する

構成されたすべての正常性ルールを取得するには、次の手順を実行します。

  1. 構成されたすべての正常性ルールを取得するための Terraform 構成ファイル(スクリプト)を書き込むディレクトリをローカルマシンに作成します。
  2. getAllHR.tf という名前のファイルを作成し、次のコードを追加します。

    terraform {
      required_providers {
        http = {
          source = "hashicorp/http"
          version = "3.1.0"
        }
        local = {
          source = "hashicorp/local"
          version = "2.2.3"
        }
      }
    }
    
    provider "http" {
      # Configuration options
    }
    
    
    data "http" "get_auth_token" {
      provider = http
      url = "${var.appdURL}/auth/${var.tenant}/default/oauth2/token"
    
      request_headers = {
        Content-Type: "application/x-www-form-urlencoded"
        Authorization = "Basic ${base64encode("${var.clientId}:${var.clientSecret}")}"
      }
      request_body = "grant_type=client_credentials"
      method = "POST"
    }
    
    
    locals {
      auth_reponse = jsondecode(data.http.get_auth_token.response_body)
    }
    
    
    data "http" "get_healthrule"{
    provider = http
      url = "${var.appdURL}/alerting/v1beta/healthRules/"
    
      request_headers = {
        Content-Type: "application/json"
        Authorization:"Bearer ${local.auth_reponse.access_token}"
        Appd-Tenant-Id: "${var.tenant}"
      }
      method = "GET"
    
    }
    
    output "get_response" {
      value = data.http.get_healthrule.response_body
    }
    
    resource "local_file" "json-data" {
        content  = data.http.get_healthrule.response_body
        filename = "getHR.json"
    }
    CODE
  3. variables.tf という名前のファイルを作成します。次のコードを追加して、ご利用の環境の変数のデフォルト値を入力します。A

    variable "appdURL" {
      description = "Endpoint of the Cloud Native Application Observability"
      type        = string
      default     = "<Your FSO Tenant URL>"
    }
    
    variable "tenant" {
      description = "Tenant ID"
      type        = string
      default     = "<Your FSO Tenant ID as per your Service Principals>"
    }
    
    variable "clientId" {
      description = "Generated Client ID"
      type        = string
      default     = "<Your Client ID as per your Service Principals>"
    }
    
    variable "clientSecret" {
      description = "Generated Client Secret"
      type        = string
      default     = "<Your Client Secret Key as per your Service Principals>"
    }
    CODE
  4. 次のコマンドを実行して、作業ディレクトリを初期化します。

    terraform init
    CODE
  5. 次のコマンドを実行して、Terraform 構成ファイルで定義されているアクションを実行します。

    terraform apply
    CODE

Terraform コマンドを実行すると、クラウドテナントで構成されたすべての正常性ルールの詳細が、同じ作業ディレクトリの getHR.json ファイルに保存されます。

正常性ルールを取得する

構成された正常性ルールを取得するには、次の手順を実行します。

  1. 構成された正常性ルールを取得するための Terraform 構成ファイル(スクリプト)を書き込むディレクトリをローカルマシンに作成します。
  2. getHR.tf という名前のファイルを作成し、次のコードを追加します。

    terraform {
      required_providers {
        http = {
          source = "hashicorp/http"
          version = "3.1.0"
        }
        local = {
          source = "hashicorp/local"
          version = "2.2.3"
        }
      }
    }
    
    provider "http" {
      # Configuration options
    }
    
    
    data "http" "get_auth_token" {
      provider = http
      url = "${var.appdURL}/auth/${var.tenant}/default/oauth2/token"
    
      request_headers = {
        Content-Type: "application/x-www-form-urlencoded"
        Authorization = "Basic ${base64encode("${var.clientId}:${var.clientSecret}")}"
      }
      request_body = "grant_type=client_credentials"
      method = "POST"
    }
    
    
    locals {
      auth_reponse = jsondecode(data.http.get_auth_token.response_body)
    }
    
    
    data "http" "get_healthrule"{
    provider = http
      url = "${var.appdURL}/alerting/v1beta/healthRules/${var.hrID}"
    
      request_headers = {
        Content-Type: "application/json"
        Authorization:"Bearer ${local.auth_reponse.access_token}"
        Appd-Tenant-Id: "${var.tenant}"
      }
      method = "GET"
    
    }
    
    output "get_response" {
      value = data.http.get_healthrule.response_body
    }
    
    resource "local_file" "json-data" {
        content  = data.http.get_healthrule.response_body
        filename = "getHR.json"
    }
    CODE
  3. variables.tf という名前のファイルを作成します。次のコードを追加して、ご利用の環境の変数のデフォルト値を入力します。A

    variable "appdURL" {
      description = "Endpoint of the Cloud Native Application Observability"
      type        = string
      default     = "<Your FSO Tenant URL>"
    }
    
    variable "tenant" {
      description = "Tenant ID"
      type        = string
      default     = "<Your FSO Tenant ID as per your Service Principals>"
    }
    
    variable "clientId" {
      description = "Generated Client ID"
      type        = string
      default     = "<Your Client ID as per your Service Principals>"
    }
    
    variable "clientSecret" {
      description = "Generated Client Secret"
      type        = string
      default     = "<Your Client Secret Key as per your Service Principals>"
    }
    
    variable "hrID" {
      description = "The ID of the Health Rule that you want to retrieve"
      type        = string
      default     = "<The Health Rule ID>"
    }
    
    
    CODE
  4. 次のコマンドを実行して、作業ディレクトリを初期化します。

    terraform init
    CODE
  5. 次のコマンドを実行して、Terraform プランで定義されているアクションを実行します。

    terraform apply
    CODE

Terraform コマンドを実行すると、指定された正常性ルールの詳細が、同じ作業ディレクトリの getHR.json ファイルに保存されます。

正常性ルールを更新する

既存の正常性ルールを更新するには、次の手順を実行します。

  1. 正常性ルールを更新するための Terraform 構成ファイル(スクリプト)を書き込むディレクトリをローカルマシンに作成します。
  2. UpdateHR.tf  という名前のファイルを作成し、次のコードを追加します。

    terraform {
      required_providers {
        http-client = {
          source = "dmachard/http-client"
          version = "0.0.3"
        }
      }
    }
    
    provider "http-client" {
      # Configuration options
    }
    
    provider "http" {
      # Configuration options
    }
    
    
    data "http" "get_auth_token" {
      provider = http
      url = "${var.appdURL}/auth/${var.tenant}/default/oauth2/token"
    
      request_headers = {
        Content-Type  =  "application/x-www-form-urlencoded"
        Authorization = "Basic ${base64encode("${var.clientId}:${var.clientSecret}")}"
      }
      request_body = "grant_type=client_credentials"
      method = "POST"
      lifecycle {
        postcondition {
          condition     = contains([200], self.status_code)
          error_message = "Status code invalid, Auth Token not generated successfully. ${self.response_body}"
        }
      }
    }
    
    
    locals {
      auth_response = jsondecode(data.http.get_auth_token.response_body)
      json_data = file("${path.module}/UpdateHR.json")
    }
    
    
    
    data "httpclient_request" "update_healthrule"{
    provider = http-client
      url = "${var.appdURL}/alerting/v1beta/healthRules/${var.hrID}"
      request_headers = {
        Content-Type   = "application/json"
        Authorization  ="Bearer ${local.auth_response.access_token}"
        Appd-Tenant-Id = "${var.tenant}"
        Accept         = "application/json"
      }
      request_method = "PUT"
      request_body   = "${local.json_data}"
    }
    
    output "get_response_code" {
      value = data.httpclient_request.update_healthrule.response_code
    }
    
    output "get_response_body" {
      value = data.httpclient_request.update_healthrule.response_body
    }
    
    
    resource "local_file" "json-data" {
        content  = data.httpclient_request.update_healthrule.response_body
        filename = "UpdateHRResponse.json"
    }
    CODE
  3. UpdateHR.json という名前のファイルを作成し、正常性ルールのペイロードとして機能する次のコードを(例として)追加します。

    {
        "name": "testTF-Check4-Update",
        "description": "Health Rule : testHRStatusFlowWarningUpgradeCriticalForPod",
        "enabled": true,
        "scheduleName": "Always",
        "waitTimeAfterViolation": "5m",
        "evaluationObjects": "k8s:pod().filter(attributes:k8s.pod.name in ('alert-pod-14'))",
        "criticalCriteria": {
            "criteriaExpression": "A",
            "conditions": [
                {
                    "name": "Condition 11",
                    "label": "A",
                    "conditionExpression": "metric:infra-agent:k8s.memory.usage.cmin(2m)&gt;95",
                    "evaluateToTrueOnNoData": false
                }
            ]
        },
        "warningCriteria": {
            "criteriaExpression": "A",
            "conditions": [
                {
                    "name": "Condition 11",
                    "label": "A",
                    "conditionExpression": "metric:infra-agent:k8s.memory.usage.cmin(2m)&gt;75",
                    "evaluateToTrueOnNoData": false
                }
            ]
        },
        "associateHealthTo": [
            {
                "objectExpr": "this"
            }
        ],
        "entityType": "k8s:pod",
        "createdAt": "2022-09-29T08:31:16Z",
        "updatedAt": "2022-09-29T08:32:41Z",
        "id": "633557d4a1113f3102195d08"
    }
    CODE
  4. variables.tf という名前のファイルを作成します。次のコードを追加し、ご利用の環境の変数のデフォルト値を入力します。

    variable "appdURL" {
      description = "Value of the Cloud Native Application Observability app"
      type        = string
      default     = "<Your FSO Tenant URL>"
    }
    
    variable "tenant" {
      description = "Tenant ID"
      type        = string
      default     = "<Your FSO Tenant ID as per your Service Principals>"
    }
    
    variable "clientId" {
      description = "Generated Client ID"
      type        = string
      default     = "<Your Client ID as per your Service Principals>"
    }
    
    variable "clientSecret" {
      description = "Generated Client Secret"
      type        = string
      default     = "<Your Client Secret Key as per your Service Principals>"
    }
    
    variable "hrID" {
      description = "The ID of the health rule that you want to update"
      type        = string
      default     = "<The Health Rule ID>"
    }
    CODE
  5. 次のコマンドを実行して、作業ディレクトリを初期化します。

    terraform init
    CODE
  6. 次のコマンドを実行して、Terraform 構成ファイルで定義されているアクションを実行します。

    terraform apply
    CODE

Terraform コマンドを実行すると、指定された正常性ルールが更新され、応答が同じ作業ディレクトリの UpdateHRResponse.json ファイルに保存されます。