[博客迁移]探索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");
修改之后,问题解决。
至此,我们已经可以获取到监控指标和监控数据,下一步我们要获取自定义的性能计数器,基于自定义的性能计数器来实现自动伸缩。
周国庆
2016/3
[博客迁移]探索Windows Azure 监控和自动伸缩系列2 - 获取虚拟机的监控定义和监控数据的更多相关文章
- [博客迁移]探索Windows Azure 监控和自动伸缩系列1 - 连接中国区Azure
最近准备基于Microsoft Azure Management Libraries 实现虚拟机的监控.主要的需求就是获取虚拟机内置的性能计数器数据,基于性能计数器实现后续的监控和自动伸缩. 作为这一 ...
- 探索Windows Azure 监控和自动伸缩系列2 - 获取虚拟机的监控定义和监控数据
上一篇博文介绍了如何连接Windows Azure: http://www.cnblogs.com/teld/p/5113063.html 本篇我们继续上次的示例代码,获取虚拟机的监控定义和监控数据. ...
- [博客迁移]探索Windows Azure 监控和自动伸缩系列3 - 启用Azure监控扩展收集自定义监控数据
上一篇我们介绍了获取Azure的监控指标和监控数据: http://www.cnblogs.com/teld/p/5113376.html 本篇我们继续:监控虚拟机的自定义性能计数器. 随着我们应用规 ...
- 探索Windows Azure 监控和自动伸缩系列1 - 连接中国区Azure
最近准备基于Microsoft Azure Management Libraries 实现虚拟机的监控.主要的需求就是获取虚拟机内置的性能计数器数据,基于性能计数器实现后续的监控和自动伸缩. 作为这一 ...
- 探索Windows Azure 监控和自动伸缩系列3 - 启用Azure监控扩展收集自定义监控数据
上一篇我们介绍了获取Azure的监控指标和监控数据: http://www.cnblogs.com/teld/p/5113376.html 本篇我们继续:监控虚拟机的自定义性能计数器. 随着我们应用规 ...
- 探索 Windows Azure 网站中的自动伸缩功能
去年10月,我们发布了若干针对 WindowsAzure平台的更新,其中一项更新是添加了基于日期的自动伸缩调度支持(在不同的日期设置不同的规则). 在这篇博客文章中,我们将了解自动伸缩的概念,并 ...
- 将 Java Spring Framework 应用程序迁移到 Windows Azure
我们刚刚发布了一个新教程和示例代码,以阐述如何在Windows Azure中使用 Java 相关技术.在该指南中,我们提供了分步教程,说明如何将 Java Spring Framework 应用程序( ...
- 博客迁移至http://www.maxzhang.com,欢迎访问!
博客迁移至http://www.maxzhang.com,欢迎访问!
- 告示:CSDN博客通道支持Windows Live Writer写blog离线好友
尊敬的各位CSDN用户: 您好! 为了更好的服务客户.CSDN已经支持Windows Live Writer离线写博客啦.Windows Live Writer于2014年5月29日正式上线啦!欢迎大 ...
随机推荐
- idea+maven+springboot+mybatis+springmvc+shiro
springboot就是把创建项目简单化,省去了以往的配置mybatis.springmvc的繁琐过程. 搭建web应用三个主要功能,请求和响应,数据库交互,权限配置. 一.idea创建项目 (1) ...
- @media screen and (max-width: 960px)与@media (max-width: 960px) 有screen与没有screen的区别
我们先来看下下面这段代码,估计很多人在响应式的网站CSS很经常看到类似下面的这段代码: @media screen and (max-width: 960px){ body{ background: ...
- mysql拼接sql的语法concat()用法
之前写了oracle拼接sql是用“||”,那么mysql也有自己的拼接的语法concat() concat()的基本语法是如下: 括号内的拼接内容语法和python中拼接显示字符串和变量.常量时一样 ...
- java JDBC (二) 防止注入/参数化
package cn.sasa.demo2; import java.sql.Connection; import java.sql.DriverManager; import java.sql.Pr ...
- java 抽象类 abstract
package cn.sasa.com; //抽象类 被abstract修饰的类 public abstract class Animal { //抽象类的成员变量 的定义 与 一般类是一样的 pri ...
- expect 自动完成交互式程序神器
expect 安装 yum -y install expect # Centossudo apt-get install expect # Ubuntu 作用:自动填写交互式程序的值. 原理:通过读取 ...
- SRWebSocket
以前有个项目里有做聊天室,就是用的SRWebSocket.现在整理下资料,主要是对网上搜索到的资料进行整合. WebSocket介绍,与Socket的区别 https://blog.csdn.net/ ...
- django基础-02:虚拟环境
4. 虚拟环境 4.1 概念 之前安装python包的命令: sudo pip3 install 包名 包的安装路径:/usr/local/lib/python3.5/dist-packages 在同 ...
- MySQL模拟Oralce闪回操作
在前面的文章中我们介绍了MySQL误操作后数据恢复(update,delete忘加where条件),大概操作是通过sed命令把binlog中相关SQL误操作给逆向回来,然后导入SQL文件来恢复错误操作 ...
- cd 命令
[root@localhost ~]# cd # 进入当前用户的家目录 [root@localhost ~]# cd ~ # 进入当前用户的家目录 [root@localhost ~]# cd /da ...