LogicMonitor + Catchpoint: 自律型ITの新時代へ

さらに詳しく

LogicMonitor REST API v3はAPI v1よりもはるかに進化しています。すべての機能強化と修正はAPI v3に対してのみ行われます。API v3への移行が必須となる主な理由は以下の通りです。

  • API v3 バージョン ヘッダーの更新
  • トークンベースの認証のサポート
  • フィルタ構文のサポート
  • サポート PATCH 方法
  • API URLの更新 

LogicMonitor REST API v1 から API v3 への移行要件

API v3 に移行するには、次のものが必要です。

  • 認証用の有効な LMv1 API トークンまたはベアラー トークン。
  • API v3 リファレンスの LogicMonitor からの Swagger ドキュメントへのアクセス。

LogicMonitor REST API v1 から API v3 への変更

API v1 と API v3 の両方で次の変更点に注意する必要があります。

  • 認証の更新
  • へのサポート PATCH エンドポイントを部分的に更新する方法
  • バージョンヘッダーの更新
  • 成功とエラーの応答形式の更新

トークンベースの認証のサポート

API v2以降、LogicMonitorはBasic認証をサポートしなくなりました。これは、認証にAPIトークンの使用を推奨するためです。APIトークンは安全であり、LogicMonitorポータルとLogicMonitor REST APIアクセスの分離、そして監査ログエントリの作成に役立ちます。

LogicMonitorは、トークンベースの認証の利用を促進するため、LMv1 APIとAPI v2およびAPI v3のベアラートークンをサポートしています。詳細については、 RESTAPI認証.

PATCH メソッドのサポート

API v3には以下のサポートが含まれています PATCH ほとんどのAPIエンドポイントでこのメソッドが利用可能です。エンドポイントの特定のフィールドを更新する場合、 PUT すべてのフィールド(つまりAPIエンドポイント全体)を更新するメソッド。詳細については、 REST API v3 Swagger ドキュメント.

REST API v1 から API v3 へのバージョン ヘッダー

次のいずれかのバージョン ヘッダーを使用して、API v3 リクエストを行うことができます。

  • ヘッダーXバージョン、値3。例:「Xバージョン: 3」
  • APIクエリパラメータ v 値は3です。例えば、「v=3」

詳細については、を参照してください。 REST API v3 Swagger ドキュメント.

cURL を使用してバージョン ヘッダーを追加する

次のコマンドを入力して -H or --header ヘッダー名と値が続きます:

curl -H "X-version: 3" https://COMPANY.logicmonitor.com/santaba/rest/device/devices/1
Bash
curl --header "X-version: 3" https://COMPANY.logicmonitor.com/santaba/rest/device/devices/1
Bash
curl "https://COMPANY.logicmonitor.com/santaba/rest/device/devices/1?v=3"
Bash

Pythonを使用してバージョンヘッダーを追加する

次のコマンドを入力し、 headers 内のパラメータ requests としょうかん。

headers = {'X-Version':'3'}
response = requests.get("https://COMPANY.logicmonitor.com/santaba/rest/device/devices/1", headers=headers)
Python
response = requests.get("https://COMPANY.logicmonitor.com/santaba/rest/device/devices/1?v=3")
Python

REST API v1 から API v3 までの API レスポンス形式

API 応答形式の主な更新は次のとおりです。

  • ラッパーフィールドの削除
    • その statuserrmsgdata フィールドは API v3 レスポンスから削除されます。
    • すべてのフィールドから data JSON レスポンスのルートに直接含まれるようになりました。
    • 新しいフィールドを追加しました errorMessageerrorCodeerrorDetail エラー応答形式に変更します。
  • 解析ロジックの更新
    • 解析する必要はありません data API 応答データ値にアクセスするためのフィールド。
    • JSON レスポンスのルートからフィールド値を直接使用できます。
  • エラー処理の更新
    • 新しいフィールドを追加しました errorMessageerrorCodeerrorDetail エラー応答形式に変更します。
    • 同じ API エンドポイントでも、API v3 のエラー コードは API v1 のエラー コードと異なる場合があります。

REST API v1 および API v3 の成功レスポンス形式の例

API v1のレスポンスペイロードはAPI v3と異なるため、レスポンスの解析方法も変更されています。APIがレスポンスを正常に取得した場合、レスポンスの形式は以下のようになります。

  • API v1 成功レスポンス
{
    "status": 200,
    "errmsg": "OK",
    "data": {
            <API specific data fields and value>
    }
}
Python
  • API v3 成功レスポンス
{
   <API specific data, same data that we are returning in "data" field of API V1 response>
}
Python

REST API v1 および API v3 のエラー応答形式の例

エラー応答の場合、形式は次のようになります。

  • API v1 エラー応答
{
    "data": null,
    "errmsg": <ERROR_MESSAGE>,
    "status": <ERROR_CODE>
}
Python
  • API v3 エラー応答
{
    "errorMessage": <ERROR_MESSAGE>,
    "errorCode": <ERROR_CODE>,
    "errorDetail": null
}
Python

REST API v1とAPI v3のパラメータの違いの比較

