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()

 

posted @ 2020-03-20 21:06  Vincen_shen  阅读(2167)  评论(0编辑  收藏  举报