在使用Azure的过程当中,不少用户但愿经过code的方式获取服务在管理门户中显示的监视信息,如虚拟机的CPU、服务总线的总消息出入数等。目前Azure的大部分服务都已经支持经过监控器的API查询和访问这些指标,使用过程当中请使用2018-01-01 API版本。api
本文首先介绍如何经过Rest API获取认证信息Authorization,而后以获取虚拟机CPU监控指标为示例演示
监控API的使用,最后介绍经过SDK获取监控。浏览器
在开发测试过程当中,能够经过Chrome浏览器F12功能快速获取认证信息。
app
关于AD应用的建立,请参考连接经过 PowerShell 获取认证凭据部分的建立示例脚本,固然也能够直接在Azure管理门户手动建立AD Application。若是是手动建立,请注意权限的授予问题,若是是第一次使用对门户不熟悉,请直接使用PowerShell脚本。dom
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
关于参数的获取,请参考建立AD应用脚本的说明。ide
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
目前监控器已经支持的监控指标请参考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
注意: Rest认证使用的是Bearer格式进行的认证,因此使用以前方式获取的token须要在前面加上"Bearer + 空格"ui
示例只是简单的演示了获取VM CPU的方式,用户能够根据本身的须要,调整请求的参数,获取其它类型资源的监控信息,固然也能够添加相应的过滤条件获取须要的信息。url
更多示例请参考: Azure监控RESTAPI演练spa
目前关于Monitor的sdk还在Preview阶段,最新版本的sdk还没法使用VS安装。rest
能够安装预览版本:0.18.1-preview进行测试,(Install-Package Microsoft.Azure.Management.Monitor -Version 0.18.1-preview)
其它包的安装
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。