検出用のランブックの作成

フリート・アプリケーション管理のリソースにインストールされているApache Tomcatホームをスキャンして識別するためのランブックを作成します。

  1. Apache Tomcatホームを検出するためのランブックを作成します
    ランブック名(Apache Tomcat Discovery)などの基本情報を指定し、Apache Tomcatカスタム製品、ライフサイクル操作(検出)および必要なオペレーティング・システム(Linux)を選択します。

    リソース間でタスク実行を実行する方法に応じて、パラレル・リソース・グループまたはローリング・リソース・グループを追加し、それらを構成します。

  2. タスクをランブックに追加します
    ノート

    「検出出力タスクとしてマーク」を選択してください。
    このアクションは、ターゲット結果タスクを識別し、ソフトウェア・インベントリ収集のためにタスクのレスポンスを読み取り、解釈する必要があることをフリート・アプリケーション管理に示します。
  3. ファイル・システムまたはオブジェクト・ストレージからランブック・タスク・スクリプトをアップロードします製品の検出用のサンプル・スクリプトを参照してください。
    スクリプトは、製品ホームまたはターゲット・パス、製品バージョン、インストール済パッチ(ある場合)、Javaホームとそのバージョンなどの情報を取得する必要があります。フリート・アプリケーション管理は、適切な実行環境をサポートしている場合、またはソフトウェア・パッケージまたはライブラリがインストールされている場合、コンピュート・インスタンスでスクリプトまたはプログラムを実行できます。このスクリプトは、検出ダウンロード・テンプレートで提供されるJSON形式で結果を出力する必要があります。検出用のサンプルJSONまたは検出用のサンプルYAMLを参照してください。
    たとえば:
    • スクリプトの詳細は、Object Storage > compartment: mycompartment > my_bucket > apache_tomcat_discovery.zip (複数のそのようなファイル)です。
    • 「コマンド」ボックスには、unzip -o -q apache_tomcat_discovery.zip; chmod +x apache_tomcat_discovery.py; python apache_tomcat_discovery.pyコマンドを指定できます。

    apache_tomcat_discovery.pyの検出スクリプトの例は、検出のサンプル・スクリプトを参照してください。

これで、Apache Tomcatホームを検出するタスクを含むランブックが作成されました。

製品の検出のサンプル・スクリプト

Apache Tomcatを検出するためのサンプルPythonスクリプトを次に示します。
import json
import os
import subprocess
import datetime


class TOMCAT:

    def _get_all_tomcats(self):
        command = ['find', '/', '-type', 'd', '-name', 'webapps']
        sp = subprocess.run(command, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
        tomcat_paths = sp.stdout.decode("utf-8").strip().replace('/webapps', '').split('\n')

        #tomcat_paths=[path for path in tomcat_paths if not 'backup' in path]

        return tomcat_paths

    def _get_details(self, path):
        tomcat_details = dict()

        try:
            command = f'{path}/bin/version.sh'
            sp = subprocess.Popen(command, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
            stdout, stderr = sp.communicate(timeout=10)
            info_list = stdout.decode("utf-8")

            for line in info_list.splitlines():
                if ':' in line:
                    key, value = line.split(':', 1)
                    tomcat_details[key.strip()] = value.strip()

            return tomcat_details

        except Exception as E:
           pass

    def _get_timestamp(self, file_path):
        if os.path.exists(file_path):
            timestamp = os.path.getmtime(file_path)
            modified_date = datetime.datetime.fromtimestamp(timestamp).strftime("%Y-%d-%m %H:%M:%S")
            return modified_date

    def _get_java_details(self):
        try:
            java_details = dict()
            command = "java -version 2>&1 | head -n 1"
            sp = subprocess.run(command, shell=True, stdout=subprocess.PIPE)
            java_version = sp.stdout.decode("utf-8").split('"')[1]

            # Getting Java Home
            command = "java -XshowSettings:properties -version 2>&1    | sed '/^[[:space:]]*java\.home/!d;s/^[[:space:]]*java\.home[[:space:]]*=[[:space:]]*//'"
            sp = subprocess.run(command, shell=True, stdout=subprocess.PIPE)
            java_home = sp.stdout.decode("utf-8").replace("\n", '')

            java_details['java_home'] = java_home
            java_details['java_version'] = java_version.split('_')[0]

            return java_details

        except Exception as E:
            pass

    def _instance(self, path, new_version_exists):
        properties = []
        installed_patches = []
        tomcat_details = self._get_details(path)

        instance = {"product": "Apache Tomcat"}
        instance.update({"version": '.'.join(tomcat_details['Server number'].split('.')[:2])})
        instance.update({"target_name": path})
        instance.update({"resource_name": ""})
        instance.update({"components": []})
        instance.update({"available_patches": []})

        if new_version_exists:
            installed_patches = [
                {
                    "patch_name": tomcat_details['Server number'],
                    "patch_applied_dt": self._get_timestamp(path),
                    "patch_description": tomcat_details['Server number'],
                    "patch_type":"Critical Patch Update",
                    "released_date":self._get_timestamp(path),
                    "severity":"CRITICAL"
                }
            ]

        else:
            installed_patches = [
                {
                    "patch_name": tomcat_details['Server number'],
                    "patch_applied_dt": self._get_timestamp(path),
                    "patch_description": tomcat_details['Server number'],
                    "patch_type": "Critical Patch Update"

                }
            ]

        instance.update({"installed_patches": installed_patches})

        for key, value in self._get_java_details().items():
            properties.append({"property_name": key, "property_value": value})

        instance.update({"properties": properties})

        return instance

    def _discovery_tomcat(self):
        targets = []
        tomcat_paths = self._get_all_tomcats()
        new_version_exists = any(['backup' in path for path in tomcat_paths])

        for path in tomcat_paths:
            if not 'backup' in path:
                targets.append(self._instance(path, new_version_exists))

        discovery_output = ({"targets": targets})

        return discovery_output


if __name__ == "__main__":
    SCRIPTS_DIR = "/opt/fams/.scripts"
    os.makedirs(SCRIPTS_DIR, exist_ok=True)
    tomcat = TOMCAT()
    tomcat_output = tomcat._discovery_tomcat()

    json_obj = json.dumps(tomcat_output, indent=4)
    print(json_obj)

    with open("{}/tomcat_discovery.json".format(SCRIPTS_DIR), 'wb') as outfile:
        outfile.write(json_obj.encode())

検出用のサンプルJSON

Apache Tomcatを検出するためのサンプルJSONを次に示します。

{
  "compartmentId": "<your_root_compartment_ocid>",
  "displayName": "Apache Tomcat Discovery",
  "description": "Runbook to discovery Apache Tomcat Product Homes on an instance",
  "operation": "DISCOVERY",
  "runbookRelevance": "PRODUCT",
  "platform": "Apache Tomcat",
  "isDefault": false,
  "osType": "LINUX",
  "estimatedTime": "1",
  "associations": {
    "groups": [
      {
        "name": "Parallel_resource_group",
        "type": "PARALLEL_RESOURCE_GROUP",
        "properties": {
          "actionOnFailure": "ABORT",
          "condition": null
        }
      }
    ],
    "tasks": [
      {
        "stepName": "Apache_Tomcat_discovery_task",
        "associationType": "TASK",
        "taskRecordDetails": {
          "scope": "LOCAL",
          "executionDetails": {
            "executionType": "SCRIPT",
            "variables": null,
            "content": {
              "sourceType": "OBJECT_STORAGE_BUCKET",
              "namespaceName": "mytenancynamespace",
              "bucketName": "demo_bucket",
              "objectName": "my_apache_tomcat_discovery.zip",
              "checksum": "APLIeko3yTq4CXNWzxcVKvHg=="
            },
            "command": "unzip -o -q my_apache_tomcat_discovery.zip; chmod +x apache_tomcat_discovery.py ; python apache_tomcat_discovery.py",
            "credentials": []
          },
          "description": null,
          "platform": "Apache Tomcat",
          "isCopyToLibraryEnabled": false,
          "osType": "LINUX",
          "properties": {
            "numRetries": 0,
            "timeoutInSeconds": 3000
          },
          "isDiscoveryOutputTask": true,
          "isApplySubjectTask": false,
          "name": "Apache Tomcat_discovery_task"
        },
        "stepProperties": {
          "actionOnFailure": "ABORT",
          "condition": null
        },
        "outputVariableMappings": []
      }
    ],
    "executionWorkflowDetails": {
      "workflow": [
        {
          "groupName": "Parallel_resource_group",
          "type": "PARALLEL_RESOURCE_GROUP",
          "steps": [
            {
              "type": "TASK",
              "stepName": "Apache_Tomcat_discovery_task"
            }
          ]
        }
      ]
    }
  }
}

検出用のYAMLのサンプル

Apache Tomcatを検出するためのサンプルYAMLを次に示します。

compartmentId: <your_root_compartment_ocid>
displayName: Apache Tomcat Discovery
description: Runbook to discovery Apache Tomcat Product Homes on an instance
operation: DISCOVERY
runbookRelevance: PRODUCT
platform: Apache Tomcat
isDefault: false
osType: LINUX
estimatedTime: "1"
associations:
  groups:
    - name: Parallel_resource_group
      type: PARALLEL_RESOURCE_GROUP
      properties:
        actionOnFailure: ABORT
        condition: null
  tasks:
    - stepName: Apache_Tomcat_discovery_task
      associationType: TASK
      taskRecordDetails:
        scope: LOCAL
        executionDetails:
          executionType: SCRIPT
          variables: null
          content:
            sourceType: OBJECT_STORAGE_BUCKET
            namespaceName: mytenancynamespace
            bucketName: demo_bucket
            objectName: my_apache_tomcat_discovery.zip
            checksum: APLIeko3yTq4CXNWzxcVKvHg==
          command: unzip -o -q my_apache_tomcat_discovery.zip; chmod +x
            apache_tomcat_discovery.py ; python apache_tomcat_discovery.py
          credentials: []
        description: null
        platform: Apache Tomcat
        isCopyToLibraryEnabled: false
        osType: LINUX
        properties:
          numRetries: 0
          timeoutInSeconds: 3000
        isDiscoveryOutputTask: true
        isApplySubjectTask: false
        name: Apache Tomcat_discovery_task
      stepProperties:
        actionOnFailure: ABORT
        condition: null
      outputVariableMappings: []
  executionWorkflowDetails:
    workflow:
      - groupName: Parallel_resource_group
        type: PARALLEL_RESOURCE_GROUP
        steps:
          - type: TASK
            stepName: Apache_Tomcat_discovery_task