VMware vROPS API接口獲取對象告警、衡量指標示例


VMware vROPS產品Rest API還是比較豐富的,調用也比較簡單。

 

vROPS Rest API在線文檔

  https://vrops-server-ip/suite-api/docs/rest/index.html

 

獲取vROPS對象告警、衡量指標 - Python示例代碼:

import requests
import time

# 不顯示HTTPS證書告警信息
requests.packages.urllib3.disable_warnings()


def get_token(host, username, password):
    """
    獲取認證Token
    """
    url = host + "/suite-api/api/auth/token/acquire"
    headers = {
        "Content-Type": "application/json",
        "Accept": "application/json"
    }
    data = {
        "username": username,
        "password": password
    }
    response = requests.post(url, headers=headers, json=data, verify=False)
    if response.status_code == 200:
        response_json = response.json()
        return response_json.get("token")
    else:
        print("Get vROPs token failed, Error: " + response.text)


def get_resource_uuid(host, headers, resource_kind, resource_name):
    """
    獲取指定對象UUID
    """
    url = host + "/suite-api/api/resources?resourceKind={}&name={}".format(
        resource_kind,
        resource_name
    )  # resourceKind: virtualMachine hostSystem

    response = requests.get(url, headers=headers, verify=False)
    if response.status_code == 200:
        response_json = response.json()
        identifier = response_json["resourceList"][0]["identifier"]     # 這里需要注意resource可能重名的情況
        return identifier


def get_resource_stat_keys(host, headers, resource_kind, resource_name):
    """
    獲取資源對象所有stat-keys, eg: mem|overhead_average, net|received_average, mem|oversized
    """
    resource_uuid = get_resource_uuid(host, headers, resource_kind, resource_name)
    url = host + "/suite-api/api/resources/{}/statkeys".format(resource_uuid)
    response = requests.get(url, headers=headers, verify=False)
    if response.status_code == 200:
        print(response.text)


def get_resource_alerts(host, headers, resource_kind, resource_name):
    """
    獲取資源對象所有Alert, 包含ACTIVE和CANCELED
    """
    resource_uuid = get_resource_uuid(host, headers, resource_kind, resource_name)
    url = host + "/suite-api/api/alerts?resourceId={}".format(resource_uuid)
    response = requests.get(url, headers=headers, verify=False)
    if response.status_code == 200:
        print(response.json()["alerts"])


def get_resource_stats_latest(host, headers, resource_kind, resource_name):
    """
    獲取資源對象最近所有stats
    """
    resource_uuid = get_resource_uuid(host, headers, resource_kind, resource_name)

    url = host + "/suite-api/api/resources/{}/stats/latest".format(resource_uuid)
    response = requests.get(url, headers=headers, verify=False)
    if response.status_code == 200:
        response_json = response.json()
        stats_list = response_json["values"][0]["stat-list"]["stat"]
        for stats in stats_list:
            print("statKey: {}, data: {}".format(stats["statKey"]["key"], str(stats["data"][0])))


def get_resource_stats_by_duration_time(host, headers, resource_kind, resource_name, stat_key):
    """
    獲取資源對象指定stats-key指定時間區間stats
    """
    resource_uuid = get_resource_uuid(host, headers, resource_kind, resource_name)
    url = host + "/suite-api/api/resources/{}/stats/query".format(resource_uuid)
    data = {
        "statKey": stat_key,   # cpu|usage_average
        "begin": int(round(time.time() * 1000)) - (1 * 60 * 60 * 1000),     # 過去1小時時間戳
        "end": int(round(time.time() * 1000)),      # 當前時間,13位時間戳
        "intervalType": "MINUTES",      # MINUTES, HOURS, DAYS, WEEKS, MONTH
        "intervalQuantifier": 10,    # 表示intervalType的時間間隔
        "rollUpType": "AVG"     # AVG, MIN, MAX
    }
    response = requests.post(url, headers=headers, json=data, verify=False)
    if response.status_code == 200:
        print(response.text)


def get_resource_stats_by_last_day(host, headers, resource_kind, resource_name, stat_key):
    """
    獲取資源對象最近24小時指定stats
    """
    resource_uuid = get_resource_uuid(host, headers, resource_kind, resource_name)
    url = host + "/suite-api/api/resources/{}/stats/query".format(resource_uuid)
    data = {
        "statKey": stat_key,
        "currentOnly": True
    }
    response = requests.post(url, headers=headers, json=data, verify=False)
    if response.status_code == 200:
        print(response.text)


def main():
    host = "https://vrops-server-ip"
    username = "api-user"  # 在vROPS中新建了一個Read-Only賬號用於API接口查詢
    password = "xxxxxxxx"
    token = get_token(host, username, password)

    resource_kind = "virtualMachine"
    resource_name = "vm-001"

    # 封裝Headers
    headers = {
        "Content-Type": "application/json",
        "Accept": "application/json",
        "Authorization": "vRealizeOpsToken " + token
    }

    # 獲取指定對象所有告警
    get_resource_alerts(host, headers, resource_kind, resource_name)

    # 獲取指定對象所有衡量指標Key
    get_resource_stat_keys(host, headers, resource_kind, resource_name)

    # 獲取指定對象所有衡量指標最新統計值
    get_resource_stats_latest(host, headers, resource_kind, resource_name)

    stat_key = ["cpu|usage_average"]  # CPU利用率衡量指標
    # 獲取指定對象在指定時間段內的指定衡量指標統計值
    get_resource_stats_by_duration_time(host, headers, resource_kind, resource_name, stat_key)

    # 獲取指定對象在最近24小時內的指定衡量指標統計值
    get_resource_stats_by_last_day(host, headers, resource_kind, resource_name, stat_key)


if __name__ == '__main__':
    main()

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM