问题描述

通过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. 2021升级版微服务教程7-OpenFeign实战开发和参数调优

    2021升级版SpringCloud教程从入门到实战精通「H版&alibaba&链路追踪&日志&事务&锁」 教程全目录「含视频」:https://gitee.c ...

  2. RandomForest 随机森林算法与模型参数的调优

    公号:码农充电站pro 主页:https://codeshellme.github.io 本篇文章来介绍随机森林(RandomForest)算法. 1,集成算法之 bagging 算法 在前边的文章& ...

  3. Springboot之文件监控

    背景:在实际环境部署构成中,由于特殊网络环境因素,有很多服务器之间的网络都是单向的,不能互相访问的,只有通过特定技术手段做到文件的单项摆渡,这就需要在两台服务器上分别写序列化程序和反序列化程序,这里不 ...

  4. pytorch——合并分割

    分割与合并 import torch import numpy as np #假设a是班级1-4的数据,每个班级里有32个学生,每个学生有8门分数 #假设b是班级5-9的数据,每个班级里有32个学生, ...

  5. Spring基于注解开发的注解使用之AOP(部分源代码分析)

    AOP底层实现动态代理 1.导入spring-aop包依赖 <!--aopV1--> <dependency> <groupId>org.springframewo ...

  6. E4.IO.pry/0-IO.break!/1动态打点调试

    IO.pry/0 IO.inspect只能在静态地打印指定的变量,Elixir的shell还可以使用IO.pry/0与IO.break!/1实现更灵活的调试方法. 假如你想查看一下函数的某个位置到底发 ...

  7. Enables DNS lookups on client IP addresses 域名的分层结构

    虚拟域名访问,路由可以到达,但无输出. http://httpd.apache.org/docs/2.2/mod/core.html#hostnamelookups 移动解析 HttpDNS_域名解析 ...

  8. git database 数据库 平面文件 Git 同其他系统的重要区别 Git 只关心文件数据的整体是否发生变化,而大多数其他系统则只关心文件内容的具体差异 Git 的设计哲学

    小结: 1.如果要浏览项目的历史更新摘要,Git 不用跑到外面的服务器上去取数据回来 2.注意 git clone  应指定版本,它复制的这个版本的全部历史信息: 各个分支  git init 数据库 ...

  9. PAT甲级1056Mice and Rice

    目录 题目介绍 题解 解题思路 代码 参考链接 题目介绍 题目链接 https://pintia.cn/problem-sets/994805342720868352/problems/9948054 ...

  10. java打exe

    参考文章: 注册码: https://www.cnblogs.com/jepson6669/p/9211208.html 官网: https://exe4j.apponic.com/ 在上篇基础上,将 ...