• Azure 服务监控API调用


    概述

    在使用Azure的过程中,很多用户希望通过code的方式获取服务在管理门户中显示的监视信息,如虚拟机的CPU、服务总线的总消息出入数等。目前Azure的大部分服务都已经支持通过监控器的API查询和访问这些指标,使用过程中请使用2018-01-01 API版本。

    本文首先介绍如何通过Rest API获取认证信息Authorization,然后以获取虚拟机CPU监控指标为示例演示
    监控API的使用,最后介绍通过SDK获取监控。


    获取认证Authorization(三种方式)

    1、通过浏览器快速获取

    在开发测试过程中,可以通过Chrome浏览器F12功能快速获取认证信息。
    image

    2、通过AD认证信息获取

    关于AD应用的创建,请参考链接通过 PowerShell 获取认证凭据部分的创建示例脚本,当然也可以直接在Azure管理门户手动创建AD Application。如果是手动创建,请注意权限的授予问题,如果是第一次使用对门户不熟悉,请直接使用PowerShell脚本。

    POST /b388b808-0ec9-4a09-a414-a7cbbd8b7e9b/oauth2/token?api-version=1.0 HTTP/1.1
    Host: login.chinacloudapi.cn
    Content-Type: application/x-www-form-urlencoded
    Cache-Control: no-cache
    Postman-Token: 5ea61bff-49b0-ec09-44bd-f9437d53932e
    
    grant_type=client_credentials&resource=https%3A%2F%2Fmanagement.chinacloudapi.cn%2F&client_id=42c02c81-eff8-4dff-cc84-4e43b6ea8a6f&client_secret=123456
    

    image

    关于参数的获取,请参考创建AD应用脚本的说明。

    3、通过账户名和秘钥获取
    POST /common/oauth2/token?api-version=1.0 HTTP/1.1
    Host: login.chinacloudapi.cn
    Content-Type: application/x-www-form-urlencoded
    Cache-Control: no-cache
    Postman-Token: c35a61d2-e2af-28e9-36b2-70ef717c7013
    
    grant_type=password&resource=https%3A%2F%2Fmanagement.chinacloudapi.cn%2F&username=tao.yu%40microsoftinternal.partner.onmschina.cn&password=123456&client_id=1950a258-227b-4e31-a9cf-717495945fc2
    

    image

    获取虚拟机CPU Rest请求示例

    目前监控器已经支持的监控指标请参考Azure监控器支持的指标

    URL: https://management.chinacloudapi.cn/subscriptions/ecf1d1be-9348-434c-86ef-f903f7bb7001/resourceGroups/yuvmtest2/providers/Microsoft.Compute/virtualMachines/yuvmtest2/providers/microsoft.insights/metrics?metricnames=Percentage CPU&api-version=2018-01-01
    
    Authorization:Bearer + 空格 + token
    

    image

    注意: Rest认证使用的是Bearer格式进行的认证,所以使用之前方式获取的token需要在前面加上"Bearer + 空格"

    示例只是简单的演示了获取VM CPU的方式,用户可以根据自己的需要,调整请求的参数,获取其它类型资源的监控信息,当然也可以添加相应的过滤条件获取需要的信息。

    更多示例请参考: Azure监控RESTAPI演练


    使用C# SDK获取监控信息

    目前关于Monitor的sdk还在Preview阶段,最新版本的sdk还无法使用VS安装。

    可以安装预览版本:0.18.1-preview进行测试,(Install-Package Microsoft.Azure.Management.Monitor -Version 0.18.1-preview)

    安装参考链接:Microsoft.Azure.Management.Monitor

    其它包的安装

    Install-Package Microsoft.Rest.ClientRuntime.Azure.Authentication -Version 2.3.4
    
    Install-Package Microsoft.IdentityModel.Clients.ActiveDirectory -Version 3.19.8
    
    

    下面的测试Code是在Net Core环境下进行的测试。

    
    using System;
    using System.Collections.Generic;
    using Microsoft.Azure.Management.Monitor;
    using Microsoft.Azure.Management.Monitor.Models;
    using Microsoft.IdentityModel.Clients.ActiveDirectory;
    using Microsoft.Rest.Azure.Authentication;
    
    namespace GetAzureVmMonitor
    {
        namespace Vmtest
        {
            class Program
            {
                //对应虚拟机属性信息
                private const string ResourceUri = "/subscriptions/ecf1d1be-9348-434c-86ef-f903f7bb7001/resourceGroups/yuvmtest2/providers/Microsoft.Compute/virtualMachines/yuvmtest2";
    
                static void Main(string[] args)
                {
                    //AD APPlication 信息
                    string domain = "b388b808-0ec9-4a09-a414-a7cbbd811e9b";
                    string clientId = "42c02c81-eff8-4df6-8884-4e43b6e11a6f";
                    string clientSecret = "123456";
                    //使用AD获取认证
                    var credentials = ApplicationTokenProvider.LoginSilentAsync(domain, new ClientCredential(clientId, clientSecret), ActiveDirectoryServiceSettings.AzureChina).Result;
    
                    //创建Monitor client对象
                    MonitorClient monitorClient = new MonitorClient(new Uri("https://management.chinacloudapi.cn"), credentials);
    
                    var actualMetrics = monitorClient.Metrics.List(resourceUri: ResourceUri,
                                                                    metric: "Percentage CPU",
                                                                    resultType: ResultType.Data
                                                                    );
    
                    IEnumerable<Metric> value = actualMetrics.Value;
                    EnumerateMetrics(value);
                    Console.ReadKey(true);
                }
    
                /// <summary>
                /// 打印输出部分结果
                /// </summary>
                /// <param name="metrics"></param>
                /// <param name="maxRecords"></param>
                private static void EnumerateMetrics(IEnumerable<Metric> metrics, int maxRecords = 5)
                {
                    foreach (var metric in metrics)
                    {
                        Console.WriteLine(metric.Id);
    
                        foreach (var test in metric.Timeseries)
                        {
                            foreach (var data in test.Data)
                            {
                                Console.WriteLine(data.TimeStamp + " : " + data.Average);
                            }
                            Console.WriteLine("----------");
                        }
                    }
                }
            }
        }
    }
    
    

    ResourceUri 对应管理门户虚拟机的Properties -> RESOURCE ID。

    参考链接

    Azure 监控器文档

    使用 REST 接口获取订阅下虚拟机信息

  • 相关阅读:
    树莓派 官方800万摄像头 参数
    51单片机 小车 L298N pwm调速 串口控制 按键控制
    51单片机 HC05蓝牙模块
    python 类属性和实例属性、方法 访问权限问题
    python 通过setup.py安装和卸载python软件包
    win10家庭版安装
    python 测试用例
    python 异常类型
    抽象工厂模式
    (转)UML类图与类的关系详解
  • 原文地址:https://www.cnblogs.com/taro/p/9713429.html
Copyright © 2020-2023  润新知