问题描述

通过Metrics监控页面,我们能得知当前资源(如Redis)的运行情况与各种指标。如果我们需要把指标下载到本地或者生成JSON数据导入到第三方的监控平台呢?Azure是否可以通过Python代码或者时Powershell脚本导出各种指标数据呢?

解决办法

可以!       PowerShell命令可以使用Get-AzMetric 或者是 az monitor metrics list命令来获取资源的Metrics值。

而使用Python代码,可以使用Metrics的REST API来实现

注:使用Powershell必须先登录到Azure。使用命令 Connect-AzAccount -Environment AzureChinaCloud 或 az cloud set --name AzureChinaCloud  和 az login。

使用Python代码则需要先获取到访问Redis Metrics的Token。获取Token可以在Azure AD中注册一个应用,然后给该应用在Redis的访问控制中赋予reader的权限即可读取Metris数据。

执行步骤

Python

步骤一:注册AAD应用,复制应用ID,客户端访问密码

  • 登录Azure平台,进入AAD页面,点击App registrations: https://portal.azure.cn/?l=en.en-us#blade/Microsoft_AAD_IAM/ActiveDirectoryMenuBlade/RegisteredApps
  • 点击“New Registration” 按钮,输入应用名称,其他值保留默认,点击保存
  • 创建成功后,进入应用页面,导入到“Certificates & secrets”页面,创建需要使用的Client Secret并复制出来,第三步需要使用
  • 在应用页面复制出Tenant ID, Applicaiton ID需要在第三步代码中使用

具体操作过程见如下动图:

步骤二:赋予获取Metrics的权限

在Redis的Access control (IAM)页面中,通过步骤一的应用名搜索并赋予Monitoring Reader权限

 

注:如没有赋予权限,则代码中会报出类似错误:

Status Code: <Response [403]>
Response Content: b'{"error":{"code":"AuthorizationFailed","message":"The client \'xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx\' with object id \'xxxxxxxx-xxxx-xxxx-xxxx-36166b5f7276\' does not have authorization to perform action \'microsoft.insights/metrics/read\' over scope \'/subscriptions/xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx/resourceGroups/xxxx-rg/providers/Microsoft.Cache/Redis/xxxx/providers/microsoft.insights\' or the scope is invalid. If access was recently granted, please refresh your credentials."}}'

步骤三:编写Python代码,使用requests来发送psot,get请求

  • 代码中主要有两部分内容:一是获取Access Token,二是获取Metrics Data
  • 高亮中的内容都是需要替换成相应的资源信息和第一步中准备的信息
  • 在获取Access Token的Body内容中,grant_type是固定值,为client_credentials。resource的值为中国区azure的管理终结点:https://management.chinacloudapi.cn
import requests
import json ##Part 1: Get Access Token aadurl="https://login.chinacloudapi.cn/<your aad tenant id>/oauth2/token" aadbody={
'grant_type':'client_credentials',
'client_id':'your aad client id',
'client_secret':'your aad client secret',
'resource':'https://management.chinacloudapi.cn'
}
rtoken= requests.post(aadurl, data=aadbody)
##print(rtoken)
objtoken = json.loads(rtoken.text)
##print(obj['access_token']) ##Part 2: Get the Metrics Value by Token
headers = {'content-type': "application/json",
'Authorization': 'Bearer '+objtoken['access_token']
} url= "https://management.chinacloudapi.cn/subscriptions/<subscriptions>/resourceGroups/<resourceGroups>/providers/Microsoft.Cache/Redis/<your redis name>/providers/microsoft.insights/metrics?api-version=2018-01-01&metricnames=expiredkeys,usedmemory"
r = requests.get(url, headers=headers)
print('Status Code: ' + str(r))
print('Response Content: ' + str(r.content))

运行效果如:

Powershell

  • 登录azure
  • 准备az monitor metrics list命令
az cloud set --name AzureChinaCloud

az login

az monitor metrics list --resource /subscriptions/<your subscriptions>/resourceGroups/<resourceGroups>/providers/Microsoft.Cache/Redis/<your redis name> --metric usedmemory --aggregation Maximum --interval PT1M

执行效果如下:

 

参考资料

将应用程序注册到 Microsoft 标识平台:https://docs.azure.cn/zh-cn/active-directory/develop/quickstart-register-app

REST API Metrics - List: https://docs.microsoft.com/en-us/rest/api/monitor/metrics/list

Get-AzMetric: https://docs.microsoft.com/en-us/powershell/module/az.monitor/get-azmetric?view=azps-5.4.0&viewFallbackFrom=azps-5.2.0

az monitor metrics list: https://docs.microsoft.com/en-us/cli/azure/monitor/metrics?view=azure-cli-latest#az_monitor_metrics_list

