全部产品
Search
文档中心

系统运维管理:ACS-ECS-BulkyInstallARMSAgent

更新时间:Sep 05, 2024

模板名称

ACS-ECS-BulkyInstallARMSAgent 批量安装ARMS插件

立即执行

模板描述

使用这个模板批量的在ECS安装ARMS插件

模板类型

自动化

所有者

Alibaba Cloud

输入参数

参数名称

描述

类型

是否必填

默认值

约束

appName

要监控的应用名称

String

licenseKey

应用监控功能的License Key

String

targets

目标实例

Json

regionId

地域ID

String

{{ ACS::RegionId }}

action

操作类型

String

install

rateControl

任务执行的并发比率

Json

{‘Mode’: ‘Concurrency’, ‘MaxErrors’: 0, ‘Concurrency’: 10}

OOSAssumeRole

OOS扮演的RAM角色

String

“”

输出参数

参数名称

描述

类型

commandOutput

String

执行此模板需要的权限策略

{
    "Version": "1",
    "Statement": [
        {
            "Action": [
                "ecs:DescribeInstances",
                "ecs:DescribeInvocationResults",
                "ecs:DescribeInvocations",
                "ecs:RunCommand"
            ],
            "Resource": "*",
            "Effect": "Allow"
        }
    ]
}

详情

ACS-ECS-BulkyInstallARMSAgent详情

模板内容

FormatVersion: OOS-2019-06-01
Description:
  en: Use this template to install ARMS agent on ecs
  zh-cn: 使用这个模板批量的在ECS安装ARMS插件
  name-en: ACS-ECS-BulkyInstallARMSAgent
  name-zh-cn: 批量安装ARMS插件
  categories:
    - run_command
Parameters:
  regionId:
    Type: String
    Label:
      en: RegionId
      zh-cn: 地域ID
    AssociationProperty: RegionId
    Default: '{{ ACS::RegionId }}'
  action:
    Type: String
    Label:
      en: Action
      zh-cn: 操作类型
    AllowedValues:
      - install
      - upgrade
      - uninstall
    Default: install
  appName:
    Type: String
    Label:
      en: AppName
      zh-cn: 要监控的应用名称
  licenseKey:
    Type: String
    Label:
      en: LicenseKey
      zh-cn: 应用监控功能的License Key
  targets:
    Type: Json
    Label:
      en: TargetInstance
      zh-cn: 目标实例
    AssociationProperty: Targets
    AssociationPropertyMetadata:
      ResourceType: 'ALIYUN::ECS::Instance'
      RegionId: regionId
  rateControl:
    Label:
      en: RateControl
      zh-cn: 任务执行的并发比率
    Type: Json
    AssociationProperty: RateControl
    Default:
      Mode: Concurrency
      MaxErrors: 0
      Concurrency: 10
  OOSAssumeRole:
    Label:
      en: OOSAssumeRole
      zh-cn: OOS扮演的RAM角色
    Type: String
    Default: ''
