GCPデバイスグループ

最終更新日: 25 年 2021 月 XNUMX 日

概要

LogicMonitorのRESTAPIを使用して、Google Cloud Platform(GCP)グループを管理できます。 GCPグループは、いくつかの重要な違いがあるデバイスグループです。 このドキュメントでは、違いと、GCPグループを追加および更新する方法について説明します。

URI: / device / groups

GCPグループの差別化要因

GCP固有のgroupType

GCPグループには特別なgroupType値があります。 具体的には、通常グループと動的グループのgroupType値はNormalですが、GCPデバイスグループの値はGCP / SERVICEです。ここで、SERVICEは、グループが作成されたGCPサービスです(ComputeEngine、CloudSQL、AppEngineなど)。オン)。 GCPアカウントレベルのグループのgroupType値はGCP / GcpRootになります。

GCP固有のフィールド

gcpRegionsInfo、gcpTestResult、gcpTestResultCodeはすべて、GCPグループに固有の読み取り専用フィールドです。 さらに重要なことに、GCPアカウントの認証情報やGCPサービス/リージョンの設定などのGCPグループ固有の情報は、デバイスグループJSONの「追加」オブジェクトに含まれています。

具体的には、追加のオブジェクトには次の情報が含まれます。

プロパティ 説明
デフォルト GCPグループ用に設定されたデフォルトのサービス設定。 これには、selectAll(監視するすべてのリージョンを選択するために使用できます)、monitoringRegions(モニタリングする特定のリージョンを選択するために使用できます)、deadOperation(MANUALLY | KEEP_7_DAYS | KEEP_14_DAYS | KEEP_30_DAYS | IMMEDIATELY)、useDefault(デフォルトオブジェクトには適用されません)が含まれます。 、nameFilter(デフォルトオブジェクトには適用されません)、tags(タグフィルターの指定に使用)、disableTerminatedHostAlerting JSON配列 「default」:{「useDefault」:true、「selectAll」:false、「monitoringRegions」:[「us-west1」、「us-east1」、「us-central1」、「us-east4」、「northamerica-northeast1」 ″、” southamerica-east1″、” europe-west1″、” europe-west2″、” europe-west3″、” europe-west4″、” asia-northeast1″、” asia-east1″、” asia-south1″、 ” asia-southeast1”、” australia-southeast1”]、“ tags”:[]、“ nameFilter”:[]、“ deadOperation”:“ KEEP_7_DAYS”、“ disableTerminatedHostAlerting”:true}
サービス GCPグループのサービス設定。 これには、selectAll(監視するすべてのリージョンを選択するために使用できます)、monitoringRegions(モニタリングする特定のリージョンを選択するために使用できます)、deadOperation(MANUALLY | KEEP_7_DAYS | KEEP_14_DAYS | KEEP_30_DAYS | IMMEDIATELY)、useDefault(デフォルト設定が必要かどうか)が含まれます。適用される)、nameFilter、タグ(タグフィルターを指定するために使用)、disableTerminatedHostAlerting、deviceDisplayNameTemplate、normalCollectorConfig JSON配列 「COMPUTEENGINE」:{「useDefault」:false、「selectAll」:false、「monitoringRegions」:[「us-west1」、「us-east1」、「us-central1」、「us-east4」、「northamerica-northeast1」 ″、” southamerica-east1″、” europe-west1″、” europe-west2″、” europe-west3″、” europe-west4″、” asia-northeast1″、” asia-east1″、” asia-south1″、 ” asia-southeast1”、” australia-southeast1”]、“ tags”:[]、“ nameFilter”:[]、“ deadOperation”:“ KEEP_7_DAYS”、“ disableTerminatedHostAlerting”:true、“ deviceDisplayNameTemplate”:“”、“ normalCollectorConfig 」:{「enable」:false、「collectors」:[]}}
アカウント GCPグループのアカウントレベルの情報。 JSON配列 「account」:{「projectId」:「development-198123」、「collectorId」:-2、「schedule」:「0 * * * *」、「serviceAccountKey」:{「type」:「service_account」、「project_id」 :「development-198123」、「private_key_id」:「**********」、「private_key」:「**********」、「client_email」:「[メール保護]」、「client_id」:「107550067926283000448」、「auth_uri」:「https://accounts.google.com/o/oauth2/auth」、「token_uri」:「https://oauth2.googleapis.com/token」、 「Auth_provider_x509_cert_url」:「https://www.googleapis.com/oauth2/v1/certs」、「Client_x509_cert_url」:「https://www.googleapis.com/robot/v1/metadata/x509/youremail%40development-198123 .iam.gserviceaccount.com”}}

GCPグループの追加

APIリクエストを認証するには、LogicMonitorでGCPサービスアカウントキーと、監視する各サービスのクラウドAPIを有効にする必要があります。 正しいサービスアカウントキーの作成とGCPAPIの有効化の詳細については、に記載されている手順に従ってください GCP環境をLogicMonitorに追加する。 サービスキーが作成され、クラウドAPIが有効になったら、/ device / groupsリソースにPOSTリクエストを送信して、GCPアカウントをLogicMonitorに追加する必要があります。

例:新しいGCPグループを投稿する

次のPythonスクリプトリクエストは、「LMGCP」という名前のGCPグループを次のパラメータで追加します。

  • デフォルトのサービス設定セクションでは、1つのリージョン(us-west1とus-eastXNUMX)のみが選択されており、リソースは自動的に削除されるように設定されていません。
  • App Engine、Compute Engine、およびCloud SQLサービスがモニタリング用に選択されています。ここで、Cloud SQLにはタグフィルターが適用され、XNUMXつのリージョンが選択され、リソースはXNUMX日後に自動的に削除されるように設定されています。
  • アカウントの自動検出頻度は設定されておらず、デフォルトでXNUMX時間ごとになります。
  • さらに、「customer」という名前のプロパティがグループに追加され、値「customerA」に設定されます。
#!/bin/env python

import requests
import json
import hashlib
import base64
import time
import hmac

#Account Info
AccessId ='48v2wRzfK94y53sq5EuF'
AccessKey ='H_D9i(f5~B^U36^K6i42=^nS~e75gy382Bf6{)P+'
Company = 'api'

#Request Info
httpVerb ='POST'
resourcePath = '/device/groups'
queryParams =''
data = '{"parentId": "1", "name": "LM GCP", "description": "awesome google cloud group", "customProperties": [{"name": "customer", "value":"customerA"}], "groupType": "GCP/GcpRoot", "extra": {"account": {"projectId": "development-198123", "collectorId": -2, "schedule": "0 * * * *", "serviceAccountKey": { "type": "service_account", "project_id": "development-198123", "private_key_id": "**********", "private_key": "**********", "client_email": "[email protected]", "client_id": "107550067926283000448", "auth_uri": "https://accounts.google.com/o/oauth2/auth", "token_uri": "https://oauth2.googleapis.com/token", "Auth_provider_x509_cert_url": "https://www.googleapis.com/oauth2/v1/certs", "Client_x509_cert_url": "https://www.googleapis.com/robot/v1/metadata/x509/youremail%40development-198123.iam.gserviceaccount.com"}}, "default": {"useDefault": true, "selectAll": false, "monitoringRegions": ["us-west1", "us-east1"], "tags": [], "nameFilter": [], "deadOperation": "KEEP_7_DAYS", "disableTerminatedHostAlerting": true}, "services": {"APPENGINE": {"useDefault": true, "selectAll": false, "monitoringRegions": [], "tags": [], "nameFilter": [], "deadOperation": "KEEP_7_DAYS", "disableTerminatedHostAlerting": true}, "COMPUTEENGINE": {"useDefault": false, "selectAll": false, "monitoringRegions": ["us-east1"], "tags": [], "nameFilter": [], "deadOperation": "KEEP_7_DAYS", "disableTerminatedHostAlerting": true, "deviceDisplayNameTemplate": "", "customNSPSchedule": "", "normalCollectorConfig": {"enable": false, "collectors": []}}, "CLOUDSQL": {"useDefault": false, "selectAll": false, "monitoringRegions": ["us-east1", "us-west1"], "tags": [{"name": "system.gcp.tag.foo", "operation": "include", "value": "bar"}], "nameFilter": [], "deadOperation": "KEEP_7_DAYS", "disableTerminatedHostAlerting": true}}}}'

#Construct URL 
url = 'https://'+ Company +'.logicmonitor.com/santaba/rest' + resourcePath + queryParams

#Get current time in milliseconds
epoch = str(int(time.time() * 1000))

#Concatenate Request details
requestVars = httpVerb + epoch + data + resourcePath

#Construct signature
hmac1 =  hmac.new(AccessKey.encode(),msg=requestVars.encode(),digestmod=hashlib.sha256).hexdigest()
signature = base64.b64encode(hmac1.encode())

#Construct headers
auth = 'LMv1 ' + AccessId + ':' + signature.decode() + ':' + epoch
headers = {'Content-Type':'application/json','Authorization':auth}

#Make request
response = requests.post(url, data=data, headers=headers)

#Print status and body of response
print('Response Status:',response.status_code)
print('Response Body:',response.content)
Pythonの3

GCPグループの更新

GCPグループを更新する場合は、最初にGETリクエストを作成してグループJSONを取得し、出力を解析して目的の値を置き換えてから、PUTを使用してリソースを更新することをおすすめします。

例:GCPグループを更新する

次のPythonスクリプトは、デバイスグループ39(たまたまGCPルートグループ)を取得するためのGETリクエストを作成し、応答を解析し、割り当てられたコレクターを置き換え、構成オブジェクトを追加してから、グループ39を更新するためのPUTリクエストを作成します。 。アカウント情報とGCPアカウントデバイスグループを使用して、GCPグループを更新します。

#!/bin/env python

import requests
import json
import hashlib
import base64
import time
import hmac

#Account Info
AccessId ='48v2wRzfK94y53sq5EuF'
AccessKey ='H_D9i(f5~B^U36^K6i42=^nS~e75gy382Bf6{)P+'
Company = 'api'

#Request Info
httpVerb ='GET'
resourcePath = '/device/groups/39'

#Construct URL 
url = 'https://'+ Company +'.logicmonitor.com/santaba/rest' + resourcePath 

#Get current time in milliseconds
epoch = str(int(time.time() * 1000))

#Concatenate Request details
requestVars = httpVerb + epoch + resourcePath

#Construct signature
hmac1 =  hmac.new(AccessKey.encode(),msg=requestVars.encode(),digestmod=hashlib.sha256).hexdigest()
signature = base64.b64encode(hmac1.encode())

#Construct headers
auth = 'LMv1 ' + AccessId + ':' + signature.decode() + ':' + epoch
headers = {'Content-Type':'application/json','Authorization':auth}

#Make request
response = requests.get(url, headers=headers)

#Parse response
jsonResponse = json.loads(response.content)

#Change Collector Id and add configuration object
group = jsonResponse['data']
group['extra']['account']['collectorId'] = 218

#PUT Request Info
httpVerb ='PUT'
resourcePath = '/device/groups/39'
queryParams =''
data = str(json.dumps(group))

#Construct URL 
url = 'https://'+ Company +'.logicmonitor.com/santaba/rest' + resourcePath +queryParams

#Get current time in milliseconds
epoch = str(int(time.time() * 1000))

#Concatenate Request details
requestVars = httpVerb + epoch + data + resourcePath

#Construct signature
hmac1 =  hmac.new(AccessKey.encode(),msg=requestVars.encode(),digestmod=hashlib.sha256).hexdigest()
signature = base64.b64encode(hmac1.encode())

#Construct headers
auth = 'LMv1 ' + AccessId + ':' + signature.decode() + ':' + epoch
headers = {'Content-Type':'application/json','Authorization':auth}

#Make request
putResponse = requests.put(url, data=data, headers=headers)

#Print status and body of response
print('Response Status:',response.status_code)
print('Response Body:',response.content)
Pythonの3
記事上で