【Azure Redis 缓存】使用Python代码获取Azure Redis的监控指标值 (含Powershell脚本方式)的更多相关文章

  1. 【Azure 环境】【Azure Developer】使用Python代码获取Azure 中的资源的Metrics定义及数据

    问题描述 使用Python SDK来获取Azure上的各种资源的Metrics的名称以及Metrics Data的示例 问题解答 通过 azure-monitor-query ,可以创建一个 metr ...

  2. Windows下Redis缓存服务器的使用 .NET StackExchange.Redis Redis Desktop Manager 转发非原创

    Windows下Redis缓存服务器的使用 .NET StackExchange.Redis Redis Desktop Manager   Redis缓存服务器是一款key/value数据库,读11 ...

  3. 【Azure Redis 缓存】 Python连接Azure Redis, 使用redis.ConnectionPool 出现 "ConnectionResetError: [Errno 104] Connection reset by peer"

    问题描述 Python连接Azure Redis, 使用redis.ConnectionPool 出现 "ConnectionResetError: [Errno 104] Connecti ...

  4. 【Azure Developer】【Python 】使用 azure.identity 和 azure.common.credentials 获取Azure AD的Access Token的两种方式

    问题描述 使用Python代码,展示如何从Azure AD 中获取目标资源的 Access Token. 如要了解如何从AAD中获取 client id,client secret,tenant id ...

  5. 【Azure Developer】使用 Python SDK连接Azure Storage Account, 计算Blob大小代码示例

    问题描述 在微软云环境中,使用python SDK连接存储账号(Storage Account)需要计算Blob大小?虽然Azure提供了一个专用工具Azure Storage Explorer可以统 ...

  6. 【Azure Developer】使用Java代码启动Azure VM(虚拟机)

    问题描述 在使用Java的启动Azure VM的过程中,遇见了com.azure.core.management.exception.ManagementException: Status code ...

  7. C# Redis 缓存应用 主要代码及版本选择

    /// <summary> /// RedisManager类主要是创建链接池管理对象的 /// </summary> public class RedisManager { ...

  8. Windows下Redis缓存服务器的使用 .NET StackExchange.Redis Redis Desktop Manager

    Redis缓存服务器是一款key/value数据库,读110000次/s,写81000次/s,因为是内存操作所以速度飞快,常见用法是存用户token.短信验证码等 官网显示Redis本身并没有Wind ...

  9. [转]Windows下Redis缓存服务器的使用 .NET StackExchange.Redis Redis Desktop Manager

    转自:http://www.cnblogs.com/oppoic/p/6165581.html Redis缓存服务器是一款key/value数据库,读110000次/s,写81000次/s,因为是内存 ...

随机推荐

  1. C# url的编码解码,xml和json的序列化和反序列化

    参考中国慕课网dot net web编程应用程序实践 using System; using System.Collections.Generic; using System.IO; using Sy ...

  2. 缓存淘汰算法 LRU 和 LFU

    LRU (Least Recently Used), 即最近最少使用用算法,是一种常见的 Cache 页面置换算法,有利于提高 Cache 命中率. LRU 的算法思想:对于每个页面,记录该页面自上一 ...

  3. 处理K8S PVC删除后pod报错

    报错如下 Jun 19 17:15:18 node1 kubelet[1722]: E0619 17:15:18.381558 1722 desired_state_of_world_populato ...

  4. LOJ10066

    LOJ10066 新的开始 题目描述 发展采矿业当然首先得有矿井,小 F 花了上次探险获得的千分之一的财富请人在岛上挖了 n 口矿井,但他似乎忘记考虑的矿井供电问题-- 为了保证电力的供应,小 F 想 ...

  5. 请你尽量全面的说一个对象在 JVM 内存中的结构?

    从 Java 14 开始,Project Valhala引入了 Value Type(或者称为 inline type),参考: Valhalla: https://openjdk.java.net/ ...

  6. js部分知识整理,google浏览器的代码调试

    整理一些学过的js知识点,包括js中3个括号的含义,this的使用,递归,google浏览器的代码调试.Location的属性及常用方法,window对象常用方法,open方法等. js括号 在js中 ...

  7. Spark Dataset DataFrame空值null,NaN判断和处理

    Spark Dataset DataFrame空值null,NaN判断和处理 import org.apache.spark.sql.SparkSession import org.apache.sp ...

  8. Java中Socket的用法

    Socket分为ServerSocket和Socket两大类: 其中ServerSocket用于服务器端,可以通过accept方法监听请求,监听到请求后返回Socket: Socket用户具体完成数据 ...

  9. windows提权常用系统漏洞与补丁编号速查对照表

    #Security Bulletin #KB #Description #Operating System CVE-2020-0787 [Windows Background Intelligent ...

  10. hadoop知识点总结(一)hadoop架构以及mapreduce工作机制

    1,为什么需要hadoop 数据分析者面临的问题 数据日趋庞大,读写都出现性能瓶颈: 用户的应用和分析结果,对实时性和响应时间要求越来越高: 使用的模型越来越复杂,计算量指数级上升. 期待的解决方案 ...