API v1 から API v3 に移行する場合は、API URL、エラー コード、フィルター構文の違いを考慮する必要があります。

フィルタ構文API v1 パラメータ値API v3 パラメータ値
値を二重引用符(“”)で囲みますGET /device/devices?filter=name:127.0.0.1GET /device/devices?filter=name:"127.0.0.1"
特殊文字「+」を使用して値を2回エンコードします GET /device/devices?filter=name:Prod+ServerGET /device/devices?filter=name:"Prod%252BServer"
「+」を除く特殊文字(&、-など)を含む値を一度エンコードします GET /device/devices?filter=name:Prod&ServerGET /device/devices?filter=name:"Prod%26Server"

REST API v1 および API v3 の API URL の更新

API v1からAPI v3に移行する場合は、API v1のURLをAPI v3のURLに置き換える必要があります。コレクターやウェブサイトなどのコンポーネントのAPI URLは変更されています。v1とv3のURLのマッピングは次の表のとおりです。

成分API v1 URLAPI v3 URL
コレクターPOST /setting/collectorsPOST /setting/collector/collectors
GET /setting/collectorsGET /setting/collector/collectors
GET /setting/collectors/id/installers/idGET /setting/collector/collectors/id/installers/id
POST /setting/collectors/id/ackdownPOST /setting/collector/collectors/id/ackdown
DELETE /setting/collectors/idDELETE /setting/collector/collectors/id
PUT /setting/collectors/idPUT /setting/collector/collectors/id
PATCH /setting/collectors/idPATCH /setting/collector/collectors/id
GET /setting/collectors/idGET /setting/collector/collectors/id
コレクターグループGET /setting/collectors/groups/idGET /setting/collector/groups/{id}
DELETE /setting/collectors/groups/idDELETE /setting/collector/groups/{id}
PUT /setting/collectors/groups/idPUT /setting/collector/groups/id
GET /setting/collectors/groupsGET /setting/collector/groups
POST /setting/collectors/groupsPOST /setting/collector/groups
ウェブサイトGET /service/smcheckpointsGET /website/smcheckpoints
GET /service/services/id/sdtsGET /website/websites/id/sdts
GET /service/services/id/propertiesGET /website/websites/{id}/properties
GET /service/services/id/graphs/id/dataGET /website/websites/id/graphs/id/data
GET /service/services/id/checkpoints/id/graphs/id/dataGET /website/websites/id/checkpoints/id/graphs/id/data
GET /service/services/id/checkpoints/id/dataGET /website/websites/id/checkpoints/id/data
GET /service/services/id/alertsGET /website/websites/id/alerts
DELETE /service/services/idDELETE /website/websites/id
PATCH /service/services/idPATCH /website/websites/id
PUT /service/services/idPUT /website/websites/id
GET /service/services/idGET /website/websites/{id}
POST /service/servicesPOST /website/websites
GET /service/servicesGET /website/websites
ウェブサイトグループGET /service/groups/id/servicesGET /website/groups/id/websites
GET /service/groups/id/sdtsGET /website/groups/id/sdts
DELETE /service/groups/idDELETE /website/groups/id
PATCH /service/groups/idPATCH /website/groups/id
GET /service/groups/idGET /website/groups/{id}
PUT /service/groups/idPUT /website/groups/id
POST /service/groupsPOST /website/groups
GET /service/groupsGET /website/groups

REST API v1 および API v3 のエラー コードの更新

LogicMonitorはAPI v3の一部のエラーコードを更新しました。API v1からAPI v3に移行する際は、API v3のエラーコードを考慮する必要があります。v1とv3のエラーコードのマッピングは次の表のとおりです。

注意:

  • エラー コード リストには追加の行が含まれる場合があります。
  • エラー メッセージは API エンドポイントによって異なる場合があります。
APIv1エラーコードAPIv3エラーコードデフォルトの新しいエラーメッセージ
34031400不正な要求
24031404そのような記録はありません
13121400不正な要求
131014042そんな会社ない
130414041TSDBにそのような記録はありません
13031400不正な要求
13021404そのような記録はありません
13011400不正な要求
130014002部分的な成功
120114002部分的な成功
110414001リソース依存
110115001クエリタイムアウト
11001429リクエストが多すぎます
10911429リクエストが多すぎます
10791400不正な要求
10781400不正な要求
10771400不正な要求
10761400不正な要求
10751413リクエスト エンティティが大きすぎます
10741074タイムリーなレポートとして閲覧を拒否
10731400不正な要求
10691404そのような記録はありません
10651404そのような記録はありません
10601404そのような記録はありません
10581404そのような記録はありません
10531404そのような記録はありません
10481404そのような記録はありません
10411403許可が拒否されました
10401400不正な要求
10371404そのような記録はありません
10351400不正な要求
10331404そのような記録はありません
10311400不正な要求
10271404そのような記録はありません
10261400不正な要求
10251400不正な要求
10221404そのような記録はありません
10201400不正な要求
10191400不正な要求
10181400不正な要求
10171400不正な要求
10161400不正な要求
10151404そのような記録はありません
10141400不正な要求
10131404そのような記録はありません
10101400不正な要求
10071400不正な要求
10041404そのような記録はありません
10011500内部エラー
10001500内部エラー
6001409レコードはすでに存在します
5031404そのような記録はありません
5001500内部エラー
4031401認証に失敗しました
4001401認証に失敗しました
1001202タスクは実行中です

