Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to use Python code to get the monitoring indicator value of Azure Redis

2025-03-30 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/01 Report--

Today, the editor will share with you how to use Python code to obtain the monitoring index value of Azure Redis. The content is detailed and the logic is clear. I believe most people still know too much about this, so share this article for your reference. I hope you can get something after reading this article. Let's take a look at it.

Problem description

Through the Metrics monitoring page, we can know the operation of current resources (such as Redis) and various metrics. What if we need to download metrics locally or generate JSON data to be imported into a third-party monitoring platform? Can Azure export all kinds of metric data through Python code or Powershell script?

Solution.

Sure! The PowerShell command can use the Get-AzMetric or az monitor metrics list command to get the Metrics value of a resource.

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

Using Python code, you can use Metrics's REST API to implement

Metrics-List:Lists the metric values for a resource. Https://docs.microsoft.com/en-us/rest/api/monitor/metrics/list

Register the application in AAD to get the Access Token that accesses Redis Metrics in Python code: (register the application to the Microsoft identity platform: https://docs.azure.cn/zh-cn/active-directory/develop/quickstart-register-app)

Note: you must first log in to Azure to use Powershell. Use the commands Connect-AzAccount-Environment AzureChinaCloud or az cloud set-name AzureChinaCloud and az login.

To use Python code, you need to first get the Token that accesses the Redis Metrics. To get Token, you can register an application in Azure AD, and then give the application the permission given to reader in the access control of Redis to read Metris data.

Execute step Python step 1: register the AAD application, copy the application ID, and access the client password

Log in to the Azure platform, go to the AAD page, and click App registrations: https://portal.azure.cn/?l=en.en-us#blade/Microsoft_AAD_IAM/ActiveDirectoryMenuBlade/RegisteredApps

Click the "New Registration" button, enter the application name, leave other values by default, and click Save.

After the creation is successful, go to the application page, import it to the "Certificates & secrets" page, create the Client Secret you need to use and copy it. The third step is to use the

When Tenant ID is copied from the application page, Applicaiton ID needs to be used in the third step of the code.

For the specific operation process, please see the following dynamic diagram:

Step 2: grant permission to obtain Metrics

In the Access control (IAM) page of Redis, search through the application name of step 1 and give Monitoring Reader permission

Note: if no permission is granted, a similar error will be reported in the code:

Status Code:

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."}}'

Step 3: write Python code and use requests to send psot,get requests

There are two main parts in the code: one is to get Access Token, the other is to get Metrics Data.

The content in the highlight needs to be replaced with the corresponding resource information and the information prepared in the first step.

In getting the Body content of Access Token, grant_type is a fixed value, which is client_credentials. The value of resource is the management endpoint of azure in China: https://management.chinacloudapi.cn

Import requestsimport json##Part 1: Get Access Tokenaadurl= "https://login.chinacloudapi.cn//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//resourceGroups//providers/Microsoft.Cache/Redis//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))

The running effect is as follows:

Powershell

Log in to azure

Prepare the az monitor metrics list command

Az cloud set-name AzureChinaCloudaz loginaz monitor metrics list-resource / subscriptions//resourceGroups//providers/Microsoft.Cache/Redis/-metric usedmemory-aggregation Maximum-interval PT1M

The execution results are as follows:

These are all the contents of the article "how to use Python code to get the monitoring index value of Azure Redis". Thank you for reading! I believe you will gain a lot after reading this article. The editor will update different knowledge for you every day. If you want to learn more knowledge, please pay attention to the industry information channel.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report