【Azure Redis 缓存】使用Python代码获取Azure Redis的监控指标值 (含Powershell脚本方式)
问题描述
通过Metrics监控页面,我们能得知当前资源(如Redis)的运行情况与各种指标。如果我们需要把指标下载到本地或者生成JSON数据导入到第三方的监控平台呢?Azure是否可以通过Python代码或者时Powershell脚本导出各种指标数据呢?
解决办法
可以! PowerShell命令可以使用Get-AzMetric 或者是 az monitor metrics list命令来获取资源的Metrics值。
- Get-AzMetric:Gets the metric values of a resource. 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: List the metric values for a resource. https://docs.microsoft.com/en-us/cli/azure/monitor/metrics?view=azure-cli-latest#az_monitor_metrics_list
而使用Python代码,可以使用Metrics的REST API来实现
Metrics - List:Lists the metric values for a resource. https://docs.microsoft.com/en-us/rest/api/monitor/metrics/list
- 在AAD中注册应用获取在Python代码中访问Redis Metrics的Access Token: (将应用程序注册到 Microsoft 标识平台: https://docs.azure.cn/zh-cn/active-directory/develop/quickstart-register-app)
注:使用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]> |
步骤三:编写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脚本方式)的更多相关文章
- 【Azure 环境】【Azure Developer】使用Python代码获取Azure 中的资源的Metrics定义及数据
问题描述 使用Python SDK来获取Azure上的各种资源的Metrics的名称以及Metrics Data的示例 问题解答 通过 azure-monitor-query ,可以创建一个 metr ...
- Windows下Redis缓存服务器的使用 .NET StackExchange.Redis Redis Desktop Manager 转发非原创
Windows下Redis缓存服务器的使用 .NET StackExchange.Redis Redis Desktop Manager Redis缓存服务器是一款key/value数据库,读11 ...
- 【Azure Redis 缓存】 Python连接Azure Redis, 使用redis.ConnectionPool 出现 "ConnectionResetError: [Errno 104] Connection reset by peer"
问题描述 Python连接Azure Redis, 使用redis.ConnectionPool 出现 "ConnectionResetError: [Errno 104] Connecti ...
- 【Azure Developer】【Python 】使用 azure.identity 和 azure.common.credentials 获取Azure AD的Access Token的两种方式
问题描述 使用Python代码,展示如何从Azure AD 中获取目标资源的 Access Token. 如要了解如何从AAD中获取 client id,client secret,tenant id ...
- 【Azure Developer】使用 Python SDK连接Azure Storage Account, 计算Blob大小代码示例
问题描述 在微软云环境中,使用python SDK连接存储账号(Storage Account)需要计算Blob大小?虽然Azure提供了一个专用工具Azure Storage Explorer可以统 ...
- 【Azure Developer】使用Java代码启动Azure VM(虚拟机)
问题描述 在使用Java的启动Azure VM的过程中,遇见了com.azure.core.management.exception.ManagementException: Status code ...
- C# Redis 缓存应用 主要代码及版本选择
/// <summary> /// RedisManager类主要是创建链接池管理对象的 /// </summary> public class RedisManager { ...
- Windows下Redis缓存服务器的使用 .NET StackExchange.Redis Redis Desktop Manager
Redis缓存服务器是一款key/value数据库,读110000次/s,写81000次/s,因为是内存操作所以速度飞快,常见用法是存用户token.短信验证码等 官网显示Redis本身并没有Wind ...
- [转]Windows下Redis缓存服务器的使用 .NET StackExchange.Redis Redis Desktop Manager
转自:http://www.cnblogs.com/oppoic/p/6165581.html Redis缓存服务器是一款key/value数据库,读110000次/s,写81000次/s,因为是内存 ...
随机推荐
- puppetlabs地址
https://yum.puppetlabs.com/el/6Server/products/i386/ rpm -Uvh http://yum.puppetlabs.com/el/6Server/ ...
- 【ORA】ORA-00257 archiver error. 错误的处理方法
今天连接数据库,结果报错,ora-00257查看 [oracle@exam oracle]$ oerr ora 00257 00257, 00000, "archiver error. Co ...
- [Usaco2005 Dec]Scales 天平
题目描述 约翰有一架用来称牛的体重的天平.与之配套的是N(1≤N≤1000)个已知质量的砝码(所有砝码质量的数值都在31位二进制内).每次称牛时,他都把某头奶牛安置在天平的某一边,然后往天平另一边加砝 ...
- RabbitMQ六种工作模式有哪些?怎样用SpringBoot整合RabbitMQ
目录 一.RabbitMQ入门程序 二.Work queues 工作模式 三.Publish / Subscribe 发布/订阅模式 四.Routing 路由模式 五.Topics 六.Header ...
- 使用npm install安装项目依赖的时候报错
使用npm install安装项目依赖的时候报错: npm ERR! code ELIFECYCLE npm ERR! errno 1 npm ERR! node-sass@4.14.1 postin ...
- poj 1038 Bugs Integrated, Inc. 题解
提供一种代码难度比较简单的做法(可能) 状态表示: 设置状态$ f[i][j] $,表示第 \(i\) 行状态为 \(j\) 的最大放置数,因为这是个阴间题,因为题目内存设置很小,所以要用滚动数组,存 ...
- Python編碼格式錯誤解決方案及案例
Python格式錯誤解決方案及案例 這幾天在玩爬蟲,在解析和提取内容時經常出現由於内容格式問題導致出錯,為防止以後出錯,整下一下,以下是這幾天的總結: 1. 特殊符號或表情符號等 背景:爬取一個烹飪教 ...
- 全栈性能测试修炼宝典-JMeter实战笔记(三)
JMeter体系结构 简介 JMeter是一款开源桌面应用软件,可用来模拟用户负载来完成性能测试工作. JMeter体系结构 X1~X5是负载模拟的一个过程,使用这些组件来完成负载的模拟 Y1:包含的 ...
- postgresql 知识的整理
.example { background-color: rgba(229, 236, 243, 1); color: rgba(0, 0, 0, 1); padding: 0.5em; margin ...
- Set、Map的区别
应用场景Set用于数据重组,Map用于数据储存Set: (1)成员不能重复(2)只有键值没有键名,类似数组(3)可以遍历,方法有add, delete,hasMap:(1)本质上是健值对的集合,类似集 ...