RamRole: '{{ OOSAssumeRole }}'
Tasks:
  - Name: getInstance
    Description:
      en: Views the ECS instances
      zh-cn: 获取ECS实例
    Action: 'ACS::SelectTargets'
    Properties:
      ResourceType: 'ALIYUN::ECS::Instance'
      RegionId: '{{ regionId }}'
      Filters:
        - '{{ targets }}'
    Outputs:
      instanceIds:
        Type: List
        ValueSelector: 'Instances.Instance[].InstanceId'
      instanceInfos:
        Type: List
        ValueSelector: '.Instances.Instance[] | {"osType":.OSType, "instanceId":.InstanceId}'
  - Name: runCommand
    Action: 'ACS::ECS::RunCommand'
    When:
      'Fn::Equals':
        - 'Fn::Jq':
            - First
            - '.|map(select(.instanceId == "{{ ACS::TaskLoopItem }}").osType)[]'
            - '{{ getInstance.instanceInfos }}'
        - linux
    Description:
      en: Execute cloud assistant command
      zh-cn: 执行云助手命令
    Properties:
      commandContent: |-
        #!/usr/bin/env bash
        regionId="{{ regionId }}"
        region=`echo $regionId|awk -F "-" '{print $2}'`
        action="{{action}}"
        licenseKey="{{licenseKey}}"
        agentBaseDir="$HOME/.arms"
        pidFileName="supervisor.pid"
        pidFile="${agentBaseDir}/${pidFileName}"
        appName="{{appName}}"
        if [[ "$regionId" =~ "cn-"  ]];then
          DOWNLOAD_URL="http://arms-apm-${region}.oss-${regionId}.aliyuncs.com/"
        elif [[ "$regionId" =~ "ap-southeast-1"  ]];then
          DOWNLOAD_URL="http://arms-apm-ap-southeast.oss-ap-southeast-1-internal.aliyuncs.com/cloud_ap-southeast-1/"
        elif [[ "$regionId" =~ "ap-northeast-1"  ]];then
          DOWNLOAD_URL="http://arms-apm-japan.oss-ap-northeast-1-internal.aliyuncs.com/"
        elif [[ "$regionId" =~ "us-west-1"  ]];then
          DOWNLOAD_URL="http://arms-apm-usw.oss-us-west-1-internal.aliyuncs.com/"
        else
          echo "Current ${regionId} is not supported."
          exit 1
        fi

        function operateJavaAgent() {
        appEnv="java"
        function installAgent() {
          wget -O- "${DOWNLOAD_URL}/install.sh" -q|sh
          currentProcess=`ps -ef |grep java|grep -v "grep --color" `
          if echo $currentProcess|grep "$appName" >/dev/null 2>&1; then
              processIndex=`echo null |${agentBaseDir}/supervisor/cli.sh "$licenseKey" $appName |grep "$appName"  |awk -F ' ' '{print $1}'`
              (echo $processIndex |${agentBaseDir}/supervisor/cli.sh "$licenseKey" $appName )  && echo "Automatically input application index $processIndex for attach $appName."
          else
            echo "It seems that no running $appName application."
          fi
        }

        function uninstallAgent() {
        if [ -f $pidFile ];
        then
            kill -9 `cat $pidFile` > /dev/null 2>&1
            echo "Finished to stop $pidFile."
        fi
        rm -rf $agentBaseDir
        }
        if [ $action = "install" ]; then
            if [ -d ${agentBaseDir}/agent ];
            then
                  uninstallAgent || exit 1
                  installAgent || exit 1
                  echo "Finished to install $appEnv arms agent."
            else
              installAgent || exit 1
              echo "Finished to install $appEnv arms agent."
            fi
        elif [ $action = "upgrade" ]; then
          installAgent || exit 1
          echo "Finished to upgrade $appEnv arms agent."
        else
          uninstallAgent || exit 1
          echo "Finished to uninstall $appEnv arms agent."
        fi
        }
        operateJavaAgent

      instanceId: '{{ ACS::TaskLoopItem }}'
      regionId: '{{ regionId }}'
      commandType: RunShellScript
    Loop:
      Items: '{{ getInstance.instanceIds }}'
      RateControl: '{{ rateControl }}'
      Outputs:
        commandOutputs:
          AggregateType: 'Fn::ListJoin'
          AggregateField: commandOutput
    Outputs:
      commandOutput:
        Type: String
        ValueSelector: invocationOutput
Outputs:
  commandOutput:
    Type: String
    Value: '{{ runCommand.commandOutputs }}'
Metadata:
  ALIYUN::OOS::Interface:
    ParameterGroups:
      - Parameters:
          - action
          - appName
          - licenseKey
        Label:
          default:
            zh-cn: 设置参数
            en: Configure Parameters
      - Parameters:
          - regionId
          - targets
        Label:
          default:
            zh-cn: 选择实例
            en: Select Ecs Instances
      - Parameters:
          - rateControl
          - OOSAssumeRole
        Label:
          default:
            zh-cn: 高级选项
            en: Control Options