温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

如何使用Python代码获取Azure Redis的监控指标值

发布时间:2022-03-29 15:55:19 来源:亿速云 阅读:159 作者:iii 栏目:移动开发

今天小编给大家分享一下如何使用Python代码获取Azure Redis的监控指标值的相关知识点,内容详细,逻辑清晰,相信大部分人都还太了解这方面的知识,所以分享这篇文章给大家参考一下,希望大家阅读完这篇文章后有所收获,下面我们一起来了解一下吧。

问题描述

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

如何使用Python代码获取Azure Redis的监控指标值

解决办法

可以!       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需要在第三步代码中使用

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

如何使用Python代码获取Azure Redis的监控指标值

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

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

 如何使用Python代码获取Azure Redis的监控指标值

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

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 requestsimport json##Part 1: Get Access Tokenaadurl="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 Tokenheaders = {'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))

运行效果如:

如何使用Python代码获取Azure Redis的监控指标值

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

执行效果如下:

如何使用Python代码获取Azure Redis的监控指标值 如何使用Python代码获取Azure Redis的监控指标值

以上就是“如何使用Python代码获取Azure Redis的监控指标值”这篇文章的所有内容,感谢各位的阅读!相信大家阅读完这篇文章都有很大的收获,小编每天都会为大家更新不同的知识,如果还想学习更多的知识,请关注亿速云行业资讯频道。

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI