探索Windows Azure 监控和自动伸缩系列2 - 获取虚拟机的监控定义和监控数据
上一篇博文介绍了如何连接Windows Azure: http://www.cnblogs.com/teld/p/5113063.html
本篇我们继续上次的示例代码,获取虚拟机的监控定义和监控数据。
有人会问,Azure Portal上已经有了监控数据,通过代码获取有意思吗?我们计划基于性能计数器的监控数据来实现应用的自动伸缩,因此可以获取到监控指标定义和监控数据应该是第一步。
在Azure的管理Portal中我们可以看到虚拟机的监控数据,目前,提供的主要有以下监控指标:
CPU Percentage;Disk Read; Disk Write; Network in;NetWork Out。
Azure中监控的Nuget主要是这个:Microsoft Azure Management Libraries
核心的几个namespace有:

我们本篇用的是Metric这个命名空间,核心类MetricClient:
namespace AzureTest
{
using Microsoft.WindowsAzure;
using Microsoft.WindowsAzure.Management.Monitoring.Metrics;
using Microsoft.WindowsAzure.Management.Monitoring.Metrics.Models;
using Microsoft.WindowsAzure.Management.Monitoring.Utilities; /// <summary>
/// 监控客户端
/// </summary>
class MonitorClient
{
private SubscriptionCloudCredentials credentials; public MonitorClient(SubscriptionCloudCredentials credentials)
{
this.credentials = credentials;
} /// <summary>
/// 获取所有的监控指标
/// </summary>
public void GetMetricDefinitions()
{
var metricsClient = new MetricsClient(credentials);
// Build the resource ID string.
var resourceId = ResourceIdBuilder.BuildVirtualMachineResourceId("cloudServiceName", "deploymentName");
Console.WriteLine("Resource Id: {0}", resourceId); //Get the metric definitions.
var metricListResponse=
metricsClient.MetricDefinitions.List(resourceId, null, null);
MetricDefinitionCollection metricDefinitions = metricListResponse.MetricDefinitionCollection;
// Display the metric definitions.
int count = ;
foreach (MetricDefinition metricDefinition in metricDefinitions.Value)
{
Console.WriteLine("MetricDefinitio: " + count++);
Console.WriteLine("Display Name: " + metricDefinition.DisplayName);
Console.WriteLine("Metric Name: " + metricDefinition.Name);
Console.WriteLine("Metric Namespace: " + metricDefinition.Namespace);
Console.WriteLine("Is Altertable: " + metricDefinition.IsAlertable);
Console.WriteLine("Min. Altertable Time Window: " + metricDefinition.MinimumAlertableTimeWindow);
Console.WriteLine();
}
}
}
}
使用上一篇我们的Azure 凭据验证器,获取一个令牌凭据TokenCloudCredentials,然后构造一个MonitorClient,获取指定虚拟机的监控数据。
static void Main(string[] args)
{
var credential = Authorizator.GetCredentials();
var client = new MonitorClient(credential);
client.GetMetricDefinitions();
Console.ReadLine();
}
第一块代码中:
var resourceId = ResourceIdBuilder.BuildVirtualMachineResourceId("cloudServiceName", "deploymentName");
这个地方通ResourceIDBuilder获取虚拟机的资源ID,对应的参数分别为:cloudServiceName和deploymentName,第一个是虚拟机使用的云服务名称,第二个是虚拟机名称即可。
Run...
出错了:
{"ForbiddenError: The server failed to authenticate the request. Verify that the certificate is valid and is associated with this subscription."}
一番Google后未果,咋整,再分析下错误信息:

