【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,因为是内存 ...
随机推荐
- /etc/hosts导致的问题
今天安装完成orzdba之后,执行./orzdba -l 报如下错误: Usage: Socket::inet_ntoa(ip_address_sv) at /var/lib/mysql/trunk/ ...
- LuoguP5748 集合划分计数
题意 一个有\(n\)个元素的集合,将其分为任意个非空子集,求方案数.集合之间是无序的,\(\{\{1,2\},\{3\}\}=\{\{3\},\{1,2\}\}\). 设\(f_n\)表示用\(n\ ...
- 一种获取context中keys和values的高效方法 | golang
我们知道,在 golang 中的 context 是一个非常重要的包,保存了代码活动的上下文.我们经常使用 WithValue() 这个方法,来往 context 中 传递一些 key value 数 ...
- 【Android】关于连续多次点击控件的控制方案(新建监听类)
参考:防止Android过快点击造成多次事件的三种方法_胖胖的博客-CSDN博客 实现逻辑很简单: 设置限定时间 在用户点击时开始计时 若计时未超过限定时间,则不允许触发点击事件 因还未学习过Rxja ...
- 2020 CSP&NOIP 游记
CSP初赛 CSP初赛 Day -1 早上打了模拟赛,T2寒假正好做过,然而还是还是被踩Orz,郑外NB!.中午出校吃了大盘鸡和拉面,还带回来了三瓶可乐. 初赛知识点看了两页不(看)想(不)看(懂)了 ...
- Vue基础之生命周期函数[残缺版]!
Vue基础之生命周期函数[残缺版]! 为什么说是残缺版呢?! 因为有一些周期函数我并没有学到!所以是残缺版! 01 beforeCreate //在实例初始化之后,数据观测 (data observe ...
- Failed to start LSB: starts php-fpm
跟nginx一样都是进程占用,记录下 [root@localhost pazzn]# systemctl status php-fpm.service ● php-fpm-72.service - L ...
- 详解Mybatisplus
详解Mybatisplus MyBatis-Plus(简称 MP)是一个 MyBatis的增强工具,在 MyBatis 的基础上只做增强不做改变,为简化开发.提高效率而生. 特性: 无侵入**:只 ...
- https://www.exploit-db.com/docs/english/45906-cors-attacks.pdf
https://www.exploit-db.com/docs/english/45906-cors-attacks.pdf What is CORS (cross-origin resource s ...
- QT串口助手(四):数据发送
作者:zzssdd2 E-mail:zzssdd2@foxmail.com 一.前言 开发环境:Qt5.12.10 + MinGW 实现的功能 串口数据的发送 ascii字符与hex字符的相互转换 自 ...