REST API v1 および API v3 のエラーコード更新例

以下は、処理フィールドの API v1 と API v3 の例です。 

  • API v1 コードブロック
import json
  
# Success Response Handling
def handle_success_response_v1(json_response):
    response = json.loads(json_response)
    status = response['status']
    errmsg = response['errmsg']
    data = response['data']
    # Process the fields from the data object
    # Example: total = data['total']
  
# Error Response Handling
def handle_error_response_v1(json_response):
    response = json.loads(json_response)
    status = response['status']
    errmsg = response['errmsg']
    data = response['data']
    # Process the error message and status
Python
  • API v1 処理フィールドのコードブロック
#!/bin/env python
  
import requests
import json
import hashlib
import base64
import time
import hmac
import getpass
  
#Account Info: LogicMonitor recommends to NEVER hardcode the credentials. Instead, retrieve the values from a secure storage.
#Note: The below is provided for illustration purposes only.
AccessId = getpass.getpass("Enter your AccessId: ")
AccessKey = getpass.getpass("Enter your AccessKey: ")
Company = 'apiAccount'
  
#Request Info
httpVerb ='GET'
resourcePath = '/device/devices/1'
fields = 'id,name,displayName'
queryParams = '?fields=' + fields
data = ''
  
#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
digest = hmac.new(
        AccessKey.encode('utf-8'),
        msg=requestVars.encode('utf-8'),
        digestmod=hashlib.sha256).hexdigest()
signature = base64.b64encode(digest.encode('utf-8')).decode('utf-8')
  
# Construct headers
auth = 'LMv1 ' + AccessId + ':' + str(signature) + ':' + epoch
  
# headers = {'Content-Type':'application/json','Authorization':auth}
headers = {'X-version':'1','Content-Type':'application/json','Authorization':auth}
  
#Make request
response = requests.get(url, data=data, headers=headers)
  
#body of response
response_body = json.loads(response.content.decode('utf-8'))
print ('Response Body:',response_body)
  
status = response_body['status']
if (status == 200):
    #Fetch data from body of response
    data = response_body['data']
  
    #Read device fields value from data
    device_name = data['name']
    device_displayName = data['displayName']
    print ("name:", device_name)
    print ("displayName:", device_displayName)
else:
    data = response_body['data']
    errmsg = response_body['errmsg']
    print ("Error status code:",status)
    print ("Error data:",data)
    print ("Error message:",errmsg)
Python
  • API v3 コードブロック
import json
  
# Success Response Handling
def handle_success_response_v3(json_response):
    response = json.loads(json_response)
    # Process the fields directly from the response
    # Example: total = response['total']
  
# Error Response Handling
def handle_error_response_v3(json_response):
    response = json.loads(json_response)
    error_message = response['errorMessage']
    error_code = response['errorCode']
    error_detail = response['errorDetail']
    # Process the error message, code, and detail
Python
  • API v3 処理フィールドのコードブロック
#!/bin/env python
  
import requests
import json
import hashlib
import base64
import time
import hmac
import getpass
  
#Account Info: LogicMonitor recommends to NEVER hardcode the credentials. Instead, retrieve the values from a secure storage.
#Note: The below is provided for illustration purposes only.
AccessId = getpass.getpass("Enter your AccessId: ")
AccessKey = getpass.getpass("Enter your AccessKey: ")
Company = 'apiAccount'
  
#Request Info
httpVerb ='GET'
resourcePath = '/device/devices/1'
fields = 'id,name,displayName'
queryParams = '?fields=' + fields
data = ''
  
#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
digest = hmac.new(
        AccessKey.encode('utf-8'),
        msg=requestVars.encode('utf-8'),
        digestmod=hashlib.sha256).hexdigest()
signature = base64.b64encode(digest.encode('utf-8')).decode('utf-8')
  
# Construct headers
auth = 'LMv1 ' + AccessId + ':' + str(signature) + ':' + epoch
headers = {'X-version':'3','Content-Type':'application/json','Authorization':auth}
  
#Make request
response = requests.get(url, data=data, headers=headers)
  
#body of response
print ('Response Status:',response.status_code)
response_body = json.loads(response.content.decode('utf-8'))
print ('Response Body:',response_body)
  
status = response.status_code
if (status == 200):
    #Read device fields value from response_body
    device_name = response_body['name']
    device_displayName = response_body['displayName']
    print ("name:", device_name)
    print ("displayName:", device_displayName)
else:
    errorMessage = response_body['errorMessage']
    errorCode = response_body['errorCode']
    errorDetail = response_body['errorDetail']
    print ("Error status code:",errorCode)
    print ("Error details:",errorDetail)
    print ("Error message:",errorMessage)
Python

14日間フルアクセス LogicMonitor プラットフォーム