可以看到,请求的Uri:
{https://management.core.windows.net/37*****-5107-*****-*******6/services/monitoring/metricdefinitions/query?&resourceId=%2Fhostedservices%2Fteldptapp%2Fdeployments%2Fteldptapp}
请求又跑到Azure Global那去了。
这个错误困扰了好久,还在StackOverflow上发了英文咨询贴,不知道洋人们如何回答了。在此多谢鞠强老大的指导,想办法将请求的Uri定位到中国区的Azure。
重新分析了代码,找到了Monitor的构造函数中,可以指定Uri,将中国区Azure的Uri指定一下:https://management.core.chinacloudapi.cn
MetricsClient metricsClient = new MetricsClient(credentials, new Uri("https://management.core.chinacloudapi.cn/"));
测试通过,ok。
获取到了监控指标定义,接下来我们获取监控数据:
namespace AzureTest
{
using Microsoft.WindowsAzure;
using Microsoft.WindowsAzure.Management.Monitoring.Metrics;
using Microsoft.WindowsAzure.Management.Monitoring.Metrics.Models;
using Microsoft.WindowsAzure.Management.Monitoring.Utilities;
/// <summary>
/// 监控客户端
/// </summary>
class MonitorClient
{
private SubscriptionCloudCredentials credentials; public MonitorClient(SubscriptionCloudCredentials credentials)
{
this.credentials = credentials;
} /// <summary>
/// 获取所有的监控指标数据
/// </summary>
public void GetMetricData()
{
var metricsClient = new MetricsClient(credentials, new Uri("https://management.core.chinacloudapi.cn/")); // Build the resource ID string.
var resourceId = ResourceIdBuilder.BuildVirtualMachineResourceId("cloudServiceName", "deploymentName");
Console.WriteLine("Resource Id: {0}", resourceId); //Get the metric definitions.
var metricListResponse = metricsClient.MetricDefinitions.List(resourceId, null, null);
MetricDefinitionCollection metricDefinitions = metricListResponse.MetricDefinitionCollection; var metricNamespace = "";
var metricNames = new List<string>();
// Display the metric definitions.
int count = ;
foreach (MetricDefinition metricDefinition in metricDefinitions.Value)
{
Console.WriteLine("MetricDefinitio: " + count++);
Console.WriteLine("Display Name: " + metricDefinition.DisplayName);
Console.WriteLine("Metric Name: " + metricDefinition.Name);
if (!metricNames.Contains(metricDefinition.Name))
metricNames.Add(metricDefinition.Name);
Console.WriteLine("Metric Namespace: " + metricDefinition.Namespace);
metricNamespace = metricDefinition.Namespace;
Console.WriteLine("Is Altertable: " + metricDefinition.IsAlertable);
Console.WriteLine("Min. Altertable Time Window: " + metricDefinition.MinimumAlertableTimeWindow);
Console.WriteLine();
} // timeGrain must be 5, 60 or 720 minutes.
TimeSpan timeGrain = TimeSpan.FromMinutes();
DateTime startTime = DateTime.UtcNow.AddHours(-);
DateTime endTime = DateTime.UtcNow; MetricValueListResponse response = metricsClient.MetricValues.List(resourceId, metricNames, metricNamespace, timeGrain, startTime, endTime); foreach (MetricValueSet value in response.MetricValueSetCollection.Value)
{
String valueName = value.Name;
Console.WriteLine("MetricValue:{0}", valueName);
foreach (MetricValue metricValue in value.MetricValues)
{
Console.WriteLine("Maximum:{0}{1}", metricValue.Maximum, value.Unit);
Console.WriteLine("Average:{0}{1}", metricValue.Average, value.Unit);
Console.WriteLine("Minimum:{0}{1}", metricValue.Minimum, value.Unit);
}
}
}
}
}
Run...
程序在metricsClient.MetricValues.List(resourceId, metricNames, metricNamespace, timeGrain, startTime, endTime);
出错了:
Additional information: <string xmlns="http://schemas.microsoft.com/2003/10/Serialization/">{"Code":"InvalidRequest","Message":"Could not retrieve metrics."}</string>
为啥不能获取监控指标呢?
这个错误,Google一番依旧未果,咨询了微软的技术工程师,给了如下指导,resouceID必须执行RoleName:
var resourceId = ResourceIdBuilder.BuildVirtualMachineResourceId("cloudService", "deploymentName", "roleName");
修改之后,问题解决。
至此,我们已经可以获取到监控指标和监控数据,下一步我们要获取自定义的性能计数器,基于自定义的性能计数器来实现自动伸缩。
周国庆
探索Windows Azure 监控和自动伸缩系列2 - 获取虚拟机的监控定义和监控数据的更多相关文章
- [博客迁移]探索Windows Azure 监控和自动伸缩系列2 - 获取虚拟机的监控定义和监控数据
上一篇博文介绍了如何连接Windows Azure: http://www.cnblogs.com/teld/p/5113063.html 本篇我们继续上次的示例代码,获取虚拟机的监控定义和监控数据. ...
- 探索Windows Azure 监控和自动伸缩系列1 - 连接中国区Azure
最近准备基于Microsoft Azure Management Libraries 实现虚拟机的监控.主要的需求就是获取虚拟机内置的性能计数器数据,基于性能计数器实现后续的监控和自动伸缩. 作为这一 ...
- [博客迁移]探索Windows Azure 监控和自动伸缩系列1 - 连接中国区Azure
最近准备基于Microsoft Azure Management Libraries 实现虚拟机的监控.主要的需求就是获取虚拟机内置的性能计数器数据,基于性能计数器实现后续的监控和自动伸缩. 作为这一 ...
- 探索Windows Azure 监控和自动伸缩系列3 - 启用Azure监控扩展收集自定义监控数据
上一篇我们介绍了获取Azure的监控指标和监控数据: http://www.cnblogs.com/teld/p/5113376.html 本篇我们继续:监控虚拟机的自定义性能计数器. 随着我们应用规 ...
- [博客迁移]探索Windows Azure 监控和自动伸缩系列3 - 启用Azure监控扩展收集自定义监控数据
上一篇我们介绍了获取Azure的监控指标和监控数据: http://www.cnblogs.com/teld/p/5113376.html 本篇我们继续:监控虚拟机的自定义性能计数器. 随着我们应用规 ...
- 探索 Windows Azure 网站中的自动伸缩功能
去年10月,我们发布了若干针对 WindowsAzure平台的更新,其中一项更新是添加了基于日期的自动伸缩调度支持(在不同的日期设置不同的规则). 在这篇博客文章中,我们将了解自动伸缩的概念,并 ...
- [转]探索 Windows Azure Storage
本文转自:https://msdn.microsoft.com/zh-tw/jj573842 概觀 儲存服務 (Storage services) 在 Windows Azure 運算模擬器中提供了可 ...
- Windows Azure上的Odoo(OpenERP)-1.创建Ubuntu虚拟机,安装PostgreSQL 数据库
前提是您必须拥有Windows Azure的账号,如果没有的话,可以去Windows Azure 中国区网站申请免费试用账号.哈哈,我就是第一批申请的试用账号,感觉自己挺幸运的.申请的过程就不写了,请 ...
- Windows Azure 自动伸缩已内置
WindowsAzure平台提供的主要优点之一是能够在有需要时快速缩放云中的应用程序以响应波动.去年7月以前,您必须编写自定义脚本或使用其他工具(如Wasabi或MetricsHub)来启用自动 ...
随机推荐
- Exception in thread "http-apr-8080-exec-6" java.lang.OutOfMemoryError: PermGen space 解决!
Exception in thread "http-apr-8080-exec-6" java.lang.OutOfMemoryError: PermGen space at ja ...
- leetcode -day19 Convert Sorted List to Binary Search Tree
1. Convert Sorted List to Binary Search Tree Given a singly linked list where elements are sorted ...
- SWT的ListVierer的使用
package com.test; import java.util.ArrayList; import java.util.List; import model.People; import org ...
- HttpClient(联网)
抽样: void GameRequest::initRequset(const char* url, cocos2d::CCObject* pTarget, cocos2d::SEL_CallFunc ...
- 在MyEclipse中编写Web Project,编码设置全集合
1.window-->preference-->general-->content type 然后在<Content Types>中展开每一颗子项,并在<Defau ...
- sql小技巧 group by datetime类型字段,只取其中的日期部分
工作中经常会遇到,要在sql中查询报表,查询结果要求按照日期来罗列, 或按照天, 或按照月,年. 这个时候我们经常会苦恼,datetime是精确到毫秒的,如果单纯的group by datetime就 ...
- hdu3709(数位dp)
求区间[l,r]内有多少个数的满足: 选一个位为中点,是的左边的数到该位的距离等于右边的位到该位的距离. 比如4139 选择3位中点, 那么左边的距离是 4 * 2 + 1 * 1 , 右边的距 ...
- Microsoft Build 2016
Microsoft Build 2016 Day 1 记录 去年今日:Microsoft Build 2015 汇总 今年的 Bulid 时间是 3.30-4.1,第一天的主角主要是 Window ...
- GUI测试要点
本人测试知识还不完整,所以下面的文字总结自网络上的文章,红色字体为我平时的测试经验,如有雷同之处,还请见谅,仅自己学习之用. 转载请说明来自博客园--邦邦酱好. ------------------- ...
- java.lang.NoClassDefFoundError: org/apache/juli/logging/LogFactory
Myeclipse 8.6使用tomcat7时间.执行javaweb如报告了以下错误项: java.lang.NoClassDefFoundError: org/apache/juli/logging ...